Skip to content

Commit

Permalink
Merge upstream changes from develop to feature
Browse files Browse the repository at this point in the history
Reason for the conflict is here:
swisspost#577 (review)

Conflicts:
      gateleen-delegate/src/test/java/org/swisspush/gateleen/delegate/DelegateTest.java
  • Loading branch information
hiddenalpha committed May 24, 2024
2 parents 02a059a + 7ec503b commit cc94fb3
Show file tree
Hide file tree
Showing 38 changed files with 227 additions and 48 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.5-SNAPSHOT</version>
<version>2.1.7-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.5-SNAPSHOT</version>
<version>2.1.7-SNAPSHOT</version>
</parent>

<artifactId>gateleen-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum StatusCode {
GONE(410, "Gone"),
LENGTH_REQUIRED(411, "Length Required"),
PRECONDITION_FAILED(412, "Precondition Failed"),
CONTENT_TOO_LARGE(413, "Content Too Large"),
PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
URI_TOO_LONG(414, "URI Too Long"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
RANGE_NOT_SATISFIABLE(416, "Range Not Satisfiable"),
Expand Down
4 changes: 4 additions & 0 deletions gateleen-delegate/README_delegate.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,7 @@ Also you have to enable the logging on the [DelegateHandler](src/main/java/org/s
```java
delegateHandler.enableResourceLogging(true);
```
### Unmatched delegate handling
The default implementation of _Delegate_ and _DelegateHandler_ end the original request with `200 OK` when the _pattern_ or the _methods_ do not match.

To override this behaviour, a _StatusCode_ can be provided in the constructor of _DelegateHandler_ as `unmatchedDelegateStatusCode` to define how the original requests should be responded when the called delegate does not match.
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.5-SNAPSHOT</version>
<version>2.1.7-SNAPSHOT</version>
</parent>

<artifactId>gateleen-delegate</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.swisspush.gateleen.core.util.HttpServerRequestUtil;
import org.swisspush.gateleen.core.util.StatusCode;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -43,6 +44,7 @@ public class Delegate {
private final Pattern pattern;
private final Set<HttpMethod> methods;
private final List<DelegateRequest> requests;
private final StatusCode unmatchedDelegateStatusCode;
private boolean delegateContainsJoltSpecRequest = false;

/**
Expand All @@ -54,12 +56,15 @@ public class Delegate {
* @param methods methods of the delegate
* @param requests requests of the delegate
*/
public Delegate(final ClientRequestCreator clientRequestCreator, final String name, final Pattern pattern, final Set<HttpMethod> methods, final List<DelegateRequest> requests) {
public Delegate(final ClientRequestCreator clientRequestCreator, final String name, final Pattern pattern,
final Set<HttpMethod> methods, final List<DelegateRequest> requests,
@Nullable StatusCode unmatchedDelegateStatusCode) {
this.clientRequestCreator = clientRequestCreator;
this.name = name;
this.pattern = pattern;
this.methods = methods;
this.requests = requests;
this.unmatchedDelegateStatusCode = unmatchedDelegateStatusCode;

this.delegateContainsJoltSpecRequest = doesDelegateContainJoltSpecRequest();
}
Expand Down Expand Up @@ -107,8 +112,15 @@ public void handle(final HttpServerRequest request) {
}
}

// end response, if nothing matches
request.response().end();
// when delegate not matched and status code is defined, respond with defined status code
if(unmatchedDelegateStatusCode != null) {
request.response().setStatusCode(unmatchedDelegateStatusCode.getStatusCode());
request.response().setStatusMessage(unmatchedDelegateStatusCode.getStatusMessage());
request.response().end();
} else {
// when delegate not matched and no status code is defined, just end response with 200 OK
request.response().end();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import org.swisspush.gateleen.core.json.transform.JoltSpec;
import org.swisspush.gateleen.core.json.transform.JoltSpecBuilder;
import org.swisspush.gateleen.core.json.transform.JoltSpecException;
import org.swisspush.gateleen.core.util.StatusCode;
import org.swisspush.gateleen.core.util.StringUtils;
import org.swisspush.gateleen.core.validation.ValidationResult;
import org.swisspush.gateleen.validation.ValidationException;
import org.swisspush.gateleen.validation.Validator;

import javax.annotation.Nullable;
import java.util.*;
import java.util.regex.Pattern;

Expand All @@ -40,17 +42,22 @@ public class DelegateFactory {
private final Map<String, Object> properties;
private final String delegatesSchema;

private final StatusCode unmatchedDelegateStatusCode;

/**
* Creates a new instance of the DelegateFactory.
*
* @param clientRequestCreator
* @param properties
* @param delegatesSchema
* @param unmatchedDelegateStatusCode
*/
public DelegateFactory(final ClientRequestCreator clientRequestCreator, final Map<String, Object> properties, final String delegatesSchema) {
public DelegateFactory(final ClientRequestCreator clientRequestCreator, final Map<String, Object> properties,
final String delegatesSchema, @Nullable StatusCode unmatchedDelegateStatusCode) {
this.clientRequestCreator = clientRequestCreator;
this.properties = properties;
this.delegatesSchema = delegatesSchema;
this.unmatchedDelegateStatusCode = unmatchedDelegateStatusCode;
}

/**
Expand Down Expand Up @@ -125,7 +132,7 @@ private Delegate createDelegate(final String delegateName, final String configSt
requests.add(new DelegateRequest(requestJsonObject, joltSpec, headerFunction));
}

return new Delegate(clientRequestCreator, delegateName, pattern, methods, requests);
return new Delegate(clientRequestCreator, delegateName, pattern, methods, requests, unmatchedDelegateStatusCode);
}

private JoltSpec parsePayloadTransformSpec(JsonObject requestJsonObject, String delegateName) throws ValidationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import org.swisspush.gateleen.core.storage.ResourceStorage;
import org.swisspush.gateleen.core.util.ResourcesUtils;
import org.swisspush.gateleen.core.util.StatusCode;
import org.swisspush.gateleen.monitoring.MonitoringHandler;
import org.swisspush.gateleen.validation.ValidationException;

import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
Expand Down Expand Up @@ -83,22 +83,37 @@ public class DelegateHandler implements Refreshable, LoggableResource {
* @param vertx vertx
* @param selfClient selfClient
* @param delegateStorage delegateStorage - only used for storing delegates
* @param monitoringHandler monitoringHandler
* @param delegatesUri delegate root
* @param properties properties
* @param doneHandler doneHandler
*/
public DelegateHandler(final Vertx vertx, final HttpClient selfClient, final ResourceStorage delegateStorage,
final MonitoringHandler monitoringHandler, final String delegatesUri,
final Map<String, Object> properties,
final Handler<Void> doneHandler) {
final String delegatesUri, final Map<String, Object> properties, final Handler<Void> doneHandler) {
this(vertx, selfClient, delegateStorage, delegatesUri, properties, null, doneHandler);
}

/**
* Creates a new instance of the DelegateHandler.
*
* @param vertx vertx
* @param selfClient selfClient
* @param delegateStorage delegateStorage - only used for storing delegates
* @param delegatesUri delegate root
* @param properties properties
* @param unmatchedDelegateStatusCode respond requests with this status code when not matched
* @param doneHandler doneHandler
*/
public DelegateHandler(final Vertx vertx, final HttpClient selfClient, final ResourceStorage delegateStorage,
final String delegatesUri, final Map<String, Object> properties,
@Nullable StatusCode unmatchedDelegateStatusCode, final Handler<Void> doneHandler) {
this.vertx = vertx;
this.delegateStorage = delegateStorage;
this.delegatesUri = delegatesUri;
this.doneHandler = doneHandler;

String delegatesSchema = ResourcesUtils.loadResource("gateleen_delegate_schema_delegates", true);
this.delegateFactory = new DelegateFactory(new ClientRequestCreator(selfClient), properties, delegatesSchema);
this.delegateFactory = new DelegateFactory(new ClientRequestCreator(selfClient), properties,
delegatesSchema, unmatchedDelegateStatusCode);

delegateNamePattern = Pattern.compile(delegatesUri + "([^/]+)(/" + DEFINITION_RESOURCE + "|/"+ EXECUTION_RESOURCE + ".*" + "|/?)");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void setUp() {
Mockito.when(vertx.eventBus()).thenReturn(Mockito.mock(EventBus.class));
Map<String, Object> properties = new HashMap<>();

delegateFactory = new DelegateFactory(new ClientRequestCreator(Mockito.mock(HttpClient.class)), properties, delegatesSchema);
delegateFactory = new DelegateFactory(new ClientRequestCreator(Mockito.mock(HttpClient.class)), properties, delegatesSchema, null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package org.swisspush.gateleen.delegate;

import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.swisspush.gateleen.core.http.DummyHttpServerRequest;

import static org.mockito.Mockito.verifyNoInteractions;

/**
* Tests some features of the DelegateHandler.
* Tests some features of the {@link DelegateHandler}.
*
* @author https://github.com/ljucam [Mario Ljuca]
*/
public class TestDelegateHandler {
public class DelegateHandlerTest {
private static final String DELEGATE_URI = "/gateleen/server/delegate/v1/delegates/";
private static DelegateHandler delegateHandler;

@BeforeClass
public static void init() {
delegateHandler = new DelegateHandler(null, null, null, null, DELEGATE_URI, null, null);
delegateHandler = new DelegateHandler(null, null, null, DELEGATE_URI,
null, null);
}

@Test
public void testHandle() {
String delegateName = "aName";
HttpServerResponse response = Mockito.mock(HttpServerResponse.class);

verifyNoInteractions(response);
Assert.assertFalse(delegateHandler.handle(new CustomHttpServerRequest(DELEGATE_URI + delegateName + "/blah", response)));
Assert.assertFalse(delegateHandler.handle(new CustomHttpServerRequest(DELEGATE_URI + delegateName, response)));
}

@Test
Expand Down Expand Up @@ -55,4 +72,29 @@ public void testGetDelegateName_Recognition() {

// --------------
}

private static class CustomHttpServerRequest extends DummyHttpServerRequest {

private final String uri;
private final HttpServerResponse response;

public CustomHttpServerRequest(String uri, HttpServerResponse response) {
this.uri = uri;
this.response = response;
}

@Override public String uri() {
return uri;
}

@Override public HttpMethod method() {
return HttpMethod.GET;
}

@Override
public HttpServerResponse response() {
return response != null ? response : super.response();
}

}
}
Loading

0 comments on commit cc94fb3

Please sign in to comment.