Search in sources :

Example 76 with HttpString

use of io.undertow.util.HttpString in project light-4j by networknt.

the class RestClientTemplate method execute.

protected <T> T execute(ServiceDef serviceDef, String path, Class<T> responseType, Map<String, ?> headerMap, HttpString method, String requestBody) throws RestClientException {
    try {
        Http2ServiceRequest http2ServiceRequest = new Http2ServiceRequest(serviceDef, path, method);
        if (statusCodesValid.isPresent())
            http2ServiceRequest.setStatusCodesValid(statusCodesValid.get());
        http2ServiceRequest.setRequestHeaders(headerMap);
        if (requestBody != null)
            http2ServiceRequest.setRequestBody(requestBody);
        return http2ServiceRequest.callForTypedObject(responseType).get();
    } catch (Exception e) {
        String errorStr = "execute the restful API call error:";
        logger.error(errorStr + e);
        throw new RestClientException(errorStr, e);
    }
}
Also used : Http2ServiceRequest(com.networknt.client.http.Http2ServiceRequest) HttpString(io.undertow.util.HttpString)

Example 77 with HttpString

use of io.undertow.util.HttpString in project light-4j by networknt.

the class ProxyBodyHandler method shouldParseBody.

/**
 * Check to make sure we should actually run the body parse on the current request.
 *
 * @param exchange - http exchange
 * @return - return true if we should run the body parser.
 */
private boolean shouldParseBody(final HttpServerExchange exchange) {
    HttpString method = exchange.getRequestMethod();
    boolean hasBody = method.equals(Methods.POST) || method.equals(Methods.PUT) || method.equals(Methods.PATCH);
    return !config.isSkipProxyBodyHandler() && hasBody && exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE) != null && exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE).startsWith("application/json");
}
Also used : HttpString(io.undertow.util.HttpString)

Example 78 with HttpString

use of io.undertow.util.HttpString in project light-4j by networknt.

the class ContentHandlerTest method testXMLContentType.

@Test
public void testXMLContentType() throws Exception {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    final String defaultContentType = "application/xml";
    final String defaultHeader = "Content-Type";
    try {
        final ClientRequest request = new ClientRequest().setPath("/xml").setMethod(Methods.GET);
        request.getRequestHeaders().put(Headers.HOST, "localhost");
        request.getRequestHeaders().put(new HttpString(defaultHeader), defaultContentType);
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    final int statusCode = reference.get().getResponseCode();
    final HeaderMap headerMap = reference.get().getResponseHeaders();
    final String header = headerMap.getFirst(defaultHeader);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(header);
    Assert.assertEquals(header, defaultContentType);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) HeaderMap(io.undertow.util.HeaderMap) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 79 with HttpString

use of io.undertow.util.HttpString in project light-4j by networknt.

the class ContentHandlerTest method testTextPlainContentType.

@Test
public void testTextPlainContentType() throws Exception {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    final String defaultContentType = "text/plain";
    final String defaultHeader = "Content-Type";
    try {
        final ClientRequest request = new ClientRequest().setPath("/").setMethod(Methods.GET);
        request.getRequestHeaders().put(Headers.HOST, "localhost");
        request.getRequestHeaders().put(new HttpString(defaultHeader), defaultContentType);
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    final int statusCode = reference.get().getResponseCode();
    final HeaderMap headerMap = reference.get().getResponseHeaders();
    final String header = headerMap.getFirst(defaultHeader);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(header);
    Assert.assertEquals(header, defaultContentType);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) HeaderMap(io.undertow.util.HeaderMap) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 80 with HttpString

use of io.undertow.util.HttpString in project light-4j by networknt.

the class CorsHttpHandlerTest method testOptionsWrongOrigin.

@Test
public void testOptionsWrongOrigin() throws Exception {
    String url = "http://localhost:7080";
    Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/").setMethod(Methods.OPTIONS);
        request.getRequestHeaders().put(Headers.HOST, "localhost");
        request.getRequestHeaders().put(new HttpString("Origin"), "http://example.com");
        request.getRequestHeaders().put(new HttpString("Access-Control-Request-Method"), "POST");
        request.getRequestHeaders().put(new HttpString("Access-Control-Request-Headers"), "X-Requested-With");
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    HeaderMap headerMap = reference.get().getResponseHeaders();
    String header = headerMap.getFirst("Access-Control-Allow-Origin");
    Assert.assertEquals(200, statusCode);
    if (statusCode == 200) {
        Assert.assertNull(header);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpString(io.undertow.util.HttpString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) HeaderMap(io.undertow.util.HeaderMap) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Aggregations

HttpString (io.undertow.util.HttpString)147 HeaderMap (io.undertow.util.HeaderMap)31 HttpServerExchange (io.undertow.server.HttpServerExchange)27 Test (org.junit.Test)25 ClientRequest (io.undertow.client.ClientRequest)23 Map (java.util.Map)23 ClientResponse (io.undertow.client.ClientResponse)21 IOException (java.io.IOException)21 HashMap (java.util.HashMap)21 URI (java.net.URI)19 ClientConnection (io.undertow.client.ClientConnection)17 HttpHandler (io.undertow.server.HttpHandler)17 Http2Client (com.networknt.client.Http2Client)14 ClientException (com.networknt.exception.ClientException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 User (com.networknt.portal.usermanagement.model.common.model.user.User)13 URISyntaxException (java.net.URISyntaxException)13 ArrayList (java.util.ArrayList)13 List (java.util.List)13 CountDownLatch (java.util.concurrent.CountDownLatch)11