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
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
throws HttpMediaTypeNotAcceptableException {


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

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

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


@RestController
@RequestMapping(path = {ROOT_PATH + "/resource", ROOT_PATH + "/resource/**"}, produces="*")
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
* @return Resource reqquested, may be UNDEFINED if not found.
* @return Resource requested, may be UNDEFINED if not found.
*/
protected Resource resource(HttpServletRequest request) {
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).
* @return Returns wrapped info object, or direct access to resource contents depending on requested operation
*/
@GetMapping
public Object resourceGet(HttpServletRequest request, HttpServletResponse response) {
@RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD})
public Object get(HttpServletRequest request, HttpServletResponse response) {
Resource resource = resource(request);
Operation operation = operation(request);
Object result;
Expand All @@ -175,14 +177,15 @@ public Object resourceGet(HttpServletRequest request, HttpServletResponse respon
if (request.getMethod().equals("HEAD")) {
result = wrapObject("", String.class);
} else if (resource.getType() == Resource.Type.DIRECTORY) {
result = wrapObject(new DirectoryMedataInfo(resource, request), DirectoryMedataInfo.class);
result = wrapObject(new ResourceDirectoryInfo(resource, request), ResourceDirectoryInfo.class);
} else {
result = resource.in();
response.setContentType(getMediaType(resource, request).toString());
}
HttpHeaders responseHeaders = new HttpHeaders();
MediaType mediaType = getMediaType(resource, request);
responseHeaders.setContentType(mediaType);
response.setContentType(mediaType.toString());

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

if (converter instanceof XStreamXMLMessageConverter) {
Expand All @@ -218,7 +221,7 @@ public void configurePersister(XStreamPersister persister, XStreamMessageConvert
@Override
protected <T> ObjectWrapper createObjectWrapper(Class<T> clazz) {
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.
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")
protected static class ResourceChildInfo {
Expand Down Expand Up @@ -344,20 +347,20 @@ public String getName() {
* @author Niels Charlier
*/
@XStreamAlias("ResourceDirectory")
protected static class DirectoryMedataInfo extends ResourceMetadataInfo {
protected static class ResourceDirectoryInfo extends ResourceMetadataInfo {

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

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

/**
* Create from resource.
* 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);
for (Resource child : resource.list()) {
children.add(new ResourceChildInfo(child.name(),
Expand Down

0 comments on commit 3e349bf

Please sign in to comment.