Example usage for org.springframework.context ApplicationContext getResource

List of usage examples for org.springframework.context ApplicationContext getResource

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getResource.

Prototype

Resource getResource(String location);

Source Link

Document

Return a Resource handle for the specified resource location.

Usage

From source file:org.springframework.rest.documentation.boot.RestDocumentationView.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    Resource resource = applicationContext.getResource("classpath:javadoc.json");
    Javadoc javadoc;//from   w  w  w  . ja  v  a2  s . c  o m
    try {
        javadoc = new ObjectMapper().readValue(resource.getInputStream(), Javadoc.class);
    } catch (Exception e) {
        throw new FatalBeanException("Failed to load javadoc JSON", e);
    }

    this.documentationGenerator = new DocumentationGenerator(applicationContext, javadoc);
}

From source file:org.springframework.web.servlet.resource.ResourceHttpRequestHandler.java

private void resolveResourceLocations() {
    if (CollectionUtils.isEmpty(this.locationValues)) {
        return;//from  w ww  .  j a v  a 2s .  co  m
    } else if (!CollectionUtils.isEmpty(this.locations)) {
        throw new IllegalArgumentException("Please set either Resource-based \"locations\" or "
                + "String-based \"locationValues\", but not both.");
    }

    ApplicationContext applicationContext = obtainApplicationContext();
    for (String location : this.locationValues) {
        if (this.embeddedValueResolver != null) {
            String resolvedLocation = this.embeddedValueResolver.resolveStringValue(location);
            if (resolvedLocation == null) {
                throw new IllegalArgumentException("Location resolved to null: " + location);
            }
            location = resolvedLocation;
        }
        Charset charset = null;
        location = location.trim();
        if (location.startsWith(URL_RESOURCE_CHARSET_PREFIX)) {
            int endIndex = location.indexOf("]", URL_RESOURCE_CHARSET_PREFIX.length());
            if (endIndex == -1) {
                throw new IllegalArgumentException("Invalid charset syntax in location: " + location);
            }
            String value = location.substring(URL_RESOURCE_CHARSET_PREFIX.length(), endIndex);
            charset = Charset.forName(value);
            location = location.substring(endIndex + 1);
        }
        Resource resource = applicationContext.getResource(location);
        this.locations.add(resource);
        if (charset != null) {
            if (!(resource instanceof UrlResource)) {
                throw new IllegalArgumentException("Unexpected charset for non-UrlResource: " + resource);
            }
            this.locationCharsets.put(resource, charset);
        }
    }
}