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);
}
}
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");
}
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);
}
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);
}
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);
}
}
Aggregations