List of usage examples for org.apache.http.client.methods HttpRequestBase releaseConnection
public void releaseConnection()
From source file:com.twosigma.cook.jobclient.JobClient.java
/** * Submits jobs and groups to Cook scheduler and start to track the jobs until they complete. Note that jobs * submitted through this API will not be listened by any listener. * * @param jobs specifies a list of {@link Job}s to be submitted. * @param groups specifies a list of {@link Group}s to be submitted. * @return the response string from Cook scheduler rest endpoint. * @throws JobClientException/* ww w.j ava2 s .c om*/ */ public void submitWithGroups(List<Job> jobs, List<Group> groups) throws JobClientException { JSONObject json = new JSONObject(); try { JSONObject groupsJSON = Group.jsonizeGroups(groups); JSONObject jobsJSON = Job.jsonizeJob(jobs); json.put("groups", groupsJSON.getJSONArray("groups")); json.put("jobs", jobsJSON.getJSONArray("jobs")); } catch (JSONException e) { throw new JobClientException("Can not jsonize jobs or groups to submit.", e); } HttpResponse httpResponse; HttpRequestBase httpRequest = makeHttpPost(_jobURI, json); try { httpResponse = executeWithRetries(httpRequest, 5, 10); } catch (IOException e) { throw releaseAndCreateException(httpRequest, null, "Can not submit POST request " + json + " via uri " + _jobURI, e); } // Get the response string. StatusLine statusLine = httpResponse.getStatusLine(); HttpEntity entity = httpResponse.getEntity(); if (entity == null) { throw releaseAndCreateException(httpRequest, null, "The response entity is null!", null); } String response = null; try { response = EntityUtils.toString(entity); // Ensure that the entity content has been fully consumed and the underlying stream has been closed. EntityUtils.consume(entity); } catch (ParseException | IOException e) { throw releaseAndCreateException(httpRequest, null, "Can not parse the response for POST request " + json + " via uri " + _jobURI, e); } if (_log.isDebugEnabled()) { _log.debug("Response String for submitting jobs and groups" + json.toString() + " is " + response); } // Base on the decision graph // http://clojure-liberator.github.io/liberator/tutorial/decision-graph.html // If the jobs and groups are submitted successfully, the status code is 201. // If a job or group uses a UUID which has been used before, the returned status code is 400 and the // return message is something like: // clojure.lang.ExceptionInfo: [Job | Group] UUID 26719da8-194f-44f9-9e6d-8a17500f5109 already used {:uuid // #uuid "26719da8-194f-44f9-9e6d-8a17500f5109"} // A flag to indicate if the submission is successful. boolean isSuccess = false; if (null != statusLine && statusLine.getStatusCode() == HttpStatus.SC_CREATED) { isSuccess = true; _log.info("Successfully execute POST request with data " + json + " via uri " + _jobURI); } else if (null != statusLine && statusLine.getStatusCode() >= HttpStatus.SC_BAD_REQUEST) { final Pattern patternUUID = Pattern.compile( "([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12} already used)"); final Matcher matchUUID = patternUUID.matcher(response); if (matchUUID.find()) { _log.info("Successfully execute POST request with several retries " + json + " via uri " + _jobURI); isSuccess = true; } else { _log.warn("Failed to execute POST request with several retries " + json + " via uri " + _jobURI); } } if (null != httpRequest) { httpRequest.releaseConnection(); } if (isSuccess) { // Update status map. for (Job job : jobs) { _activeUUIDToJob.put(job.getUUID(), job); } for (Group group : groups) { _activeUUIDToGroup.put(group.getUUID(), group); } } else { _log.error("Failed to submit jobs " + json.toString()); throw releaseAndCreateException(httpRequest, httpResponse, "The response of POST request " + json + " via uri " + _jobURI + ": " + statusLine.getReasonPhrase() + ", " + statusLine.getStatusCode() + " Body is " + response, null); } }
From source file:com.liferay.petra.json.web.service.client.BaseJSONWebServiceClientImpl.java
protected String execute(HttpRequestBase httpRequestBase) throws JSONWebServiceInvocationException, JSONWebServiceTransportException { signRequest(httpRequestBase);/*ww w . j a va2 s.c o m*/ HttpHost httpHost = new HttpHost(_hostName, _hostPort, _protocol); try { if (_closeableHttpAsyncClient == null) { afterPropertiesSet(); } Future<HttpResponse> future = null; if (!isNull(_login) && !isNull(_password)) { HttpClientContext httpClientContext = HttpClientContext.create(); AuthCache authCache = new BasicAuthCache(); AuthScheme authScheme = null; if (!isNull(_proxyHostName)) { authScheme = new BasicScheme(ChallengeState.PROXY); } else { authScheme = new BasicScheme(ChallengeState.TARGET); } authCache.put(httpHost, authScheme); httpClientContext.setAttribute(ClientContext.AUTH_CACHE, authCache); future = _closeableHttpAsyncClient.execute(httpHost, httpRequestBase, httpClientContext, null); } else { future = _closeableHttpAsyncClient.execute(httpHost, httpRequestBase, null); } HttpResponse httpResponse = future.get(); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (_logger.isTraceEnabled()) { _logger.trace("Server returned status " + statusCode); } HttpEntity httpEntity = httpResponse.getEntity(); if ((statusCode == HttpServletResponse.SC_NO_CONTENT) || (((httpEntity == null) || (httpEntity.getContentLength() == 0)) && _isStatus2XX(statusCode))) { return null; } String content = EntityUtils.toString(httpEntity, _CHARSET); if ((httpEntity.getContentType() != null) && _isApplicationJSONContentType(httpEntity)) { content = updateJSON(content); } if (_isStatus2XX(statusCode)) { return content; } else if ((statusCode == HttpServletResponse.SC_BAD_REQUEST) || (statusCode == HttpServletResponse.SC_FORBIDDEN) || (statusCode == HttpServletResponse.SC_METHOD_NOT_ALLOWED) || (statusCode == HttpServletResponse.SC_NOT_ACCEPTABLE) || (statusCode == HttpServletResponse.SC_NOT_FOUND)) { throw new JSONWebServiceInvocationException(content, statusCode); } else if (statusCode == HttpServletResponse.SC_UNAUTHORIZED) { throw new JSONWebServiceTransportException.AuthenticationFailure( "Not authorized to access JSON web service"); } throw new JSONWebServiceTransportException.CommunicationFailure("Server returned status " + statusCode, statusCode); } catch (ExecutionException ee) { throw new JSONWebServiceTransportException.CommunicationFailure("Unable to transmit request", ee); } catch (InterruptedException ie) { throw new JSONWebServiceTransportException.CommunicationFailure("Unable to transmit request", ie); } catch (IOException ioe) { throw new JSONWebServiceTransportException.CommunicationFailure("Unable to transmit request", ioe); } finally { httpRequestBase.releaseConnection(); } }
From source file:org.instagram4j.DefaultInstagramClient.java
private <T> Result<T> requestEntity(HttpRequestBase method, Class<T> type, boolean signableRequest) throws InstagramException { method.getParams().setParameter("http.useragent", "Instagram4j/1.0"); JsonParser jp = null;/*from w w w.j ava 2 s . c om*/ HttpResponse response = null; ResultMeta meta = null; try { method.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); if (signableRequest) setEnforceHeader(method); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 15000); client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 30000); if (LOG.isDebugEnabled()) LOG.debug(String.format("Requesting entity entry point %s, method %s", method.getURI().toString(), method.getMethod())); autoThrottle(); response = client.execute(method); jp = createParser(response, method); JsonToken tok = jp.nextToken(); if (tok != JsonToken.START_OBJECT) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw createInstagramException("Instagram request failed", method.getURI().toString(), response, null, null); throw createInstagramException("Invalid response format from Instagram API", method.getURI().toString(), response, null, null); } T data = null; while (true) { tok = jp.nextValue(); if (tok == JsonToken.START_ARRAY) { throw createInstagramException("Unexpected array in entity response " + jp.getCurrentName(), method.getURI().toString(), response, meta, null); } else if (tok == JsonToken.START_OBJECT) { // Should be "data" or "meta" String name = jp.getCurrentName(); if ("meta".equals(name)) meta = jp.readValueAs(ResultMeta.class); else if ("data".equals(name)) { if (type != null) data = jp.readValueAs(type); else jp.readValueAs(Map.class); // Consume & ignore } else throw createInstagramException("Unexpected field name " + name, method.getURI().toString(), response, meta, null); } else break; } if (data == null && meta == null && response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw createInstagramException("Instagram request failed", method.getURI().toString(), response, null, null); Result<T> result = new Result<T>(null, meta, data); setRateLimits(response, result); return result; } catch (JsonParseException e) { throw createInstagramException("Error parsing response from Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } catch (JsonProcessingException e) { throw createInstagramException("Error parsing response from Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } catch (IOException e) { throw createInstagramException("Error communicating with Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } finally { if (jp != null) try { jp.close(); } catch (IOException e) { } method.releaseConnection(); } }
From source file:com.lehman.ic9.net.httpClient.java
/** * Performs the actual HTTP request. This method is called from the GET and POST * methods. //from w w w.ja v a2s .co m * @param getString is a boolean flag with true for string and false for binary. * @param post is a boolean flag with true for post and false for get. * @return A Javascript object with the results of the request. * @throws ic9exception Exception * @throws NoSuchMethodException Exception * @throws ScriptException Exception */ private Map<String, Object> performRequest(boolean getString, httpReqType reqType) throws ic9exception, NoSuchMethodException, ScriptException { Map<String, Object> ret = this.eng.newObj(null); HttpRequestBase httpReq = null; if (reqType == httpReqType.GET) httpReq = new HttpGet(this.u.toString()); else if (reqType == httpReqType.POST) httpReq = new HttpPost(this.u.toString()); else if (reqType == httpReqType.PUT) httpReq = new HttpPut(this.u.toString()); else if (reqType == httpReqType.DELETE) httpReq = new HttpDelete(this.u.toString()); // Set cookies from JS object. this.cs.clear(); Object[] jscookies = (Object[]) this.eng.getJavaArray(this.jsobj.get("cookies")); for (Object tobj : jscookies) { @SuppressWarnings("unchecked") Map<String, Object> cobj = (Map<String, Object>) tobj; this.cs.addCookie(this.setApacheCookie(cobj)); } HttpClientContext ctx = HttpClientContext.create(); ctx.setCookieStore(this.cs); ctx.setCredentialsProvider(this.cp); ctx.setRequestConfig(this.rcb.build()); // Set headers. @SuppressWarnings("unchecked") Map<String, Object> headers = (Map<String, Object>) this.jsobj.get("headers"); for (String key : headers.keySet()) { String val = (String) headers.get(key); httpReq.addHeader(key, val); } CloseableHttpResponse resp = null; try { if (this.cli == null) { this.buildClient(httpReq); } if (reqType == httpReqType.POST && this.respEnt != null) ((HttpPost) httpReq).setEntity(this.respEnt); else if (reqType == httpReqType.PUT && this.respEnt != null) ((HttpPut) httpReq).setEntity(this.respEnt); resp = this.cli.execute(httpReq, ctx); HttpEntity ent = resp.getEntity(); this.getResponseInfo(ret, resp); if (ent != null) { if (getString) ret.put("content", this.getContentString(ent.getContent())); else { Map<String, Object> obj = this.eng.newObj("Buffer"); obj.put("data", this.getContentBinary(ent.getContent())); ret.put("content", obj); } EntityUtils.consume(ent); } } catch (ClientProtocolException e) { throw new ic9exception("httpClient.performRequest(): Client protocol exception. " + e.getMessage()); } catch (IOException e) { throw new ic9exception("httpClient.performRequest(): IO exception. " + e.getMessage()); } catch (KeyManagementException e) { throw new ic9exception("httpClient.performRequest(): Key management exception. " + e.getMessage()); } catch (NoSuchAlgorithmException e) { throw new ic9exception("httpClient.performRequest(): No such algorithm exception. " + e.getMessage()); } catch (KeyStoreException e) { throw new ic9exception("httpClient.performRequest(): Key store exception. " + e.getMessage()); } catch (AuthenticationException e) { throw new ic9exception("httpClient.performRequest(): Authentication exception. " + e.getMessage()); } catch (Exception e) { e.printStackTrace(); throw new ic9exception("httpClient.performRequest(): Unhandled exception. " + e.getMessage()); } finally { // Reset credentials if (this.creds != null) { this.creds = null; this.atype = authType.NONE; } if (resp != null) { try { resp.close(); } catch (IOException e) { } } if (reqType == httpReqType.POST || reqType == httpReqType.PUT) this.respEnt = null; } // Release the connection. httpReq.releaseConnection(); return ret; }
From source file:com.algolia.search.saas.APIClient.java
private JSONObject _requestByHost(HttpRequestBase req, String host, String url, String json, HashMap<String, String> errors) throws AlgoliaException { req.reset();/* w ww. j a va 2 s. com*/ // set URL try { req.setURI(new URI("https://" + host + url)); } catch (URISyntaxException e) { // never reached throw new IllegalStateException(e); } // set auth headers req.setHeader("X-Algolia-Application-Id", this.applicationID); if (forwardAdminAPIKey == null) { req.setHeader("X-Algolia-API-Key", this.apiKey); } else { req.setHeader("X-Algolia-API-Key", this.forwardAdminAPIKey); req.setHeader("X-Forwarded-For", this.forwardEndUserIP); req.setHeader("X-Forwarded-API-Key", this.forwardRateLimitAPIKey); } for (Entry<String, String> entry : headers.entrySet()) { req.setHeader(entry.getKey(), entry.getValue()); } // set user agent req.setHeader("User-Agent", "Algolia for Java " + version); // set JSON entity if (json != null) { if (!(req instanceof HttpEntityEnclosingRequestBase)) { throw new IllegalArgumentException("Method " + req.getMethod() + " cannot enclose entity"); } req.setHeader("Content-type", "application/json"); try { StringEntity se = new StringEntity(json, "UTF-8"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); ((HttpEntityEnclosingRequestBase) req).setEntity(se); } catch (UnsupportedEncodingException e) { throw new AlgoliaException("Invalid JSON Object: " + json); // $COVERAGE-IGNORE$ } } RequestConfig config = RequestConfig.custom().setSocketTimeout(httpSocketTimeoutMS) .setConnectTimeout(httpConnectTimeoutMS).setConnectionRequestTimeout(httpConnectTimeoutMS).build(); req.setConfig(config); HttpResponse response; try { response = httpClient.execute(req); } catch (IOException e) { // on error continue on the next host errors.put(host, String.format("%s=%s", e.getClass().getName(), e.getMessage())); return null; } try { int code = response.getStatusLine().getStatusCode(); if (code / 100 == 4) { String message = ""; try { message = EntityUtils.toString(response.getEntity()); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (code == 400) { throw new AlgoliaException(code, message.length() > 0 ? message : "Bad request"); } else if (code == 403) { throw new AlgoliaException(code, message.length() > 0 ? message : "Invalid Application-ID or API-Key"); } else if (code == 404) { throw new AlgoliaException(code, message.length() > 0 ? message : "Resource does not exist"); } else { throw new AlgoliaException(code, message.length() > 0 ? message : "Error"); } } if (code / 100 != 2) { try { errors.put(host, EntityUtils.toString(response.getEntity())); } catch (IOException e) { errors.put(host, String.valueOf(code)); } // KO, continue return null; } try { InputStream istream = response.getEntity().getContent(); InputStreamReader is = new InputStreamReader(istream, "UTF-8"); JSONTokener tokener = new JSONTokener(is); JSONObject res = new JSONObject(tokener); is.close(); return res; } catch (IOException e) { return null; } catch (JSONException e) { throw new AlgoliaException("JSON decode error:" + e.getMessage()); } } finally { req.releaseConnection(); } }
From source file:org.instagram4j.DefaultInstagramClient.java
@SuppressWarnings("unchecked") private <T> Result<T[]> requestEntities(HttpRequestBase method, Class<T> type) throws InstagramException { method.getParams().setParameter("http.useragent", "Instagram4j/1.0"); JsonParser jp = null;//from ww w . j a v a 2s .c o m HttpResponse response = null; ResultMeta meta = null; try { method.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); setEnforceHeader(method); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 15000); client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 30000); if (LOG.isDebugEnabled()) LOG.debug(String.format("Requesting entities entry point %s, method %s", method.getURI().toString(), method.getMethod())); autoThrottle(); response = client.execute(method); jp = createParser(response, method); JsonToken tok = jp.nextToken(); if (tok != JsonToken.START_OBJECT) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw createInstagramException("Instagram request failed", method.getURI().toString(), response, null, null); throw createInstagramException("Invalid response format from Instagram API", method.getURI().toString(), response, null, null); } Pagination pagination = null; T[] data = null; while (true) { tok = jp.nextValue(); if (tok == JsonToken.START_ARRAY) { // Should be "data" String name = jp.getCurrentName(); if (!"data".equals(name)) throw createInstagramException("Unexpected field name " + name, method.getURI().toString(), response, meta, null); List<T> items = new ArrayList<T>(); tok = jp.nextToken(); if (tok == JsonToken.START_OBJECT) { if (type != null) { T item; while ((item = jp.readValueAs(type)) != null) items.add(item); } else jp.readValueAs(Map.class); // Consume & ignore } data = (T[]) Array.newInstance(type, items.size()); System.arraycopy(items.toArray(), 0, data, 0, items.size()); } else if (tok == JsonToken.START_OBJECT) { // Should be "pagination" or "meta" String name = jp.getCurrentName(); if ("pagination".equals(name)) pagination = jp.readValueAs(Pagination.class); else if ("meta".equals(name)) meta = jp.readValueAs(ResultMeta.class); else throw createInstagramException("Unexpected field name " + name, method.getURI().toString(), response, meta, null); } else break; } if (data == null && meta == null && response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw createInstagramException("Instagram request failed", method.getURI().toString(), response, null, null); Result<T[]> result = new Result<T[]>(pagination, meta, data); setRateLimits(response, result); return result; } catch (JsonParseException e) { throw createInstagramException("Error parsing response from Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } catch (JsonProcessingException e) { throw createInstagramException("Error parsing response from Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } catch (IOException e) { throw createInstagramException("Error communicating with Instagram: " + e.getMessage(), method.getURI().toString(), response, meta, e); } finally { if (jp != null) try { jp.close(); } catch (IOException e) { } method.releaseConnection(); } }
From source file:com.clustercontrol.http.util.GetHttpResponse.java
/** * URL??/*from w w w . j a va 2s. c o m*/ * * @param url URL * @param timeout * @return * @throws KeyStoreException * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws IOException * @throws ClientProtocolException */ public boolean execute(String url, String post) { Response result = new Response(); try { CloseableHttpClient client = getHttpClient(); result.url = url; if (m_authType != null && !AuthType.NONE.equals(m_authType)) { URI uri = new URI(url); Credentials credential = null; String authSchema = null; switch (m_authType) { case BASIC: credential = new UsernamePasswordCredentials(m_authUser, m_authPassword); authSchema = "basic"; break; case NTLM: credential = new NTCredentials(m_authUser, m_authPassword, null, null); authSchema = "ntlm"; break; case DIGEST: credential = new UsernamePasswordCredentials(m_authUser, m_authPassword); authSchema = "digest"; break; default: m_log.warn("Auth type is unexpected value. AuthType = " + m_authType.name()); } if (credential != null) { AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_REALM, authSchema); if (m_cledentialProvider.getCredentials(scope) == null) { m_cledentialProvider.setCredentials(scope, credential); } } } HttpRequestBase request = null; if (post != null && !post.isEmpty()) { List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); for (String ss : post.split("&")) { int index = ss.indexOf("="); if (index <= 0) { continue; } urlParameters.add(new BasicNameValuePair(ss.substring(0, index), ss.substring(index + 1))); } if (m_log.isTraceEnabled()) { m_log.trace("post1=" + post + ", post2=" + urlParameters); } HttpPost requestPost = new HttpPost(url); Charset charset = Consts.UTF_8; try { charset = Charset.forName( HinemosPropertyUtil.getHinemosPropertyStr("monitor.http.post.charset", "UTF-8")); } catch (UnsupportedCharsetException e) { m_log.warn("UnsupportedCharsetException " + e.getMessage()); } requestPost.setEntity(new UrlEncodedFormEntity(urlParameters, charset)); requestPost.addHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"); request = requestPost; } else { request = new HttpGet(url); } // Execute the method. try { long start = HinemosTime.currentTimeMillis(); HttpResponse response = client.execute(request); result.responseTime = HinemosTime.currentTimeMillis() - start; result.statusCode = response.getStatusLine().getStatusCode(); // Header Header[] headers = response.getAllHeaders(); if (headers != null && headers.length > 0) { StringBuffer header = new StringBuffer(); for (int i = 0; i < headers.length; i++) { header.append((i != 0 ? "\n" : "") + headers[i]); } result.headerString = header.toString(); result.headers = Arrays.asList(headers); } if (result.statusCode == HttpStatus.SC_OK) { result.success = true; // Content-Type?text?????Body? Header header = response.getFirstHeader(HTTP.CONTENT_TYPE); boolean contentTypeFlag = false; String[] contentTypes = HinemosPropertyUtil .getHinemosPropertyStr(TARGET_CONTENT_TYPE_KEY, "text").split(","); if (header != null && header.getValue() != null) { String value = header.getValue(); for (String contentType : contentTypes) { if (value.indexOf(contentType) != -1) { contentTypeFlag = true; break; } } } if (contentTypeFlag) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (InputStream in = response.getEntity().getContent()) { byte[] buffer = new byte[BUFF_SIZE]; while (out.size() < BODY_MAX_SIZE) { int len = in.read(buffer); if (len < 0) { break; } out.write(buffer, 0, len); } } // ????HTTP ? meta ??????? // HTTP ?? // // Content-Type: text/html; charset=euc-jp // // meta ? // // <meta http-equiv="Content-Type" content="text/html; charset=euc-jp"> // <meta charset="euc-jp"> // // HTML ???meta ????? // // ???????????????? // ??????????????????? // ???????????????? // // ?????????????????? // // 1. HTTP ? Content-Type ? charset ???????? // ????????? // // 2. ????????"JISAutoDetect" ??? // ???? // // 3. ??????meta ?? // // 4. meta ?????????? // ???????? // ??????????? String charset = "JISAutoDetect"; Matcher m = chasetPattern.matcher(header.getValue()); if (m.matches()) charset = m.group(1); String content = new String(out.toByteArray(), charset); CharsetParser parser = new CharsetParser(); ParserDelegator p = new ParserDelegator(); p.parse(new StringReader(content), parser, true); if (parser.charset != null && !charset.equals(parser.charset)) { charset = parser.charset; content = new String(out.toByteArray(), charset); } result.responseBody = content; } else { result.errorMessage = MessageConstant.MESSAGE_FAIL_TO_CHECK_NOT_TEXT.getMessage(); } } else { result.errorMessage = response.getStatusLine().toString(); } } finally { request.releaseConnection(); } } catch (UnsupportedEncodingException e) { m_log.info("execute(): " + e.getMessage() + " class=" + e.getClass().getName()); result.errorMessage = "http receiving failure. (unsupported encoding)"; result.exception = e; } catch (IOException e) { m_log.info("execute(): Fatal transport error. " + e.getMessage() + " class=" + e.getClass().getName()); result.errorMessage = "http requesting failure. (I/O error : unreachable or timeout)"; result.exception = e; } catch (Exception e) { m_log.info("execute(): " + e.getMessage() + " class=" + e.getClass().getName()); result.errorMessage = "http requesting failure. " + e.getMessage() + "(" + e.getClass().getSimpleName() + ")"; result.exception = e; } m_requestResult = result; return m_requestResult.success; }
From source file:org.springframework.extensions.webscripts.connector.RemoteClient.java
/** * Service a remote URL and write the the result into an output stream. * If an InputStream is provided then a POST will be performed with the content * pushed to the url. Otherwise a standard GET will be performed. * /* w w w .java 2s .co m*/ * @param url The URL to open and retrieve data from * @param in The optional InputStream - if set a POST or similar will be performed * @param out The OutputStream to write result to * @param res Optional HttpServletResponse - to which response headers will be copied - i.e. proxied * @param status The status object to apply the response code too * * @return encoding specified by the source URL - may be null * * @throws IOException */ private String service(URL url, InputStream in, OutputStream out, HttpServletRequest req, HttpServletResponse res, ResponseStatus status) throws IOException { final boolean trace = logger.isTraceEnabled(); final boolean debug = logger.isDebugEnabled(); if (debug) { logger.debug("Executing " + "(" + requestMethod + ") " + url.toString()); if (in != null) logger.debug(" - InputStream supplied - will push..."); if (out != null) logger.debug(" - OutputStream supplied - will stream response..."); if (req != null && res != null) logger.debug(" - Full Proxy mode between servlet request and response..."); } // aquire and configure the HttpClient HttpClient httpClient = createHttpClient(url); URL redirectURL = url; HttpResponse response; HttpRequestBase method = null; int retries = 0; // Only process redirects if we are not processing a 'push' int maxRetries = in == null ? this.maxRedirects : 1; try { do { // Release a previous method that we processed due to a redirect if (method != null) { method.reset(); method = null; } switch (this.requestMethod) { default: case GET: method = new HttpGet(redirectURL.toString()); break; case PUT: method = new HttpPut(redirectURL.toString()); break; case POST: method = new HttpPost(redirectURL.toString()); break; case DELETE: method = new HttpDelete(redirectURL.toString()); break; case HEAD: method = new HttpHead(redirectURL.toString()); break; case OPTIONS: method = new HttpOptions(redirectURL.toString()); break; } // proxy over any headers from the request stream to proxied request if (req != null) { Enumeration<String> headers = req.getHeaderNames(); while (headers.hasMoreElements()) { String key = headers.nextElement(); if (key != null) { key = key.toLowerCase(); if (!this.removeRequestHeaders.contains(key) && !this.requestProperties.containsKey(key) && !this.requestHeaders.containsKey(key)) { method.setHeader(key, req.getHeader(key)); if (trace) logger.trace("Proxy request header: " + key + "=" + req.getHeader(key)); } } } } // apply request properties, allows for the assignment and override of specific header properties // firstly pre-configured headers are applied and overridden/augmented by runtime request properties final Map<String, String> headers = (Map<String, String>) this.requestHeaders.clone(); headers.putAll(this.requestProperties); if (headers.size() != 0) { for (Map.Entry<String, String> entry : headers.entrySet()) { String headerName = entry.getKey(); String headerValue = headers.get(headerName); if (headerValue != null) { method.setHeader(headerName, headerValue); } if (trace) logger.trace("Set request header: " + headerName + "=" + headerValue); } } // Apply cookies if (this.cookies != null && !this.cookies.isEmpty()) { StringBuilder builder = new StringBuilder(128); for (Map.Entry<String, String> entry : this.cookies.entrySet()) { if (builder.length() != 0) { builder.append(';'); } builder.append(entry.getKey()); builder.append('='); builder.append(entry.getValue()); } String cookieString = builder.toString(); if (debug) logger.debug("Setting Cookie header: " + cookieString); method.setHeader(HEADER_COOKIE, cookieString); } // HTTP basic auth support if (this.username != null && this.password != null) { String auth = this.username + ':' + this.password; method.addHeader("Authorization", "Basic " + Base64.encodeBytes(auth.getBytes(), Base64.DONT_BREAK_LINES)); if (debug) logger.debug("Applied HTTP Basic Authorization for user: " + this.username); } // prepare the POST/PUT entity data if input supplied if (in != null) { method.setHeader(HEADER_CONTENT_TYPE, getRequestContentType()); if (debug) logger.debug("Set Content-Type=" + getRequestContentType()); boolean urlencoded = getRequestContentType().startsWith(X_WWW_FORM_URLENCODED); if (!urlencoded) { // apply content-length here if known (i.e. from proxied req) // if this is not set, then the content will be buffered in memory long contentLength = -1L; if (req != null) { String contentLengthStr = req.getHeader(HEADER_CONTENT_LENGTH); if (contentLengthStr != null) { try { long actualContentLength = Long.parseLong(contentLengthStr); if (actualContentLength > 0) { contentLength = actualContentLength; } } catch (NumberFormatException e) { logger.warn("Can't parse 'Content-Length' header from '" + contentLengthStr + "'. The contentLength is set to -1"); } } } if (debug) logger.debug(requestMethod + " entity Content-Length=" + contentLength); // remove the Content-Length header as the setEntity() method will perform this explicitly method.removeHeaders(HEADER_CONTENT_LENGTH); try { // Apache doc for AbstractHttpEntity states: // HttpClient must use chunk coding if the entity content length is unknown (== -1). HttpEntity entity = new InputStreamEntity(in, contentLength); ((HttpEntityEnclosingRequest) method) .setEntity(contentLength == -1L || contentLength > 16384L ? entity : new BufferedHttpEntity(entity)); ((HttpEntityEnclosingRequest) method).setHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE); } catch (IOException e) { // During the creation of the BufferedHttpEntity the underlying stream can be closed by the client, // this happens if the request is discarded by the browser - we don't log this IOException as INFO // as that would fill the logs with unhelpful noise - enable DEBUG logging to see these messages. throw new RuntimeException(e.getMessage(), e); } } else { if (req != null) { // apply any supplied request parameters Map<String, String[]> postParams = req.getParameterMap(); if (postParams != null) { List<NameValuePair> params = new ArrayList<NameValuePair>(postParams.size()); for (String key : postParams.keySet()) { String[] values = postParams.get(key); for (int i = 0; i < values.length; i++) { params.add(new BasicNameValuePair(key, values[i])); } } } // ensure that the Content-Length header is not directly proxied - as the underlying // HttpClient will encode the body as appropriate - cannot assume same as the original client sent method.removeHeaders(HEADER_CONTENT_LENGTH); } else { // Apache doc for AbstractHttpEntity states: // HttpClient must use chunk coding if the entity content length is unknown (== -1). HttpEntity entity = new InputStreamEntity(in, -1L); ((HttpEntityEnclosingRequest) method).setEntity(entity); ((HttpEntityEnclosingRequest) method).setHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE); } } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Execute the method to get the response response = httpClient.execute(method); redirectURL = processResponse(redirectURL, response); } while (redirectURL != null && ++retries < maxRetries); // record the status code for the internal response object int responseCode = response.getStatusLine().getStatusCode(); if (responseCode >= HttpServletResponse.SC_INTERNAL_SERVER_ERROR && this.exceptionOnError) { buildProxiedServerError(response); } else if (responseCode == HttpServletResponse.SC_SERVICE_UNAVAILABLE) { // Occurs when server is down and likely an ELB response throw new ConnectException(response.toString()); } boolean allowResponseCommit = (responseCode != HttpServletResponse.SC_UNAUTHORIZED || commitResponseOnAuthenticationError); status.setCode(responseCode); if (debug) logger.debug("Response status code: " + responseCode); // walk over headers that are returned from the connection // if we have a servlet response, push the headers back to the existing response object // otherwise, store headers on status Header contentType = null; Header contentLength = null; for (Header header : response.getAllHeaders()) { // NOTE: Tomcat does not appear to be obeying the servlet spec here. // If you call setHeader() the spec says it will "clear existing values" - i.e. not // add additional values to existing headers - but for Server and Transfer-Encoding // if we set them, then two values are received in the response... // In addition handle the fact that the key can be null. final String key = header.getName(); if (key != null) { if (!key.equalsIgnoreCase(HEADER_SERVER) && !key.equalsIgnoreCase(HEADER_TRANSFER_ENCODING)) { if (res != null && allowResponseCommit && !this.removeResponseHeaders.contains(key.toLowerCase())) { res.setHeader(key, header.getValue()); } // store headers back onto status status.setHeader(key, header.getValue()); if (trace) logger.trace("Response header: " + key + "=" + header.getValue()); } // grab a reference to the the content-type header here if we find it if (contentType == null && key.equalsIgnoreCase(HEADER_CONTENT_TYPE)) { contentType = header; // additional optional processing based on the Content-Type header processContentType(url, res, contentType); } // grab a reference to the Content-Length header here if we find it else if (contentLength == null && key.equalsIgnoreCase(HEADER_CONTENT_LENGTH)) { contentLength = header; } } } // locate response encoding from the headers String encoding = null; String ct = null; if (contentType != null) { ct = contentType.getValue(); int csi = ct.indexOf(CHARSETEQUALS); if (csi != -1) { encoding = ct.substring(csi + CHARSETEQUALS.length()); if ((csi = encoding.lastIndexOf(';')) != -1) { encoding = encoding.substring(0, csi); } if (debug) logger.debug("Response charset: " + encoding); } } if (debug) logger.debug("Response encoding: " + contentType); // generate container driven error message response for specific response codes if (res != null && responseCode == HttpServletResponse.SC_UNAUTHORIZED && allowResponseCommit) { res.sendError(responseCode, response.getStatusLine().getReasonPhrase()); } else { // push status to existing response object if required if (res != null && allowResponseCommit) { res.setStatus(responseCode); } // perform the stream write from the response to the output int bufferSize = this.bufferSize; if (contentLength != null) { long length = Long.parseLong(contentLength.getValue()); if (length < bufferSize) { bufferSize = (int) length; } } copyResponseStreamOutput(url, res, out, response, ct, bufferSize); } // if we get here call was successful return encoding; } catch (ConnectTimeoutException | SocketTimeoutException timeErr) { // caught a socket timeout IO exception - apply internal error code logger.info("Exception calling (" + requestMethod + ") " + url.toString()); status.setCode(HttpServletResponse.SC_REQUEST_TIMEOUT); status.setException(timeErr); status.setMessage(timeErr.getMessage()); if (res != null) { //return a Request Timeout error res.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT, timeErr.getMessage()); } throw timeErr; } catch (UnknownHostException | ConnectException hostErr) { // caught an unknown host IO exception logger.info("Exception calling (" + requestMethod + ") " + url.toString()); status.setCode(HttpServletResponse.SC_SERVICE_UNAVAILABLE); status.setException(hostErr); status.setMessage(hostErr.getMessage()); if (res != null) { // return server error code res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE, hostErr.getMessage()); } throw hostErr; } catch (IOException ioErr) { // caught a general IO exception - apply generic error code so one gets returned logger.info("Exception calling (" + requestMethod + ") " + url.toString()); status.setCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); status.setException(ioErr); status.setMessage(ioErr.getMessage()); if (res != null) { res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ioErr.getMessage()); } throw ioErr; } catch (RuntimeException e) { // caught an exception - apply generic error code so one gets returned logger.debug("Exception calling (" + requestMethod + ") " + url.toString()); status.setCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); status.setException(e); status.setMessage(e.getMessage()); if (res != null) { res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } throw e; } finally { // reset state values if (method != null) { method.releaseConnection(); } setRequestContentType(null); this.requestMethod = HttpMethod.GET; } }
From source file:org.alfresco.dataprep.AlfrescoHttpClient.java
/** * Execute HttpClient request./*from w w w . j a v a 2 s . com*/ * @param userName String user name * @param password String password * @param request HttpRequestBase the request * @return {@link HttpResponse} response */ public HttpResponse executeRequest(final String userName, final String password, HttpRequestBase request) { HttpResponse response = null; client = getHttpClientWithBasicAuth(userName, password); try { response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { throw new RuntimeException("Invalid user name or password"); } } catch (IOException e) { logger.error(response); throw new RuntimeException("Error while executing request", e); } finally { request.releaseConnection(); close(); } return response; }