Java Code Examples for org.springframework.http.converter.ResourceRegionHttpMessageConverter

The following examples show how to use org.springframework.http.converter.ResourceRegionHttpMessageConverter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void afterPropertiesSet() throws Exception {
	if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
		logger.warn("Locations list is empty. No resources will be served unless a " +
				"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
	}

	if (this.resourceResolvers.isEmpty()) {
		this.resourceResolvers.add(new PathResourceResolver());
	}
	initAllowedLocations();

	if (this.resourceHttpMessageConverter == null) {
		this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
	}
	if (this.resourceRegionHttpMessageConverter == null) {
		this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
	}

	this.contentNegotiationStrategy = initContentNegotiationStrategy();
}
 
Example 2
Source Project: spring-analysis-note   Source File: ResourceHttpRequestHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	resolveResourceLocations();

	if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
		logger.warn("Locations list is empty. No resources will be served unless a " +
				"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
	}

	if (this.resourceResolvers.isEmpty()) {
		this.resourceResolvers.add(new PathResourceResolver());
	}

	initAllowedLocations();

	// Initialize immutable resolver and transformer chains
	this.resolverChain = new DefaultResourceResolverChain(this.resourceResolvers);
	this.transformerChain = new DefaultResourceTransformerChain(this.resolverChain, this.resourceTransformers);

	if (this.resourceHttpMessageConverter == null) {
		this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
	}
	if (this.resourceRegionHttpMessageConverter == null) {
		this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
	}

	this.contentNegotiationStrategy = initContentNegotiationStrategy();
}
 
Example 3
Source Project: java-technology-stack   Source File: ResourceHttpRequestHandler.java    License: MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	resolveResourceLocations();

	if (logger.isWarnEnabled() && CollectionUtils.isEmpty(this.locations)) {
		logger.warn("Locations list is empty. No resources will be served unless a " +
				"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
	}

	if (this.resourceResolvers.isEmpty()) {
		this.resourceResolvers.add(new PathResourceResolver());
	}

	initAllowedLocations();

	// Initialize immutable resolver and transformer chains
	this.resolverChain = new DefaultResourceResolverChain(this.resourceResolvers);
	this.transformerChain = new DefaultResourceTransformerChain(this.resolverChain, this.resourceTransformers);

	if (this.resourceHttpMessageConverter == null) {
		this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
	}
	if (this.resourceRegionHttpMessageConverter == null) {
		this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
	}

	this.contentNegotiationStrategy = initContentNegotiationStrategy();
}
 
Example 4
Source Project: spring-analysis-note   Source File: ResourceHttpRequestHandler.java    License: MIT License 4 votes vote down vote up
/**
 * Return the configured resource region converter.
 * @since 4.3
 */
@Nullable
public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter() {
	return this.resourceRegionHttpMessageConverter;
}
 
Example 5
Source Project: java-technology-stack   Source File: ResourceHttpRequestHandler.java    License: MIT License 4 votes vote down vote up
/**
 * Return the configured resource region converter.
 * @since 4.3
 */
@Nullable
public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter() {
	return this.resourceRegionHttpMessageConverter;
}
 
Example 6
Source Project: spring-fu   Source File: ResourceConverterInitializer.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	context.registerBean("resourceHttpMessageConverter", HttpMessageConverter.class, (Supplier<HttpMessageConverter>) ResourceHttpMessageConverter::new);
	context.registerBean("resourceRegionHttpMessageConverter", HttpMessageConverter.class, ResourceRegionHttpMessageConverter::new);
}
 
Example 7
@Override
public void afterPropertiesSet() throws Exception {
	this.setResourceHttpMessageConverter(new ResourceHttpMessageConverter());
	this.setResourceRegionHttpMessageConverter(new ResourceRegionHttpMessageConverter());
}
 
Example 8
@EntityCustomAction(action="pronunciation",viewKey=EntityView.VIEW_SHOW)
public Object getNamePronunciation(OutputStream out, EntityView view, Map<String,Object> params, EntityReference ref) {
	if (!sakaiProxy.isLoggedIn()) {
		throw new SecurityException("You must be logged in to get the name pronunciation of the student.");
	}
	String uuid = sakaiProxy.ensureUuid(ref.getId());
	if(StringUtils.isBlank(uuid)) {
		throw new EntityNotFoundException("Invalid user.", ref.getId());
	}
	
	MimeTypeByteArray mtba = profileLogic.getUserNamePronunciation(uuid);
	if(mtba != null && mtba.getBytes() != null) {
		try {
			HttpServletResponse response = requestGetter.getResponse();
			HttpServletRequest request = requestGetter.getRequest();
			response.setHeader("Expires", "0");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
			response.setContentType(mtba.getMimeType());

			// Are we processing a Range request
			if (request.getHeader(HttpHeaders.RANGE) == null) {
				// Not a Range request
				byte[] bytes = mtba.getBytes();
				response.setContentLengthLong(bytes.length);
				out.write(bytes);
				return new ActionReturn(Formats.UTF_8, mtba.getMimeType() , out);
				} else {
				// A Range request - we use springs HttpRange class
				Resource resource = new ByteArrayResource(mtba.getBytes());
				response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
				response.setContentLengthLong(resource.contentLength());
				response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
				try {
					ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(request);
					ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

					List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
					ResourceRegionHttpMessageConverter messageConverter = new ResourceRegionHttpMessageConverter();

					if (httpRanges.size() == 1) {
						ResourceRegion resourceRegion = httpRanges.get(0).toResourceRegion(resource);
						messageConverter.write(resourceRegion, null, outputMessage);
					} else {
						messageConverter.write(HttpRange.toResourceRegions(httpRanges, resource), null, outputMessage);
					}
				} catch (IllegalArgumentException iae) {
					response.setHeader("Content-Range", "bytes */" + resource.contentLength());
					response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
					log.warn("Name pronunciation request failed to send the requested range for {}, {}", ref.getReference(), iae.getMessage());
				}
			}
		} catch (Exception e) {
			throw new EntityException("Name pronunciation request failed, " + e.getMessage(), ref.getReference());
		}
	}
	return null;
}
 
