Skip to content

Commit

Permalink
consistent classnaming, use request charset if specified
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkarlsen committed Apr 3, 2019
1 parent 61605d1 commit 47ee9bd
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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);
}
}
Expand Down

0 comments on commit 47ee9bd

Please sign in to comment.