Commit 365a3b42 authored by Andrea Aime's avatar Andrea Aime
Browse files

Improved filenames and extensions in WFS3 responses (and WFS GetFeature responses as a side effect)

parent b0890403
......@@ -138,4 +138,12 @@ public abstract class AbstractHTMLResponse extends Response {
* @return
*/
protected abstract ResourceInfo getResource(Object value);
@Override
public String getAttachmentFileName(Object value, Operation operation) {
return getFileName(value, operation) + ".html";
}
/** The name of the file for the response */
protected abstract String getFileName(Object value, Operation operation);
}
......@@ -10,6 +10,7 @@ import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.config.GeoServer;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.Operation;
import org.geoserver.wfs3.NCNameResourceCodec;
public class CollectionHTMLResponse extends AbstractHTMLResponse {
......@@ -33,4 +34,9 @@ public class CollectionHTMLResponse extends AbstractHTMLResponse {
}
return null;
}
@Override
protected String getFileName(Object value, Operation operation) {
return ((CollectionDocument) value).getName();
}
}
/*
* (c) 2018 Open Source Geospatial Foundation - all rights reserved
* * This code is licensed under the GPL 2.0 license, available at the root
* * application directory.
*
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.wfs3.response;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.config.GeoServer;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.Operation;
public class CollectionsHTMLResponse extends AbstractHTMLResponse {
......@@ -25,4 +25,9 @@ public class CollectionsHTMLResponse extends AbstractHTMLResponse {
protected ResourceInfo getResource(Object value) {
return null;
}
@Override
protected String getFileName(Object value, Operation operation) {
return "collections";
}
}
......@@ -144,4 +144,9 @@ public class GetFeatureHTMLOutputFormat extends WFSGetFeatureOutputFormat {
}
return info;
}
@Override
protected String getExtension(FeatureCollectionResponse response) {
return "html";
}
}
......@@ -244,4 +244,9 @@ public class GetFeatureMapboxOutputFormat extends WFSGetFeatureOutputFormat {
public void setGridSets(DefaultGridsets gridSets) {
this.gridSets = gridSets;
}
@Override
protected String getExtension(FeatureCollectionResponse response) {
return "mbtile";
}
}
......@@ -11,6 +11,7 @@ import java.io.IOException;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.config.GeoServer;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.Operation;
public class LandingPageHTMLResponse extends AbstractHTMLResponse {
......@@ -28,4 +29,9 @@ public class LandingPageHTMLResponse extends AbstractHTMLResponse {
protected ResourceInfo getResource(Object value) {
return null;
}
@Override
protected String getFileName(Object value, Operation operation) {
return "landingPage";
}
}
......@@ -8,6 +8,7 @@ import io.swagger.v3.oas.models.OpenAPI;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.config.GeoServer;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.Operation;
/** Returns a swagger-UI HTML wrapper for the JSON response */
public class OpenAPIHTMLResponse extends AbstractHTMLResponse {
......@@ -25,4 +26,9 @@ public class OpenAPIHTMLResponse extends AbstractHTMLResponse {
protected ResourceInfo getResource(Object value) {
return null;
}
@Override
protected String getFileName(Object value, Operation operation) {
return "api";
}
}
......@@ -96,4 +96,10 @@ public class StyleDocumentResponse extends Response {
}
return requestedFormat;
}
@Override
public String getAttachmentFileName(Object value, Operation operation) {
StyleInfo style = (StyleInfo) value;
return style.getName() + "." + (style.getFormat() == null ? ".style" : style.getFormat());
}
}
......@@ -121,6 +121,7 @@ public class StyleTest extends WFS3TestSupport {
final MockHttpServletResponse response = getAsServletResponse("wfs3/styles/dashed?f=sld");
assertEquals(OK.value(), response.getStatus());
assertEquals(SLDHandler.MIMETYPE_10, response.getContentType());
assertEquals("inline; filename=dashed.sld", response.getHeader("Content-Disposition"));
final Document dom = dom(response, true);
assertXpathEvaluatesTo("SLD Cook Book: Dashed line", "//sld:UserStyle/sld:Title", dom);
assertXpathEvaluatesTo("1", "count(//sld:Rule)", dom);
......@@ -324,7 +325,7 @@ public class StyleTest extends WFS3TestSupport {
public void testMBStyle() throws Exception {
String styleBody = loadStyle("mbcircle.json");
// use a name not found in the style body
final MockHttpServletResponse response =
MockHttpServletResponse response =
postAsServletResponse("wfs3/styles", styleBody, MBStyleHandler.MIME_TYPE);
assertEquals(CREATED.value(), response.getStatus());
assertEquals(
......@@ -363,9 +364,11 @@ public class StyleTest extends WFS3TestSupport {
"circle", "//sld:PointSymbolizer/sld:Graphic/sld:Mark/sld:WellKnownName", dom);
// .. then MBStyle
DocumentContext mbstyle =
getAsJSONPath(
response =
getAsMockHttpServletResponse(
"wfs3/styles/circles?f=application%2Fvnd.geoserver.mbstyle%2Bjson", 200);
assertEquals("inline; filename=circles.mbstyle", response.getHeader("Content-Disposition"));
DocumentContext mbstyle = getAsJSONPath(response);
assertEquals("circles", mbstyle.read("$.name"));
}
......@@ -413,6 +416,7 @@ public class StyleTest extends WFS3TestSupport {
getAsServletResponse(
"wfs3/styles/cssline?f=application%2Fvnd.geoserver.geocss%2Bcss");
assertEquals(200, response.getStatus());
assertEquals("inline; filename=cssline.css", response.getHeader("Content-Disposition"));
assertEqualsIgnoreNewLineStyle(
"* {\n" + " stroke: black;\n" + " stroke-width: 3;\n" + "}",
response.getContentAsString());
......
......@@ -15,6 +15,7 @@ import java.util.Set;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.catalog.ResourceInfo;
......@@ -314,4 +315,50 @@ public abstract class WFSGetFeatureOutputFormat extends WFSResponse {
protected abstract void write(
FeatureCollectionResponse featureCollection, OutputStream output, Operation getFeature)
throws IOException, ServiceException;
@Override
public String getAttachmentFileName(Object value, Operation operation) {
FeatureCollectionResponse response;
if (value instanceof FeatureCollectionResponse) {
response = (FeatureCollectionResponse) value;
} else {
response = FeatureCollectionResponse.adapt(value);
}
final String fileName;
if (response.getTypeNames() != null) {
fileName =
response.getTypeNames()
.stream()
.map(tn -> tn.getLocalPart())
.collect(Collectors.joining("_"));
} else if (response.getTypeName() != null) {
fileName = response.getTypeName().getLocalPart();
} else {
fileName = "features";
}
return fileName + "." + getExtension(response);
}
/**
* Sets the rigth extension for the response
*
* @param response
* @return
*/
protected String getExtension(FeatureCollectionResponse response) {
String mimeType = getMimeType(null, null);
if (mimeType != null) {
// guesswork
if (mimeType.contains("gml")) {
return "xml";
} else if (mimeType.contains("json")) {
return "json";
}
// otehrwise use the default
String[] typeParts = mimeType.split(";");
return typeParts[0].split("/")[0];
} else {
return "bin";
}
}
}
......@@ -560,4 +560,9 @@ public class GeoJSONGetFeatureResponse extends WFSGetFeatureOutputFormat {
public String getCharset(Operation operation) {
return gs.getGlobal().getSettings().getCharset();
}
@Override
public String getAttachmentFileName(Object value, Operation operation) {
return super.getAttachmentFileName(value, operation);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment