Example usage for org.springframework.core.io ContextResource getPathWithinContext

List of usage examples for org.springframework.core.io ContextResource getPathWithinContext

Introduction

In this page you can find the example usage for org.springframework.core.io ContextResource getPathWithinContext.

Prototype

String getPathWithinContext();

Source Link

Document

Return the path within the enclosing 'context'.

Usage

From source file:mvctest.web.TestController.java

@RequestMapping(value = "/appCtxGetResourcesLikeSwfFlowDefinitionResourceFactory")
public void appCtxGetResourcesLikeSwfFlowDefinitionResourceFactory(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    final String pattern = getRequiredStringParameter(request, "pattern");
    final String basePath = getStringParameter(request, "basePath", null);
    final String title = "From ApplicationContext/ResourceLoader's getResources() like SWF's FlowDefinitionResourceFactory";
    StringBuilder builder = new StringBuilder();
    builder.append("<html>\n<head>\n<title>").append(title).append("</title>\n</head>\n<body>\n");
    builder.append("<h1>").append(title).append("</h1>\n");
    builder.append("base path: ").append(basePath).append("<br />\n");
    builder.append("pattern: ").append(pattern).append("<br />\n");
    builder.append("<hr />\n");

    if (this.resourceLoader instanceof ResourcePatternResolver) {
        ResourcePatternResolver resolver = (ResourcePatternResolver) this.resourceLoader;
        Resource[] resources;/*from  ww w.  j ava2s  .  com*/
        if (basePath == null) {
            resources = resolver.getResources(pattern);
        } else {
            if (basePath.endsWith(SLASH) || pattern.startsWith(SLASH)) {
                resources = resolver.getResources(basePath + pattern);
            } else {
                resources = resolver.getResources(basePath + SLASH + pattern);
            }
        }

        for (Resource resource : resources) {
            builder.append("<h3>Resource: ").append(resource.getURL()).append("</h3>\n");
            if (resource instanceof ContextResource) {
                ContextResource contextResource = (ContextResource) resource;
                builder.append("<p>PathWithinContext: ").append(contextResource.getPathWithinContext())
                        .append("</p>\n");
            } else {
                builder.append("<p>Resource is not a ContextResource but rather a [")
                        .append(resource.getClass().getName()).append("].</p>\n");
            }
            builder.append("<p>Flow ID: ").append(getFlowId(basePath, resource)).append("</p>\n");
            builder.append("<hr />\n");
        }
    }

    builder.append("</body>\n</html>\n");
    response.getWriter().write(builder.toString());
}