forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS3HttpFixture.java
86 lines (72 loc) · 2.94 KB
/
S3HttpFixture.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
package fixture.s3;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.elasticsearch.ExceptionsHelper;
import org.junit.rules.ExternalResource;
import java.io.IOException;
import java.util.Objects;
import java.util.function.BiPredicate;
import static fixture.aws.AwsCredentialsUtils.ANY_REGION;
import static fixture.aws.AwsCredentialsUtils.checkAuthorization;
import static fixture.aws.AwsCredentialsUtils.fixedAccessKey;
import static fixture.aws.AwsFixtureUtils.getLocalFixtureAddress;
public class S3HttpFixture extends ExternalResource {
private HttpServer server;
private final boolean enabled;
private final String bucket;
private final String basePath;
private final BiPredicate<String, String> authorizationPredicate;
public S3HttpFixture(boolean enabled) {
this(enabled, "bucket", "base_path_integration_tests", fixedAccessKey("s3_test_access_key", ANY_REGION, "s3"));
}
public S3HttpFixture(boolean enabled, String bucket, String basePath, BiPredicate<String, String> authorizationPredicate) {
this.enabled = enabled;
this.bucket = bucket;
this.basePath = basePath;
this.authorizationPredicate = authorizationPredicate;
}
protected HttpHandler createHandler() {
return new S3HttpHandler(bucket, basePath) {
@Override
public void handle(final HttpExchange exchange) throws IOException {
try {
if (checkAuthorization(authorizationPredicate, exchange)) {
super.handle(exchange);
}
} catch (Error e) {
// HttpServer catches Throwable, so we must throw errors on another thread
ExceptionsHelper.maybeDieOnAnotherThread(e);
throw e;
}
}
};
}
public String getAddress() {
return "http://" + server.getAddress().getHostString() + ":" + server.getAddress().getPort();
}
public void stop(int delay) {
server.stop(delay);
}
protected void before() throws Throwable {
if (enabled) {
this.server = HttpServer.create(getLocalFixtureAddress(), 0);
this.server.createContext("/", Objects.requireNonNull(createHandler()));
server.start();
}
}
@Override
protected void after() {
if (enabled) {
stop(0);
}
}
}