Skip to content

Commit

Permalink
Merge pull request #587 from swisspost/develop
Browse files Browse the repository at this point in the history
PR for release
  • Loading branch information
mcweba authored Jun 7, 2024
2 parents fe437dc + f3ddc06 commit b2aa5ac
Show file tree
Hide file tree
Showing 48 changed files with 460 additions and 490 deletions.
2 changes: 1 addition & 1 deletion gateleen-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>

<artifactId>gateleen-cache</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gateleen-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>

<artifactId>gateleen-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public interface GateleenExceptionFactory {

public Exception newException(String msg, Throwable cause);

/** Convenience overload for {@link #newIllegalStateException(String, Throwable)}. */
public default IllegalStateException newIllegalStateException(String msg) { return newIllegalStateException(msg, null); }

/** Convenience overload for {@link #newIllegalStateException(String, Throwable)}. */
public default IllegalStateException newIllegalStateException(Throwable cause) { return newIllegalStateException(null, cause); }

public IllegalStateException newIllegalStateException(String msg, Throwable cause);

public ReplyException newReplyException(ReplyFailure failureType, int failureCode, String message);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public Exception newException(String message, Throwable cause) {
return new GateleenNoStacktraceException(message, cause);
}

@Override
public IllegalStateException newIllegalStateException(String msg, Throwable cause) {
if (cause instanceof IllegalStateException) return (IllegalStateException) cause;
return new NoStackIllegalStateException(msg);
}

@Override
public ReplyException newReplyException(ReplyFailure failureType, int failureCode, String message) {
return new GateleenNoStackReplyException(failureType, failureCode, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public Exception newException(String message, Throwable cause) {
return new Exception(message, cause);
}

@Override
public IllegalStateException newIllegalStateException(String msg, Throwable cause) {
return new IllegalStateException(msg, cause);
}

@Override
public ReplyException newReplyException(ReplyFailure failureType, int failureCode, String message) {
return new ReplyException(failureType, failureCode, message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.swisspush.gateleen.core.exception;

public class NoStackIllegalStateException extends IllegalStateException {

public NoStackIllegalStateException() {
super();
}

public NoStackIllegalStateException(String s) {
super(s);
}

public NoStackIllegalStateException(String message, Throwable cause) {
super(message, cause);
}

public NoStackIllegalStateException(Throwable cause) {
super(cause);
}

@Override
public Throwable fillInStackTrace() {
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public void setRoutingContexttHandler(Handler<RoutingContext> wrappedRoutingCont

@Override
protected HttpClientRequest doRequest(HttpMethod method, String uri) {
return new LocalHttpClientRequest(method, uri, vertx, wrappedRoutingContexttHandler, exceptionFactory, new LocalHttpServerResponse(vertx));
return new LocalHttpClientRequest(method, uri, vertx, wrappedRoutingContexttHandler, exceptionFactory, new LocalHttpServerResponse(vertx, exceptionFactory));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.gateleen.core.exception.GateleenExceptionFactory;
import org.swisspush.gateleen.core.util.StatusCode;

/**
Expand All @@ -16,6 +17,7 @@
public class LocalHttpServerResponse extends BufferBridge implements FastFailHttpServerResponse {

private static final Logger logger = LoggerFactory.getLogger(LocalHttpServerResponse.class);
private final GateleenExceptionFactory exceptionFactory;
private int statusCode;
private String statusMessage;
private static final String EMPTY = "";
Expand Down Expand Up @@ -118,8 +120,9 @@ public HttpClientResponse exceptionHandler(Handler<Throwable> handler) {
};


public LocalHttpServerResponse(Vertx vertx) {
public LocalHttpServerResponse(Vertx vertx, GateleenExceptionFactory exceptionFactory) {
super(vertx);
this.exceptionFactory = exceptionFactory;
// Attach most simple possible exception handler to base.
setExceptionHandler(thr -> logger.error("Processing of response failed.", thr));
}
Expand Down Expand Up @@ -214,9 +217,10 @@ public Future<Void> write(String chunk, String enc) {
public Future<Void> write(Buffer data) {
// emulate Vertx's HttpServerResponseImpl
if (!chunked && !headers.contains(HttpHeaders.CONTENT_LENGTH)) {
IllegalStateException ex = new IllegalStateException("You must set the Content-Length header to be the total size of the message "
IllegalStateException ex = exceptionFactory.newIllegalStateException(""
+ "You must set the Content-Length header to be the total size of the message "
+ "body BEFORE sending any data if you are not using HTTP chunked encoding.");
logger.error("non-proper HttpServerResponse occured", ex);
logger.debug("non-proper HttpServerResponse occurred", ex);
throw ex;
}
ensureBound();
Expand Down
2 changes: 1 addition & 1 deletion gateleen-delegate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>

<artifactId>gateleen-delegate</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gateleen-delta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>

<artifactId>gateleen-delta</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
Expand All @@ -17,13 +19,17 @@
import org.swisspush.gateleen.core.util.*;
import org.swisspush.gateleen.core.util.ExpansionDeltaUtil.CollectionResourceContainer;
import org.swisspush.gateleen.core.util.ExpansionDeltaUtil.SlashHandling;
import org.swisspush.gateleen.logging.LogAppenderRepository;
import org.swisspush.gateleen.logging.LoggingHandler;
import org.swisspush.gateleen.logging.LoggingResourceManager;
import org.swisspush.gateleen.routing.Router;
import org.swisspush.gateleen.routing.Rule;
import org.swisspush.gateleen.routing.RuleFeaturesProvider;
import org.swisspush.gateleen.routing.RuleProvider;

import java.util.*;

import static org.swisspush.gateleen.logging.LoggingHandler.SKIP_LOGGING_HEADER;
import static org.swisspush.gateleen.routing.RuleFeatures.Feature.DELTA_ON_BACKEND;

/**
Expand Down Expand Up @@ -54,18 +60,28 @@ public class DeltaHandler implements RuleProvider.RuleChangesObserver {
private boolean rejectLimitOffsetRequests;

private RuleProvider ruleProvider;

private final Vertx vertx;
private final LoggingResourceManager loggingResourceManager;
private final LogAppenderRepository logAppenderRepository;
private RuleFeaturesProvider ruleFeaturesProvider = new RuleFeaturesProvider(new ArrayList<>());

public DeltaHandler(RedisProvider redisProvider, HttpClient httpClient, RuleProvider ruleProvider) {
this(redisProvider, httpClient, ruleProvider, false);
public DeltaHandler(Vertx vertx, RedisProvider redisProvider, HttpClient httpClient, RuleProvider ruleProvider,
LoggingResourceManager loggingResourceManager, LogAppenderRepository logAppenderRepository) {
this(vertx,redisProvider, httpClient, ruleProvider, loggingResourceManager, logAppenderRepository, false);
}

public DeltaHandler(RedisProvider redisProvider, HttpClient httpClient, RuleProvider ruleProvider, boolean rejectLimitOffsetRequests) {
public DeltaHandler(Vertx vertx, RedisProvider redisProvider, HttpClient httpClient, RuleProvider ruleProvider,
LoggingResourceManager loggingResourceManager, LogAppenderRepository logAppenderRepository,
boolean rejectLimitOffsetRequests) {
this.vertx = vertx;
this.redisProvider = redisProvider;
this.httpClient = httpClient;
this.rejectLimitOffsetRequests = rejectLimitOffsetRequests;

this.ruleProvider = ruleProvider;
this.loggingResourceManager = loggingResourceManager;
this.logAppenderRepository = logAppenderRepository;
this.ruleProvider.registerObserver(this);
}

Expand Down Expand Up @@ -284,6 +300,9 @@ private DeltaResourcesContainer getDeltaResourceNames(List<String> subResourceNa
private void handleCollectionGET(final HttpServerRequest request, final String updateId, final Logger log) {
request.pause();

final LoggingHandler loggingHandler = new LoggingHandler(loggingResourceManager, logAppenderRepository, request, vertx.eventBus());
loggingHandler.request(request.headers());

final HttpMethod method = HttpMethod.GET;
final String targetUri = ExpansionDeltaUtil.constructRequestUri(request.path(), request.params(), null, null, SlashHandling.KEEP);
log.debug("constructed uri for request: {}", targetUri);
Expand All @@ -299,6 +318,7 @@ private void handleCollectionGET(final HttpServerRequest request, final String u
cReq.headers().setAll(request.headers());
// add a marker header to signalize, that in the next loop of the mainverticle we should pass the deltahandler
cReq.headers().set(DELTA_BACKEND_HEADER, "");
cReq.headers().set(SKIP_LOGGING_HEADER, "true");
cReq.headers().set("Accept", "application/json");
cReq.setChunked(true);
request.handler(cReq::write);
Expand All @@ -307,6 +327,8 @@ private void handleCollectionGET(final HttpServerRequest request, final String u
HttpClientResponse cRes = asyncResult1.result();
HttpServerRequestUtil.prepareResponse(request, cRes);

loggingHandler.setResponse(cRes);

if (cRes.headers().contains(DELTA_HEADER)) {
cRes.handler(data -> request.response().write(data));
cRes.endHandler(v1 -> request.response().end());
Expand All @@ -327,7 +349,7 @@ private void handleCollectionGET(final HttpServerRequest request, final String u
log.trace("DeltaHandler: deltaResourceKeys for targetUri ({}): {}", targetUri, deltaResourceKeys);
}

if (deltaResourceKeys.size() > 0) {
if (!deltaResourceKeys.isEmpty()) {
if (log.isTraceEnabled()) {
log.trace("DeltaHandler: targetUri ({}) using mget command.", targetUri);
}
Expand All @@ -345,9 +367,12 @@ private void handleCollectionGET(final HttpServerRequest request, final String u

JsonObject result = buildResultJsonObject(deltaResourcesContainer.getResourceNames(),
dataContainer.getCollectionName());
String responseBody = result.toString();
request.response().putHeader(DELTA_HEADER,
"" + deltaResourcesContainer.getMaxUpdateId());
request.response().end(result.toString());
loggingHandler.appendResponsePayload(Buffer.buffer(responseBody));
loggingHandler.log();
request.response().end(responseBody);
})).onFailure(event -> {
log.error("Redis: handleCollectionGET failed", event);
handleError(request, "error reading delta information");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.swisspush.gateleen.delta;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.*;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
Expand All @@ -19,6 +16,8 @@
import org.swisspush.gateleen.core.http.DummyHttpServerResponse;
import org.swisspush.gateleen.core.redis.RedisProvider;
import org.swisspush.gateleen.core.util.StatusCode;
import org.swisspush.gateleen.logging.LogAppenderRepository;
import org.swisspush.gateleen.logging.LoggingResourceManager;
import org.swisspush.gateleen.routing.Router;
import org.swisspush.gateleen.routing.Rule;
import org.swisspush.gateleen.routing.RuleProvider;
Expand All @@ -36,6 +35,9 @@ public class DeltaHandlerTest {
private RedisAPI redisAPI;
private RedisProvider redisProvider;
private RuleProvider ruleProvider;
private Vertx vertx;
private LoggingResourceManager loggingResourceManager;
private LogAppenderRepository logAppenderRepository;
private Router router = mock(Router.class);
private HttpServerRequest request;
private HttpServerResponse response;
Expand All @@ -44,6 +46,9 @@ public class DeltaHandlerTest {
@Before
public void before() {
redisAPI = mock(RedisAPI.class);
vertx = mock(Vertx.class);
loggingResourceManager = mock(LoggingResourceManager.class);
logAppenderRepository = mock(LogAppenderRepository.class);

redisProvider = mock(RedisProvider.class);
when(redisProvider.redis()).thenReturn(Future.succeededFuture(redisAPI));
Expand All @@ -69,7 +74,7 @@ public void before() {

@Test
public void testIsDeltaRequest(TestContext context) {
DeltaHandler deltaHandler = new DeltaHandler(redisProvider, null, ruleProvider);
DeltaHandler deltaHandler = new DeltaHandler(vertx, redisProvider, null, ruleProvider, loggingResourceManager, logAppenderRepository);
deltaHandler.rulesChanged(List.of(
rule("/gateleen/server/res_1", false),
rule("/gateleen/server/res_2", true))
Expand Down Expand Up @@ -152,7 +157,7 @@ public void testIsDeltaRequest(TestContext context) {

@Test
public void testDeltaNoExpiry() {
DeltaHandler deltaHandler = new DeltaHandler(redisProvider, null, ruleProvider);
DeltaHandler deltaHandler = new DeltaHandler(vertx, redisProvider, null, ruleProvider, loggingResourceManager, logAppenderRepository);
deltaHandler.handle(request, router);

verify(redisAPI, times(1)).set(eq(Arrays.asList("delta:resources:a:b:c", "555")), any());
Expand All @@ -163,7 +168,7 @@ public void testDeltaNoExpiry() {
public void testDeltaWithExpiry() {
requestHeaders.add("x-expire-after", "123");

DeltaHandler deltaHandler = new DeltaHandler(redisProvider, null, ruleProvider);
DeltaHandler deltaHandler = new DeltaHandler(vertx, redisProvider, null, ruleProvider, loggingResourceManager, logAppenderRepository);
deltaHandler.handle(request, router);

verify(redisAPI, times(1)).setex(eq("delta:resources:a:b:c"), eq("123"), eq("555"), any());
Expand All @@ -182,7 +187,7 @@ public void testFailingRedisProviderAccess(TestContext context) {
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
when(request.response().end(bodyCaptor.capture())).thenReturn(Future.succeededFuture());

DeltaHandler deltaHandler = new DeltaHandler(redisProvider, null, ruleProvider);
DeltaHandler deltaHandler = new DeltaHandler(vertx, redisProvider, null, ruleProvider, loggingResourceManager, logAppenderRepository);
deltaHandler.handle(request, router);

context.assertEquals(StatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), statusCodeCaptor.getValue(), "StatusCode should be 500");
Expand All @@ -191,7 +196,7 @@ public void testFailingRedisProviderAccess(TestContext context) {

@Test
public void testRejectLimitOffsetParameters(TestContext context) {
DeltaHandler deltaHandler = new DeltaHandler(redisProvider, null, ruleProvider, true);
DeltaHandler deltaHandler = new DeltaHandler(vertx, redisProvider, null, ruleProvider, loggingResourceManager, logAppenderRepository, true);
final DummyHttpServerResponse response = new DummyHttpServerResponse();
DeltaRequest request = new DeltaRequest(MultiMap.caseInsensitiveMultiMap()
.add("delta", "0")
Expand Down
2 changes: 1 addition & 1 deletion gateleen-expansion/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>

<artifactId>gateleen-expansion</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gateleen-hook-js/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>
<artifactId>gateleen-hook-js</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion gateleen-hook/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>

<artifactId>gateleen-hook</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gateleen-kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.swisspush.gateleen</groupId>
<artifactId>gateleen</artifactId>
<version>2.1.7-SNAPSHOT</version>
<version>2.1.8-SNAPSHOT</version>
</parent>

<artifactId>gateleen-kafka</artifactId>
Expand Down
Loading

0 comments on commit b2aa5ac

Please sign in to comment.