List of usage examples for org.apache.http.protocol HttpCoreContext getRequest
public HttpRequest getRequest()
From source file:com.clxcommunications.xms.EmptyAsyncConsumer.java
@Override protected Void buildResult(HttpContext context) throws Exception { int code = response.getStatusLine().getStatusCode(); String content = sb.toString(); switch (code) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: if (!content.isEmpty()) { log.warn("Expected empty body but got '{}'", content); }/*from w w w .j a v a 2s.c o m*/ return null; case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_FORBIDDEN: ApiError error = json.readValue(content, ApiError.class); throw new ErrorResponseException(error); case HttpStatus.SC_NOT_FOUND: HttpCoreContext coreContext = HttpCoreContext.adapt(context); RequestLine rl = coreContext.getRequest().getRequestLine(); throw new NotFoundException(rl.getUri()); case HttpStatus.SC_UNAUTHORIZED: throw new UnauthorizedException(); default: ContentType contentType = ContentType.getLenient(response.getEntity()); response.setEntity(new StringEntity(content, contentType)); throw new UnexpectedResponseException(response); } }
From source file:com.clxcommunications.xms.JsonApiAsyncConsumer.java
@Override protected T buildResult(HttpContext context) throws Exception { int code = response.getStatusLine().getStatusCode(); InputStream inputStream = bios.toInputStream(); switch (code) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: return json.readValue(inputStream, jsonClass); case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_FORBIDDEN: ApiError error = json.readValue(inputStream, ApiError.class); throw new ErrorResponseException(error); case HttpStatus.SC_NOT_FOUND: HttpCoreContext coreContext = HttpCoreContext.adapt(context); RequestLine rl = coreContext.getRequest().getRequestLine(); throw new NotFoundException(rl.getUri()); case HttpStatus.SC_UNAUTHORIZED: throw new UnauthorizedException(); default:/*from ww w . j a v a2 s .c o m*/ ContentType type = ContentType.getLenient(response.getEntity()); InputStreamEntity entity = new InputStreamEntity(inputStream, bios.size(), type); response.setEntity(entity); throw new UnexpectedResponseException(response); } }
From source file:org.ballerinalang.composer.service.tryit.service.HttpTryItClient.java
/** * {@inheritDoc}// ww w . j a v a 2s. c o m */ @Override public String execute() throws TryItException { try { HttpClient client = HttpClientBuilder.create().build(); HttpCoreContext localContext = new HttpCoreContext(); // Create url for the request. String requestUrl = this.buildUrl(); String httpMethod = this.clientArgs.get(TryItConstants.HTTP_METHOD).getAsString(); switch (httpMethod.toLowerCase(Locale.getDefault())) { case "get": case "delete": case "options": case "head": case "trace": HttpRequestBase httpRequest = new TryItHttpRequestBase(httpMethod.toUpperCase(Locale.getDefault())); // Setting the url for the request. httpRequest.setURI(URI.create(requestUrl)); // Setting the request headers. JsonArray getHeaders = this.clientArgs.getAsJsonArray(TryItConstants.REQUEST_HEADERS); for (JsonElement getHeader : getHeaders) { JsonObject header = getHeader.getAsJsonObject(); if (StringUtils.isNotBlank(header.get("key").getAsString()) && StringUtils.isNotBlank(header.get("value").getAsString())) { httpRequest.setHeader(header.get("key").getAsString(), header.get("value").getAsString()); } } // Setting the content type. if (StringUtils.isBlank(this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString())) { httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType()); } else { httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString()); } // To track how long the request took. long start = System.currentTimeMillis(); // Executing the request. HttpResponse response = client.execute(httpRequest, localContext); long elapsed = System.currentTimeMillis() - start; return jsonStringifyResponse(response, localContext.getRequest().getAllHeaders(), requestUrl, elapsed); default: HttpEntityEnclosingRequestBase httpEntityRequest = new TryItHttpEntityEnclosingRequestBase( httpMethod.toUpperCase(Locale.getDefault())); // Setting the url for the request. httpEntityRequest.setURI(URI.create(requestUrl)); // Setting the request headers. JsonArray getEntityHeaders = this.clientArgs.getAsJsonArray(TryItConstants.REQUEST_HEADERS); for (JsonElement getHeader : getEntityHeaders) { JsonObject header = getHeader.getAsJsonObject(); if (StringUtils.isNotBlank(header.get("key").getAsString()) && StringUtils.isNotBlank(header.get("value").getAsString())) { httpEntityRequest.setHeader(header.get("key").getAsString(), header.get("value").getAsString()); } } // Setting the body of the request. StringEntity postEntity = new StringEntity( this.clientArgs.get(TryItConstants.REQUEST_BODY).getAsString()); httpEntityRequest.setEntity(postEntity); // Setting the content type. if (StringUtils.isBlank(this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString())) { httpEntityRequest.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType()); } else { httpEntityRequest.setHeader(HttpHeaders.CONTENT_TYPE, this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString()); } // To track how long the request took. long entityRequestStart = System.currentTimeMillis(); // Executing the request. HttpResponse entityResponse = client.execute(httpEntityRequest, localContext); long entityRequestDuration = System.currentTimeMillis() - entityRequestStart; return jsonStringifyResponse(entityResponse, localContext.getRequest().getAllHeaders(), requestUrl, entityRequestDuration); } } catch (IOException e) { throw new TryItException(e.getMessage()); } }
From source file:org.ballerinalang.composer.service.workspace.tryit.HttpTryItClient.java
/** * {@inheritDoc}// w ww. java2s .c o m */ @Override public String execute() throws TryItException { try { HttpClient client = HttpClientBuilder.create().build(); HttpCoreContext localContext = new HttpCoreContext(); // Create url for the request. String requestUrl = this.buildUrl(); String httpMethod = this.clientArgs.get(TryItConstants.HTTP_METHOD).getAsString(); switch (httpMethod.toLowerCase(Locale.getDefault())) { case "get": case "delete": case "options": case "head": case "trace": HttpRequestBase httpRequest = new TryItHttpRequestBase(httpMethod.toUpperCase(Locale.getDefault())); // Setting the url for the request. httpRequest.setURI(URI.create(requestUrl)); // Setting the request headers. JsonArray getHeaders = this.clientArgs.getAsJsonArray(TryItConstants.REQUEST_HEADERS); for (JsonElement getHeader : getHeaders) { JsonObject header = getHeader.getAsJsonObject(); if (StringUtils.isNotBlank(header.get("key").getAsString()) && StringUtils.isNotBlank(header.get("value").getAsString())) { httpRequest.setHeader(header.get("key").getAsString(), header.get("value").getAsString()); } } // Setting the content type. if (StringUtils.isBlank(this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString())) { httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType()); } else { httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString()); } // To track how long the request took. long start = System.currentTimeMillis(); // Executing the request. HttpResponse response = client.execute(httpRequest, localContext); long elapsed = System.currentTimeMillis() - start; return jsonStringifyResponse(response, localContext.getRequest().getAllHeaders(), requestUrl, elapsed); default: HttpEntityEnclosingRequestBase httpEntityRequest = new TryItHttpEntityEnclosingRequestBase( httpMethod.toUpperCase(Locale.getDefault())); // Setting the url for the request. httpEntityRequest.setURI(URI.create(requestUrl)); // Setting the request headers. JsonArray getEntityHeaders = this.clientArgs.getAsJsonArray(TryItConstants.REQUEST_HEADERS); for (JsonElement getHeader : getEntityHeaders) { JsonObject header = getHeader.getAsJsonObject(); if (StringUtils.isNotBlank(header.get("key").getAsString()) && StringUtils.isNotBlank(header.get("value").getAsString())) { httpEntityRequest.setHeader(header.get("key").getAsString(), header.get("value").getAsString()); } } // Setting the body of the request. StringEntity postEntity = new StringEntity( this.clientArgs.get(TryItConstants.REQUEST_BODY).getAsString()); httpEntityRequest.setEntity(postEntity); // Setting the content type. if (StringUtils.isBlank(this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString())) { httpEntityRequest.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.TEXT_PLAIN.getMimeType()); } else { httpEntityRequest.setHeader(HttpHeaders.CONTENT_TYPE, this.clientArgs.get(TryItConstants.CONTENT_TYPE).getAsString()); } // To track how long the request took. long entityRequestStart = System.currentTimeMillis(); // Executing the request. HttpResponse entityResponse = client.execute(httpEntityRequest, localContext); long entityRequestDuration = System.currentTimeMillis() - entityRequestStart; return jsonStringifyResponse(entityResponse, localContext.getRequest().getAllHeaders(), requestUrl, entityRequestDuration); } } catch (java.io.IOException e) { throw new TryItException(e.getMessage()); } }