List of usage examples for org.apache.http.util EntityUtils toByteArray
public static byte[] toByteArray(HttpEntity httpEntity) throws IOException
From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java
/** * Performs an HTTP/S request by invoking the provided HttpMethod object. If the HTTP * response code doesn't match the expected value, an exception is thrown. * * @param httpMethod// ww w.j av a 2s . c o m * the object containing a request target and all other information necessary to perform the * request * @param expectedResponseCodes * the HTTP response code(s) that indicates a successful request. If the response code received * does not match this value an error must have occurred, so an exception is thrown. * @param context * An HttpContext to facilitate information sharing in the HTTP chain * @throws ServiceException * all exceptions are wrapped in an ServiceException. Depending on the kind of error that * occurred, this exception may contain additional error information available from an XML * error response document. */ protected HttpResponse performRequest(HttpUriRequest httpMethod, int[] expectedResponseCodes, HttpContext context) throws ServiceException { HttpResponse response = null; InterfaceLogBean reqBean = new InterfaceLogBean(httpMethod.getURI().toString(), "", ""); try { if (log.isDebugEnabled()) { log.debug("Performing " + httpMethod.getMethod() + " request for '" + httpMethod.getURI().toString() + "', expecting response codes: " + "[" + ServiceUtils.join(expectedResponseCodes, ",") + "]"); log.debug("Headers: " + Arrays.asList(httpMethod.getAllHeaders())); } log.debug("Endpoint: " + getEndpoint()); // Variables to manage S3 Internal Server 500 or 503 Service Unavailable errors. boolean completedWithoutRecoverableError = true; int internalErrorCount = 0; int requestTimeoutErrorCount = 0; int redirectCount = 0; int authFailureCount = 0; boolean wasRecentlyRedirected = false; // Perform the request, sleeping and retrying when errors are encountered. int responseCode = -1; do { // Build the authorization string for the method (Unless we have just been redirected). if (!wasRecentlyRedirected) { authorizeHttpRequest(httpMethod, context); } else { // Reset redirection flag wasRecentlyRedirected = false; } response = httpClient.execute(httpMethod, context); responseCode = response.getStatusLine().getStatusCode(); reqBean.setRespParams("[responseCode: " + responseCode + "][x-amz-request-id: " + response.getFirstHeader("x-amz-request-id").getValue() + "]"); if (responseCode == 307) { // Retry on Temporary Redirects, using new URI from location header authorizeHttpRequest(httpMethod, context); // Re-authorize *before* we change the URI Header locationHeader = response.getFirstHeader("location"); // deal with implementations of HttpUriRequest if (httpMethod instanceof HttpRequestBase) { ((HttpRequestBase) httpMethod).setURI(new URI(locationHeader.getValue())); } else if (httpMethod instanceof RequestWrapper) { ((RequestWrapper) httpMethod).setURI(new URI(locationHeader.getValue())); } completedWithoutRecoverableError = false; redirectCount++; wasRecentlyRedirected = true; if (redirectCount > 5) { reqBean.setResponseInfo("Exceeded 307 redirect limit (5).", "-1"); throw new ServiceException("Exceeded 307 redirect limit (5)."); } } else if (responseCode == 500 || responseCode == 503) { // Retry on S3 Internal Server 500 or 503 Service Unavailable errors. completedWithoutRecoverableError = false; reqBean.setResponseInfo("Internal Server error(s).", "-1"); ilog.error(reqBean); sleepOnInternalError(++internalErrorCount); } else { completedWithoutRecoverableError = true; } String contentType = ""; if (response.getFirstHeader("Content-Type") != null) { contentType = response.getFirstHeader("Content-Type").getValue(); } if (log.isDebugEnabled()) { log.debug("Response for '" + httpMethod.getMethod() + "'. Content-Type: " + contentType + ", Headers: " + Arrays.asList(response.getAllHeaders())); log.debug("Response entity: " + response.getEntity()); if (response.getEntity() != null) { log.debug("Entity length: " + response.getEntity().getContentLength()); } } // Check we received the expected result code. boolean didReceiveExpectedResponseCode = false; for (int i = 0; i < expectedResponseCodes.length && !didReceiveExpectedResponseCode; i++) { if (responseCode == expectedResponseCodes[i]) { didReceiveExpectedResponseCode = true; } } if (log.isDebugEnabled()) { log.debug("Received expected response code: " + didReceiveExpectedResponseCode); log.debug(" expected code(s): " + Arrays.toString(expectedResponseCodes) + "."); } if (!didReceiveExpectedResponseCode) { if (log.isDebugEnabled()) { log.debug("Response xml: " + isXmlContentType(contentType)); log.debug("Response entity: " + response.getEntity()); log.debug("Response entity length: " + (response.getEntity() == null ? "??" : "" + response.getEntity().getContentLength())); } if (response.getEntity() != null && response.getEntity().getContentLength() != 0) { if (log.isDebugEnabled()) { log.debug("Response '" + httpMethod.getURI().getRawPath() + "' - Received error response with XML message"); } StringBuilder sb = new StringBuilder(); BufferedReader reader = null; try { reader = new BufferedReader( new InputStreamReader(new HttpMethodReleaseInputStream(response))); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } } finally { if (reader != null) { reader.close(); } } EntityUtils.consume(response.getEntity()); // Throw exception containing the XML message document. ServiceException exception = new ServiceException("S3 Error Message.", sb.toString()); exception.setResponseCode(responseCode); exception.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders())); reqBean.setResponseInfo("http status: " + responseCode, exception.getErrorCode()); ilog.error(reqBean); if ("RequestTimeout".equals(exception.getErrorCode())) { int retryMaxCount = jets3tProperties.getIntProperty("httpclient.retry-max", 5); if (requestTimeoutErrorCount < retryMaxCount) { requestTimeoutErrorCount++; if (log.isWarnEnabled()) { log.warn("Retrying connection that failed with RequestTimeout error" + ", attempt number " + requestTimeoutErrorCount + " of " + retryMaxCount); } completedWithoutRecoverableError = false; } else { if (log.isErrorEnabled()) { log.error("Exceeded maximum number of retries for RequestTimeout errors: " + retryMaxCount); } throw exception; } } else if ("RequestTimeTooSkewed".equals(exception.getErrorCode())) { // this.timeOffset = RestUtils.getAWSTimeAdjustment(); if (log.isWarnEnabled()) { log.warn("Adjusted time offset in response to RequestTimeTooSkewed error. " + "Local machine and S3 server disagree on the time by approximately " + (this.timeOffset / 1000) + " seconds. Retrying connection."); } completedWithoutRecoverableError = false; throw new ServiceException("S3 Error Message.", sb.toString()); } else if (responseCode == 500 || responseCode == 503) { // Retrying after 500 or 503 error, don't throw exception. } else if (responseCode == 307) { // Retrying after Temporary Redirect 307, don't throw exception. if (log.isDebugEnabled()) { log.debug("Following Temporary Redirect to: " + httpMethod.getURI().toString()); } } // Special handling for S3 object PUT failures causing NoSuchKey errors - Issue #85 else if (responseCode == 404 && "PUT".equalsIgnoreCase(httpMethod.getMethod()) && "NoSuchKey".equals(exception.getErrorCode()) // If PUT operation is trying to copy an existing source object, don't ignore 404 && httpMethod.getFirstHeader(getRestHeaderPrefix() + "copy-source") == null) { // Retrying after mysterious PUT NoSuchKey error caused by S3, don't throw exception. if (log.isDebugEnabled()) { log.debug("Ignoring NoSuchKey/404 error on PUT to: " + httpMethod.getURI().toString()); } completedWithoutRecoverableError = false; } else if ((responseCode == 403 || responseCode == 401) && this.isRecoverable403(httpMethod, exception)) { completedWithoutRecoverableError = false; authFailureCount++; if (authFailureCount > 1) { throw new ServiceException("Exceeded 403 retry limit (1)."); } if (log.isDebugEnabled()) { log.debug("Retrying after 403 Forbidden"); } } else { throw exception; } } else { reqBean.setResponseInfo("http status:" + responseCode, "-1"); ilog.error(reqBean); // Consume response content and release connection. String responseText = null; byte[] responseBody = null; if (response.getEntity() != null) { responseBody = EntityUtils.toByteArray(response.getEntity()); } if (responseBody != null && responseBody.length > 0) { responseText = new String(responseBody); } if (log.isDebugEnabled()) { log.debug("Releasing error response without XML content"); } EntityUtils.consume(response.getEntity()); if (responseCode == 500 || responseCode == 503) { // Retrying after InternalError 500, don't throw exception. } else { // Throw exception containing the HTTP error fields. HttpException httpException = new HttpException(responseCode, response.getStatusLine().getReasonPhrase()); ServiceException exception = new ServiceException( "Request Error" + (responseText != null ? " [" + responseText + "]." : "."), httpException); reqBean.setResponseInfo( "Request Error" + (responseText != null ? " [" + responseText + "]." : "."), "-1"); ilog.error(reqBean); exception.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders())); throw exception; } } // Print warning message if a non-fatal error occurred (we only reach this // point in the code if an exception isn't thrown above) if (log.isWarnEnabled()) { String requestDescription = httpMethod.getMethod() + " '" + httpMethod.getURI().getPath() + (httpMethod.getURI().getQuery() != null && httpMethod.getURI().getQuery().length() > 0 ? "?" + httpMethod.getURI().getQuery() : "") + "'" + " -- ResponseCode: " + responseCode + ", ResponseStatus: " + response.getStatusLine().getReasonPhrase() + ", Request Headers: [" + ServiceUtils.join(httpMethod.getAllHeaders(), ", ") + "]" + ", Response Headers: [" + ServiceUtils.join(response.getAllHeaders(), ", ") + "]"; requestDescription = requestDescription.replaceAll("[\\n\\r\\f]", ""); // Remove any newlines. log.warn("Error Response: " + requestDescription); } } } while (!completedWithoutRecoverableError); } catch (Throwable t) { if (log.isDebugEnabled()) { String msg = "Rethrowing as a ServiceException error in performRequest: " + t; if (t.getCause() != null) { msg += ", with cause: " + t.getCause(); } if (log.isTraceEnabled()) { log.trace(msg, t); } else { log.debug(msg); } } if (log.isDebugEnabled() && !shuttingDown) { log.debug("Releasing HttpClient connection after error: " + t.getMessage()); } httpMethod.abort(); ServiceException serviceException; if (t instanceof ServiceException) { serviceException = (ServiceException) t; } else { MxDelegate.getInstance().registerS3ServiceExceptionEvent(); serviceException = new ServiceException("Request Error: " + t, t); } // Add S3 request and host IDs from HTTP headers to exception, if they are available // and have not already been populated by parsing an XML error response. if (!serviceException.isParsedFromXmlMessage() && response != null && response.getFirstHeader(Constants.AMZ_REQUEST_ID_1) != null && response.getFirstHeader(Constants.AMZ_REQUEST_ID_2) != null) { serviceException.setRequestAndHostIds( response.getFirstHeader(Constants.AMZ_REQUEST_ID_1).getValue(), response.getFirstHeader(Constants.AMZ_REQUEST_ID_2).getValue()); serviceException.setResponseHeaders(RestUtils.convertHeadersToMap(response.getAllHeaders())); } if (response != null) { try { serviceException.setResponseCode(response.getStatusLine().getStatusCode()); serviceException.setResponseStatus(response.getStatusLine().getReasonPhrase()); } catch (NullPointerException e) { // If no network connection is available, status info is not available } } if (httpMethod.getFirstHeader("Host") != null) { serviceException.setRequestHost(httpMethod.getFirstHeader("Host").getValue()); } if (response != null && response.getFirstHeader("Date") != null) { serviceException.setResponseDate(response.getFirstHeader("Date").getValue()); } reqBean.setResponseInfo(serviceException.getErrorMessage(), serviceException.getErrorCode()); throw serviceException; } reqBean.setRespTime(new Date()); reqBean.setTargetAddr(getEndpoint()); reqBean.setResultCode("0"); ilog.info(reqBean); return response; }
From source file:com.mhise.util.MHISEUtil.java
public static String CallWebService(String url, String soapAction, String envelope, DefaultHttpClient httpClient) {/* w w w . j av a 2 s. c om*/ // request parameters HttpParams params = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, 60000); HttpConnectionParams.setSoTimeout(params, 60000); // set parameter HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true); // POST the envelope HttpPost httppost = new HttpPost(url); // add headers httppost.setHeader("action", soapAction); httppost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8;"); String responseString = ""; try { // the entity holds the request HttpEntity entity = new StringEntity(envelope); httppost.setEntity(entity); // Response handler ResponseHandler<String> rh = new ResponseHandler<String>() { // invoked when client receives response public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { //Log.e("CallWebService",getResponseBody(response)); // get response entity HttpEntity entity = response.getEntity(); // read the response as byte array StringBuffer out = new StringBuffer(); byte[] b = EntityUtils.toByteArray(entity); String str = new String(b, 0, b.length); Log.e("string length", "" + str); //int index = b.length-1; //Log.e("b lase string",""+(char)(b[index-2])+(char)(b[index-1])+(char)(b[index])); // write the response byte array to a string buffer out.append(new String(b, 0, b.length)); return out.toString(); //return getResponseBody(response); } }; try { responseString = httpClient.execute(httppost, rh); } catch (Exception e) { Logger.debug("MHISEUtil-->CallWebService -->", "finally clause"); } } catch (Exception e) { Logger.debug("MHISEUtil-->CallWebService -->", "finally clause"); } // close the connection httpClient.getConnectionManager().closeExpiredConnections(); httpClient.getConnectionManager().shutdown(); Log.e("Response", "" + responseString); return responseString; }
From source file:com.ntsync.android.sync.client.NetworkUtilities.java
/** * // ww w . j av a 2 s .c o m * @param authtoken * @return KeySalt and KeyCheck. null if server was returning nothing * @throws IOException * @throws AuthenticationException * @throws AuthenticatorException * @throws OperationCanceledException * @throws ServerException * @throws NetworkErrorException */ public static byte[] getKeySalt(Context context, String accountName, String authtoken) throws AuthenticationException, OperationCanceledException, ServerException, NetworkErrorException { boolean retry; int retryCount = 0; String currAuthtoken = authtoken; byte[] pwdSalt = null; do { retry = false; final HttpGet get = new HttpGet(PWDSALT_URI); HttpEntity entity = null; try { final HttpResponse resp = getHttpClient(context).execute(get, createHttpContext(accountName, currAuthtoken)); entity = resp.getEntity(); int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { byte[] respBytes = EntityUtils.toByteArray(entity); if (BuildConfig.DEBUG) { Log.v(TAG, "Get Response-Lenght:" + (respBytes != null ? respBytes.length : "null")); } pwdSalt = respBytes == null || respBytes.length == 0 ? null : respBytes; } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { AccountManager accountManager = AccountManager.get(context); currAuthtoken = retryAuthentification(retryCount, accountManager, currAuthtoken, accountName, resp); retry = true; } else { throw new ServerException("Server error in query getPwdSalt: " + resp.getStatusLine()); } } catch (IOException ex) { throw new NetworkErrorException(ex); } finally { consumeContent(entity); } retryCount++; } while (retry); return pwdSalt; }
From source file:com.jzboy.couchdb.Database.java
/** * Get the speficied document attachment as a byte array. * @param docId id of the document to which the attachment belongs * @param fileName name of the attachment * @return the streaming attachment// w ww . j a v a 2s . c o m * @throws IOException if the HttpClient throws an IOException * @throws URISyntaxException if there was a problem constructing a URI for this database * @throws CouchDBException if CouchDB returns an error - response code >= 300. The response * details are encapsulated in the exception. */ public byte[] getAttachment(String docId, String fileName) throws URISyntaxException, IOException, CouchDBException { HttpResponse res = server.getHttpClient().getHttpResponse(URITemplates.attachment(this, docId, fileName)); if (res.getStatusLine().getStatusCode() >= 300) { res.getEntity().consumeContent(); // necessary in order to release the underlying connection throw new CouchDBException(res.getStatusLine().getStatusCode(), res.getStatusLine().getReasonPhrase()); } return EntityUtils.toByteArray(res.getEntity()); }
From source file:com.ntsync.android.sync.client.NetworkUtilities.java
/** * //from ww w .j av a 2 s . c o m * @param authtoken * @return null if server returned no restrictions. * @throws IOException * @throws AuthenticationException * @throws ParserConfigurationException * @throws SAXException * @throws AuthenticatorException * @throws OperationCanceledException * @throws NetworkErrorException * @throws ServerException */ public static Restrictions getRestrictions(Context context, Account account, String authtoken, AccountManager accountManager) throws AuthenticationException, OperationCanceledException, AuthenticatorException, NetworkErrorException, ServerException { String currAuthtoken = authtoken; HttpEntity entity = null; Restrictions restr = null; boolean retry; int retryCount = 0; do { retry = false; final HttpGet get = new HttpGet(RESTRICTIONS_URI); try { final HttpResponse resp = getHttpClient(context).execute(get, createHttpContext(account.name, currAuthtoken)); entity = resp.getEntity(); int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { byte[] respBytes = EntityUtils.toByteArray(entity); if (BuildConfig.DEBUG) { Log.v(TAG, "Get Response-Lenght:" + (respBytes != null ? respBytes.length : "null")); } if (respBytes != null && respBytes.length > 0) { restr = RequestGenerator.parseRestr(new ByteArrayInputStream(respBytes)); } } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { currAuthtoken = retryAuthentification(retryCount, accountManager, currAuthtoken, account.name, resp); retry = true; } else { throw new ServerException("Server error in query getRestrictions: " + resp.getStatusLine()); } } catch (IOException e) { throw new NetworkErrorException(e); } finally { consumeContent(entity); } retryCount++; } while (retry); return restr; }
From source file:de.escidoc.core.common.business.indexing.IndexingHandler.java
/** * Return all PIDs contained in given index. * * @param indexName name of the index//www . ja v a2s .c o m * @return List of PIDs * @throws SystemException Thrown if a framework internal error occurs. */ private Set<String> getPids(final String indexName) throws SystemException { try { final StaxParser sp = new StaxParser(); final SrwScanResponseHandler handler = new SrwScanResponseHandler(sp); sp.addHandler(handler); String lastTerm = ""; boolean running = true; while (running) { handler.resetNoOfDocumentTerms(); final HttpResponse response = connectionUtility .getRequestURL(new URL(EscidocConfiguration.getInstance().get(EscidocConfiguration.SRW_URL) + "/search/" + indexName + Constants.SRW_TERM_PATTERN.matcher(SRW_QUERY).replaceFirst(lastTerm))); final String lastLastTerm = handler.getLastTerm(); sp.parse(new ByteArrayInputStream(EntityUtils.toByteArray(response.getEntity()))); lastTerm = handler.getLastTerm(); if (handler.getNoOfDocumentTerms() == 0) { running = false; } else if (lastTerm.equals(lastLastTerm)) { throw new SystemException("duplicate PID in Scan Operation"); } } return handler.getTerms(); } catch (final IOException e) { throw new SystemException(e.getMessage(), e); } catch (final Exception e) { throw new SystemException(e.getMessage(), e); } }
From source file:com.ntsync.android.sync.client.NetworkUtilities.java
/** * Get current Prices for a Currency//from w ww .j a v a 2s.c om * * @param authtoken * can be null * @return null if server returned no prices. * @throws IOException * @throws AuthenticationException * @throws ParserConfigurationException * @throws SAXException * @throws AuthenticatorException * @throws OperationCanceledException * @throws ServerException * @throws NetworkErrorException */ public static List<Price> getPrices(Context context, Account account, String authtoken, AccountManager accountManager, String currency) throws OperationCanceledException, AuthenticationException, NetworkErrorException, ServerException { String currAuthtoken = authtoken; HttpEntity entity = null; List<Price> prices = null; boolean retry; int retryCount = 0; do { retry = false; final HttpGet get = new HttpGet(PRICE_URI + currency); LogHelper.logD(TAG, "getPrices with URI: {}", get.getURI()); try { final HttpResponse resp = getHttpClient(context).execute(get, createHttpContext(account.name, currAuthtoken)); entity = resp.getEntity(); int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { ObjectMapper mapper = new ObjectMapper(); Class<?> clz = Price.class; JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clz); byte[] respBytes = EntityUtils.toByteArray(entity); prices = mapper.readValue(respBytes, type); } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { currAuthtoken = retryAuthentification(retryCount, accountManager, currAuthtoken, account.name, resp); retry = true; } else { throw new ServerException("Server error in query getPrices: " + resp.getStatusLine()); } } catch (IOException e) { throw new NetworkErrorException(e); } finally { consumeContent(entity); } retryCount++; } while (retry); return prices; }
From source file:cz.cesnet.shongo.connector.device.CiscoMCUConnector.java
/** * Perform http request for given {@code file} * * @param file//from w w w .ja v a 2 s . c om * @return content as {@link MediaData} * @throws CommandException */ private synchronized MediaData execHttp(String file) throws CommandException { try { URL requestUrl = getDeviceHttpUrl(file); HttpGet request = new HttpGet(requestUrl.toURI()); HttpContext context = new BasicHttpContext(); HttpResponse response = httpClient.execute(request, context); HttpRequest responseRequest = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); StatusLine responseStatusLine = response.getStatusLine(); if (responseStatusLine.getStatusCode() == HttpStatus.SC_OK) { if (responseRequest.getRequestLine().getUri().startsWith("/login.html")) { // Perform login loginHttp(); // Perform the request again response = httpClient.execute(request, context); } HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { byte[] mediaContent = EntityUtils.toByteArray(responseEntity); MediaType mediaType = detector.detect(TikaInputStream.get(mediaContent), new Metadata()); return new MediaData(mediaType, mediaContent); } } throw new RuntimeException(response.getStatusLine().toString()); } catch (CommandException exception) { throw exception; } catch (Exception exception) { throw new CommandException("Http request " + file + " failed.", exception); } }
From source file:org.apache.awf.web.SystemTest.java
@Test public void serverChunkedBodyRequest() throws Exception { HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/echo"); StringEntity se = new StringEntity("azertyuiopqsdfghjklmwxc\nvbn1234567890"); se.setChunked(true);//from w w w . j a v a 2 s.c om httpPost.setEntity(se); HttpResponse httpResponse = httpclient.execute(httpPost); assertEquals("azertyuiopqsdfghjklmwxc\nvbn1234567890", new String(EntityUtils.toByteArray(httpResponse.getEntity()))); }