Example 9
@EntityCustomAction(action="pronunciation",viewKey=EntityView.VIEW_SHOW)
public Object getNamePronunciation(OutputStream out, EntityView view, Map<String,Object> params, EntityReference ref) {
	if (!sakaiProxy.isLoggedIn()) {
		throw new SecurityException("You must be logged in to get the name pronunciation of the student.");
	}
	String uuid = sakaiProxy.ensureUuid(ref.getId());
	if(StringUtils.isBlank(uuid)) {
		throw new EntityNotFoundException("Invalid user.", ref.getId());
	}
	
	MimeTypeByteArray mtba = profileLogic.getUserNamePronunciation(uuid);
	if(mtba != null && mtba.getBytes() != null) {
		try {
			HttpServletResponse response = requestGetter.getResponse();
			HttpServletRequest request = requestGetter.getRequest();
			response.setHeader("Expires", "0");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
			response.setContentType(mtba.getMimeType());

			// Are we processing a Range request
			if (request.getHeader(HttpHeaders.RANGE) == null) {
				// Not a Range request
				byte[] bytes = mtba.getBytes();
				response.setContentLengthLong(bytes.length);
				out.write(bytes);
				return new ActionReturn(Formats.UTF_8, mtba.getMimeType() , out);
				} else {
				// A Range request - we use springs HttpRange class
				Resource resource = new ByteArrayResource(mtba.getBytes());
				response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
				response.setContentLengthLong(resource.contentLength());
				response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
				try {
					ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(request);
					ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

					List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
					ResourceRegionHttpMessageConverter messageConverter = new ResourceRegionHttpMessageConverter();

					if (httpRanges.size() == 1) {
						ResourceRegion resourceRegion = httpRanges.get(0).toResourceRegion(resource);
						messageConverter.write(resourceRegion, null, outputMessage);
					} else {
						messageConverter.write(HttpRange.toResourceRegions(httpRanges, resource), null, outputMessage);
					}
				} catch (IllegalArgumentException iae) {
					response.setHeader("Content-Range", "bytes */" + resource.contentLength());
					response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
					log.warn("Name pronunciation request failed to send the requested range for {}, {}", ref.getReference(), iae.getMessage());
				}
			}
		} catch (Exception e) {
			throw new EntityException("Name pronunciation request failed, " + e.getMessage(), ref.getReference());
		}
	}
	return null;
}
 
Example 10
Source Project: spring-analysis-note   Source File: ResourceHttpRequestHandler.java    License: MIT License 2 votes vote down vote up
/**
 * Configure the {@link ResourceRegionHttpMessageConverter} to use.
 * <p>By default a {@link ResourceRegionHttpMessageConverter} will be configured.
 * @since 4.3
 */
public void setResourceRegionHttpMessageConverter(@Nullable ResourceRegionHttpMessageConverter messageConverter) {
	this.resourceRegionHttpMessageConverter = messageConverter;
}
 
Example 11
Source Project: java-technology-stack   Source File: ResourceHttpRequestHandler.java    License: MIT License 2 votes vote down vote up
/**
 * Configure the {@link ResourceRegionHttpMessageConverter} to use.
 * <p>By default a {@link ResourceRegionHttpMessageConverter} will be configured.
 * @since 4.3
 */
public void setResourceRegionHttpMessageConverter(@Nullable ResourceRegionHttpMessageConverter messageConverter) {
	this.resourceRegionHttpMessageConverter = messageConverter;
}
 
Example 12
/**
 * Configure the {@link ResourceRegionHttpMessageConverter} to use.
 * <p>By default a {@link ResourceRegionHttpMessageConverter} will be configured.
 * @since 4.3
 */
public void setResourceRegionHttpMessageConverter(ResourceRegionHttpMessageConverter resourceRegionHttpMessageConverter) {
	this.resourceRegionHttpMessageConverter = resourceRegionHttpMessageConverter;
}
 
Example 13
/**
 * Return the configured resource region converter.
 * @since 4.3
 */
public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter() {
	return this.resourceRegionHttpMessageConverter;
}