Skip to content

Commit

Permalink
Fixup resource refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe committed Mar 31, 2017
1 parent 2bf5bf6 commit 3e349bf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
Expand Up @@ -38,6 +38,8 @@ private final class DefaultContentNegotiation implements ContentNegotiationStrat
@Override @Override
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
throws HttpMediaTypeNotAcceptableException { throws HttpMediaTypeNotAcceptableException {


Object request = webRequest.getNativeRequest(); Object request = webRequest.getNativeRequest();
List<MediaType> list = new ArrayList<>(); List<MediaType> list = new ArrayList<>();
if( request instanceof HttpServletRequest){ if( request instanceof HttpServletRequest){
Expand Down
Expand Up @@ -26,9 +26,12 @@
import org.geoserver.rest.util.RESTUtils; import org.geoserver.rest.util.RESTUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;


Expand All @@ -45,7 +48,6 @@


import static org.geoserver.rest.RestBaseController.ROOT_PATH; import static org.geoserver.rest.RestBaseController.ROOT_PATH;



@RestController @RestController
@RequestMapping(path = {ROOT_PATH + "/resource", ROOT_PATH + "/resource/**"}, produces="*") @RequestMapping(path = {ROOT_PATH + "/resource", ROOT_PATH + "/resource/**"}, produces="*")
public class ResourceController extends RestBaseController { public class ResourceController extends RestBaseController {
Expand Down Expand Up @@ -96,10 +98,10 @@ protected static MediaType getMediaType(Resource resource, HttpServletRequest re
} }


/** /**
* Access resource requested, note this may be {@link Resource.Type.UNDEFINED} * Access resource requested, note this may be UNDEFINED
* *
* @param request * @param request
* @return Resource reqquested, may be UNDEFINED if not found. * @return Resource requested, may be UNDEFINED if not found.
*/ */
protected Resource resource(HttpServletRequest request) { protected Resource resource(HttpServletRequest request) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
Expand Down Expand Up @@ -159,8 +161,8 @@ protected static String formatHtmlLink(String link) {
* @param response Response provided allowing us to set headers (content type, content length, Resource-Parent, Resource-Type). * @param response Response provided allowing us to set headers (content type, content length, Resource-Parent, Resource-Type).
* @return Returns wrapped info object, or direct access to resource contents depending on requested operation * @return Returns wrapped info object, or direct access to resource contents depending on requested operation
*/ */
@GetMapping @RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD})
public Object resourceGet(HttpServletRequest request, HttpServletResponse response) { public Object get(HttpServletRequest request, HttpServletResponse response) {
Resource resource = resource(request); Resource resource = resource(request);
Operation operation = operation(request); Operation operation = operation(request);
Object result; Object result;
Expand All @@ -175,14 +177,15 @@ public Object resourceGet(HttpServletRequest request, HttpServletResponse respon
if (request.getMethod().equals("HEAD")) { if (request.getMethod().equals("HEAD")) {
result = wrapObject("", String.class); result = wrapObject("", String.class);
} else if (resource.getType() == Resource.Type.DIRECTORY) { } else if (resource.getType() == Resource.Type.DIRECTORY) {
result = wrapObject(new DirectoryMedataInfo(resource, request), DirectoryMedataInfo.class); result = wrapObject(new ResourceDirectoryInfo(resource, request), ResourceDirectoryInfo.class);
} else { } else {
result = resource.in(); HttpHeaders responseHeaders = new HttpHeaders();
response.setContentType(getMediaType(resource, request).toString()); MediaType mediaType = getMediaType(resource, request);
} responseHeaders.setContentType(mediaType);
response.setContentType(mediaType.toString());


//UriComponents uriComponents = getUriComponents(name, workspaceName, builder); result = new ResponseEntity(resource.in(), responseHeaders, HttpStatus.OK);
//headers.setLocation(uriComponents.toUri()); }
response.setHeader("Location", href(resource.path())); response.setHeader("Location", href(resource.path()));
response.setHeader("Last-Modified", FORMAT_HEADER.format(resource.lastmodified()).toString()); response.setHeader("Last-Modified", FORMAT_HEADER.format(resource.lastmodified()).toString());
if (!"".equals(resource.path())) { if (!"".equals(resource.path())) {
Expand All @@ -203,7 +206,7 @@ public Object resourceGet(HttpServletRequest request, HttpServletResponse respon
public void configurePersister(XStreamPersister persister, XStreamMessageConverter converter) { public void configurePersister(XStreamPersister persister, XStreamMessageConverter converter) {
XStream xstream = persister.getXStream(); XStream xstream = persister.getXStream();
xstream.alias("child", ResourceChildInfo.class); xstream.alias("child", ResourceChildInfo.class);
xstream.alias("ResourceDirectory", DirectoryMedataInfo.class); xstream.alias("ResourceDirectory", ResourceDirectoryInfo.class);
xstream.alias("ResourceMetadata", ResourceMetadataInfo.class); xstream.alias("ResourceMetadata", ResourceMetadataInfo.class);


if (converter instanceof XStreamXMLMessageConverter) { if (converter instanceof XStreamXMLMessageConverter) {
Expand All @@ -218,7 +221,7 @@ public void configurePersister(XStreamPersister persister, XStreamMessageConvert
@Override @Override
protected <T> ObjectWrapper createObjectWrapper(Class<T> clazz) { protected <T> ObjectWrapper createObjectWrapper(Class<T> clazz) {
return new ObjectToMapWrapper<T>(clazz, Arrays.asList(AtomLink.class, return new ObjectToMapWrapper<T>(clazz, Arrays.asList(AtomLink.class,
DirectoryMedataInfo.class, ResourceMetadataInfo.class, ResourceParentInfo.class, ResourceChildInfo.class)); ResourceDirectoryInfo.class, ResourceMetadataInfo.class, ResourceParentInfo.class, ResourceChildInfo.class));
} }
/** /**
* Operation requested from the REST endpoint. * Operation requested from the REST endpoint.
Expand Down Expand Up @@ -260,7 +263,7 @@ public AtomLink getLink() {
} }


/** /**
* Lists Resource for html, json, xml output, as the contents of {@link ResourceController.DirectoryMedataInfo}. * Lists Resource for html, json, xml output, as the contents of {@link ResourceDirectoryInfo}.
*/ */
@XStreamAlias("child") @XStreamAlias("child")
protected static class ResourceChildInfo { protected static class ResourceChildInfo {
Expand Down Expand Up @@ -344,20 +347,20 @@ public String getName() {
* @author Niels Charlier * @author Niels Charlier
*/ */
@XStreamAlias("ResourceDirectory") @XStreamAlias("ResourceDirectory")
protected static class DirectoryMedataInfo extends ResourceMetadataInfo { protected static class ResourceDirectoryInfo extends ResourceMetadataInfo {


private List<ResourceChildInfo> children = new ArrayList<ResourceChildInfo>(); private List<ResourceChildInfo> children = new ArrayList<ResourceChildInfo>();


public DirectoryMedataInfo(String name, ResourceParentInfo parent, Date lastModified, public ResourceDirectoryInfo(String name, ResourceParentInfo parent, Date lastModified,
String type) { String type) {
super(name, parent, lastModified, type); super(name, parent, lastModified, type);
} }


/** /**
* Create from resource. * Create from resource.
* The class must be static for serialization, but output is request dependent so passing on self. * The class must be static for serialization, but output is request dependent so passing on self.
*/ */
public DirectoryMedataInfo(Resource resource, HttpServletRequest request) { public ResourceDirectoryInfo(Resource resource, HttpServletRequest request) {
super(resource, request, true); super(resource, request, true);
for (Resource child : resource.list()) { for (Resource child : resource.list()) {
children.add(new ResourceChildInfo(child.name(), children.add(new ResourceChildInfo(child.name(),
Expand Down

0 comments on commit 3e349bf

Please sign in to comment.