fixing ImageMosaic pubblication. Still something wrong in coverage config/removal.

This commit is contained in:
ccancellieri 2011-11-10 13:13:46 +01:00
parent 04449f8bdb
commit 3fa1545019
9 changed files with 317 additions and 171 deletions

View File

@ -40,7 +40,9 @@ import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
/** /**
* Connect to a GeoServer instance to publish or modify data. * Connect to a GeoServer instance to publish or modify data.
@ -509,11 +511,13 @@ public class GeoServerRESTPublisher {
return null; return null;
} }
String coverageName = covList.get(0).getName(); String coverageName = covList.get(0).getName();
coverageEncoder.setName(FilenameUtils.getBaseName(mosaicDir.getName()));
configureCoverage(coverageEncoder, workspace, storeName, coverageName);
configureCoverage(coverageEncoder, store.getWorkspaceName(), storeName, coverageName);
configureLayer(workspace, storeName, layerEncoder); configureLayer(workspace, storeName, layerEncoder);
} catch (Exception e) { } catch (Exception e) {
if (LOGGER.isEnabledFor(Level.WARN))
LOGGER.warn("Could not configure external mosaic:" + storeName, e); LOGGER.warn("Could not configure external mosaic:" + storeName, e);
store = null; // TODO: should we remove the configured store? store = null; // TODO: should we remove the configured store?
} }
@ -735,6 +739,43 @@ public class GeoServerRESTPublisher {
return configureLayer(null, layerName, layer); return configureLayer(null, layerName, layer);
} }
public boolean removeLayer(final String workspace, final String layerName) {
final String fqLayerName;
// this null check is here only for backward compatibility. workspace shall be mandatory.
if(workspace == null) {
fqLayerName = layerName;
if (LOGGER.isEnabledFor(Level.WARN)){
LOGGER.warn("Null workspace while removing layer : " + layerName + " -- This behavior is deprecated.");
}
} else {
fqLayerName = workspace + ":" + layerName;
}
if (layerName==null) {
if (LOGGER.isEnabledFor(Level.ERROR)){
LOGGER.error("Null layerName : " + layerName);
}
return false;
}
final String url = restURL + "/rest/layers/" + fqLayerName;
boolean result=HTTPUtils.delete(url, gsuser, gspass);
if (result) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Layer successfully removed: " + fqLayerName);
}
} else {
if (LOGGER.isEnabledFor(Level.WARN))
LOGGER.warn("Error removing layer " + fqLayerName);
}
return result;
}
/** /**
* Allows to configure some layer attributes such as WmsPath and DefaultStyle * Allows to configure some layer attributes such as WmsPath and DefaultStyle
* *
@ -743,16 +784,23 @@ public class GeoServerRESTPublisher {
// TODO: check this usecase, layer should always be defined // TODO: check this usecase, layer should always be defined
if (layer.isEmpty()) { if (layer.isEmpty()) {
if (LOGGER.isEnabledFor(Level.WARN))
LOGGER.warn("Null layer name while configuring layer -- This behavior is suspicious."); LOGGER.warn("Null layer name while configuring layer -- This behavior is suspicious.");
return true; return true;
} }
String fqLayerName = workspace + ":" + layerName; final String fqLayerName;
// this null check is here only for backward compatibility. workspace shall be mandatory. // this null check is here only for backward compatibility. workspace shall be mandatory.
if(workspace == null) { if(workspace == null) {
LOGGER.warn("Null workspace while configuring layer : " + layerName + " -- This behavior is deprecated.");
fqLayerName = layerName; fqLayerName = layerName;
if (LOGGER.isEnabledFor(Level.WARN)){
LOGGER.warn("Null workspace while configuring layer : " + layerName + " -- This behavior is deprecated.");
}
} else {
fqLayerName = workspace + ":" + layerName;
} }
final String url = restURL + "/rest/layers/" + fqLayerName; final String url = restURL + "/rest/layers/" + fqLayerName;
@ -764,6 +812,7 @@ public class GeoServerRESTPublisher {
LOGGER.info("Layer successfully configured: " + fqLayerName); LOGGER.info("Layer successfully configured: " + fqLayerName);
} }
} else { } else {
if (LOGGER.isEnabledFor(Level.WARN))
LOGGER.warn("Error configuring layer " + fqLayerName + " (" + sendResult + ")"); LOGGER.warn("Error configuring layer " + fqLayerName + " (" + sendResult + ")");
} }
@ -785,6 +834,7 @@ public class GeoServerRESTPublisher {
LOGGER.debug("Coverage successfully configured " + wsname + ":" + csname + ":" + cname); LOGGER.debug("Coverage successfully configured " + wsname + ":" + csname + ":" + cname);
} }
} else { } else {
if (LOGGER.isEnabledFor(Level.WARN))
LOGGER.warn("Error configuring coverage " + wsname + ":" + csname + ":" + cname + " (" + sendResult + ")"); LOGGER.warn("Error configuring coverage " + wsname + ":" + csname + ":" + cname + " (" + sendResult + ")");
} }

View File

@ -41,9 +41,10 @@ public class GSPostGISDatastoreEncoder extends PropertyXMLEncoder {
public GSPostGISDatastoreEncoder() { public GSPostGISDatastoreEncoder() {
super("dataStore"); super("dataStore");
addContent(connectionParameters.getRoot());
addType("PostGIS"); // may be overwritten with e.g. "PostGIS (JNDI)" addType("PostGIS"); // may be overwritten with e.g. "PostGIS (JNDI)"
addDatabaseType("postgis"); addDatabaseType("postgis");
addContent(connectionParameters.getRoot());
} }
/** /**

View File

@ -162,6 +162,14 @@ public abstract class GSResourceEncoder<T extends GSDimensionInfoEncoder>
set(NAME, name); set(NAME, name);
} }
public String getName(){
final Element nameNode=get(NAME);
if (nameNode!=null)
return nameNode.getText();
else
return null;
}
private final static String TITLE="title"; private final static String TITLE="title";
/** /**
* Add the 'title' node with a text value from 'title' * Add the 'title' node with a text value from 'title'

View File

@ -43,6 +43,9 @@ public class GSWorkspaceEncoder extends PropertyXMLEncoder {
super(WORKSPACE); super(WORKSPACE);
} }
/**
* @param name the workspace name
*/
public GSWorkspaceEncoder(String name) { public GSWorkspaceEncoder(String name) {
super(WORKSPACE); super(WORKSPACE);
addName(name); addName(name);
@ -54,7 +57,7 @@ public class GSWorkspaceEncoder extends PropertyXMLEncoder {
* @throws IllegalStateException if name is already set * @throws IllegalStateException if name is already set
* @deprecated will be set to protected in the next release * @deprecated will be set to protected in the next release
*/ */
public void addName(String name) { public void addName(final String name) {
final Element el=ElementUtils.contains(getRoot(),NAME); final Element el=ElementUtils.contains(getRoot(),NAME);
if (el==null) if (el==null)
add(NAME, name); add(NAME, name);
@ -66,7 +69,7 @@ public class GSWorkspaceEncoder extends PropertyXMLEncoder {
* add or change (if already set) the workspace name * add or change (if already set) the workspace name
* @param name * @param name
*/ */
public void setName(String name) { public void setName(final String name) {
final Element el=ElementUtils.contains(getRoot(),NAME); final Element el=ElementUtils.contains(getRoot(),NAME);
if (el==null) if (el==null)
add(NAME, name); add(NAME, name);

View File

@ -27,7 +27,9 @@ package it.geosolutions.geoserver.rest.encoder.coverage;
import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils;
import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder; import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import org.jdom.Element; import org.jdom.Element;
import org.jdom.filter.Filter; import org.jdom.filter.Filter;
@ -60,6 +62,8 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
if (obj instanceof Element) { if (obj instanceof Element) {
if (((Element)obj).getName().equals(ENTRY)){ if (((Element)obj).getName().equals(ENTRY)){
final Element el=((Element)obj).getChild(STRING); final Element el=((Element)obj).getChild(STRING);
if (el==null)
return false;
if (el.getText().equals(this.name)){ if (el.getText().equals(this.name)){
return true; return true;
} }
@ -87,10 +91,11 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
* @param val * @param val
*/ */
public void addAllowMultithreading(final boolean val){ public void addAllowMultithreading(final boolean val){
final Element param=new Element(ENTRY); final List<Element> list=new ArrayList<Element>(2);
param.addContent(new Element(STRING).setText(allowMultithreading)); list.add(new Element(STRING).setText(allowMultithreading));
param.addContent(new Element(STRING).setText((val)?"true":"false")); list.add(new Element(STRING).setText((val)?"true":"false"));
parameters.addContent(param);
parameters.add(null,list);
} }
private final static Filter allowMultithreadingFilter=new parametersFilter(allowMultithreading); private final static Filter allowMultithreadingFilter=new parametersFilter(allowMultithreading);
@ -108,7 +113,7 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
final Element param=new Element(ENTRY); final Element param=new Element(ENTRY);
param.addContent(new Element(STRING).setText(filter)); param.addContent(new Element(STRING).setText(filter));
param.addContent(new Element(STRING).setText(val)); param.addContent(new Element(STRING).setText(val));
parameters.addContent(param); parameters.add(null,param);
} }
private final static Filter filterFilter=new parametersFilter(filter); private final static Filter filterFilter=new parametersFilter(filter);
@ -123,10 +128,10 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
* @param val * @param val
*/ */
public void addMaxAllowedTiles(final int val){ public void addMaxAllowedTiles(final int val){
final Element param=new Element(ENTRY); final List<Element> list=new ArrayList<Element>(2);
param.addContent(new Element(STRING).setText(maxAllowedTiles)); list.add(new Element(STRING).setText(maxAllowedTiles));
param.addContent(new Element(STRING).setText(String.valueOf(val))); list.add(new Element(STRING).setText(String.valueOf(val)));
parameters.addContent(param); parameters.add(null,list);
} }
private final static Filter maxAllowedTilesFilter=new parametersFilter(maxAllowedTiles); private final static Filter maxAllowedTilesFilter=new parametersFilter(maxAllowedTiles);
@ -141,10 +146,10 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
* @param val * @param val
*/ */
public void addInputTransparentColor(final String val){ public void addInputTransparentColor(final String val){
final Element param=new Element(ENTRY); final List<Element> list=new ArrayList<Element>(2);
param.addContent(new Element(STRING).setText(inputTransparentColor)); list.add(new Element(STRING).setText(inputTransparentColor));
param.addContent(new Element(STRING).setText(val)); list.add(new Element(STRING).setText(val));
parameters.addContent(param); parameters.add(null,list);
} }
private final static Filter inputTransparentColorFilter=new parametersFilter(inputTransparentColor); private final static Filter inputTransparentColorFilter=new parametersFilter(inputTransparentColor);
@ -159,10 +164,10 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
* @param val * @param val
*/ */
public void addOutputTransparentColor(final String val){ public void addOutputTransparentColor(final String val){
final Element param=new Element(ENTRY); final List<Element> list=new ArrayList<Element>(2);
param.addContent(new Element(STRING).setText(outputTransparentColor)); list.add(new Element(STRING).setText(outputTransparentColor));
param.addContent(new Element(STRING).setText(val)); list.add(new Element(STRING).setText(val));
parameters.addContent(param); parameters.add(null,list);
} }
private final static Filter outputTransparentColorFilter=new parametersFilter(outputTransparentColor); private final static Filter outputTransparentColorFilter=new parametersFilter(outputTransparentColor);
@ -178,10 +183,10 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
* @deprecated will be set to protected in the next release * @deprecated will be set to protected in the next release
*/ */
public void addSUGGESTED_TILE_SIZE(final String val){ public void addSUGGESTED_TILE_SIZE(final String val){
final Element param=new Element(ENTRY); final List<Element> list=new ArrayList<Element>(2);
param.addContent(new Element(STRING).setText(SUGGESTED_TILE_SIZE)); list.add(new Element(STRING).setText(SUGGESTED_TILE_SIZE));
param.addContent(new Element(STRING).setText(val)); list.add(new Element(STRING).setText(val));
parameters.addContent(param); parameters.add(null,list);
} }
private final static Filter SUGGESTED_TILE_SIZEFilter=new parametersFilter(SUGGESTED_TILE_SIZE); private final static Filter SUGGESTED_TILE_SIZEFilter=new parametersFilter(SUGGESTED_TILE_SIZE);
@ -196,10 +201,10 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
* @param val * @param val
*/ */
public void addUSE_JAI_IMAGEREAD(final boolean val){ public void addUSE_JAI_IMAGEREAD(final boolean val){
final Element param=new Element(ENTRY); final List<Element> list=new ArrayList<Element>(2);
param.addContent(new Element(STRING).setText(USE_JAI_IMAGEREAD)); list.add(new Element(STRING).setText(USE_JAI_IMAGEREAD));
param.addContent(new Element(STRING).setText((val)?"true":"false")); list.add(new Element(STRING).setText((val)?"true":"false"));
parameters.addContent(param); parameters.add(null,list);
} }
private final static Filter USE_JAI_IMAGEREADFilter=new parametersFilter(USE_JAI_IMAGEREAD); private final static Filter USE_JAI_IMAGEREADFilter=new parametersFilter(USE_JAI_IMAGEREAD);
@ -214,10 +219,10 @@ public class GSImageMosaicEncoder extends GSCoverageEncoder {
* @param val * @param val
*/ */
public void addBackgroundValues(final String val){ public void addBackgroundValues(final String val){
final Element param=new Element(ENTRY); final List<Element> list=new ArrayList<Element>(2);
param.addContent(new Element(STRING).setText(backgroundValues)); list.add(new Element(STRING).setText(backgroundValues));
param.addContent(new Element(STRING).setText(val)); list.add(new Element(STRING).setText(val));
parameters.addContent(param); parameters.add(null,list);
} }
private final static Filter backgroundValuesFilter=new parametersFilter(backgroundValues); private final static Filter backgroundValuesFilter=new parametersFilter(backgroundValues);

View File

@ -25,6 +25,9 @@
package it.geosolutions.geoserver.rest.encoder.utils; package it.geosolutions.geoserver.rest.encoder.utils;
import java.util.Iterator;
import java.util.List;
import org.jdom.Content; import org.jdom.Content;
import org.jdom.Element; import org.jdom.Element;
import org.jdom.filter.Filter; import org.jdom.filter.Filter;
@ -63,6 +66,22 @@ import org.jdom.filter.Filter;
* </entry> * </entry>
* </listName>} * </listName>}
* *
* This can be also add list of compounded Elements <br/>
*
* <listName>
* <entry>
* <String>AllowMultithreading</String>
* <enabled>false</enabled>
* </entry>
* <entry>
*
* <enabled>true</enabled>
* <attribute>ele</attribute>
* <presentation>LIST</presentation>
* </dimensionInfo>
* </entry>
* </listName>}
*
* <PRE> * <PRE>
* *
* @author ETj (etj at geo-solutions.it) * @author ETj (etj at geo-solutions.it)
@ -76,10 +95,21 @@ public class NestedElementEncoder extends XmlElement {
static class NestedElementFilter implements Filter { static class NestedElementFilter implements Filter {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final String key; private final String key;
private final String value;
private final Element root; private final Element root;
public NestedElementFilter(Element root, String key) {
/**
* if key is null we only check for children name if value is null we
* only check for key attribute
*
* @param root
* @param key
* @param value
*/
public NestedElementFilter(Element root, String key, String value) {
this.key = key; this.key = key;
this.root = root; this.root = root;
this.value = value;
} }
public boolean matches(Object obj) { public boolean matches(Object obj) {
@ -87,16 +117,33 @@ public class NestedElementEncoder extends XmlElement {
final Element el = ((Element) obj); final Element el = ((Element) obj);
if (root.isAncestor(el)) { if (root.isAncestor(el)) {
if (el.getName().equals(ENTRY)/* && el.getText().equals(value) */) { if (el.getName().equals(ENTRY)/* && el.getText().equals(value) */) {
boolean keyCheck=true;
if (key != null) { if (key != null) {
if (el.getAttribute(KEY).getValue().equals(key)) if (el.getAttribute(KEY).getValue().equals(key)) {
return true; keyCheck=true;
} else {
keyCheck=false;
}
}
if (value != null)
return keyCheck&&checkChilds(el, value);
else else
return keyCheck;
}
}
}
return false; return false;
} }
private static boolean checkChilds(Element el, String value) {
final List<Element> childList = el.getChildren();
final Iterator<Element> childIt = childList.iterator();
while (childIt.hasNext()) {
final Element child = childIt.next();
if (child.getName().equals(value)) {
return true; return true;
} }
} }
}
return false; return false;
} }
}; };
@ -105,52 +152,70 @@ public class NestedElementEncoder extends XmlElement {
super(listName); super(listName);
} }
// public void set(final String key, final String value) { public void set(final String key, final String value) {
// // if some previous similar object is found // if some previous similar object is found
// final Element search; final Element search;
// if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter(getRoot(), key))) != null) { if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter(
// // remove it getRoot(), key, value))) != null) {
// ElementUtils.remove(getRoot(), search); // remove it
// } ElementUtils.remove(getRoot(), search);
// // add the new entry }
// add(key,value); // add the new entry
// } add(key, value);
}
// public void add(final String key, final Element value) { public void set(final String key, final Element value) {
// final Element entryElem = new Element(ENTRY); // if some previous similar object is found
// if (key != null) final Element search;
// entryElem.setAttribute(KEY, key); if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter(
// getRoot(), key, value.getName()))) != null) {
// entryElem.addContent(value); // remove it
// ElementUtils.remove(getRoot(), search);
// this.addContent(entryElem); }
// } // add the new entry
add(key, value);
}
// public void add(final String key, final String value) { public void add(final String key, final Element value) {
// final Element entryElem = new Element(ENTRY);
//
// if (key != null)
// entryElem.setAttribute(KEY, key);
//
// entryElem.setText(value);
//
// this.addContent(entryElem);
// }
public void add(final String key, final Content value) {
final Element entryElem = new Element(ENTRY); final Element entryElem = new Element(ENTRY);
if (key != null) if (key != null)
entryElem.setAttribute(KEY, key); entryElem.setAttribute(KEY, key);
entryElem.setContent(value); entryElem.addContent(value);
this.addContent(entryElem); this.addContent(entryElem);
} }
public void set(final String key, final Content value) { public void add(final String key, final String value) {
final Element entryElem = new Element(ENTRY);
if (key != null)
entryElem.setAttribute(KEY, key);
entryElem.setText(value);
this.addContent(entryElem);
}
public void add(final String key, final List<Element> list) {
final Element entryElem = new Element(ENTRY);
if (key != null)
entryElem.setAttribute(KEY, key);
// final Iterator<Element> it=list.iterator();
// while (it.hasNext()){
// final Element child=it.next();
entryElem.addContent(list);
// }
this.addContent(entryElem);
}
public void set(final String key, final List<Element> value) {
// if some previous similar object is found // if some previous similar object is found
final Element search; final Element search;
if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter(getRoot(), key))) != null) { if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter(
getRoot(), key, value.get(0).getValue()))) != null) {
// remove it // remove it
ElementUtils.remove(search, search); ElementUtils.remove(search, search);
} }
@ -161,11 +226,10 @@ public class NestedElementEncoder extends XmlElement {
public boolean remove(final String key) { public boolean remove(final String key) {
// if some previous similar object is found // if some previous similar object is found
final Element search; final Element search;
if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter(getRoot(), key))) != null) { if ((search = ElementUtils.contains(getRoot(), new NestedElementFilter(
getRoot(), key, null))) != null) {
return ElementUtils.remove(search, search); return ElementUtils.remove(search, search);
} } else
else
return false; return false;
} }
} }

