Skip to content

Commit

Permalink
send etag for images
Browse files Browse the repository at this point in the history
  • Loading branch information
culmat committed Jan 24, 2024
1 parent f1293f4 commit 1a1ae4a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
28 changes: 26 additions & 2 deletions src/main/java/com/baloise/azure/FunctionalOrgEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -25,6 +26,7 @@
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpResponseMessage.Builder;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.BindingName;
Expand Down Expand Up @@ -104,9 +106,31 @@ public HttpResponseMessage v1(
}

private HttpResponseMessage createAvatarResponse(HttpRequestMessage<Optional<String>> request, String id) throws IOException {
return request.createResponseBuilder(HttpStatus.OK)
byte[] avatar = graph().avatar(id);
String myETag = String.valueOf(Arrays.hashCode(avatar));
String theirETag = ignoreKeyCase(request.getHeaders()).get("If-None-Match");
boolean sameEtag = Objects.equals(myETag, theirETag);
Builder response = request
.createResponseBuilder(sameEtag? HttpStatus.NOT_MODIFIED : HttpStatus.OK)
.header("Content-Type","image/jpeg")
.body(graph().avatar(id)).build();
.header("ETag",myETag);
if(!sameEtag) {
response = response
.header("Content-Length",String.valueOf(avatar.length))
.body(avatar);
}
return response.build();
}

private Map<String, String> ignoreKeyCase(Map<String, String> mixedMap) {
Map<String, String> lowMap = new HashMap<>() {
@Override
public String get(Object key) {
return super.get(key.toString().toLowerCase());
}
};
mixedMap.entrySet().stream().forEach(e->lowMap.put(e.getKey().toLowerCase(), e.getValue()));
return lowMap;
}

private HttpResponseMessage createRootResponse(HttpRequestMessage<Optional<String>> request, ExecutionContext context)
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/com/baloise/azure/DevServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,13 @@ public void start() throws IOException {
Method method = functionMapping.get(path.peek());
ResponseBuilderImpl.HttpResponseMessageImpl resp = (ResponseBuilderImpl.HttpResponseMessageImpl) method.invoke(getInstance(method), parameterMappings.get(method).map(path, exg));
resp.getHeaders().forEach((k,v)-> exg.getResponseHeaders().add(k,v));
byte response[] = getResponseBytes(resp);
exg.sendResponseHeaders(resp.getStatusCode(), response.length);
out.write(response);
if(hasBody(resp)) {
byte response[] = getResponseBytes(resp);
exg.sendResponseHeaders(resp.getStatusCode(), response.length);
out.write(response);
} else {
exg.sendResponseHeaders(resp.getStatusCode(), -1);
}
} catch (Throwable t) {
logger.log(Level.WARNING, t.getLocalizedMessage(), t);
}
Expand All @@ -301,8 +305,13 @@ public void start() throws IOException {
server.start();
}

private boolean hasBody(com.baloise.azure.DevServer.ResponseBuilderImpl.HttpResponseMessageImpl resp) {
return resp.getBody()!= null;
}

private byte[] getResponseBytes(com.baloise.azure.DevServer.ResponseBuilderImpl.HttpResponseMessageImpl resp) throws UnsupportedEncodingException {
Object body = resp.getBody();
if(body == null) return new byte[0];
if(body instanceof byte[]) {
return (byte[]) body;
}
Expand Down

0 comments on commit 1a1ae4a

Please sign in to comment.