Example usage for org.springframework.web.util UrlPathHelper decodeRequestString

List of usage examples for org.springframework.web.util UrlPathHelper decodeRequestString

Introduction

In this page you can find the example usage for org.springframework.web.util UrlPathHelper decodeRequestString.

Prototype

public String decodeRequestString(HttpServletRequest request, String source) 

Source Link

Document

Decode the given source string with a URLDecoder.

Usage

From source file:info.joseluismartin.gtc.mvc.CacheController.java

/**
 * Parse request and create a new Tile.//  w w  w .ja va2  s .  c  o  m
 * Try to find tile in memory cache first, if not found, try to read from disk, if not found,
 * download tile from server and write it to cache.
 * @throws ServletException 
 */
@RequestMapping("/**")
protected void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

    UrlPathHelper pathHelper = new UrlPathHelper();
    String requestString = pathHelper.getPathWithinApplication(req);

    if (requestString == null)
        throw new ServletException("The request must include a cache path");

    if (req.getQueryString() != null)
        requestString += "?" + req.getQueryString();

    String cachePath = null;
    String query = null;
    Matcher m = URI_PATTERN.matcher(requestString);

    if (m.find()) {
        cachePath = m.group(1);
        query = pathHelper.decodeRequestString(req, m.group(2));
        if (log.isDebugEnabled()) {
            log.debug("received request: cachePath [" + cachePath + "] query [" + query + "]");
        }
    }

    TileCache cache = cacheService.findCache(cachePath);

    if (cache == null) {
        throw new ServletException("There is not tile cache configured for path [" + cachePath + "]");
    }

    // Have a cache to handle request, now test the tile
    Tile tile = cache.getTile(query);

    String remoteUrlString = cache.getServerUrl(query) + (query.startsWith("?") ? query : "/" + query);
    URL remoteUrl = new URL(remoteUrlString);

    if (tile == null) {
        proxyConnection(req, resp, requestString, cache, remoteUrlString, remoteUrl);
    } else {
        if (tile.isEmpty()) {
            // try three times...
            for (int i = 0; i < 3; i++) {
                try {
                    downloadTile(tile, remoteUrl);
                    break;
                } catch (IOException ioe) {
                    log.error("Error downloading the tile, try: " + i, ioe);
                }
            }
            cache.storeTile(tile);
        }
        // may be still empty
        if (!tile.isEmpty()) {
            resp.setContentType(tile.getMimeType());
            resp.getOutputStream().write(tile.getImage());
        } else {
            throw new ServletException("Cannot download tile: " + tile.toString());
        }
    }
}