Skip to content

Commit

Permalink
Collection description operation
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Jun 19, 2019
1 parent c531975 commit ffb7f4f
Show file tree
Hide file tree
Showing 21 changed files with 413 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*
*
*/

/*
Expand All @@ -14,6 +14,12 @@

package org.geoserver.api;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.geoserver.config.GeoServer;
import org.geoserver.platform.GeoServerResourceLoader;
import org.springframework.core.GenericTypeResolver;
Expand All @@ -32,15 +38,6 @@
import org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.print.attribute.standard.RequestingUserName;
import javax.servlet.http.HttpServletRequest;

/**
* Customized {@link RequestResponseBodyMethodProcessor} that uses its own content negotiation
* manager and can handle HTML annotated responses
Expand Down Expand Up @@ -83,8 +80,7 @@ protected <T> void writeWithMessageConverters(
HttpMessageNotWritableException {
HTMLResponseBody htmlResponseBody = returnType.getMethodAnnotation(HTMLResponseBody.class);
MediaType mediaType = getMediaTypeToUse(value, returnType, inputMessage, outputMessage);
if (htmlResponseBody != null
&& MediaType.TEXT_HTML.isCompatibleWith(mediaType)) {
if (htmlResponseBody != null && MediaType.TEXT_HTML.isCompatibleWith(mediaType)) {
// direct HTML encoding based on annotations
SimpleHTTPMessageConverter converter =
new SimpleHTTPMessageConverter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/*
* (c) 2019 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.
*
*/

/*
* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/

package org.geoserver.api;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*
*/

/* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/

package org.geoserver.api;
Expand All @@ -18,6 +12,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -26,8 +21,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.print.attribute.standard.RequestingUserName;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.geoserver.config.GeoServer;
Expand All @@ -47,6 +40,7 @@
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -87,7 +81,8 @@ public class APIDispatcher extends AbstractController

protected RequestMappingHandlerAdapter handlerAdapter;
protected HandlerMethodReturnValueHandlerComposite returnValueHandlers;
protected APIContentNegotiationManager contentNegotiationManager = new APIContentNegotiationManager();
protected APIContentNegotiationManager contentNegotiationManager =
new APIContentNegotiationManager();
private List<HttpMessageConverter<?>> messageConverters;

// SHARE
Expand Down Expand Up @@ -224,9 +219,10 @@ protected ModelAndView handleRequestInternal(
// store it in the thread local
Dispatcher.REQUEST.set(dr);
// add a thread local with info on formats, base urls, and the like
RequestInfo requestInfo = new RequestInfo(httpRequest);
requestInfo.setRequestedMediaTypes(contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(dr.getHttpRequest())));
requestInfo.setConverters(messageConverters);
RequestInfo requestInfo = new RequestInfo(httpRequest, this);
requestInfo.setRequestedMediaTypes(
contentNegotiationManager.resolveMediaTypes(
new ServletWebRequest(dr.getHttpRequest())));
RequestInfo.set(requestInfo);

// lookup the handler adapter (same as service and operation)
Expand Down Expand Up @@ -464,4 +460,30 @@ private static String getRequestUri(HttpServletRequest request) {
}
return uri;
}

public List<HttpMessageConverter<?>> getConverters() {
return messageConverters;
}

public Collection<MediaType> getProducibleMediaTypes(Class<?> responseType, boolean addHTML) {
List<MediaType> result = new ArrayList<>();
for (HttpMessageConverter<?> converter : this.messageConverters) {
if (converter instanceof GenericHttpMessageConverter) {
if (((GenericHttpMessageConverter<?>) converter)
.canWrite(responseType, responseType, null)) {
result.addAll(converter.getSupportedMediaTypes());
}
} else if (converter.canWrite(responseType, null)) {
result.addAll(converter.getSupportedMediaTypes());
}
}
if (addHTML) {
result.add(MediaType.TEXT_HTML);
}

return result.stream()
.filter(mt -> mt.isConcrete())
.distinct()
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*
*
*/

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*
*
*/

/*
Expand All @@ -12,6 +12,9 @@
*/
package org.geoserver.api;

