Skip to content

Commit

Permalink
Handle resource content GET
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe committed Mar 31, 2017
1 parent 99a3bc8 commit c96bfdb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
Expand Up @@ -72,6 +72,7 @@ protected void configureMessageConverters(List<HttpMessageConverter<?>> converte
gsConverters.add(new XStreamJSONMessageConverter());
gsConverters.add(new XStreamCatalogListConverter.XMLXStreamListConverter());
gsConverters.add(new XStreamCatalogListConverter.JSONXStreamListConverter());
gsConverters.add(new InputStreamConverter());

//Deal with the various Style handler
EntityResolver entityResolver = catalog.getResourcePool().getEntityResolver();
Expand Down
@@ -0,0 +1,45 @@
package org.geoserver.rest.converters;

import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
* Created by tbarsballe on 2017-03-31.
*/
public class InputStreamConverter extends BaseMessageConverter {
@Override
public boolean canRead(Class clazz, MediaType mediaType) {
return false;
}

@Override
public boolean canWrite(Class clazz, MediaType mediaType) {
return InputStream.class.isAssignableFrom(clazz);
}

@Override
public List<MediaType> getSupportedMediaTypes() {
return Arrays.asList(MediaType.ALL);
}

@Override
public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
throw new UnsupportedOperationException();
}

@Override
public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
IOUtils.copy((InputStream)o,outputMessage.getBody());
((InputStream) o).close();
}
}
Expand Up @@ -63,7 +63,7 @@
import freemarker.template.ObjectWrapper;

@RestController
@RequestMapping(path = {ROOT_PATH + "/resource", ROOT_PATH + "/resource/**"}, produces="*")
@RequestMapping(path = {ROOT_PATH + "/resource", ROOT_PATH + "/resource/**"})
public class ResourceController extends RestBaseController {
private ResourceStore resources;
static Logger LOGGER = Logging.getLogger("org.geoserver.catalog.rest");
Expand Down Expand Up @@ -176,8 +176,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
*/
@RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD})
public Object resourceGet(HttpServletRequest request, HttpServletResponse response) {
@RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD}, produces = {MediaType.ALL_VALUE})
public Object get(HttpServletRequest request, HttpServletResponse response) {
Resource resource = resource(request);
Operation operation = operation(request);
Object result;
Expand All @@ -189,16 +189,16 @@ public Object resourceGet(HttpServletRequest request, HttpServletResponse respon
if (resource.getType() == Resource.Type.UNDEFINED) {
throw new ResourceNotFoundException("Undefined resource path.");
} else {
HttpHeaders responseHeaders = new HttpHeaders();
MediaType mediaType = getMediaType(resource, request);
responseHeaders.setContentType(mediaType);
response.setContentType(mediaType.toString());

if (request.getMethod().equals("HEAD")) {
result = wrapObject("", String.class);
result = new ResponseEntity("", responseHeaders, HttpStatus.OK);
} else if (resource.getType() == Resource.Type.DIRECTORY) {
result = wrapObject(new ResourceDirectoryInfo(resource, request), ResourceDirectoryInfo.class);
} else {
HttpHeaders responseHeaders = new HttpHeaders();
MediaType mediaType = getMediaType(resource, request);
responseHeaders.setContentType(mediaType);
response.setContentType(mediaType.toString());

result = new ResponseEntity(resource.in(), responseHeaders, HttpStatus.OK);
}
response.setHeader("Location", href(resource.path()));
Expand Down Expand Up @@ -242,7 +242,7 @@ public void resourceDelete(HttpServletRequest request){
}

/**
* Verifies mime type and use {@link RESTUtil
* Verifies mime type and use {@link RESTUtils}
* @param directory
* @param filename
* @param request
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.w3c.dom.Document;

Expand Down Expand Up @@ -155,6 +156,18 @@ public void testResourceHeaders() throws Exception {
assertContentType("image/png", response);
}

@Test
public void testResourceHead() throws Exception {
MockHttpServletResponse response = headAsServletResponse(RestBaseController.ROOT_PATH+"/resource/mydir2/fake.png");
Assert.assertEquals(
FORMAT_HEADER.format(getDataDirectory().get("/mydir2/fake.png").lastmodified()),
response.getHeader("Last-Modified"));
Assert.assertEquals("http://localhost:8080/geoserver"+RestBaseController.ROOT_PATH+"/resource/mydir2",
response.getHeader("Resource-Parent"));
Assert.assertEquals("resource", response.getHeader("Resource-Type"));
assertContentType("image/png", response);
}

@Test
public void testSpecialCharacterNames() throws Exception {
// if the file system encoded the file with a ? we need to skip this test
Expand Down Expand Up @@ -239,6 +252,17 @@ public void testDirectoryHeaders() throws Exception {
Assert.assertEquals("directory", response.getHeader("Resource-Type"));
assertContentType("application/xml", response);
}

@Test
public void testDirectoryHead() throws Exception {
MockHttpServletResponse response = headAsServletResponse(RestBaseController.ROOT_PATH+"/resource/mydir?format=xml");
Assert.assertEquals(FORMAT_HEADER.format(myRes.parent().lastmodified()),
response.getHeader("Last-Modified"));
Assert.assertEquals("http://localhost:8080/geoserver"+RestBaseController.ROOT_PATH+"/resource/",
response.getHeader("Resource-Parent"));
Assert.assertEquals("directory", response.getHeader("Resource-Type"));
assertContentType("application/xml", response);
}

@Test
public void testDirectoryMimeTypes() throws Exception {
Expand Down Expand Up @@ -348,4 +372,13 @@ public void testErrorResponseCodes() throws Exception {

}

//TODO: Migrate this (properly) to GeoServerSystemTestSupport)
public MockHttpServletResponse headAsServletResponse(String path) throws Exception {
MockHttpServletRequest request = createRequest( path );
request.setMethod( "HEAD" );
request.setContent(new byte[]{});

return dispatch( request, null );
}

}

0 comments on commit c96bfdb

Please sign in to comment.