Skip to content

Commit

Permalink
Support character encoding for ResourceController paths
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe committed Mar 31, 2017
1 parent 6d20191 commit f123ccc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
Expand Up @@ -5,31 +5,13 @@
package org.geoserver.rest.resources; package org.geoserver.rest.resources;




import static org.geoserver.rest.RestBaseController.ROOT_PATH; import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.BufferedInputStream; import freemarker.template.ObjectWrapper;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.geoserver.AtomLink; import org.geoserver.AtomLink;
import org.geoserver.config.util.XStreamPersister; import org.geoserver.config.util.XStreamPersister;
import org.geoserver.ows.URLMangler; import org.geoserver.ows.URLMangler;
import org.geoserver.ows.util.ResponseUtils; import org.geoserver.ows.util.ResponseUtils;
import org.geoserver.platform.resource.Paths;
import org.geoserver.platform.resource.Resource; import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.ResourceStore; import org.geoserver.platform.resource.ResourceStore;
import org.geoserver.platform.resource.ResourceStoreFactory; import org.geoserver.platform.resource.ResourceStoreFactory;
Expand All @@ -51,18 +33,23 @@
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;


import com.thoughtworks.xstream.XStream; import javax.servlet.http.HttpServletRequest;
import com.thoughtworks.xstream.annotations.XStreamAlias; import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;


import freemarker.template.ObjectWrapper; import static org.geoserver.rest.RestBaseController.ROOT_PATH;


@RestController @RestController
@RequestMapping(path = {ROOT_PATH + "/resource", ROOT_PATH + "/resource/**"}) @RequestMapping(path = {ROOT_PATH + "/resource", ROOT_PATH + "/resource/**"})
Expand Down Expand Up @@ -121,12 +108,15 @@ protected static MediaType getMediaType(Resource resource, HttpServletRequest re
* @return Resource requested, 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 = request.getPathInfo();
path = path.substring((ROOT_PATH+"/resource").length()); //Strip off "/resource"
if ("".equals(path)) { //root path = path.substring(9);
path = Paths.BASE; //handle special characters
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RestException("Could not decode the resource URL to UTF-8 format", HttpStatus.INTERNAL_SERVER_ERROR);
} }

return resources.get(path); return resources.get(path);
} }


Expand Down Expand Up @@ -179,7 +169,7 @@ protected static String formatHtmlLink(String link) {
* @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
*/ */
@RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD}, produces = {MediaType.ALL_VALUE}) @RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD}, produces = {MediaType.ALL_VALUE})
public Object get(HttpServletRequest request, HttpServletResponse response) { public Object resourceGet(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 Down Expand Up @@ -259,7 +249,7 @@ else if (operation == Operation.METADATA ){
* Delete resource * Delete resource
*/ */
@DeleteMapping @DeleteMapping
public void resourceDelete(HttpServletRequest request){ public void resourceDelete(HttpServletRequest request) {
Resource resource = resource(request); Resource resource = resource(request);
boolean removed = resource.delete(); boolean removed = resource.delete();
if (!removed) { if (!removed) {
Expand Down
Expand Up @@ -172,6 +172,7 @@ public void testResourceHead() throws Exception {
public void testSpecialCharacterNames() throws Exception { public void testSpecialCharacterNames() throws Exception {
// if the file system encoded the file with a ? we need to skip this test // if the file system encoded the file with a ? we need to skip this test
Assume.assumeTrue(getDataDirectory().get("po?zie").getType() == Type.UNDEFINED); Assume.assumeTrue(getDataDirectory().get("po?zie").getType() == Type.UNDEFINED);
Assert.assertEquals(Type.DIRECTORY, getDataDirectory().get("poëzie").getType());
XMLUnit.setXpathNamespaceContext(NS_XML); XMLUnit.setXpathNamespaceContext(NS_XML);
Document doc = getAsDOM(RestBaseController.ROOT_PATH+"/resource/po%c3%abzie?format=xml"); Document doc = getAsDOM(RestBaseController.ROOT_PATH+"/resource/po%c3%abzie?format=xml");
XMLAssert.assertXpathEvaluatesTo("http://localhost:8080/geoserver"+RestBaseController.ROOT_PATH+"/resource/po%C3%ABzie/caf%C3%A9", XMLAssert.assertXpathEvaluatesTo("http://localhost:8080/geoserver"+RestBaseController.ROOT_PATH+"/resource/po%C3%ABzie/caf%C3%A9",
Expand Down

0 comments on commit f123ccc

Please sign in to comment.