List of usage examples for org.apache.http.client.methods HttpUriRequest getURI
URI getURI();
From source file:com.akop.bach.parser.XboxLiveParser.java
private GameOverviewInfo parseGameOverview(String url) throws IOException, ParserException { String loadUrl = url;/* w w w. j av a 2 s.c o m*/ if (PATTERN_GAME_OVERVIEW_REDIRECTING_URL.matcher(url).find()) { // Redirecting URL; figure out where it's redirecting HttpParams p = mHttpClient.getParams(); try { p.setParameter("http.protocol.max-redirects", 1); HttpGet httpget = new HttpGet(url); HttpContext context = new BasicHttpContext(); HttpResponse response = mHttpClient.execute(httpget, context); HttpEntity entity = response.getEntity(); if (entity != null) entity.consumeContent(); HttpUriRequest request = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); try { loadUrl = new URI(url).resolve(request.getURI()).toString(); } catch (URISyntaxException e) { if (App.getConfig().logToConsole()) e.printStackTrace(); } if (App.getConfig().logToConsole()) App.logv("Redirection URL determined to be " + loadUrl); } finally { p.setParameter("http.protocol.max-redirects", 0); } } String page = getResponse(loadUrl.concat("?NoSplash=1")); GameOverviewInfo overview = new GameOverviewInfo(); Matcher m; if (!(m = PATTERN_GAME_OVERVIEW_TITLE.matcher(page)).find()) throw new ParserException(mContext, R.string.error_no_details_available); overview.Title = htmlDecode(m.group(1)); if ((m = PATTERN_GAME_OVERVIEW_DESCRIPTION.matcher(page)).find()) overview.Description = htmlDecode(m.group(1)); if ((m = PATTERN_GAME_OVERVIEW_MANUAL.matcher(page)).find()) overview.ManualUrl = m.group(1); if ((m = PATTERN_GAME_OVERVIEW_ESRB.matcher(page)).find()) { overview.EsrbRatingDescription = htmlDecode(m.group(1)); overview.EsrbRatingIconUrl = m.group(2); } if ((m = PATTERN_GAME_OVERVIEW_BANNER.matcher(page)).find()) overview.BannerUrl = m.group(1); m = PATTERN_GAME_OVERVIEW_IMAGE.matcher(page); while (m.find()) overview.Screenshots.add(m.group(1)); return overview; }
From source file:com.yunmall.ymsdk.net.http.AsyncHttpClient.java
/** * Puts a new request in queue as a new thread in pool to be executed * * @param client HttpClient to be used for request, can differ in single requests * @param contentType MIME body type, for POST and PUT requests, may be null * @param context Context of Android application, to hold the reference of request * @param httpContext HttpContext in which the request will be executed * @param responseHandler ResponseHandler or its subclass to put the response into * @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete, * HttpPost, HttpGet, HttpPut, etc. * @return RequestHandle of future request process *//*from w ww .ja v a 2 s.co m*/ protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) { if (uriRequest == null) { throw new IllegalArgumentException("HttpUriRequest must not be null"); } if (responseHandler == null) { throw new IllegalArgumentException("ResponseHandler must not be null"); } if (responseHandler.getUseSynchronousMode()) { throw new IllegalArgumentException( "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead."); } if (contentType != null) { if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) { YmLog.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type"); } else { uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType); } } responseHandler.setRequestHeaders(uriRequest.getAllHeaders()); responseHandler.setRequestURI(uriRequest.getURI()); AsyncHttpRequest request = new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler); threadPool.submit(request); RequestHandle requestHandle = new RequestHandle(request); if (context != null) { // Add request to request map List<RequestHandle> requestList = requestMap.get(context); synchronized (requestMap) { if (requestList == null) { requestList = Collections.synchronizedList(new LinkedList<RequestHandle>()); requestMap.put(context, requestList); } } if (responseHandler instanceof RangeFileAsyncHttpResponseHandler) ((RangeFileAsyncHttpResponseHandler) responseHandler).updateRequestHeaders(uriRequest); requestList.add(requestHandle); Iterator<RequestHandle> iterator = requestList.iterator(); while (iterator.hasNext()) { if (iterator.next().shouldBeGarbageCollected()) { iterator.remove(); } } } return requestHandle; }
From source file:io.personium.core.rs.box.PersoniumEngineSvcCollectionResource.java
/** * relay??./*from w w w . ja v a 2 s. c om*/ * @param method * @param uriInfo URI * @param path ?? * @param headers * @param is * @return JAX-RS Response */ public Response relaycommon(String method, UriInfo uriInfo, String path, HttpHeaders headers, InputStream is) { String cellName = this.davRsCmp.getCell().getName(); String boxName = this.davRsCmp.getBox().getName(); String requestUrl = String.format("http://%s:%s/%s/%s/%s/service/%s", PersoniumUnitConfig.getEngineHost(), PersoniumUnitConfig.getEnginePort(), PersoniumUnitConfig.getEnginePath(), cellName, boxName, path); // baseUrl? String baseUrl = uriInfo.getBaseUri().toString(); // ??? HttpClient client = new DefaultHttpClient(); HttpUriRequest req = null; if (method.equals(HttpMethod.POST)) { HttpPost post = new HttpPost(requestUrl); InputStreamEntity ise = new InputStreamEntity(is, -1); ise.setChunked(true); post.setEntity(ise); req = post; } else if (method.equals(HttpMethod.PUT)) { HttpPut put = new HttpPut(requestUrl); InputStreamEntity ise = new InputStreamEntity(is, -1); ise.setChunked(true); put.setEntity(ise); req = put; } else if (method.equals(HttpMethod.DELETE)) { HttpDelete delete = new HttpDelete(requestUrl); req = delete; } else { HttpGet get = new HttpGet(requestUrl); req = get; } req.addHeader("X-Baseurl", baseUrl); req.addHeader("X-Request-Uri", uriInfo.getRequestUri().toString()); if (davCmp instanceof DavCmpFsImpl) { DavCmpFsImpl dcmp = (DavCmpFsImpl) davCmp; req.addHeader("X-Personium-Fs-Path", dcmp.getFsPath()); req.addHeader("X-Personium-Fs-Routing-Id", dcmp.getCellId()); } req.addHeader("X-Personium-Box-Schema", this.davRsCmp.getBox().getSchema()); // ??? MultivaluedMap<String, String> multivalueHeaders = headers.getRequestHeaders(); for (Iterator<Entry<String, List<String>>> it = multivalueHeaders.entrySet().iterator(); it.hasNext();) { Entry<String, List<String>> entry = it.next(); String key = (String) entry.getKey(); if (key.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) { continue; } List<String> valueList = (List<String>) entry.getValue(); for (Iterator<String> i = valueList.iterator(); i.hasNext();) { String value = (String) i.next(); req.setHeader(key, value); } } if (log.isDebugEnabled()) { log.debug("?EngineRelay " + req.getMethod() + " " + req.getURI()); Header[] reqHeaders = req.getAllHeaders(); for (int i = 0; i < reqHeaders.length; i++) { log.debug("RelayHeader[" + reqHeaders[i].getName() + "] : " + reqHeaders[i].getValue()); } } // Engine?? HttpResponse objResponse = null; try { objResponse = client.execute(req); } catch (ClientProtocolException e) { throw PersoniumCoreException.ServiceCollection.SC_INVALID_HTTP_RESPONSE_ERROR; } catch (Exception ioe) { throw PersoniumCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(ioe); } // ResponseBuilder res = Response.status(objResponse.getStatusLine().getStatusCode()); Header[] headersResEngine = objResponse.getAllHeaders(); // ? for (int i = 0; i < headersResEngine.length; i++) { // Engine????Transfer-Encoding???? // ?MW????????Content-Length???Transfer-Encoding???? // 2?????????????????????? if ("Transfer-Encoding".equalsIgnoreCase(headersResEngine[i].getName())) { continue; } // Engine????Date???? // Web??MW?Jetty???2????????? if (HttpHeaders.DATE.equalsIgnoreCase(headersResEngine[i].getName())) { continue; } res.header(headersResEngine[i].getName(), headersResEngine[i].getValue()); } InputStream isResBody = null; // ? HttpEntity entity = objResponse.getEntity(); if (entity != null) { try { isResBody = entity.getContent(); } catch (IllegalStateException e) { throw PersoniumCoreException.ServiceCollection.SC_UNKNOWN_ERROR.reason(e); } catch (IOException e) { throw PersoniumCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(e); } final InputStream isInvariable = isResBody; // ?? StreamingOutput strOutput = new StreamingOutput() { @Override public void write(final OutputStream os) throws IOException { int chr; try { while ((chr = isInvariable.read()) != -1) { os.write(chr); } } finally { isInvariable.close(); } } }; res.entity(strOutput); } // ?? return res.build(); }
From source file:illab.nabal.proxy.AbstractContext.java
/** * Execute a request and retrieve a response as a string value. * /*from ww w . ja va2 s . c om*/ * @param request * @param isEmptyAllowed * @return responseString * @throws Exception */ protected synchronized String getResponseString(HttpUriRequest request, boolean isEmptyAllowed) throws Exception { String responseString = null; if (request != null) { AndroidHttpClient client = AndroidHttpClient.newInstance(SystemProperties.USER_AGENT_ANDROID, mContext); try { HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { StringHelper strings = new StringHelper(); InputStream instream = entity.getContent(); int len; byte[] tmp = new byte[2048]; while ((len = instream.read(tmp)) != -1) { strings.append(new String(tmp, 0, len, UTF_8)); } responseString = strings.toString().trim(); Log.v(TAG, " ## HTTP response for " + request.getURI() + " : \n" + responseString); strings = null; entity.consumeContent(); } else { Log.w(TAG, "entity IS NULL"); } StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode > HttpStatus.SC_MULTIPLE_CHOICES) { Log.e(TAG, "HTTP status : " + statusCode + " - " + statusLine.getReasonPhrase()); JSONObject responseJson = null; try { responseJson = new JSONObject(responseString); } catch (Exception e) { // DO NOTHING } // if response came in JSON format if (responseJson != null) { Log.e(TAG, "responseJson.toString() : " + responseJson.toString()); NetworkException ne = new NetworkException(statusCode, statusLine.getReasonPhrase(), responseJson.toString()); ne.setResponseJson(responseJson); throw ne; } // if response didn't came in JSON format else { Log.e(TAG, "responseString : " + responseString); NetworkException ne = new NetworkException(statusCode, statusLine.getReasonPhrase(), responseString); ne.setResponseString(responseString); throw ne; } } } catch (Exception e) { request.abort(); Log.e(TAG, e.getMessage()); throw e; } finally { if (client != null) { client.close(); } } } // throw an exception if empty reponse is not allowed if (isEmptyAllowed == false && StringHelper.isEmpty(responseString) == true) { Log.e(TAG, "HTTP response is empty"); throw new NetworkException("HTTP response is empty."); } return responseString; }
From source file:com.fdwills.external.http.AsyncHttpClient.java
/** * Puts a new request in queue as a new thread in pool to be executed * * @param client HttpClient to be used for request, can differ in single requests * @param contentType MIME body type, for POST and PUT requests, may be null * @param context Context of Android application, to hold the reference of request * @param httpContext HttpContext in which the request will be executed * @param responseHandler ResponseHandler or its subclass to put the response into * @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete, * HttpPost, HttpGet, HttpPut, etc. * @return RequestHandle of future request process *///from w w w.j a v a2 s . co m protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) { if (uriRequest == null) { throw new IllegalArgumentException("HttpUriRequest must not be null"); } if (responseHandler == null) { throw new IllegalArgumentException("ResponseHandler must not be null"); } if (responseHandler.getUseSynchronousMode()) { throw new IllegalArgumentException( "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead."); } if (contentType != null) { if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) { Log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type"); } else { uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType); } } responseHandler.setRequestHeaders(uriRequest.getAllHeaders()); responseHandler.setRequestURI(uriRequest.getURI()); AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context); threadPool.submit(request); RequestHandle requestHandle = new RequestHandle(request); if (context != null) { // Add request to request map List<RequestHandle> requestList = requestMap.get(context); synchronized (requestMap) { if (requestList == null) { requestList = Collections.synchronizedList(new LinkedList<RequestHandle>()); requestMap.put(context, requestList); } } requestList.add(requestHandle); Iterator<RequestHandle> iterator = requestList.iterator(); while (iterator.hasNext()) { if (iterator.next().shouldBeGarbageCollected()) { iterator.remove(); } } } return requestHandle; }
From source file:derson.com.httpsender.AsyncHttpClient.AsyncHttpClient.java
/** * Puts a new request in queue as a new thread in pool to be executed * * @param client HttpClient to be used for request, can differ in single requests * @param contentType MIME body type, for POST and PUT requests, may be null * @param context Context of Android application, to hold the reference of request * @param httpContext HttpContext in which the request will be executed * @param responseHandler ResponseHandler or its subclass to put the response into * @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete, * HttpPost, HttpGet, HttpPut, etc. * @return RequestHandle of future request process *//*from w ww. ja v a 2 s . com*/ protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) { if (uriRequest == null) { throw new IllegalArgumentException("HttpUriRequest must not be null"); } if (responseHandler == null) { throw new IllegalArgumentException("ResponseHandler must not be null"); } // if (responseHandler.getUseSynchronousMode()) { // throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead."); // } if (contentType != null) { if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) { HPLog.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type"); } else { uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType); } } responseHandler.setRequestHeaders(uriRequest.getAllHeaders()); responseHandler.setRequestURI(uriRequest.getURI()); AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context); threadPool.submit(request); RequestHandle requestHandle = new RequestHandle(request); if (context != null) { // Add request to request map List<RequestHandle> requestList = requestMap.get(context); synchronized (requestMap) { if (requestList == null) { requestList = Collections.synchronizedList(new LinkedList<RequestHandle>()); requestMap.put(context, requestList); } } requestList.add(requestHandle); Iterator<RequestHandle> iterator = requestList.iterator(); while (iterator.hasNext()) { if (iterator.next().shouldBeGarbageCollected()) { iterator.remove(); } } } return requestHandle; }
From source file:org.dasein.cloud.terremark.TerremarkMethod.java
public Document invoke(boolean debug) throws TerremarkException, CloudException, InternalException { if (logger.isTraceEnabled()) { logger.trace("ENTER - " + TerremarkMethod.class.getName() + ".invoke(" + debug + ")"); }//from ww w .ja va 2s. com try { if (logger.isDebugEnabled()) { logger.debug("Talking to server at " + url); } if (parameters != null) { URIBuilder uri = null; try { uri = new URIBuilder(url); } catch (URISyntaxException e) { e.printStackTrace(); } for (NameValuePair parameter : parameters) { uri.addParameter(parameter.getName(), parameter.getValue()); } url = uri.toString(); } HttpUriRequest method = null; if (methodType.equals(HttpMethodName.GET)) { method = new HttpGet(url); } else if (methodType.equals(HttpMethodName.POST)) { method = new HttpPost(url); } else if (methodType.equals(HttpMethodName.DELETE)) { method = new HttpDelete(url); } else if (methodType.equals(HttpMethodName.PUT)) { method = new HttpPut(url); } else if (methodType.equals(HttpMethodName.HEAD)) { method = new HttpHead(url); } else { method = new HttpGet(url); } HttpResponse status = null; try { HttpClient client = new DefaultHttpClient(); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "Dasein Cloud"); attempts++; String proxyHost = provider.getProxyHost(); if (proxyHost != null) { int proxyPort = provider.getProxyPort(); boolean ssl = url.startsWith("https"); params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort, ssl ? "https" : "http")); } for (Map.Entry<String, String> entry : headers.entrySet()) { method.addHeader(entry.getKey(), entry.getValue()); } if (body != null && body != "" && (methodType.equals(HttpMethodName.PUT) || methodType.equals(HttpMethodName.POST))) { try { HttpEntity entity = new StringEntity(body, "UTF-8"); ((HttpEntityEnclosingRequestBase) method).setEntity(entity); } catch (UnsupportedEncodingException e) { logger.warn(e); } } if (wire.isDebugEnabled()) { wire.debug(methodType.name() + " " + method.getURI()); for (Header header : method.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } if (body != null) { wire.debug(body); } } try { status = client.execute(method); if (wire.isDebugEnabled()) { wire.debug("HTTP STATUS: " + status); } } catch (IOException e) { logger.error("I/O error from server communications: " + e.getMessage()); e.printStackTrace(); throw new InternalException(e); } int statusCode = status.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_ACCEPTED) { try { InputStream input = status.getEntity().getContent(); try { return parseResponse(input); } finally { input.close(); } } catch (IOException e) { logger.error("Error parsing response from Teremark: " + e.getMessage()); e.printStackTrace(); throw new CloudException(CloudErrorType.COMMUNICATION, statusCode, null, e.getMessage()); } } else if (statusCode == HttpStatus.SC_NO_CONTENT) { logger.debug("Recieved no content in response. Creating an empty doc."); DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = null; try { docBuilder = dbfac.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return docBuilder.newDocument(); } else if (statusCode == HttpStatus.SC_FORBIDDEN) { String msg = "OperationNotAllowed "; try { msg += parseResponseToString(status.getEntity().getContent()); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } wire.error(msg); throw new TerremarkException(statusCode, "OperationNotAllowed", msg); } else { String response = "Failed to parse response."; ParsedError parsedError = null; try { response = parseResponseToString(status.getEntity().getContent()); parsedError = parseErrorResponse(response); } catch (IllegalStateException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } if (logger.isDebugEnabled()) { logger.debug("Received " + status + " from " + url); } if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) { if (attempts >= 5) { String msg; wire.warn(response); if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE) { msg = "Cloud service is currently unavailable."; } else { msg = "The cloud service encountered a server error while processing your request."; try { msg = msg + "Response from server was:\n" + response; } catch (RuntimeException runException) { logger.warn(runException); } catch (Error error) { logger.warn(error); } } wire.error(response); logger.error(msg); if (parsedError != null) { throw new TerremarkException(parsedError); } else { throw new CloudException("HTTP Status " + statusCode + msg); } } else { try { Thread.sleep(5000L); } catch (InterruptedException e) { /* ignore */ } return invoke(); } } wire.error(response); if (parsedError != null) { throw new TerremarkException(parsedError); } else { String msg = "\nResponse from server was:\n" + response; logger.error(msg); throw new CloudException("HTTP Status " + statusCode + msg); } } } finally { try { if (status != null) { EntityUtils.consume(status.getEntity()); } } catch (IOException e) { e.printStackTrace(); } } } finally { if (logger.isTraceEnabled()) { logger.trace("EXIT - " + TerremarkMethod.class.getName() + ".invoke()"); } } }
From source file:com.fujitsu.dc.core.rs.box.DcEngineSvcCollectionResource.java
/** * relay??./*from www . j a v a2s .co m*/ * @param method * @param uriInfo URI * @param path ?? * @param headers * @param is * @return JAX-RS Response */ public Response relaycommon(String method, UriInfo uriInfo, String path, HttpHeaders headers, InputStream is) { String cellName = this.davRsCmp.getCell().getName(); String boxName = this.davRsCmp.getBox().getName(); String requestUrl = String.format("http://%s:%s/%s/%s/%s/service/%s", DcCoreConfig.getEngineHost(), DcCoreConfig.getEnginePort(), DcCoreConfig.getEnginePath(), cellName, boxName, path); // baseUrl? String baseUrl = uriInfo.getBaseUri().toString(); // ??? HttpClient client = new DefaultHttpClient(); HttpUriRequest req = null; if (method.equals(HttpMethod.POST)) { HttpPost post = new HttpPost(requestUrl); InputStreamEntity ise = new InputStreamEntity(is, -1); ise.setChunked(true); post.setEntity(ise); req = post; } else if (method.equals(HttpMethod.PUT)) { HttpPut put = new HttpPut(requestUrl); InputStreamEntity ise = new InputStreamEntity(is, -1); ise.setChunked(true); put.setEntity(ise); req = put; } else if (method.equals(HttpMethod.DELETE)) { HttpDelete delete = new HttpDelete(requestUrl); req = delete; } else { HttpGet get = new HttpGet(requestUrl); req = get; } req.addHeader("X-Baseurl", baseUrl); req.addHeader("X-Request-Uri", uriInfo.getRequestUri().toString()); // ?INDEXIDTYPE? if (davCmp instanceof DavCmpEsImpl) { DavCmpEsImpl test = (DavCmpEsImpl) davCmp; req.addHeader("X-Dc-Es-Index", test.getEsColType().getIndex().getName()); req.addHeader("X-Dc-Es-Id", test.getNodeId()); req.addHeader("X-Dc-Es-Type", test.getEsColType().getType()); req.addHeader("X-Dc-Es-Routing-Id", this.davRsCmp.getCell().getId()); req.addHeader("X-Dc-Box-Schema", this.davRsCmp.getBox().getSchema()); } // ??? MultivaluedMap<String, String> multivalueHeaders = headers.getRequestHeaders(); for (Iterator<Entry<String, List<String>>> it = multivalueHeaders.entrySet().iterator(); it.hasNext();) { Entry<String, List<String>> entry = it.next(); String key = (String) entry.getKey(); if (key.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) { continue; } List<String> valueList = (List<String>) entry.getValue(); for (Iterator<String> i = valueList.iterator(); i.hasNext();) { String value = (String) i.next(); req.setHeader(key, value); } } if (log.isDebugEnabled()) { log.debug("?EngineRelay " + req.getMethod() + " " + req.getURI()); Header[] reqHeaders = req.getAllHeaders(); for (int i = 0; i < reqHeaders.length; i++) { log.debug("RelayHeader[" + reqHeaders[i].getName() + "] : " + reqHeaders[i].getValue()); } } // Engine?? HttpResponse objResponse = null; try { objResponse = client.execute(req); } catch (ClientProtocolException e) { throw DcCoreException.ServiceCollection.SC_INVALID_HTTP_RESPONSE_ERROR; } catch (Exception ioe) { throw DcCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(ioe); } // ResponseBuilder res = Response.status(objResponse.getStatusLine().getStatusCode()); Header[] headersResEngine = objResponse.getAllHeaders(); // ? for (int i = 0; i < headersResEngine.length; i++) { // Engine????Transfer-Encoding???? // ?MW????????Content-Length???Transfer-Encoding???? // 2?????????????????????? if ("Transfer-Encoding".equalsIgnoreCase(headersResEngine[i].getName())) { continue; } // Engine????Date???? // Web??MW?Jetty???2????????? if (HttpHeaders.DATE.equalsIgnoreCase(headersResEngine[i].getName())) { continue; } res.header(headersResEngine[i].getName(), headersResEngine[i].getValue()); } InputStream isResBody = null; // ? HttpEntity entity = objResponse.getEntity(); if (entity != null) { try { isResBody = entity.getContent(); } catch (IllegalStateException e) { throw DcCoreException.ServiceCollection.SC_UNKNOWN_ERROR.reason(e); } catch (IOException e) { throw DcCoreException.ServiceCollection.SC_ENGINE_CONNECTION_ERROR.reason(e); } final InputStream isInvariable = isResBody; // ?? StreamingOutput strOutput = new StreamingOutput() { @Override public void write(final OutputStream os) throws IOException { int chr; try { while ((chr = isInvariable.read()) != -1) { os.write(chr); } } finally { isInvariable.close(); } } }; res.entity(strOutput); } // ?? return res.build(); }
From source file:com.example.pierre.applicompanies.library_http.AsyncHttpClient.java
/** * Puts a new request in queue as a new thread in pool to be executed * * @param client HttpClient to be used for request, can differ in single requests * @param contentType MIME body type, for POST and PUT requests, may be null * @param context Context of Android application, to hold the reference of request * @param httpContext HttpContext in which the request will be executed * @param responseHandler ResponseHandler or its subclass to put the response into * @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete, * HttpPost, HttpGet, HttpPut, etc. * @return RequestHandle of future request process *//*from w w w . ja v a 2 s .co m*/ protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) { if (uriRequest == null) { throw new IllegalArgumentException("HttpUriRequest must not be null"); } if (responseHandler == null) { throw new IllegalArgumentException("ResponseHandler must not be null"); } if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) { throw new IllegalArgumentException( "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead."); } if (contentType != null) { if (uriRequest instanceof HttpEntityEnclosingRequestBase && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null) { Log.w(LOG_TAG, "Passed contentType will be ignored because HttpEntity sets content type"); } else { uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType); } } responseHandler.setRequestHeaders(uriRequest.getAllHeaders()); responseHandler.setRequestURI(uriRequest.getURI()); AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context); threadPool.submit(request); RequestHandle requestHandle = new RequestHandle(request); if (context != null) { // Add request to request map List<RequestHandle> requestList = requestMap.get(context); synchronized (requestMap) { if (requestList == null) { requestList = Collections.synchronizedList(new LinkedList<RequestHandle>()); requestMap.put(context, requestList); } } requestList.add(requestHandle); Iterator<RequestHandle> iterator = requestList.iterator(); while (iterator.hasNext()) { if (iterator.next().shouldBeGarbageCollected()) { iterator.remove(); } } } return requestHandle; }
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//from w ww. j a va2s. co 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; }