Skip to content

Commit

Permalink
Merge pull request #29 from swisspost/feature/defaultHeaders
Browse files Browse the repository at this point in the history
Feature/default headers
  • Loading branch information
mcweba authored Mar 28, 2023
2 parents 80d01a9 + f2880f6 commit 7baf4c8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 15 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ The resources (files) can be zipped in any path depth, the path will be mapped t

The API
--------
HTTP Method: `POST`
URL: `http://localhost:8686/mirror`
payload:
> POST http://localhost:8686/mirror
Payload:

{
"path": "relative/path/to/the/zip.zip" // Relative path (see config parameter mirrorRootPath) to the zip
["content-type": "application/json"] // optional - content-type of the content elements in zip file
}



Configuration
-------------

Expand All @@ -34,9 +33,23 @@ Configuration
"selfClientHost": "localhost" // where the verticle access the zip, standard is localhost
"selfClientPort": 7012 // where the verticle access the zip, standard is 7012
"mirrorHost": "localhost" // where the zip file entries are putted, standard is localhost
"mirrorPort": "7012" // where the zip file entries are putted, standard is 7012
"mirrorPort": 7012 // where the zip file entries are putted, standard is 7012
"mirrorRootPath": "/root" // the root path, that is used to get the zip and to put the resources, standard is "/root"
"internalRequestHeaders": // Array of arrays holding request headers which are added to all outgoing requests
}

Example:
```
{
"serverPort": 8686,
"selfClientHost": "localhost",
"selfClientPort": 7012,
"mirrorHost": "localhost",
"mirrorPort": 7012,
"mirrorRootPath": "/root",
"internalRequestHeaders": [["x-foo", "bar"], ["x-bar", "zzz"]]
}
```

Dependencies
------------
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/swisspush/mirror/MirrorRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -53,11 +54,15 @@ class MirrorRequestHandler {

private ZipEntryPutter zipEntryPutter;

MirrorRequestHandler(HttpClient selfHttpClient, HttpClient mirrorHttpClient, HttpServerResponse response, String mirrorRootPath) {
private final Map<String, String> internalRequestHeaders;

MirrorRequestHandler(HttpClient selfHttpClient, HttpClient mirrorHttpClient, HttpServerResponse response,
String mirrorRootPath, Map<String, String> internalRequestHeaders) {
this.selfHttpClient = selfHttpClient;
this.mirrorHttpClient = mirrorHttpClient;
this.response = response;
this.mirrorRootPath = mirrorRootPath;
this.internalRequestHeaders = internalRequestHeaders;
}


Expand Down Expand Up @@ -110,14 +115,21 @@ private void performDeltaMirror(String path) {
});
}

private void handleInternalRequestHeaders(HttpClientRequest request){
if(internalRequestHeaders != null) {
request.headers().addAll(internalRequestHeaders);
}
}

