Java Code Examples for org.springframework.util.DigestUtils#appendMd5DigestAsHex()

The following examples show how to use org.springframework.util.DigestUtils#appendMd5DigestAsHex() . 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
Source File: AbstractSockJsService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (request.getMethod() != HttpMethod.GET) {
		sendMethodNotAllowed(response, HttpMethod.GET);
		return;
	}

	String content = String.format(IFRAME_CONTENT, getSockJsClientLibraryUrl());
	byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
	StringBuilder builder = new StringBuilder("\"0");
	DigestUtils.appendMd5DigestAsHex(contentBytes, builder);
	builder.append('"');
	String etagValue = builder.toString();

	List<String> ifNoneMatch = request.getHeaders().getIfNoneMatch();
	if (!CollectionUtils.isEmpty(ifNoneMatch) && ifNoneMatch.get(0).equals(etagValue)) {
		response.setStatusCode(HttpStatus.NOT_MODIFIED);
		return;
	}

	response.getHeaders().setContentType(new MediaType("text", "html", StandardCharsets.UTF_8));
	response.getHeaders().setContentLength(contentBytes.length);

	// No cache in order to check every time if IFrame are authorized
	addNoCacheHeaders(response);
	response.getHeaders().setETag(etagValue);
	response.getBody().write(contentBytes);
}
 
Example 2
Source File: ShallowEtagHeaderFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Generate the ETag header value from the given response body byte array.
 * <p>The default implementation generates an MD5 hash.
 * @param inputStream the response body as an InputStream
 * @param isWeak whether the generated ETag should be weak
 * @return the ETag header value
 * @see org.springframework.util.DigestUtils
 */
protected String generateETagHeaderValue(InputStream inputStream, boolean isWeak) throws IOException {
	// length of W/ + " + 0 + 32bits md5 hash + "
	StringBuilder builder = new StringBuilder(37);
	if (isWeak) {
		builder.append("W/");
	}
	builder.append("\"0");
	DigestUtils.appendMd5DigestAsHex(inputStream, builder);
	builder.append('"');
	return builder.toString();
}
 
Example 3
Source File: AbstractSockJsService.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (request.getMethod() != HttpMethod.GET) {
		sendMethodNotAllowed(response, HttpMethod.GET);
		return;
	}

	String content = String.format(IFRAME_CONTENT, getSockJsClientLibraryUrl());
	byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
	StringBuilder builder = new StringBuilder("\"0");
	DigestUtils.appendMd5DigestAsHex(contentBytes, builder);
	builder.append('"');
	String etagValue = builder.toString();

	List<String> ifNoneMatch = request.getHeaders().getIfNoneMatch();
	if (!CollectionUtils.isEmpty(ifNoneMatch) && ifNoneMatch.get(0).equals(etagValue)) {
		response.setStatusCode(HttpStatus.NOT_MODIFIED);
		return;
	}

	response.getHeaders().setContentType(new MediaType("text", "html", StandardCharsets.UTF_8));
	response.getHeaders().setContentLength(contentBytes.length);

	// No cache in order to check every time if IFrame are authorized
	addNoCacheHeaders(response);
	response.getHeaders().setETag(etagValue);
	response.getBody().write(contentBytes);
}
 
Example 4
Source File: ShallowEtagHeaderFilter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Generate the ETag header value from the given response body byte array.
 * <p>The default implementation generates an MD5 hash.
 * @param inputStream the response body as an InputStream
 * @param isWeak whether the generated ETag should be weak
 * @return the ETag header value
 * @see org.springframework.util.DigestUtils
 */
protected String generateETagHeaderValue(InputStream inputStream, boolean isWeak) throws IOException {
	// length of W/ + " + 0 + 32bits md5 hash + "
	StringBuilder builder = new StringBuilder(37);
	if (isWeak) {
		builder.append("W/");
	}
	builder.append("\"0");
	DigestUtils.appendMd5DigestAsHex(inputStream, builder);
	builder.append('"');
	return builder.toString();
}
 
Example 5
Source File: ShallowEtagHeaderFilter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate the ETag header value from the given response body byte array.
 * <p>The default implementation generates an MD5 hash.
 * @param inputStream the response body as an InputStream
 * @param isWeak whether the generated ETag should be weak
 * @return the ETag header value
 * @see org.springframework.util.DigestUtils
 */
protected String generateETagHeaderValue(InputStream inputStream, boolean isWeak) throws IOException {
	// length of W/ + 0 + " + 32bits md5 hash + "
	StringBuilder builder = new StringBuilder(37);
	if (isWeak) {
		builder.append("W/");
	}
	builder.append("\"0");
	DigestUtils.appendMd5DigestAsHex(inputStream, builder);
	builder.append('"');
	return builder.toString();
}
 
Example 6
Source File: AbstractSockJsService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
	if (!HttpMethod.GET.equals(request.getMethod())) {
		sendMethodNotAllowed(response, HttpMethod.GET);
		return;
	}

	String content = String.format(IFRAME_CONTENT, getSockJsClientLibraryUrl());
	byte[] contentBytes = content.getBytes(UTF8_CHARSET);
	StringBuilder builder = new StringBuilder("\"0");
	DigestUtils.appendMd5DigestAsHex(contentBytes, builder);
	builder.append('"');
	String etagValue = builder.toString();

	List<String> ifNoneMatch = request.getHeaders().getIfNoneMatch();
	if (!CollectionUtils.isEmpty(ifNoneMatch) && ifNoneMatch.get(0).equals(etagValue)) {
		response.setStatusCode(HttpStatus.NOT_MODIFIED);
		return;
	}

	response.getHeaders().setContentType(new MediaType("text", "html", UTF8_CHARSET));
	response.getHeaders().setContentLength(contentBytes.length);

	// No cache in order to check every time if IFrame are authorized
	addNoCacheHeaders(response);
	response.getHeaders().setETag(etagValue);
	response.getBody().write(contentBytes);
}
 
Example 7
Source File: AbstractMetadataController.java    From initializr with Apache License 2.0 4 votes vote down vote up
protected String createUniqueId(String content) {
	StringBuilder builder = new StringBuilder();
	DigestUtils.appendMd5DigestAsHex(content.getBytes(StandardCharsets.UTF_8), builder);
	return builder.toString();
}
 
Example 8
Source File: ShallowEtagHeaderFilter.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Generate the ETag header value from the given response body byte array.
 * <p>The default implementation generates an MD5 hash.
 * @param inputStream the response body as an InputStream
 * @return the ETag header value
 * @see org.springframework.util.DigestUtils
 */
protected String generateETagHeaderValue(InputStream inputStream) throws IOException {
	StringBuilder builder = new StringBuilder("\"0");
	DigestUtils.appendMd5DigestAsHex(inputStream, builder);
	builder.append('"');
	return builder.toString();
}