import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.IOException;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.WorkspaceInfo;
import org.geoserver.ows.LocalWorkspace;
Expand All @@ -21,11 +24,6 @@
import org.geoserver.template.GeoServerTemplateLoader;
import org.geoserver.template.TemplateUtils;

import java.io.IOException;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreemarkerTemplateSupport {

private static Configuration templateConfig = TemplateUtils.getSafeConfiguration();
Expand Down
47 changes: 12 additions & 35 deletions src/community/api/src/main/java/org/geoserver/api/RequestInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@
*/
package org.geoserver.api;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.geoserver.ows.URLMangler;
import org.geoserver.ows.util.ResponseUtils;
import org.springframework.http.MediaType;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

/**
* An object which contains information about the "page" or "resource" being accessed in a rest
* request.
Expand All @@ -43,14 +37,14 @@
public class RequestInfo {

/** key to reference this object by */
public static final String KEY = "RequestInfo";
public static final String KEY = "APIRequestInfo";

String baseURL;
String servletPath;
String pagePath;
String extension;
List<MediaType> requestedMediaTypes;
List<HttpMessageConverter<?>> converters;
APIDispatcher dispatcher;

private Map<String, String[]> queryMap;

Expand All @@ -62,7 +56,9 @@ public RequestInfo() {}
*
* @param request
*/
public RequestInfo(HttpServletRequest request) {
public RequestInfo(HttpServletRequest request, APIDispatcher dispatcher) {
this.dispatcher = dispatcher;

// http://host:port/appName
baseURL =
request.getRequestURL()
Expand Down Expand Up @@ -184,37 +180,18 @@ public void setRequestedMediaTypes(List<MediaType> requestedMediaTypes) {
}

public List<HttpMessageConverter<?>> getConverters() {
return converters;
}

public void setConverters(List<HttpMessageConverter<?>> converters) {
this.converters = converters;
return dispatcher.getConverters();
}

public Collection<MediaType> getProducibleMediaTypes(Class<?> responseType, boolean addHTML) {
List<MediaType> result = new ArrayList<>();
for (HttpMessageConverter<?> converter : this.converters) {
if (converter instanceof GenericHttpMessageConverter) {
if (((GenericHttpMessageConverter<?>) converter)
.canWrite(responseType, responseType, null)) {
result.addAll(converter.getSupportedMediaTypes());
}
} else if (converter.canWrite(responseType, null)) {
result.addAll(converter.getSupportedMediaTypes());
}
}
if (addHTML) {
result.add(MediaType.TEXT_HTML);
}

return result.stream().filter(mt -> mt.isConcrete()).distinct().collect(Collectors.toList());
return dispatcher.getProducibleMediaTypes(responseType, addHTML);
}

public boolean isFormatRequested(MediaType mediaType) {
if (requestedMediaTypes == null) {
return false;
}

return requestedMediaTypes.stream().anyMatch(curr -> mediaType.isCompatibleWith(curr));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*
*
*/

/*
Expand All @@ -16,7 +16,6 @@

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) 2019 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*
*
*/

/* (c) 2018 Open Source Geospatial Foundation - all rights reserved
Expand All @@ -16,7 +16,14 @@
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geoserver.api.RequestInfo;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.config.GeoServer;
Expand All @@ -29,18 +36,9 @@
import org.opengis.feature.type.FeatureType;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/** Description of a single collection, that will be serialized to JSON/XML/HTML */
@JsonPropertyOrder({"name", "title", "description", "extent", "links"})
@JacksonXmlRootElement(localName = "Collection")
@JacksonXmlRootElement(localName = "Collection", namespace = "http://www.opengis.net/wfs/3.0")
public class CollectionDocument extends AbstractDocument {
static final Logger LOGGER = Logging.getLogger(CollectionDocument.class);

Expand All @@ -51,8 +49,7 @@ public class CollectionDocument extends AbstractDocument {
FeatureTypeInfo featureType;
String mapPreviewURL;

public CollectionDocument(
GeoServer geoServer, FeatureTypeInfo featureType) {
public CollectionDocument(GeoServer geoServer, FeatureTypeInfo featureType) {
// basic info
String collectionId = NCNameResourceCodec.encode(featureType);
setName(collectionId);
Expand Down

0 comments on commit ffb7f4f

Please sign in to comment.