Example usage for org.springframework.util AntPathMatcher extractPathWithinPattern

List of usage examples for org.springframework.util AntPathMatcher extractPathWithinPattern

Introduction

In this page you can find the example usage for org.springframework.util AntPathMatcher extractPathWithinPattern.

Prototype

@Override
public String extractPathWithinPattern(String pattern, String path) 

Source Link

Document

Given a pattern and a full path, determine the pattern-mapped part.

Usage

From source file:com.bennavetta.appsite.webapi.ContentController.java

/**
 * Given a request that was matched against a @RequestMapping method, extract a trailing path from it.
 * For example, given a mapping of '/foo/**' and a request of '/foo/bar/baz', this would return 'bar/baz'.
 * @param request the matched request//www .ja  va 2s.  c o m
 * @return the final path component
 * @see http://stackoverflow.com/questions/3686808/spring-3-requestmapping-get-path-value
 */
public static String extractPathFromPattern(HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    return finalPath;
}

From source file:org.ng200.openolympus.controller.PartialsController.java

@RequestMapping("/partials/**")
String getMapping(final HttpServletRequest request) {
    final String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    final String bestMatchPattern = (String) request
            .getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    final AntPathMatcher apm = new AntPathMatcher();
    final String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    return "partials/" + finalPath + "::content";

}

From source file:org.dawnsci.marketplace.controllers.FileController.java

@RequestMapping(value = "/{solution}/**", method = { RequestMethod.GET, RequestMethod.HEAD })
@ResponseBody// w ww.j a  v a 2s.c  om
public ResponseEntity<FileSystemResource> getFile(@PathVariable("solution") String solution,
        HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    AntPathMatcher apm = new AntPathMatcher();
    path = apm.extractPathWithinPattern(bestMatchPattern, path);
    File file = fileService.getFile(solution, path);
    if (file.exists() && file.isFile()) {
        try {
            String detect = tika.detect(file);
            MediaType mediaType = MediaType.parseMediaType(detect);
            return ResponseEntity.ok().contentLength(file.length()).contentType(mediaType)
                    .lastModified(file.lastModified()).body(new FileSystemResource(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        throw new ResourceNotFoundException();
    }
    return null;
}

From source file:eu.supersede.gr.rest.ResourcesRest.java

@RequestMapping("/**")
public byte[] getResource(final HttpServletRequest request) {

    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    //      System.out.println( "Serving page " + finalPath );

    Resource resource = resourceLoader.getResource("classpath:static/ahprp/" + finalPath);

    //      try {
    //         System.out.println( resource.getFile().getAbsolutePath() );
    //      } catch (IOException e1) {
    //         e1.printStackTrace();
    //      }//from   w ww  .  j  a v  a2s  .  co  m
    //      Resource resource = resourceLoader.getResource("classpath:static/game.html");

    try {
        InputStream is = resource.getInputStream();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            int read = is.read();
            while (read != -1) {
                byteArrayOutputStream.write(read);
                read = is.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = byteArrayOutputStream.toByteArray();
        return bytes;

    } catch (IOException e) {
        e.printStackTrace();
    }

    // Something went wrong
    return ("Failed to load resource '" + finalPath).getBytes();
}

From source file:org.wallride.web.controller.guest.article.ArticleIndexController.java

private String extractPathFromPattern(final HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    return finalPath;
}

From source file:org.ngrinder.infra.spring.RemainedPathMethodArgumentResolver.java

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    AntPathMatcher pathMatcher = new AntPathMatcher();
    RequestMapping requestMappingOnMethod = parameter.getMethodAnnotation(RequestMapping.class);
    RequestMapping requestMappingOnClass = getDeclaringClassRequestMapping(parameter);
    String combine = pathMatcher.combine(requestMappingOnClass.value()[0], requestMappingOnMethod.value()[0]);
    return PathUtils.removePrependedSlash(pathMatcher.extractPathWithinPattern(combine,
            (String) webRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE,
                    NativeWebRequest.SCOPE_REQUEST)));
}

From source file:bjerne.gallery.controller.GalleryController.java

/**
 * Due to some Spring MVC oddities with pattern matching, the following
 * method was put in place to correctly extract the path from the URL path
 * (remember, the URL path contains more information than just the image
 * path)./*from  w w  w .  j av  a 2 s .com*/
 * 
 * @param request
 *            Request
 * @return The public image path.
 */
private String extractPathFromPattern(final HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);
    return finalPath;
}