use of io.helidon.common.http.Http in project helidon by oracle.
the class ClientMain method main.
/**
* Executes WebClient examples.
*
* If no argument provided it will take server port from configuration server.port.
*
* User can override port from configuration by main method parameter with the specific port.
*
* @param args main method
*/
public static void main(String[] args) {
Config config = Config.create();
String url;
if (args.length == 0) {
ConfigValue<Integer> port = config.get("server.port").asInt();
if (!port.isPresent() || port.get() == -1) {
throw new IllegalStateException("Unknown port! Please specify port as a main method parameter " + "or directly to config server.port");
}
url = "http://localhost:" + port.get() + "/greet";
} else {
url = "http://localhost:" + Integer.parseInt(args[0]) + "/greet";
}
WebClient webClient = WebClient.builder().baseUri(url).config(config.get("client")).addMediaSupport(JsonpSupport.create()).build();
performPutMethod(webClient).flatMapSingle(it -> performGetMethod(webClient)).flatMapSingle(it -> followRedirects(webClient)).flatMapSingle(it -> getResponseAsAnJsonObject(webClient)).flatMapSingle(it -> saveResponseToFile(webClient)).flatMapSingle(it -> clientMetricsExample(url, config)).await();
}
use of io.helidon.common.http.Http in project helidon by oracle.
the class Main method routingAsFilter.
/**
* All routing rules (routes) are evaluated in a definition order. The {@link Handler} assigned with the first valid route
* for given request is called. It is a responsibility of each handler to process in one of the following ways:
* <ul>
* <li>Respond using one of {@link io.helidon.webserver.ServerResponse#send() ServerResponse.send(...)} method.</li>
* <li>Continue to next valid route using {@link io.helidon.webserver.ServerRequest#next() ServerRequest.next()} method.
* <i>It is possible to define filtering handlers.</i></li>
* </ul>
* <p>
* If no valid {@link Handler} is found then routing respond by {@code HTTP 404} code.
* <p>
* If selected {@link Handler} doesn't process request than the request <b>stacks</b>!
* <p>
* <b>Blocking operations:</b><br>
* For performance reason, {@link Handler} can be called directly by a selector thread. It is not good idea to block
* such thread. If request must be processed by a blocking operation then such processing should be deferred to another
* thread.
*/
public void routingAsFilter() {
Routing routing = Routing.builder().any((req, res) -> {
System.out.println(req.method() + " " + req.path());
// Filters are just routing handlers which calls next()
req.next();
}).post("/post-endpoint", (req, res) -> res.status(Http.Status.CREATED_201).send()).get("/get-endpoint", (req, res) -> res.status(Http.Status.NO_CONTENT_204).send("Hello World!")).build();
startServer(routing);
}
use of io.helidon.common.http.Http in project helidon by oracle.
the class Main method errorHandling.
/**
* Request processing can cause error represented by {@link Throwable}. It is possible to register custom
* {@link io.helidon.webserver.ErrorHandler ErrorHandlers} for specific processing.
* <p>
* If error is not processed by a custom {@link io.helidon.webserver.ErrorHandler ErrorHandler} than default one is used.
* It respond with <i>HTTP 500 code</i> unless error is not represented
* by {@link HttpException HttpException}. In such case it reflects its content.
*/
public void errorHandling() {
Routing routing = Routing.builder().post("/compute", Handler.create(String.class, (req, res, str) -> {
int result = 100 / Integer.parseInt(str);
res.send(String.valueOf("100 / " + str + " = " + result));
})).error(Throwable.class, (req, res, ex) -> {
ex.printStackTrace(System.out);
req.next();
}).error(NumberFormatException.class, (req, res, ex) -> res.status(Http.Status.BAD_REQUEST_400).send()).error(ArithmeticException.class, (req, res, ex) -> res.status(Http.Status.PRECONDITION_FAILED_412).send()).build();
startServer(routing);
}
use of io.helidon.common.http.Http in project helidon by oracle.
the class RestApiBase method requestJsonPayload.
/**
* Create a supplier for a response with JSON request.
* Defaults to "{@code () -> clientRequest.submit(jsonObject)}".
* Also configures content type and accept headers.
*
* @param path path requested
* @param request API request
* @param method HTTP method
* @param requestId ID of this request
* @param requestBuilder {@link io.helidon.webclient.WebClient} request builder
* @param jsonObject JSON object that should be sent as a request entity
*
* @return supplier of a web client response
*/
protected Supplier<Single<WebClientResponse>> requestJsonPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, JsonObject jsonObject) {
requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
AtomicBoolean updated = new AtomicBoolean();
return () -> {
// we should only update request builder once - if a retry is done, it should not be reset
if (updated.compareAndSet(false, true)) {
Single<WebClientRequestBuilder> res = updateRequestBuilder(requestBuilder, path, request, method, requestId, jsonObject);
return res.flatMapSingle(it -> it.submit(jsonObject));
} else {
return requestBuilder.submit(jsonObject);
}
};
}
use of io.helidon.common.http.Http in project helidon by oracle.
the class RestApiBase method requestBytesPayload.
/**
* Create a supplier for a response with publisher request.
* Defaults to "{@code () -> clientRequest.submit(publisher)}".
* Also configures content type and accept headers.
*
* @param path path requested
* @param request API request
* @param method HTTP method
* @param requestId ID of this request
* @param requestBuilder {@link io.helidon.webclient.WebClient} request builder
* @param publisher publisher to be used as request entity
*
* @return supplier of a web client response
*/
protected Supplier<Single<WebClientResponse>> requestBytesPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, Flow.Publisher<DataChunk> publisher) {
requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_OCTET_STREAM));
AtomicBoolean updated = new AtomicBoolean();
return () -> {
if (updated.compareAndSet(false, true)) {
return updateRequestBuilderBytesPayload(requestBuilder, path, request, method, requestId).flatMapSingle(it -> it.submit(publisher));
} else {
return requestBuilder.submit(publisher);
}
};
}
Aggregations