List of usage examples for org.apache.http.client ClientProtocolException ClientProtocolException
public ClientProtocolException(final Throwable cause)
From source file:com.vsct.strowgr.monitoring.aggregator.nsq.NsqLookupClient.java
public Set<String> getTopics() throws UnavailableNsqException { try {//ww w . j a va 2 s . com LOGGER.info("Listing all nsq topics"); HttpGet uri = new HttpGet("http://" + host + ":" + port + TOPICS_ENDPOINT); return client.execute(uri, new ResponseHandler<Set<String>>() { @Override public Set<String> handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException { int status = httpResponse.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = httpResponse.getEntity(); NsqLookupResponse<NsqLookupTopics> nsqResponse = mapper.readValue(entity.getContent(), new TypeReference<NsqLookupResponse<NsqLookupTopics>>() { }); return nsqResponse.data.topics; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }); } catch (IOException e) { LOGGER.error("error while querying nsqlookup for list of topics"); throw new UnavailableNsqException(e); } }
From source file:com.vsct.dt.strowgr.admin.template.locator.UriTemplateLocator.java
public Optional<String> readTemplate(String uri) { try {/*from w ww.j a v a 2s . com*/ HttpGet getTemplate = new HttpGet(uri); getTemplate.addHeader("Content-Type", "text/plain; charset=utf-8"); LOGGER.debug("get template {}", uri); return client.execute(getTemplate, (response) -> { Optional<String> result = Optional.empty(); int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); String entitySer = EntityUtils.toString(entity, "UTF-8"); if (entitySer == null) { throw new IllegalStateException("template from " + uri + " has null content."); } else { LOGGER.debug("template from " + uri + " starts with " + entitySer.substring(0, Math.max(20, entitySer.length()))); } result = Optional.of(entitySer); } else if (status != 404) { throw new ClientProtocolException("Unexpected response status: " + status); } return result; }); } catch (IOException e) { LOGGER.error("Can't retrieve template from " + uri, e); throw new RuntimeException(e); } }
From source file:com.sicongtang.http.httpcomponents.httpclient.basic.ClientWithResponseHandler.java
public void execute(int count, String url) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w .j a v a2 s . c om HttpGet httpget = new HttpGet(url); System.out.println(count + " Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.print("----------------------------------------"); System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:org.apache.camel.component.social.SocialHttpClient.java
private void signOAuth(HttpUriRequest request) throws ClientProtocolException { if (consumer != null) { try {/*from w w w . j a v a 2 s . c o m*/ consumer.sign(request); } catch (Exception e) { throw new ClientProtocolException(e); } } }
From source file:com.dnastack.bob.service.processor.util.HttpUtils.java
/** * Executes GET/POST and obtain the response. * * @param request request//from w ww . j av a 2 s .com * * @return response */ public static String executeRequest(HttpRequestBase request) { String response = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try { ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; response = httpclient.execute(request, responseHandler); } catch (IOException ex) { // ignore, response already set to null } finally { try { httpclient.close(); } catch (IOException ex) { // ignore } } return response; }
From source file:com.helger.peppol.httpclient.SMPHttpResponseHandlerUnsigned.java
@Override @Nonnull//from w w w . j a va2 s . c om public T handleEntity(@Nonnull final HttpEntity aEntity) throws IOException { // Read the payload final T ret = m_aMarshaller.read(aEntity.getContent()); if (ret == null) throw new ClientProtocolException("Malformed XML document returned from SMP server"); return ret; }
From source file:com.helger.peppol.smpclient.SMPHttpResponseHandlerWriteOperations.java
@Nullable public Object handleResponse(@Nonnull final HttpResponse aHttpResponse) throws IOException { final StatusLine aStatusLine = aHttpResponse.getStatusLine(); final HttpEntity aEntity = aHttpResponse.getEntity(); if (aStatusLine.getStatusCode() >= 300) throw new HttpResponseException(aStatusLine.getStatusCode(), aStatusLine.getReasonPhrase()); if (aEntity == null) throw new ClientProtocolException("Response from SMP server contains no content"); EntityUtils.consume(aEntity);/* ww w . jav a 2 s. com*/ return null; }
From source file:counsil.WebClient.java
public String getFromURL(String ip, int port, String URL) throws IOException { String outString = ""; Integer portInt = port;//from w w w. j a v a2s . c o m RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2 * 1000).build(); CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); BasicConfigurator.configure(); try { HttpGet httpget = new HttpGet("http://" + ip + ":" + portInt.toString() + URL); // Create a custom response handler ResponseHandler<String> responseHandler = (HttpResponse response) -> { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } }; String responseBody = httpclient.execute(httpget, responseHandler); outString = responseBody; } finally { httpclient.close(); } return outString; }
From source file:org.apache.droids.protocol.http.HttpProtocol.java
@Override public ManagedContentEntity load(URI uri) throws IOException { HttpGet httpget = new HttpGet(uri); HttpResponse response = httpclient.execute(httpget); StatusLine statusline = response.getStatusLine(); if (statusline.getStatusCode() >= HttpStatus.SC_BAD_REQUEST) { httpget.abort();/*from www . j a v a2s. c om*/ throw new HttpResponseException(statusline.getStatusCode(), statusline.getReasonPhrase()); } HttpEntity entity = response.getEntity(); if (entity == null) { // Should _almost_ never happen with HTTP GET requests. throw new ClientProtocolException("Empty entity"); } long maxlen = httpclient.getParams().getLongParameter(DroidsHttpClient.MAX_BODY_LENGTH, 0); return new HttpContentEntity(entity, maxlen); }
From source file:com.seleritycorp.context.RequestResponseHandler.java
@Override public JsonObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException { StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity); JsonParser parser = new JsonParser(); // The API does not return partials or some such, so any response that is not a 200, // indicates issues. if (statusCode != 200) { log.trace("Raw response: " + responseString); String errorMessage = statusCode + " " + statusLine.getReasonPhrase(); // Instead of the pure HTTP status information, we try to get a descriptive error // message. Context API returns JSON objects that indicate the error. So we // opportunistically parse the content and drill down to get the error message. try {//w ww . java 2s. c om JsonObject json = parser.parse(responseString).getAsJsonObject(); String hint = ""; if (json.has("errorCode")) { hint = json.toString(); } else if (json.has("errorMessage")) { hint = json.get("errorMessage").getAsString(); } if (hint != null && !hint.isEmpty()) { errorMessage += ". (" + hint + ")"; } } catch (Exception e) { // Extracting a more detailed error message failed. So we move forward with only the // HTTP status line. } throw new ClientProtocolException("Server responded with " + errorMessage); } // At this point, response has a 200 HTTP status code. String contentType = entity.getContentType().getValue(); String parameterlessContentType = contentType.split("[; ]", 2)[0]; if (!"application/json".equals(parameterlessContentType)) { // Response is not Json log.trace("Raw response: " + responseString); throw new ClientProtocolException( "Received content type '" + contentType + "' instead of 'application/json'"); } // At this point, response got sent as JSON // Parse the string to JSON JsonObject ret = parser.parse(responseString).getAsJsonObject(); return ret; }