本文整理汇总了Java中io.undertow.client.ClientResponse类的典型用法代码示例。如果您正苦于以下问题:Java ClientResponse类的具体用法?Java ClientResponse怎么用?Java ClientResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClientResponse类属于io.undertow.client包,在下文中一共展示了ClientResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: prepareResponseChannel
import io.undertow.client.ClientResponse; //导入依赖的package包/类
private void prepareResponseChannel(ClientResponse response, ClientExchange exchange) {
String encoding = response.getResponseHeaders().getLast(TRANSFER_ENCODING);
boolean chunked = encoding != null && Headers.CHUNKED.equals(new HttpString(encoding));
String length = response.getResponseHeaders().getFirst(CONTENT_LENGTH);
if (exchange.getRequest().getMethod().equals(Methods.HEAD)) {
connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
} else if (chunked) {
connection.getSourceChannel().setConduit(new ChunkedStreamSourceConduit(connection.getSourceChannel().getConduit(), pushBackStreamSourceConduit, bufferPool, responseFinishedListener, exchange));
} else if (length != null) {
try {
long contentLength = Long.parseLong(length);
connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), contentLength, responseFinishedListener));
} catch (NumberFormatException e) {
handleError(new IOException(e));
throw e;
}
} else if (response.getProtocol().equals(Protocols.HTTP_1_1)) {
connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
} else {
state |= CLOSE_REQ;
}
}
示例2: testHttp2Post
import io.undertow.client.ClientResponse; //导入依赖的package包/类
/**
* This is an example for post request. Please note that you need to set header TRANSFER_ENCODING
* and pass the request body into the callback function.
*
* @throws Exception
*/
public void testHttp2Post() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection = client.connect(new URI("https://localhost:8443"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/post");
if(securityEnabled) {
// call OAuth 2.0 provider service to get a JWT access token here and
// put it into the request header. Optionally, you can put a traceabilityId
// into the header.
}
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, "post"));
latch.await(100, TimeUnit.MILLISECONDS);
} finally {
IoUtils.safeClose(connection);
}
System.out.println("testHttp2Post: statusCode = " + reference.get().getResponseCode() + " body = " + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
}
示例3: testMultipleHttp2Get
import io.undertow.client.ClientResponse; //导入依赖的package包/类
public void testMultipleHttp2Get(int round) throws Exception {
final Http2Client client = Http2Client.getInstance();
final List<AtomicReference<ClientResponse>> references = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(round);
final ClientConnection connection = client.connect(new URI("https://localhost:8443"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < round; i++) {
AtomicReference<ClientResponse> reference = new AtomicReference<>();
references.add(i, reference);
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/get");
request.getRequestHeaders().put(Headers.HOST, "localhost");
connection.sendRequest(request, client.createClientCallback(reference, latch));
}
}
});
latch.await(10, TimeUnit.SECONDS);
/*
for (final AtomicReference<ClientResponse> reference : references) {
System.out.println(reference.get().getAttachment(Http2Client.RESPONSE_BODY));
System.out.println(reference.get().getProtocol().toString());
}
*/
} finally {
IoUtils.safeClose(connection);
}
}
示例4: testMultipleHttp2Post
import io.undertow.client.ClientResponse; //导入依赖的package包/类
public void testMultipleHttp2Post(int round) throws Exception {
final Http2Client client = Http2Client.getInstance();
final List<AtomicReference<ClientResponse>> references = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(round);
final ClientConnection connection = client.connect(new URI("https://localhost:8443"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < round; i++) {
AtomicReference<ClientResponse> reference = new AtomicReference<>();
references.add(i, reference);
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/post");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
request.getRequestHeaders().put(Headers.HOST, "localhost");
connection.sendRequest(request, client.createClientCallback(reference, latch, "post"));
}
}
});
latch.await(10, TimeUnit.SECONDS);
/*
for (final AtomicReference<ClientResponse> reference : references) {
System.out.println(reference.get().getAttachment(Http2Client.RESPONSE_BODY));
System.out.println(reference.get().getProtocol().toString());
}
*/
} finally {
IoUtils.safeClose(connection);
}
}
示例5: handleEvent
import io.undertow.client.ClientResponse; //导入依赖的package包/类
public void handleEvent(AjpClientChannel channel) {
try {
AbstractAjpClientStreamSourceChannel result = channel.receive();
if(result == null) {
return;
}
if(result instanceof AjpClientResponseStreamSourceChannel) {
AjpClientResponseStreamSourceChannel response = (AjpClientResponseStreamSourceChannel) result;
response.setFinishListener(responseFinishedListener);
ClientResponse cr = new ClientResponse(response.getStatusCode(), response.getReasonPhrase(), currentRequest.getRequest().getProtocol(), response.getHeaders());
if (response.getStatusCode() == 100) {
currentRequest.setContinueResponse(cr);
} else {
currentRequest.setResponseChannel(response);
currentRequest.setResponse(cr);
}
} else {
//TODO: ping, pong ETC
Channels.drain(result, Long.MAX_VALUE);
}
} catch (Exception e) {
UndertowLogger.CLIENT_LOGGER.exceptionProcessingRequest(e);
safeClose(connection);
currentRequest.setFailed(e instanceof IOException ? (IOException) e : new IOException(e));
}
}
示例6: responseReady
import io.undertow.client.ClientResponse; //导入依赖的package包/类
void responseReady(SpdySynReplyStreamSourceChannel result) {
this.response = result;
HeaderMap headers = result.getHeaders();
final String status = result.getHeaders().getFirst(SpdyClientConnection.STATUS);
int statusCode = 500;
if (status != null && status.length() > 3) {
statusCode = Integer.parseInt(status.substring(0, 3));
}
headers.remove(SpdyClientConnection.VERSION);
headers.remove(SpdyClientConnection.STATUS);
clientResponse = new ClientResponse(statusCode, status != null ? status.substring(3) : "", clientRequest.getProtocol(), headers);
if (responseListener != null) {
responseListener.completed(this);
}
}
示例7: testHttp2Get
import io.undertow.client.ClientResponse; //导入依赖的package包/类
/**
* This is a simple example that create a new HTTP 2.0 connection for get request
* and close the connection after the call is done. As you can see that it is using
* a hard coded uri which points to an statically deployed service on fixed ip and port
*
* @throws Exception
*/
public void testHttp2Get() throws Exception {
// Create one CountDownLatch that will be reset in the callback function
final CountDownLatch latch = new CountDownLatch(1);
// Create an HTTP 2.0 connection to the server
final ClientConnection connection = client.connect(new URI("https://localhost:8443"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
// Create an AtomicReference object to receive ClientResponse from callback function
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/get");
// this is to ask client module to pass through correlationId and traceabilityId as well as
// getting access token from oauth2 server automatically and attatch authorization headers.
if(securityEnabled) {
// call OAuth 2.0 provider service to get a JWT access token here and
// put it into the request header. Optionally, you can put a traceabilityId
// into the header.
TokenResponse tokenResponse = getAccessToken();
// you should check if the token is expired here
expiration = tokenResponse.getExpiresIn();
String token = tokenResponse.getAccessToken();
// traceabilityId should be a uuid or db sequence
client.addAuthTokenTrace(request, token, "traceabilityId");
}
// send request to server with a callback function provided by Http2Client
connection.sendRequest(request, client.createClientCallback(reference, latch));
// wait for 100 millisecond to timeout the request.
latch.await(100, TimeUnit.MILLISECONDS);
} finally {
// here the connection is closed after one request. It should be used for in frequent
// request as creating a new connection is costly with TLS handshake and ALPN.
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
System.out.println("testHttp2Get: statusCode = " + statusCode + " body = " + body);
}
示例8: testHttp2GetReuse
import io.undertow.client.ClientResponse; //导入依赖的package包/类
/**
* This is a simple example that create a new HTTP 2.0 connection for get request
* and close the connection after the call is done. As you can see that it is using
* a hard coded uri which points to an statically deployed service on fixed ip and port
*
* @throws Exception
*/
public void testHttp2GetReuse() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/get");
if(securityEnabled) {
}
// send request to server with a callback function provided by Http2Client
reusedConnection.sendRequest(request, client.createClientCallback(reference, latch));
// wait for 100 millisecond to timeout the request.
latch.await(100, TimeUnit.MILLISECONDS);
System.out.println("testHttp2GetReuse: statusCode = " + reference.get().getResponseCode() + " body = " + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
}
示例9: testHttp2PostResue
import io.undertow.client.ClientResponse; //导入依赖的package包/类
/**
* This is an example for post request. Please note that you need to set header TRANSFER_ENCODING
* and pass the request body into the callback function.
*
* @throws Exception
*/
public void testHttp2PostResue() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/post");
if(securityEnabled) {
}
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
reusedConnection.sendRequest(request, client.createClientCallback(reference, latch, "post"));
latch.await(100, TimeUnit.MILLISECONDS);
System.out.println("testHttp2PostReuse: statusCode = " + reference.get().getResponseCode() + " body = " + reference.get().getAttachment(Http2Client.RESPONSE_BODY));
}
示例10: getAccessToken
import io.undertow.client.ClientResponse; //导入依赖的package包/类
/**
* In this example, I am going to use a customized grant type to get access token. This grant type assumes
* that the client authenticated the user and send the user info to OAuth 2.0 provider to get an access token.
* Note that the client must be a strusted client.
* @return
*/
private TokenResponse getAccessToken() throws Exception {
TokenResponse tokenResponse = null;
Map<String, String> params = new HashMap<>();
params.put("grant_type", "client_authenticated_user");
params.put("userId", "admin");
params.put("userType", "Employee");
params.put("transit", "12345"); // This is the custom claim that need to be shown in JWT token.
String s = Http2Client.getFormDataString(params);
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
// all the connection information should be from client.yml
connection = client.connect(new URI("https://localhost:6882"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath("/oauth2/token");
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(Headers.AUTHORIZATION, "Basic " + encodeCredentials("f7d42348-c647-4efb-a52d-4c5787421e72", "f6h1FTI8Q3-7UScPZDzfXA"));
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/x-www-form-urlencoded");
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, client.createClientCallback(reference, latch, s));
latch.await(1, TimeUnit.SECONDS);
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
tokenResponse = Config.getInstance().getMapper().readValue(body, TokenResponse.class);
} catch (Exception e) {
throw new ClientException(e);
}
return tokenResponse;
}
示例11: verifyHandshake
import io.undertow.client.ClientResponse; //导入依赖的package包/类
private static void verifyHandshake(ClientResponse response, String secretKey) throws IOException {
HeaderMap headers = response.getResponseHeaders();
String acceptValue = headers.getFirst(HandshakeUtil.SEC_HORNETQ_REMOTING_ACCEPT);
if (acceptValue == null) {
throw new IOException(HandshakeUtil.SEC_HORNETQ_REMOTING_ACCEPT + " header not found");
}
String expectedResponse = HandshakeUtil.createExpectedResponse(secretKey);
if(!acceptValue.equals(expectedResponse)) {
throw new IOException(HandshakeUtil.SEC_HORNETQ_REMOTING_ACCEPT + " value of " + acceptValue + " did not match expected " + expectedResponse);
}
}
示例12: setContinueResponse
import io.undertow.client.ClientResponse; //导入依赖的package包/类
void setContinueResponse(ClientResponse response) {
this.continueResponse = response;
if (continueNotification != null) {
this.continueNotification.handleContinue(this);
}
}
示例13: setResponse
import io.undertow.client.ClientResponse; //导入依赖的package包/类
void setResponse(ClientResponse response) {
this.response = response;
if (responseCallback != null) {
this.responseCallback.completed(this);
}
}
示例14: getResponse
import io.undertow.client.ClientResponse; //导入依赖的package包/类
@Override
public ClientResponse getResponse() {
return response;
}
示例15: getContinueResponse
import io.undertow.client.ClientResponse; //导入依赖的package包/类
@Override
public ClientResponse getContinueResponse() {
return continueResponse;
}