private void performMirror(String path) {
String mirrorPath = mirrorRootPath + "/mirror/" + path;

LOG.debug("mirror - get zip file: {}", mirrorPath);
mirrorHttpClient.request(HttpMethod.GET, mirrorPath).onComplete(event -> {
HttpClientRequest request = event.result();
handleInternalRequestHeaders(request);
request.exceptionHandler(ex -> {
LOG.error("Exception occured in Mirror-Get-Request to {}", mirrorPath, ex);
LOG.error("Exception occurred in Mirror-Get-Request to {}", mirrorPath, ex);
sendResponse(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), ex.toString());
}).send(asyncResult -> {
HttpClientResponse zipResponse = asyncResult.result();
Expand Down Expand Up @@ -184,7 +196,7 @@ private void performMirror(String path) {
return;
}

zipEntryPutter = new ZipEntryPutter(selfHttpClient, mirrorRootPath, zipIterator);
zipEntryPutter = new ZipEntryPutter(selfHttpClient, mirrorRootPath, zipIterator, internalRequestHeaders);
zipEntryPutter.doneHandler(done -> {
/*
* If all the PUTs were successful and only then, the deltasync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
* <ul>
* <li>receives and parses MirrorRequest</li>
Expand All @@ -31,8 +33,10 @@ public class ResourcesMirrorHandler implements Handler<HttpServerRequest> {
* @param mirrorRootPath the root path, that is used to get the zip and to put the resources
* @param mirrorHttpClient where the verticle access the zip
* @param selfHttpClient where the zip file entries are putted
* @param internalRequestHeaders map containing the request headers to add to all requests
*/
ResourcesMirrorHandler(Vertx vertx, String mirrorRootPath, HttpClient mirrorHttpClient, HttpClient selfHttpClient) {
ResourcesMirrorHandler(Vertx vertx, String mirrorRootPath, HttpClient mirrorHttpClient, HttpClient selfHttpClient,
Map<String, String> internalRequestHeaders) {
this.router = Router.router(vertx);

router.postWithRegex(".*mirror").handler(ctx -> ctx.request().bodyHandler(buffer -> {
Expand Down Expand Up @@ -66,7 +70,8 @@ public class ResourcesMirrorHandler implements Handler<HttpServerRequest> {
// parameter (&delta=x).
String xDeltaSync = body.getString("x-delta-sync");

new MirrorRequestHandler(selfHttpClient, mirrorHttpClient, response, mirrorRootPath).perform(path, xDeltaSync);
new MirrorRequestHandler(selfHttpClient, mirrorHttpClient, response, mirrorRootPath, internalRequestHeaders)
.perform(path, xDeltaSync);
}));
}

Expand Down
44 changes: 41 additions & 3 deletions src/main/java/org/swisspush/mirror/ResourcesMirrorMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
* Initializes the ResourcesMirrorHandler.
Expand All @@ -15,6 +21,8 @@
*/
public class ResourcesMirrorMod extends AbstractVerticle {

private static final Logger LOG = LoggerFactory.getLogger(ResourcesMirrorMod.class);

@Override
public void start(Promise<Void> startPromise) {

Expand All @@ -36,22 +44,52 @@ public void start(Promise<Void> startPromise) {
// the root path used for accessing the zip file
String mirrorRootPath = config.getString("mirrorRootPath", "/root");

JsonArray defaultRequestHeaders = config.getJsonArray("internalRequestHeaders");
Map<String, String> internalRequestHeaders = buildInternalRequestHeaders(defaultRequestHeaders);

final HttpClient selfClient = vertx.createHttpClient(buildHttpClientOptions(selfHost, selfPort, selfTimeout));
final HttpClient mirrorClient = vertx.createHttpClient(buildHttpClientOptions(mirrorHost, mirrorPort, mirrorTimeout));

// in Vert.x 2x 100-continues was activated per default, in vert.x 3x it is off per default.
HttpServerOptions options = new HttpServerOptions().setHandle100ContinueAutomatically(true);

vertx.createHttpServer(options).requestHandler(new ResourcesMirrorHandler(vertx, mirrorRootPath, mirrorClient, selfClient)).listen(serverPort, result -> {
if(result.succeeded()){
vertx.createHttpServer(options).requestHandler(new ResourcesMirrorHandler(vertx, mirrorRootPath, mirrorClient,
selfClient, internalRequestHeaders)).listen(serverPort, result -> {
if (result.succeeded()) {
startPromise.complete();
} else {
startPromise.fail(result.cause());
}
});
}

private HttpClientOptions buildHttpClientOptions(String host, int port, int timeout){
private Map<String, String> buildInternalRequestHeaders(JsonArray headersArray) {
HashMap<String, String> headersMap = new HashMap<>();
if(headersArray == null) {
return headersMap;
}

for (int i = 0; i < headersArray.size(); i++) {
try {
JsonArray array = headersArray.getJsonArray(i);
if(array.size() == 2) {
String headerName = array.getString(0);
String headerValue = array.getString(1);
if(headerName != null && headerValue != null) {
headersMap.put(headerName, headerValue);
}
} else {
LOG.warn("Invalid configuration entry for internal request header: {}", array);
}
} catch (ClassCastException ex) {
LOG.warn("Got invalid configuration resource for internal request headers. Not going to use it!");
}
}

return headersMap;
}

private HttpClientOptions buildHttpClientOptions(String host, int port, int timeout) {
return new HttpClientOptions()
.setDefaultHost(host)
.setDefaultPort(port)
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/swisspush/mirror/ZipEntryPutter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.MimeTypeResolver;

import java.util.Map;

/**
* Visits a ZipInputStream (wrapped in a {@link ZipIterator}) entry by entry
* and executes a http-PUT with its content for each entry.
Expand Down Expand Up @@ -40,10 +42,13 @@ public class ZipEntryPutter {
final JsonArray loadedResources = new JsonArray();
private boolean success = true;

public ZipEntryPutter(HttpClient httpClient, String mirrorRootPath, ZipIterator zipIterator) {
private final Map<String, String> internalRequestHeaders;

public ZipEntryPutter(HttpClient httpClient, String mirrorRootPath, ZipIterator zipIterator, Map<String, String> internalRequestHeaders) {
this.httpClient = httpClient;
this.mirrorRootPath = mirrorRootPath;
this.zipIterator = zipIterator;
this.internalRequestHeaders = internalRequestHeaders;
}

public void doneHandler(Handler<AsyncResult<Void>> doneHandler) {
Expand Down Expand Up @@ -83,6 +88,7 @@ public void handleNext() {
LOG.debug("mirror - put resource: {}", absolutePath);
httpClient.request(HttpMethod.PUT, absolutePath).onComplete(event -> {
HttpClientRequest cReq = event.result();
handleInternalRequestHeaders(cReq);
cReq.exceptionHandler(ex -> {
LOG.error("mirror - error in put request for {}", relativePath, ex);
addLoadedResourceInfo(relativePath, false);
Expand Down Expand Up @@ -116,6 +122,12 @@ public void handleNext() {

}

private void handleInternalRequestHeaders(HttpClientRequest request){
if(internalRequestHeaders != null) {
request.headers().addAll(internalRequestHeaders);
}
}

private void addLoadedResourceInfo(String relativePath, boolean success) {
LOG.debug("result fo {}: success={}", relativePath, success);
if (!success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public static void setupBeforeClass(TestContext context) {
mirrorConfig.put("mirrorRootPath", "");
mirrorConfig.put("serverPort", SERVER_PORT);
mirrorConfig.put("mirrorPort", SOURCE_PORT);

JsonArray internalRequestHeaders = new JsonArray();
JsonArray fooRequestHeader = new JsonArray().add("x-foo").add("zzz");
internalRequestHeaders.add(fooRequestHeader);
mirrorConfig.put("internalRequestHeaders", internalRequestHeaders);

DeploymentOptions mirrorOptions = new DeploymentOptions().setConfig(mirrorConfig);

// deploy verticle (mirror)
Expand Down Expand Up @@ -236,7 +242,7 @@ private void writePUTDeltaResponse(final HttpServerRequest request, final int ex

@Test
public void testMirror_replaceInvalidDeltaParameter(TestContext context) {
MirrorRequestHandler rmh = new MirrorRequestHandler(null, null, null, null);
MirrorRequestHandler rmh = new MirrorRequestHandler(null, null, null, null, null);

/*
* case 1
Expand Down

0 comments on commit 7baf4c8

Please sign in to comment.