Skip to content
Permalink
Browse files
consistent classnaming, use request charset if specified
  • Loading branch information
davidkarlsen committed Apr 3, 2019
1 parent 61605d1 commit 47ee9bd6e385c5559e3e31b2e3670630c9f4607a
Showing 1 changed file with 7 additions and 4 deletions.
@@ -23,6 +23,7 @@

import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
@@ -36,15 +37,15 @@
*/
@Provider
@Priority(Priorities.HEADER_DECORATOR)
public class ClientDigestInterceptor implements WriterInterceptor {
public class CreateDigestInterceptor implements WriterInterceptor {
private static final String DIGEST_HEADER_NAME = "Digest";
private final String digestAlgorithmName;

public ClientDigestInterceptor() {
public CreateDigestInterceptor() {
this(MessageDigestInputStream.ALGO_SHA_256);
}

public ClientDigestInterceptor(String digestAlgorithmName) {
public CreateDigestInterceptor(String digestAlgorithmName) {
this.digestAlgorithmName = digestAlgorithmName;
}

@@ -54,9 +55,11 @@ public void aroundWriteTo(WriterInterceptorContext context) throws IOException {
if (context.getHeaders().keySet().stream().noneMatch(DIGEST_HEADER_NAME::equalsIgnoreCase)
&& context.getOutputStream() instanceof CacheAndWriteOutputStream) {
CacheAndWriteOutputStream cacheAndWriteOutputStream = (CacheAndWriteOutputStream) context.getOutputStream();
String encoding = context.getMediaType().getParameters()
.getOrDefault(MediaType.CHARSET_PARAMETER, StandardCharsets.UTF_8.toString());
// not so nice - would be better to have a stream
String digest = SignatureHeaderUtils.createDigestHeader(
new String(cacheAndWriteOutputStream.getBytes(), StandardCharsets.UTF_8), digestAlgorithmName);
new String(cacheAndWriteOutputStream.getBytes(), encoding), digestAlgorithmName);
context.getHeaders().add(DIGEST_HEADER_NAME, digest);
}
}

0 comments on commit 47ee9bd

Please sign in to comment.