List of usage examples for io.vertx.core.http HttpClientResponse statusMessage
String statusMessage();
From source file:com.hubrick.vertx.rest.exception.HttpStatusCodeException.java
License:Apache License
protected HttpStatusCodeException(HttpClientResponse httpClientResponse, List<HttpMessageConverter> httpMessageConverters, byte[] responseBody) { super(httpClientResponse.statusMessage()); checkNotNull(httpClientResponse, "httpClientResponse must not be null"); checkNotNull(httpMessageConverters, "dataMappers must not be null"); checkArgument(!httpMessageConverters.isEmpty(), "dataMappers must not be empty"); checkNotNull(responseBody, "responseBody must not be null"); this.httpClientResponse = httpClientResponse; this.httpMessageConverters = httpMessageConverters; this.responseBody = responseBody; }
From source file:com.hubrick.vertx.rest.impl.DefaultRestClientRequest.java
License:Apache License
private void handleResponse(HttpClientResponse httpClientResponse, Class clazz) { final Integer firstStatusDigit = httpClientResponse.statusCode() / 100; if (firstStatusDigit == 4 || firstStatusDigit == 5) { httpClientResponse.bodyHandler((buffer) -> { httpClientResponse.exceptionHandler(null); if (log.isWarnEnabled()) { final String body = new String(buffer.getBytes(), Charsets.UTF_8); log.warn("Http request to {} FAILED. Return status: {}, message: {}, body: {}", new Object[] { uri, httpClientResponse.statusCode(), httpClientResponse.statusMessage(), body }); }/*from w w w .jav a2s .c om*/ RuntimeException exception = null; switch (firstStatusDigit) { case 4: exception = new HttpClientErrorException(httpClientResponse, httpMessageConverters, buffer.getBytes()); break; case 5: exception = new HttpServerErrorException(httpClientResponse, httpMessageConverters, buffer.getBytes()); break; } handleException(exception); }); } else { httpClientResponse.bodyHandler((buffer) -> { httpClientResponse.exceptionHandler(null); if (log.isDebugEnabled()) { final String body = new String(buffer.getBytes(), Charsets.UTF_8); log.debug("Http request to {} {} SUCCESSFUL. Return status: {}, message: {}, body: {}", new Object[] { method, uri, httpClientResponse.statusCode(), httpClientResponse.statusMessage(), body }); } final RestClientResponse<T> restClientResponse = new DefaultRestClientResponse( httpMessageConverters, clazz, buffer.getBytes(), httpClientResponse, exceptionHandler); handleResponse(restClientResponse); }); } }
From source file:io.apiman.gateway.platforms.vertx3.http.HttpApiFactory.java
License:Apache License
public static ApiResponse buildResponse(HttpClientResponse response, Set<String> suppressHeaders) { ApiResponse apimanResponse = new ApiResponse(); apimanResponse.setCode(response.statusCode()); apimanResponse.setMessage(response.statusMessage()); multimapToMap(apimanResponse.getHeaders(), response.headers(), suppressHeaders); return apimanResponse; }
From source file:io.apiman.gateway.platforms.vertx3.http.HttpServiceFactory.java
License:Apache License
public static VertxServiceResponse buildResponse(HttpClientResponse response, Set<String> suppressHeaders) { VertxServiceResponse apimanResponse = new VertxServiceResponse(); apimanResponse.setCode(response.statusCode()); apimanResponse.setMessage(response.statusMessage()); multimapToMap(apimanResponse.getHeaders(), response.headers(), suppressHeaders); return apimanResponse; }
From source file:io.nitor.api.backend.auth.SetupAzureAdConnectAuth.java
License:Apache License
static void processGraphTokenResponse(HttpClientResponse resp, RoutingContext ctx, HttpClient httpClient, CookieSessionHandler sessionHandler, List<GraphQuery> graphQueries, String originalUrl) { if (resp.statusCode() != OK.code()) { resp.bodyHandler(body -> {//from www .j a v a 2 s . c o m logger.warn("Failed to fetch graph access token: " + resp.statusMessage() + " - " + resp.getHeader(WWW_AUTHENTICATE) + " ::: " + body); ctx.reroute(GET, UNAUTHORIZED_PATH); }); return; } resp.bodyHandler(body -> { JsonObject json = body.toJsonObject(); String token = json.getString("access_token"); String refreshToken = json.getString("refresh_token"); // clean out sensitive stuff json.put("access_token", "<censored>"); json.put("refresh_token", "<censored>"); logger.debug("Got graph access response: {}", json); final AtomicInteger pendingRequests = new AtomicInteger(graphQueries.size()); final Map<String, String> sessionData = new HashMap<>(); ofNullable(refreshToken).ifPresent(t -> sessionData.put(GRAPH_ACCESS_TOKEN_KEY, t)); for (GraphQuery query : graphQueries) { String clientRequestId = UUID.randomUUID().toString(); logger.debug("Requesting " + query.graphQueryURI + "[" + clientRequestId + "]"); httpClient.getAbs(query.graphQueryURI).putHeader(AUTHORIZATION, "Bearer " + token) .putHeader(ACCEPT, APPLICATION_JSON).putHeader("client-request-id", clientRequestId) .setTimeout(SECONDS.toMillis(10)).exceptionHandler(err -> { if (pendingRequests.getAndSet(-1) != -1) { logger.error("Failed to fetch user information [" + clientRequestId + "]", err); ctx.reroute(GET, UNAUTHORIZED_PATH); } }).handler(r -> processMicrosoftUserInformation(r, ctx, sessionHandler, query.headerMappings, originalUrl, pendingRequests, sessionData, clientRequestId)) .end(); } }); }
From source file:io.nitor.api.backend.auth.SetupAzureAdConnectAuth.java
License:Apache License
static void processMicrosoftUserInformation(HttpClientResponse resp, RoutingContext ctx, CookieSessionHandler sessionHandler, Map<String, String> headerMappings, String originalUrl, AtomicInteger pendingRequests, Map<String, String> sessionData, String clientRequestId) { if (resp.statusCode() != OK.code()) { if (pendingRequests.getAndSet(-1) != -1) { logger.warn("Failed to fetch graph information [" + clientRequestId + "]: " + resp.statusMessage() + " - " + resp.getHeader(WWW_AUTHENTICATE)); ctx.reroute(GET, UNAUTHORIZED_PATH); return; }// w w w . j ava2s . c om } resp.bodyHandler(body -> { JsonObject response = body.toJsonObject(); logger.debug("Got graph response [" + clientRequestId + "]: {}", response); synchronized (sessionData) { headerMappings.forEach((header, pointer) -> ofNullable(JsonPointer.fetch(response, pointer)) .ifPresent(val -> sessionData.put(header, val))); } if (pendingRequests.decrementAndGet() == 0) { sessionHandler.setSessionData(ctx, sessionData); ctx.response().setStatusCode(TEMPORARY_REDIRECT.code()) .putHeader(CACHE_CONTROL, "no-cache, no-store, must-revalidate").putHeader(EXPIRES, "0") .putHeader(LOCATION, originalUrl).end(); } }); }
From source file:io.nitor.api.backend.msgraph.GraphQueryHandler.java
License:Apache License
private void mapResponse(HttpClientResponse cres, HttpServerResponse sres, String clientRequestId) { cres.exceptionHandler(t -> {//from ww w. j a v a2 s. c om logger.error("Error processing graph request [" + clientRequestId + "]", t); if (!sres.ended()) { sres.setStatusCode(BAD_GATEWAY.code()); sres.end(); } }); // TODO Together with the client-request-id always log the request-id, timestamp and x-ms-ags-diagnostic from the HTTP response headers sres.setStatusCode(cres.statusCode()); sres.setStatusMessage(cres.statusMessage()); MultiMap headers = sres.headers(); cres.headers().forEach(entry -> { String key = entry.getKey(); String lKey = key.toLowerCase(); if ("server".equals(lKey) || "accept-ranges".equals(lKey) || "transfer-encoding".equals(lKey) || "date".equals(lKey) || "connection".equals(lKey)) { return; } headers.add(key, entry.getValue()); }); if (!headers.contains("content-length")) { sres.setChunked(true); } Pump resPump = Pump.pump(cres, sres); cres.endHandler(v -> { if (!sres.ended()) sres.end(); }); resPump.start(); }
From source file:io.nitor.api.backend.proxy.SimpleLogProxyTracer.java
License:Apache License
String dumpCRes(HttpClientResponse res) { return "\n\t" + res.statusCode() + " " + res.statusMessage() + dumpHeaders(res.headers(), "\t"); }
From source file:io.nitor.api.backend.s3.S3Handler.java
License:Apache License
private void mapResponse(HttpClientResponse cres, HttpServerResponse sres) { cres.exceptionHandler(t -> {/*from w ww. j a va 2s. c om*/ logger.error("Error processing s3 request", t); if (!sres.ended()) { sres.setStatusCode(502); sres.end(); } }); sres.setStatusCode(cres.statusCode()); sres.setStatusMessage(cres.statusMessage()); if (cres.statusCode() != 200 && cres.statusCode() != 206) { sres.end(); return; } MultiMap headers = sres.headers(); cres.headers().forEach(entry -> { String key = entry.getKey(); if (key.startsWith("x-amz-")) { return; } String lKey = key.toLowerCase(); if ("server".equals(lKey) || "accept-ranges".equals(lKey) || "transfer-encoding".equals(lKey) || "date".equals(lKey) || "connection".equals(lKey)) { return; } headers.add(key, entry.getValue()); }); // TODO handle http 1.0 that requires connection header Pump resPump = Pump.pump(cres, sres); cres.endHandler(v -> sres.end()); resPump.start(); }
From source file:org.apache.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl.java
License:Apache License
@VisibleForTesting @SuppressWarnings("unchecked") protected <T> Handler<RestResponse> syncHandler(CountDownLatch countDownLatch, Class<T> cls, Holder<T> holder) { return restResponse -> { RequestContext requestContext = restResponse.getRequestContext(); HttpClientResponse response = restResponse.getResponse(); if (response == null) { // ?SC if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) { retry(requestContext, syncHandler(countDownLatch, cls, holder)); } else { countDownLatch.countDown(); }/*from www. j av a 2 s . co m*/ return; } holder.setStatusCode(response.statusCode()); response.bodyHandler(bodyBuffer -> { if (cls.getName().equals(HttpClientResponse.class.getName())) { holder.value = (T) response; countDownLatch.countDown(); return; } if (cls.equals(String.class)) { holder.setValue((T) bodyBuffer.toString()); countDownLatch.countDown(); return; } // no need to support 304 in this place if (!HttpStatusClass.SUCCESS.equals(HttpStatusClass.valueOf(response.statusCode()))) { LOGGER.warn("get response for {} failed, {}:{}, {}", cls.getName(), response.statusCode(), response.statusMessage(), bodyBuffer.toString()); countDownLatch.countDown(); return; } try { holder.value = JsonUtils.readValue(bodyBuffer.getBytes(), cls); } catch (Exception e) { holder.setStatusCode(0).setThrowable(e); LOGGER.warn("read value failed and response message is {}", bodyBuffer.toString()); } countDownLatch.countDown(); }); }; }