Skip to content

Commit

Permalink
4.x: Refactor Http class (#7570)
Browse files Browse the repository at this point in the history
* Move HTTP method to a top level class
* Move HTTP status to a top level class
* Move HTTP HeaderName to a top level class
* Move HTTP HeaderNames to a top level class
* Move HTTP Header to a top level class
* Move HTTP Headers to a top level class HeaderValues
* Move HTTP DateTime to a top level class
* Tying loose ends (archetypes, deprecation)
  • Loading branch information
tomas-langer committed Sep 12, 2023
1 parent c4860f1 commit 7dce029
Show file tree
Hide file tree
Showing 475 changed files with 5,576 additions and 5,172 deletions.
9 changes: 3 additions & 6 deletions archetypes/helidon/src/main/archetype/common/extra.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ java -cp "target/classes:target/libs/*" {{package}}.WebClientMain PORT
<list key="modules">
<value if="${flavor} == 'mp'">io.helidon.microprofile.faulttolerance</value>
</list>
<list key="Main-helidon-imports" if="${flavor} == 'mp'">
<value>io.helidon.http.Http</value>
</list>
<list key="Main-other-imports" if="${flavor} == 'mp'">
<value>java.util.concurrent.TimeoutException</value>
</list>
Expand Down Expand Up @@ -325,9 +322,9 @@ restrictive-cors:
<value>io.helidon.cors.CrossOriginConfig</value>
</list>
<list key="MainTest-static-imports" if="${flavor} == 'se'">
<value>io.helidon.http.Http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN</value>
<value>io.helidon.http.Http.HeaderNames.HOST</value>
<value>io.helidon.http.Http.HeaderNames.ORIGIN</value>
<value>io.helidon.http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN</value>
<value>io.helidon.http.HeaderNames.HOST</value>
<value>io.helidon.http.HeaderNames.ORIGIN</value>
</list>
<list key="MainTest-static-imports">
<value if="${flavor} == 'se'">org.hamcrest.CoreMatchers.containsString</value>
Expand Down
18 changes: 10 additions & 8 deletions archetypes/helidon/src/main/archetype/common/media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
try (Http1ClientResponse response = client.get("/greet")
.request()) {
assertThat(response.status(), is(Http.Status.OK_200));
assertThat(response.status(), is(Status.OK_200));
JsonObject json = response.as(JsonObject.class);
assertThat(json.getString("message"), is("Hello World!"));
}
Expand Down Expand Up @@ -113,15 +113,15 @@
@Test
void testGreet() {
try (Http1ClientResponse response = client.get("/greet").request()) {
assertThat(response.status(), is(Http.Status.OK_200));
assertThat(response.status(), is(Status.OK_200));
assertThat(response.as(Message.class).getMessage(), is("Hello World!"));
}
}
@Test
void testGreetJoe() {
try (Http1ClientResponse response = client.get("/greet/Joe").request()) {
assertThat(response.status(), is(Http.Status.OK_200));
assertThat(response.status(), is(Status.OK_200));
assertThat(response.as(Message.class).getMessage(), is("Hello Joe!"));
}
}
Expand Down Expand Up @@ -171,14 +171,16 @@
<value>jersey.media.multipart</value>
</list>
<list key="Main-helidon-imports" if="${flavor} == 'se'">
<value>io.helidon.http.Http</value>
<value>io.helidon.http.Http.Header</value>
<value>io.helidon.http.Header</value>
<value>io.helidon.http.Status</value>
<value>io.helidon.http.HeaderNames</value>
<value>io.helidon.http.HeaderValues</value>
<value>io.helidon.webserver.staticcontent.StaticContentService</value>
</list>
<list key="Main-routing-builder" if="${flavor} == 'se'">
<value order="1"><![CDATA[.any("/", (req, res) -> {
res.status(Http.Status.MOVED_PERMANENTLY_301);
res.header(Http.Headers.createCached(Http.HeaderNames.LOCATION, "/ui"));
res.status(Status.MOVED_PERMANENTLY_301);
res.header(HeaderValues.createCached(HeaderNames.LOCATION, "/ui"));
res.send();
})
.register("/ui", StaticContentService.builder("WEB")
Expand All @@ -191,7 +193,7 @@
@Test
void testFileService() {
try (Http1ClientResponse response = client.get("/api").request()) {
assertThat(response.status(), is(Http.Status.OK_200));
assertThat(response.status(), is(Status.OK_200));
}
}
]]></value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ curl -s -X GET http://localhost:8080/health
@Test
void testHealth() {
try (Http1ClientResponse response = client.get("/observe/health").request()) {
assertThat(response.status(), is(Http.Status.OK_200));
assertThat(response.status(), is(Status.OK_200));
assertThat(response.as(String.class), containsString("status\":\"UP"));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package {{package}};

import io.helidon.http.Http;
import io.helidon.http.HeaderNames;
import io.helidon.config.Config;
import io.helidon.cors.CrossOriginConfig;
import io.helidon.microprofile.tests.junit5.HelidonTest;
Expand Down Expand Up @@ -29,46 +29,46 @@ class TestCORS {
void testAnonymousGreetWithCors() {
Response r = target.path("/simple-greet")
.request()
.header(Http.HeaderNames.ORIGIN.defaultCase(), "http://foo.com")
.header(Http.HeaderNames.HOST.defaultCase(), "here.com")
.header(HeaderNames.ORIGIN.defaultCase(), "http://foo.com")
.header(HeaderNames.HOST.defaultCase(), "here.com")
.get();
assertThat("HTTP response", r.getStatus(), is(200));
String payload = fromPayload(r);
assertThat("HTTP response payload", payload.contains("Hello World!"), is(true));
assertThat("CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN,
r.getHeaders().getFirst(Http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.defaultCase()),
r.getHeaders().getFirst(HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.defaultCase()),
is("http://foo.com"));
}

@Test
void testCustomGreetingWithCors() {
Response r = target.path("/simple-greet")
.request()
.header(Http.HeaderNames.ORIGIN.defaultCase(), "http://foo.com")
.header(Http.HeaderNames.HOST.defaultCase(), "here.com")
.header(HeaderNames.ORIGIN.defaultCase(), "http://foo.com")
.header(HeaderNames.HOST.defaultCase(), "here.com")
.header("Access-Control-Request-Method", "PUT")
.options();
assertThat("pre-flight status", r.getStatus(), is(200));
MultivaluedMap<String, Object> responseHeaders = r.getHeaders();
assertThat("Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS,
r.getHeaders().getFirst(Http.HeaderNames.ACCESS_CONTROL_ALLOW_METHODS.defaultCase()),
r.getHeaders().getFirst(HeaderNames.ACCESS_CONTROL_ALLOW_METHODS.defaultCase()),
is("PUT"));
assertThat( "Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN,
r.getHeaders().getFirst(Http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.defaultCase()),
r.getHeaders().getFirst(HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.defaultCase()),
is("http://foo.com"));
Invocation.Builder builder = target.path("/simple-greet")
.request()
.headers(responseHeaders)
.header(Http.HeaderNames.ORIGIN.defaultCase(), "http://foo.com")
.header(Http.HeaderNames.HOST.defaultCase(), "here.com");
.header(HeaderNames.ORIGIN.defaultCase(), "http://foo.com")
.header(HeaderNames.HOST.defaultCase(), "here.com");
r = putResponse("Cheers", builder);
assertThat("HTTP response3", r.getStatus(), is(200));
assertThat( "Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN,
r.getHeaders().getFirst(Http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.defaultCase()),
r.getHeaders().getFirst(HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.defaultCase()),
is("http://foo.com"));
assertThat(fromPayload(r), containsString("Cheers World!"));
}
Expand All @@ -77,8 +77,8 @@ class TestCORS {
void testGreetingChangeWithCorsAndOtherOrigin() {
Invocation.Builder builder = target.path("/simple-greet")
.request()
.header(Http.HeaderNames.ORIGIN.defaultCase(), "http://other.com")
.header(Http.HeaderNames.HOST.defaultCase(), "here.com");
.header(HeaderNames.ORIGIN.defaultCase(), "http://other.com")
.header(HeaderNames.HOST.defaultCase(), "here.com");
Response r = putResponse("Ahoy", builder);
boolean isOverriding = Config.create().get("cors").exists();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<value>io.helidon.webserver.http.HttpRouting</value>
</list>
<list key="MainTest-helidon-imports">
<value>io.helidon.http.Http</value>
<value>io.helidon.http.Status</value>
<value>io.helidon.config.Config</value>
<value>io.helidon.webserver.testing.junit5.SetUpRoute</value>
<value>io.helidon.webclient.http1.Http1Client</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@Test
void testSimpleGreet() {
try (Http1ClientResponse response = client.get("/simple-greet").request()) {
assertThat(response.status(), is(Http.Status.OK_200));
assertThat(response.status(), is(Status.OK_200));
assertThat(response.as(String.class), is("Hello World!"));
}
}
Expand Down
18 changes: 9 additions & 9 deletions archetypes/helidon/src/main/archetype/se/custom/extra.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@
</map>
</list>
<list key="Main-helidon-imports" if="${extra} contains 'fault-tolerance'">
<value>io.helidon.http.Http</value>
<value>io.helidon.http.Status</value>
<value>io.helidon.faulttolerance.BulkheadException</value>
<value>io.helidon.faulttolerance.CircuitBreakerOpenException</value>
<value>io.helidon.faulttolerance.TimeoutException</value>
</list>
<list key="Main-routing-builder" if="${extra} contains 'fault-tolerance'">
<value><![CDATA[.register("/", new FtService())
.error(BulkheadException.class,
(req, res, ex) -> res.status(Http.Status.SERVICE_UNAVAILABLE_503).send("bulkhead"))
(req, res, ex) -> res.status(Status.SERVICE_UNAVAILABLE_503).send("bulkhead"))
.error(CircuitBreakerOpenException.class,
(req, res, ex) -> res.status(Http.Status.SERVICE_UNAVAILABLE_503).send("circuit breaker"))
(req, res, ex) -> res.status(Status.SERVICE_UNAVAILABLE_503).send("circuit breaker"))
.error(TimeoutException.class,
(req, res, ex) -> res.status(Http.Status.REQUEST_TIMEOUT_408).send("timeout"))
(req, res, ex) -> res.status(Status.REQUEST_TIMEOUT_408).send("timeout"))
.error(Throwable.class,
(req, res, ex) -> res.status(Http.Status.INTERNAL_SERVER_ERROR_500)
(req, res, ex) -> res.status(Status.INTERNAL_SERVER_ERROR_500)
.send(ex.getClass().getName() + ": " + ex.getMessage()))]]></value>
</list>
<list key="Abstract-tests" if="${extra} contains 'fault-tolerance'">
Expand Down Expand Up @@ -75,7 +75,7 @@
try (Http1ClientResponse third = client.get().path("/bulkhead/10000").request()) {
// registered an error handler in Main
assertThat(third.status(), is(Http.Status.OK_200));
assertThat(third.status(), is(Status.OK_200));
assertThat(third.as(String.class), is("Call rejected: 1"));
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@
.request();
// registered an error handler in Main
assertThat(clientResponse.status(), is(Http.Status.SERVICE_UNAVAILABLE_503));
assertThat(clientResponse.status(), is(Status.SERVICE_UNAVAILABLE_503));
assertThat(clientResponse.as(String.class), is("circuit breaker"));
}
Expand Down Expand Up @@ -163,7 +163,7 @@
.request()) {
// no error handler specified
assertThat(clientResponse.status(), is(Http.Status.INTERNAL_SERVER_ERROR_500));
assertThat(clientResponse.status(), is(Status.INTERNAL_SERVER_ERROR_500));
assertThat(clientResponse.as(String.class), is("java.lang.RuntimeException: reactive failure"));
}
}
Expand All @@ -181,7 +181,7 @@
.path("/timeout/1000")
.request()) {
// error handler specified in Main
assertThat(clientResponse.status(), is(Http.Status.REQUEST_TIMEOUT_408));
assertThat(clientResponse.status(), is(Status.REQUEST_TIMEOUT_408));
assertThat(clientResponse.as(String.class), is("timeout"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import java.util.Map;
import java.util.stream.Stream;

import io.helidon.http.ContentDisposition;
import io.helidon.http.Http;
import io.helidon.http.Http.Header;
import io.helidon.http.Header;
import io.helidon.http.HeaderValues;
import io.helidon.http.HeaderNames;
import io.helidon.http.ServerResponseHeaders;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.http.media.multipart.MultiPart;
Expand All @@ -25,15 +26,15 @@ import jakarta.json.Json;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonBuilderFactory;

import static io.helidon.http.Http.Status.BAD_REQUEST_400;
import static io.helidon.http.Http.Status.MOVED_PERMANENTLY_301;
import static io.helidon.http.Http.Status.NOT_FOUND_404;
import static io.helidon.http.Status.BAD_REQUEST_400;
import static io.helidon.http.Status.MOVED_PERMANENTLY_301;
import static io.helidon.http.Status.NOT_FOUND_404;

/**
* File service.
*/
final class FileService implements HttpService {
private static final Http.Header UI_LOCATION = Http.Headers.createCached(Http.HeaderNames.LOCATION, "/ui");
private static final Header UI_LOCATION = HeaderValues.createCached(HeaderNames.LOCATION, "/ui");
private final JsonBuilderFactory jsonFactory;
private final Path storage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.concurrent.atomic.AtomicReference;
{{#GreetService-imports}}
{{.}}
{{/GreetService-imports}}
import io.helidon.http.Http;
import io.helidon.http.Status;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.HttpService;
import io.helidon.webserver.http.ServerRequest;
Expand Down Expand Up @@ -80,13 +80,13 @@ public class GreetService implements HttpService {
if (message.getGreeting() == null) {
Message errorMessage = new Message();
errorMessage.setMessage("No greeting provided");
response.status(Http.Status.BAD_REQUEST_400)
response.status(Status.BAD_REQUEST_400)
.send(errorMessage);
return;
}

greeting.set(message.getGreeting());
response.status(Http.Status.NO_CONTENT_204).send();
response.status(Status.NO_CONTENT_204).send();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.concurrent.atomic.AtomicReference;
{{#GreetService-imports}}
{{.}}
{{/GreetService-imports}}
import io.helidon.http.Http;
import io.helidon.http.Status;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.HttpService;
import io.helidon.webserver.http.ServerRequest;
Expand Down Expand Up @@ -105,13 +105,13 @@ class GreetService implements HttpService {
JsonObject jsonErrorObject = JSON.createObjectBuilder()
.add("error", "No greeting provided")
.build();
response.status(Http.Status.BAD_REQUEST_400)
response.status(Status.BAD_REQUEST_400)
.send(jsonErrorObject);
return;
}

greeting.set(jo.getString("greeting"));
response.status(Http.Status.NO_CONTENT_204).send();
response.status(Status.NO_CONTENT_204).send();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.concurrent.atomic.AtomicReference;
{{#GreetService-imports}}
{{.}}
{{/GreetService-imports}}
import io.helidon.http.Http;
import io.helidon.http.Status;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.HttpService;
import io.helidon.webserver.http.ServerRequest;
Expand Down Expand Up @@ -91,7 +91,7 @@ class GreetService implements HttpService {

private void updateGreeting(String update, ServerResponse response) {
greeting.set(update);
response.status(Http.Status.NO_CONTENT_204).send();
response.status(Status.NO_CONTENT_204).send();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.util.concurrent.atomic.AtomicReference;
{{#MetricsService-imports}}
import {{.}};
{{/MetricsService-imports}}
import io.helidon.http.Http;
import io.helidon.config.Config;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.HttpService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package {{package}};

import java.util.concurrent.TimeUnit;

import io.helidon.http.Http;
import io.helidon.http.Header;
import io.helidon.config.Config;
import io.helidon.config.ConfigSources;
import io.helidon.webserver.WebServer;
Expand Down Expand Up @@ -84,7 +84,7 @@ public final class OutboundOverrideJwt {
.get("/hello", (req, res) -> {
// This is the token. It should be bearer <signed JWT base64 encoded>
req.headers().first(Http.Header.AUTHORIZATION)
req.headers().first(Header.AUTHORIZATION)
.ifPresent(System.out::println);
String username = req.context()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package {{package}};

import io.helidon.common.LazyValue;
import io.helidon.common.context.Contexts;
import io.helidon.http.Http;
import io.helidon.http.HttpMediaTypes;
import io.helidon.http.Status;
import io.helidon.webclient.http1.Http1Client;
import io.helidon.webclient.http1.Http1ClientResponse;
import io.helidon.webserver.WebServer;
Expand Down Expand Up @@ -42,7 +42,7 @@ class Service1 implements HttpService {
.get(SecurityContext.class)
.ifPresentOrElse(context -> {
try (Http1ClientResponse clientRes = client.get().get(path).request()) {
if (clientRes.status() == Http.Status.OK_200) {
if (clientRes.status() == Status.OK_200) {
res.send(clientRes.entity().as(String.class));
} else {
res.send("Request failed, status: " + clientRes.status());
Expand Down
Loading

0 comments on commit 7dce029

Please sign in to comment.