List of usage examples for org.apache.http.client HttpResponseException HttpResponseException
public HttpResponseException(final int statusCode, final String s)
From source file:com.helger.peppol.smpclient.SMPHttpResponseHandlerUnsigned.java
@Nonnull public T 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"); // 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 ww w. ja v a 2 s. c o m }
From source file:com.puppetlabs.puppetdb.javaclient.test.MockConnector.java
@Override public <V> V get(String urlStr, Map<String, String> params, Type type) throws IOException { InputStream mockResponses = getClass().getResourceAsStream("/mock_responses.json"); assertNotNull("Unable to open 'mock_responses.json'", mockResponses); Object mocks;/*from w w w . j a v a2 s. c om*/ try { mocks = gson.fromJson(new InputStreamReader(mockResponses, UTF_8), Object.class); } finally { mockResponses.close(); } assertTrue("mock_responses did not produce a Map", mocks instanceof Map); Object mock = mocks; int dash = urlStr.indexOf('/'); if (dash != 0) // The URL must start with a dash throw new HttpResponseException(HttpStatus.SC_NOT_FOUND, urlStr); int start = 1; dash = urlStr.indexOf('/', start); if (dash < 0) dash = urlStr.length(); while (dash > 0) { String segment = urlStr.substring(start, dash); mock = ((Map<?, ?>) mocks).get(segment); if (mock == null) throw new HttpResponseException(HttpStatus.SC_NOT_FOUND, urlStr); if (dash == urlStr.length()) break; if (!(mock instanceof Map<?, ?>)) throw new HttpResponseException(HttpStatus.SC_NOT_FOUND, urlStr); mocks = mock; start = dash + 1; dash = urlStr.indexOf('/', start); if (dash < 0) dash = urlStr.length(); } // Check if we are less qualified than the nested map depth in which case we must concatenate // the values of the maps below into a list if (isCollectionType(type)) { if (mock instanceof Map) { List<Object> result = new ArrayList<Object>(); flattenMaps((Map<?, ?>) mock, result); mock = result; } } else if (mock instanceof List) mock = ((List<?>) mock).get(0); // Convert to expected type return gson.fromJson(gson.toJson(mock), type); }
From source file:com.polyvi.xface.http.XAsyncHttpResponseHandler.java
/** * ??http response?/*from ww w. j ava 2 s . c o m*/ * * @param response */ public void sendResponseMessage(HttpResponse response) { StatusLine status = response.getStatusLine(); String responseBody = null; HttpEntity entity = null; HttpEntity temp = response.getEntity(); if (temp != null) { try { entity = new BufferedHttpEntity(temp); responseBody = EntityUtils.toString(entity, "UTF-8"); } catch (IOException e) { sendFailureMessage(e, null); } } // ?300 ?? ? if (status.getStatusCode() >= 300) { sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody); } else { sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody); } }
From source file:fm.last.musicbrainz.coverart.impl.DefaultCoverArtArchiveClientTest.java
@Test(expected = CoverArtException.class) public void nullMbidThrowsCoverArtException() throws Exception { doThrow(new HttpResponseException(HttpStatus.SC_BAD_REQUEST, "")).when(httpClient) .execute(any(HttpGet.class), eq(FetchJsonListingResponseHandler.INSTANCE)); client.getByMbid(null);/*from w ww . ja v a 2 s . co m*/ }
From source file:com.unifonic.sdk.resources.http.EmailResourceImpl.java
@Override public EmailResponse send(Map<String, String> param) throws IOException { OTSRestResponse response = sendRequest(emailUrl.urlSend(), param); if (response.getStatusCode() < 400) { Type type = new TypeToken<ResponseModel<EmailResponse>>() { }.getType();/*w w w.ja va2 s.c o m*/ ResponseModel<EmailResponse> respData = GSON.fromJson(response.getData(), type); return respData.create(); } else if (response.getStatusCode() == 400) { EmailResponse resp = GSON.fromJson(response.getData(), EmailResponse.class); return resp; } else { throw new HttpResponseException(response.getStatusCode(), response.getReasonPhrase()); } }
From source file:com.unifonic.sdk.resources.http.VerifyResourceImpl.java
@Override public GetCodeResponse getCode(Map<String, String> map) throws IOException { OTSRestResponse response = sendRequest(verifyUrl.urlGetCode(), map); if (response.getStatusCode() < 400) { Type type = new TypeToken<ResponseModel<GetCodeResponse>>() { }.getType();/*ww w . jav a2 s .co m*/ ResponseModel<GetCodeResponse> respData = GSON.fromJson(response.getData(), type); return respData.create(); } else if (response.getStatusCode() == 400) { GetCodeResponse resp = GSON.fromJson(response.getData(), GetCodeResponse.class); return resp; } else { throw new HttpResponseException(response.getStatusCode(), response.getReasonPhrase()); } }
From source file:org.kontalk.xmppserver.registration.checkmobi.CheckmobiValidationClient.java
public RequestResult request(String number) throws IOException { try {/* ww w. j av a 2 s .c o m*/ JsonObject data = _request(number); try { String id = data.getAsJsonPrimitive("id").getAsString(); String dialingNumber = data.has("dialing_number") ? data.getAsJsonPrimitive("dialing_number").getAsString() : null; return new RequestResult(id, dialingNumber); } catch (NullPointerException e) { // simulate bad request throw new HttpResponseException(400, "Bad request"); } } catch (HttpResponseException e) { return new RequestResult(e); } }
From source file:project.latex.balloon.writer.HttpDataWriter.java
void sendPostRequest(String rawString) throws IOException { String jsonString = getJsonStringFromRawData(rawString); CloseableHttpClient httpclient = HttpClients.createDefault(); StringEntity entity = new StringEntity(jsonString, ContentType.create("plain/text", Consts.UTF_8)); HttpPost httppost = new HttpPost(receiverUrl); httppost.addHeader("content-type", "application/json"); httppost.setEntity(entity);//from w w w . java2 s .com ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset)); StringBuilder stringBuilder = new StringBuilder(); String line = reader.readLine(); while (line != null) { stringBuilder.append(line); line = reader.readLine(); } return stringBuilder.toString(); } }; String responseString = httpclient.execute(httppost, responseHandler); logger.info(responseString); }
From source file:org.tellervo.desktop.wsi.JaxbResponseHandler.java
/** * Returns the response body as an XML document if the response was successful (a * 2xx status code). If no response body exists, this returns null. If the * response was unsuccessful (>= 300 status code), throws an * {@link HttpResponseException}.//from w w w .j a v a 2 s. c o m */ public T handleResponse(final HttpResponse response) throws HttpResponseException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } HttpEntity entity = response.getEntity(); return entity == null ? null : toDocument(entity, null); }
From source file:me.ixfan.wechatkit.util.JsonResponseHandler.java
@Override public JsonObject handleResponse(HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); }//www .ja v a 2 s.c o m if (entity == null) { throw new ClientProtocolException("Response contains no content"); } ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); if (null == charset) { charset = StandardCharsets.UTF_8; } Reader reader = new InputStreamReader(entity.getContent(), charset); return new JsonParser().parse(reader).getAsJsonObject(); }