View File

@ -57,8 +57,28 @@ public class PropertyXMLEncoder extends XmlElement {
super(rootName); super(rootName);
} }
protected void get(final String key, final String value) {
public void set(final String key, final String value) { }
protected Element get(final String key) {
return get(getRoot(), key);
}
private Element get(final Element el, final String key) {
if (el==null)
return null;
if (key.contains("/")) {
final int i = key.indexOf("/");
final String parentName = key.substring(0, i);
final String newkey = key.substring(i + 1);
return get(ElementUtils.contains(el, parentName),newkey);
} else {
return ElementUtils.contains(el, key);
}
}
protected void set(final String key, final String value) {
if (key != null && value != null) { if (key != null && value != null) {
set(getRoot(), key, value); set(getRoot(), key, value);
} }
@ -88,7 +108,7 @@ public class PropertyXMLEncoder extends XmlElement {
} }
} }
public void add(final String key, final String value) { protected void add(final String key, final String value) {
if (key != null && value != null) { if (key != null && value != null) {
add(this.getRoot(), key, value); add(this.getRoot(), key, value);
} }

View File

@ -56,21 +56,21 @@ public class XmlElement{
@SuppressWarnings("unused") @SuppressWarnings("unused")
private XmlElement(){root=null;}; private XmlElement(){root=null;};
public void add(final String nodename, final String nodetext) { protected void add(final String nodename, final String nodetext) {
add(nodename,new Text(nodetext)); add(nodename,new Text(nodetext));
} }
public void add(final String nodename, final Content nodetext) { protected void add(final String nodename, final Content nodetext) {
final Element el=new Element(nodename); final Element el=new Element(nodename);
el.setContent(nodetext); el.setContent(nodetext);
this.addContent(el); this.addContent(el);
} }
public void set(final String nodename, final String nodetext) { protected void set(final String nodename, final String nodetext) {
set(nodename,new Text(nodetext)); set(nodename,new Text(nodetext));
} }
public void set(final String nodename, final Content nodeContent) { protected void set(final String nodename, final Content nodeContent) {
final Element el=ElementUtils.contains(getRoot(),nodename); final Element el=ElementUtils.contains(getRoot(),nodename);
if (el==null){ if (el==null){
add(nodename,nodeContent); add(nodename,nodeContent);
@ -80,7 +80,7 @@ public class XmlElement{
} }
} }
public Element addContent(Content child){ protected Element addContent(Content child){
return root.addContent(child); return root.addContent(child);
} }

View File

@ -44,13 +44,8 @@ public class GSWorkspaceEncoderTest extends TestCase {
final GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder("WS1"); final GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder("WS1");
LOGGER.info(wsenc.toString()); LOGGER.info(wsenc.toString());
try{ wsenc.setName("test_name");
wsenc.addName("test_name"); LOGGER.info(wsenc.toString());
// NEVER HERE
Assert.assertTrue(false);
}catch (IllegalStateException e){
Assert.assertTrue(true);
}
wsenc.setName("new_name"); wsenc.setName("new_name");
LOGGER.info(wsenc.toString()); LOGGER.info(wsenc.toString());