Skip to content

Commit

Permalink
Allow global setting for the use of eTags and disable by default (fra…
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 authored Sep 5, 2023
1 parent ba3f824 commit 217d15e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Upcoming (7.9)
`servlet.WebServices11.enabled=true` or
`servlet.BrowserBinding.enabled=true`
- Some API endpoints have been deprecated. Users are encouraged to change over to the new API, however in order to restore the deprecated functionality the property 'iaf-api.allowDeprecated' can be set to true.
- ApiListener eTag generation has been disabled by default, set api.etag.enabled=true to enable default etag generation.


7.8-RC1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class ApiListener extends PushingListenerAdapter implements HasPhysicalDe

private final @Getter(onMethod = @__(@Override)) String domain = "Http";
private @Getter String uriPattern;
private @Getter Boolean updateEtag = null;
private @Getter boolean updateEtag = AppConstants.getInstance().getBoolean("api.etag.enabled", false);
private @Getter String operationId;

private @Getter HttpMethod method = HttpMethod.GET;
Expand Down Expand Up @@ -263,9 +263,9 @@ public void setCharacterEncoding(String charset) {

/**
* Automatically generate and validate etags
* @ff.default <code>true</code> for repeatable responses
* @ff.default <code>false</code>, can be changed by setting the property <code>api.etag.enabled</code>.
*/
public void setUpdateEtag(Boolean updateEtag) {
public void setUpdateEtag(boolean updateEtag) {
this.updateEtag = updateEtag;
}

Expand Down Expand Up @@ -381,7 +381,7 @@ public String toString() {
builder.append(" produces["+getProduces()+"]");
builder.append(" consumes["+getConsumes()+"]");
builder.append(" messageIdHeader["+getMessageIdHeader()+"]");
builder.append(" updateEtag["+getUpdateEtag()+"]");
builder.append(" updateEtag["+isUpdateEtag()+"]");
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void init() throws ServletException {
if (cache == null) {
cache = ApiCacheManager.getInstance();
}

super.init();
}

Expand Down Expand Up @@ -410,7 +411,7 @@ private void handleRequest(HttpServletRequest request, HttpServletResponse respo
}
}
}
messageContext.put(UPDATE_ETAG_CONTEXT_KEY, listener.getUpdateEtag());
messageContext.put(UPDATE_ETAG_CONTEXT_KEY, listener.isUpdateEtag());

/*
* Check authorization
Expand Down Expand Up @@ -529,14 +530,7 @@ else if(segment.startsWith("{") && segment.endsWith("}")) {
/*
* Calculate an eTag over the processed result and store in cache
*/
Boolean updateEtag = messageContext.getBoolean(UPDATE_ETAG_CONTEXT_KEY);
if (updateEtag==null) {
updateEtag=listener.getUpdateEtag();
}
if (updateEtag==null) {
updateEtag=result==null || result.isRepeatable();
}
if(updateEtag) {
if (Boolean.TRUE.equals(messageContext.getBoolean(UPDATE_ETAG_CONTEXT_KEY))) {
log.debug("calculating etags over processed result");
String cleanPattern = listener.getCleanPattern();
if(!Message.isEmpty(result) && method == HttpMethod.GET && cleanPattern != null) { //If the data has changed, generate a new eTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
Expand Down Expand Up @@ -851,12 +850,13 @@ public void customExitCode() throws ServletException, IOException, ListenerExcep
}

@Test
public void apiListenerWithRepeatableMessageShouldReturnEtag() throws Exception {
public void apiListenerWithExplicitlyEnabledEtag() throws Exception {
// Arrange
String uri="/etag1";
Message repeatableMessage = new Message("{\"tralalalallala\":true}", new MessageContext().withModificationTime("2023-01-13 14:02:00"));
new ApiListenerBuilder(uri, Methods.GET)
.withResponseContent(repeatableMessage)
.setUpdateEtag(true)
.build();

Map<String, String> headers = new HashMap<String, String>();
Expand All @@ -877,13 +877,12 @@ public void apiListenerWithRepeatableMessageShouldReturnEtag() throws Exception
}

@Test
public void apiListenerWithNonRepeatableMessageShouldNotReturnEtag() throws Exception {
public void apiListenerWithRepeatableMessageAndGloballyDisabled() throws Exception {
// Arrange
String uri="/etag2";
Message repeatableMessage = Message.asMessage(new Message("{\"tralalalallala\":true}").asByteArray());
Message nonRepeatableMessage = new Message(new FilterInputStream(repeatableMessage.asInputStream()) {}, new MessageContext().withModificationTime("2023-01-13 14:02:00"));
new ApiListenerBuilder(uri, Methods.GET)
.withResponseContent(nonRepeatableMessage)
.withResponseContent(repeatableMessage)
.build();

Map<String, String> headers = new HashMap<String, String>();
Expand All @@ -900,7 +899,6 @@ public void apiListenerWithNonRepeatableMessageShouldNotReturnEtag() throws Exce
assertFalse(result.containsHeader("etag"));
assertEquals("no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0", result.getHeader("Cache-Control"));
assertTrue(result.containsHeader("pragma"));
assertEquals("Fri, 13 Jan 2023 13:02:00 GMT", result.getHeader("Last-Modified"));
}

@Test
Expand Down Expand Up @@ -1690,6 +1688,11 @@ public ApiListenerBuilder(String uri, Methods method, MediaTypes consumes, Media

}

public ApiListenerBuilder setUpdateEtag(boolean roles) {
listener.setUpdateEtag(roles);
return this;
}

public ApiListenerBuilder setAuthenticationRoles(String roles) {
listener.setAuthenticationRoles(roles);
return this;
Expand Down

0 comments on commit 217d15e

Please sign in to comment.