Skip to content

Commit

Permalink
[GEOS-8447] Trying to create a stored query with an already existing …
Browse files Browse the repository at this point in the history
…id needs to be denied with a specific exception code and status code
  • Loading branch information
aaime committed Nov 30, 2017
1 parent 7d45716 commit 0d46066
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ public CreateStoredQuery(WFSInfo wfs, StoredQueryProvider storedQueryProvider) {

public CreateStoredQueryResponseType run(CreateStoredQueryType request) throws WFSException {
for (StoredQueryDescriptionType sqd : request.getStoredQueryDefinition()) {
if(storedQueryProvider.getStoredQuery(sqd.getId()) != null) {
WFSException e = new WFSException(request, "Stored query already exists", WFSException.DUPLICATE_STORED_QUERY_ID_VALUE);
e.setLocator(sqd.getId());
throw e;
}

validateStoredQuery(request, sqd);

try {
storedQueryProvider.createStoredQuery(sqd);
}
catch(Exception e) {
} catch(Exception e) {
throw new WFSException(request, "Error occured creating stored query", e);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/wfs/src/main/java/org/geoserver/wfs/StoredQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import org.eclipse.emf.common.util.EList;
import org.geoserver.catalog.Catalog;
import org.geoserver.wfs.kvp.QNameKvpParser;
import org.geoserver.wfs.xml.FeatureTypeSchemaBuilder;
import org.geotools.filter.v2_0.FES;
import org.geotools.gml3.v3_2.GML;
import org.geotools.wfs.v2_0.WFS;
import org.geotools.wfs.v2_0.WFSConfiguration;
import org.geotools.xml.Parser;
Expand Down Expand Up @@ -223,6 +225,7 @@ public List<QueryType> compile(StoredQueryType query) {
if (catalog != null) {
p.getNamespaces().add(new CatalogNamespaceSupport(catalog));
}
p.getNamespaces().declarePrefix("gml", GML.NAMESPACE);
try {
QueryType compiled =
(QueryType) p.parse(new ByteArrayInputStream(sb.toString().getBytes()));
Expand Down
1 change: 1 addition & 0 deletions src/wfs/src/main/java/org/geoserver/wfs/WFSException.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class WFSException extends ServiceException {
public static final String NOT_FOUND = "NotFound";
public static final String OPERATION_PROCESSING_FAILED = "OperationProcessingFailed";
public static final String INVALID_VALUE = "InvalidValue";
public static final String DUPLICATE_STORED_QUERY_ID_VALUE = "DuplicateStoredQueryIdValue";

public enum Code {
OperationProcessingFailed
Expand Down
44 changes: 31 additions & 13 deletions src/wfs/src/test/java/org/geoserver/wfs/v2_0/StoredQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.geoserver.platform.ServiceException;
import org.geoserver.wfs.StoredQuery;
import org.geoserver.wfs.StoredQueryProvider;
import org.geoserver.wfs.WFSException;
import org.geoserver.wfs.xml.FeatureTypeSchemaBuilder;
import org.geoserver.wfs.xml.v1_1_0.WFSConfiguration;
import org.geotools.wfs.v2_0.WFS;
Expand Down Expand Up @@ -91,8 +92,34 @@ public void testCreateStoredQuery() throws Exception {
assertEquals("wfs:ListStoredQueriesResponse", dom.getDocumentElement().getNodeName());
XMLAssert.assertXpathEvaluatesTo("1", "count(//wfs:StoredQuery)", dom);

xml =
"<wfs:CreateStoredQuery service='WFS' version='2.0.0' " +
xml = getCreatePrimitiveWithinQuery();

dom = postAsDOM("wfs", xml);
assertEquals("wfs:CreateStoredQueryResponse", dom.getDocumentElement().getNodeName());
assertEquals("OK", dom.getDocumentElement().getAttribute("status"));

dom = getAsDOM("wfs?request=ListStoredQueries");
XMLAssert.assertXpathEvaluatesTo("2", "count(//wfs:StoredQuery)", dom);
XMLAssert.assertXpathExists("//wfs:StoredQuery[@id = 'myStoredQuery']", dom);
XMLAssert.assertXpathExists("//wfs:ReturnFeatureType[text() = 'sf:PrimitiveGeoFeature']", dom);
}

@Test
public void testDuplicateStoredQuery() throws Exception {
String xml = getCreatePrimitiveWithinQuery();
Document dom = postAsDOM("wfs", xml);
assertEquals("wfs:CreateStoredQueryResponse", dom.getDocumentElement().getNodeName());
assertEquals("OK", dom.getDocumentElement().getAttribute("status"));

MockHttpServletResponse response = postAsServletResponse("wfs", xml);
assertEquals(400, response.getStatus());
dom = dom(new ByteArrayInputStream(response.getContentAsByteArray()));

checkOws11Exception(dom, "2.0.0", WFSException.DUPLICATE_STORED_QUERY_ID_VALUE, "myStoredQuery");
}

public String getCreatePrimitiveWithinQuery() {
return "<wfs:CreateStoredQuery service='WFS' version='2.0.0' " +
" xmlns:wfs='http://www.opengis.net/wfs/2.0' " +
" xmlns:fes='http://www.opengis.org/fes/2.0' " +
" xmlns:gml='http://www.opengis.net/gml/3.2' " +
Expand All @@ -114,18 +141,9 @@ public void testCreateStoredQuery() throws Exception {
" </wfs:Query> " +
" </wfs:QueryExpressionText> " +
" </wfs:StoredQueryDefinition> " +
"</wfs:CreateStoredQuery>";

dom = postAsDOM("wfs", xml);
assertEquals("wfs:CreateStoredQueryResponse", dom.getDocumentElement().getNodeName());
assertEquals("OK", dom.getDocumentElement().getAttribute("status"));

dom = getAsDOM("wfs?request=ListStoredQueries");
XMLAssert.assertXpathEvaluatesTo("2", "count(//wfs:StoredQuery)", dom);
XMLAssert.assertXpathExists("//wfs:StoredQuery[@id = 'myStoredQuery']", dom);
XMLAssert.assertXpathExists("//wfs:ReturnFeatureType[text() = 'sf:PrimitiveGeoFeature']", dom);
"</wfs:CreateStoredQuery>";
}

@Test
public void testCreateStoredQueryMismatchingTypes() throws Exception {
String xml =
Expand Down

0 comments on commit 0d46066

Please sign in to comment.