Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package net.northfuse.resources;
 

Author(s):
tylers2
public final class ResourceHandlerAdapter extends WebContentGenerator implements HandlerAdapter {
	private static final Logger LOG = LoggerFactory.getLogger(ResourceHandlerAdapter.class);

Creates a ResourceHandlerAdapter.
		super();
	}

	public boolean supports(Object handler) {
		return ResourceHandler.class.isAssignableFrom(handler.getClass());
	}

	public ModelAndView handle(HttpServletRequest requestHttpServletResponse responseObject othrows Exception {
		checkAndPrepare(requestresponsetrue);
		Resource resource = handler.getAggregatedResource();
		// check the resource's media type
		MediaType mediaType = handler.getMediaType();
		// header phase
		setHeaders(responseresourcemediaType);
		if (new ServletWebRequest(requestresponse).checkNotModified(resource.lastModified())) {
			.debug("Resource not modified - returning 304");
			return null;
		}
		writeContent(responseresource);
		return null;
	}

	public long getLastModified(HttpServletRequest requestObject handler) {
		return -1;
	}

Set headers on the given servlet response. Called for GET requests as well as HEAD requests.

Parameters:
response current servlet response
resource the identified resource (never null)
mediaType the resource's media type (never null)
Throws:
java.io.IOException in case of errors while setting the headers
	protected void setHeaders(HttpServletResponse responseResource resourceMediaType mediaTypethrows IOException {
		long length = resource.contentLength();
		if (length > .) {
			throw new IOException("Resource content too long (beyond Integer.MAX_VALUE): " + resource);
		}
		response.setContentLength((intlength);
		response.setContentType(mediaType.toString());
	}

Write the actual content out to the given servlet response, streaming the resource's content.

Parameters:
response current servlet response
resource the identified resource (never null)
Throws:
java.io.IOException in case of errors while writing the content
	protected void writeContent(HttpServletResponse responseResource resourcethrows IOException {
		FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
	}
New to GrepCode? Check out our FAQ X