List of usage examples for org.apache.http.client.methods HttpRequestBase getURI
public URI getURI()
From source file:org.apache.solr.client.solrj.impl.HttpSolrServer.java
public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { HttpRequestBase method = null; InputStream is = null;/*from w ww . ja v a2 s .co m*/ SolrParams params = request.getParams(); Collection<ContentStream> streams = requestWriter.getContentStreams(request); String path = requestWriter.getPath(request); if (path == null || !path.startsWith("/")) { path = DEFAULT_PATH; } ResponseParser parser = request.getResponseParser(); if (parser == null) { parser = this.parser; } // The parser 'wt=' and 'version=' params are used instead of the original // params ModifiableSolrParams wparams = new ModifiableSolrParams(params); if (parser != null) { wparams.set(CommonParams.WT, parser.getWriterType()); wparams.set(CommonParams.VERSION, parser.getVersion()); } if (invariantParams != null) { wparams.add(invariantParams); } int tries = maxRetries + 1; try { while (tries-- > 0) { // Note: since we aren't do intermittent time keeping // ourselves, the potential non-timeout latency could be as // much as tries-times (plus scheduling effects) the given // timeAllowed. try { if (SolrRequest.METHOD.GET == request.getMethod()) { if (streams != null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!"); } method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(wparams, false)); } else if (SolrRequest.METHOD.POST == request.getMethod()) { String url = baseUrl + path; boolean hasNullStreamName = false; if (streams != null) { for (ContentStream cs : streams) { if (cs.getName() == null) { hasNullStreamName = true; break; } } } boolean isMultipart = (this.useMultiPartPost || (streams != null && streams.size() > 1)) && !hasNullStreamName; // only send this list of params as query string params ModifiableSolrParams queryParams = new ModifiableSolrParams(); for (String param : this.queryParams) { String[] value = wparams.getParams(param); if (value != null) { for (String v : value) { queryParams.add(param, v); } wparams.remove(param); } } LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>(); if (streams == null || isMultipart) { HttpPost post = new HttpPost(url + ClientUtils.toQueryString(queryParams, false)); post.setHeader("Content-Charset", "UTF-8"); if (!isMultipart) { post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); } List<FormBodyPart> parts = new LinkedList<FormBodyPart>(); Iterator<String> iter = wparams.getParameterNamesIterator(); while (iter.hasNext()) { String p = iter.next(); String[] vals = wparams.getParams(p); if (vals != null) { for (String v : vals) { if (isMultipart) { parts.add(new FormBodyPart(p, new StringBody(v, Charset.forName("UTF-8")))); } else { postParams.add(new BasicNameValuePair(p, v)); } } } } if (isMultipart && streams != null) { for (ContentStream content : streams) { String contentType = content.getContentType(); if (contentType == null) { contentType = BinaryResponseParser.BINARY_CONTENT_TYPE; // default } String name = content.getName(); if (name == null) { name = ""; } parts.add(new FormBodyPart(name, new InputStreamBody(content.getStream(), contentType, content.getName()))); } } if (parts.size() > 0) { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT); for (FormBodyPart p : parts) { entity.addPart(p); } post.setEntity(entity); } else { //not using multipart post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8")); } method = post; } // It is has one stream, it is the post body, put the params in the URL else { String pstr = ClientUtils.toQueryString(wparams, false); HttpPost post = new HttpPost(url + pstr); // Single stream as body // Using a loop just to get the first one final ContentStream[] contentStream = new ContentStream[1]; for (ContentStream content : streams) { contentStream[0] = content; break; } if (contentStream[0] instanceof RequestWriter.LazyContentStream) { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } else { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } method = post; } } else { throw new SolrServerException("Unsupported method: " + request.getMethod()); } } catch (NoHttpResponseException r) { method = null; if (is != null) { is.close(); } // If out of tries then just rethrow (as normal error). if (tries < 1) { throw r; } } } } catch (IOException ex) { throw new SolrServerException("error reading streams", ex); } // XXX client already has this set, is this needed? method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects); method.addHeader("User-Agent", AGENT); InputStream respBody = null; boolean shouldClose = true; boolean success = false; try { // Execute the method. final HttpResponse response = httpClient.execute(method); int httpStatus = response.getStatusLine().getStatusCode(); // Read the contents respBody = response.getEntity().getContent(); Header ctHeader = response.getLastHeader("content-type"); String contentType; if (ctHeader != null) { contentType = ctHeader.getValue(); } else { contentType = ""; } // handle some http level checks before trying to parse the response switch (httpStatus) { case HttpStatus.SC_OK: case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_CONFLICT: // 409 break; case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: if (!followRedirects) { throw new SolrServerException( "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ")."); } break; default: if (processor == null) { throw new RemoteSolrException(httpStatus, "Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:" + response.getStatusLine().getReasonPhrase(), null); } } if (processor == null) { // no processor specified, return raw stream NamedList<Object> rsp = new NamedList<Object>(); rsp.add("stream", respBody); // Only case where stream should not be closed shouldClose = false; success = true; return rsp; } String procCt = processor.getContentType(); if (procCt != null) { String procMimeType = ContentType.parse(procCt).getMimeType().trim().toLowerCase(Locale.ROOT); String mimeType = ContentType.parse(contentType).getMimeType().trim().toLowerCase(Locale.ROOT); if (!procMimeType.equals(mimeType)) { // unexpected mime type String msg = "Expected mime type " + procMimeType + " but got " + mimeType + "."; Header encodingHeader = response.getEntity().getContentEncoding(); String encoding; if (encodingHeader != null) { encoding = encodingHeader.getValue(); } else { encoding = "UTF-8"; // try UTF-8 } try { msg = msg + " " + IOUtils.toString(respBody, encoding); } catch (IOException e) { throw new RemoteSolrException(httpStatus, "Could not parse response with encoding " + encoding, e); } RemoteSolrException e = new RemoteSolrException(httpStatus, msg, null); throw e; } } // if(true) { // ByteArrayOutputStream copy = new ByteArrayOutputStream(); // IOUtils.copy(respBody, copy); // String val = new String(copy.toByteArray()); // System.out.println(">RESPONSE>"+val+"<"+val.length()); // respBody = new ByteArrayInputStream(copy.toByteArray()); // } NamedList<Object> rsp = null; String charset = EntityUtils.getContentCharSet(response.getEntity()); try { rsp = processor.processResponse(respBody, charset); } catch (Exception e) { throw new RemoteSolrException(httpStatus, e.getMessage(), e); } if (httpStatus != HttpStatus.SC_OK) { String reason = null; try { NamedList err = (NamedList) rsp.get("error"); if (err != null) { reason = (String) err.get("msg"); // TODO? get the trace? } } catch (Exception ex) { } if (reason == null) { StringBuilder msg = new StringBuilder(); msg.append(response.getStatusLine().getReasonPhrase()); msg.append("\n\n"); msg.append("request: " + method.getURI()); reason = java.net.URLDecoder.decode(msg.toString(), UTF_8); } throw new RemoteSolrException(httpStatus, reason, null); } success = true; return rsp; } catch (ConnectException e) { throw new SolrServerException("Server refused connection at: " + getBaseURL(), e); } catch (SocketTimeoutException e) { throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(), e); } catch (IOException e) { throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e); } finally { if (respBody != null && shouldClose) { try { respBody.close(); } catch (Throwable t) { } // ignore if (!success) { method.abort(); } } } }
From source file:org.artifactory.repo.HttpRepo.java
/** * Adds default headers and extra headers to the HttpRequest method * The extra headers are unique headers that should be added to the remote server request according to special * requirement example : user adds extra headers through the User Plugin (BeforeRemoteDownloadAction) * @param method Method to execute/*from w w w. j a va 2 s . c o m*/ * @param extraHeaders Extra headers to add to the remote server request */ @SuppressWarnings({ "deprecation" }) private void addDefaultHeadersAndQueryParams(HttpRequestBase method, Map<String, String> extraHeaders) { //Explicitly force keep alive method.setHeader(HttpHeaders.CONNECTION, "Keep-Alive"); //Add the current requester host id Set<String> originatedHeaders = RepoRequests.getOriginatedHeaders(); for (String originatedHeader : originatedHeaders) { method.addHeader(ARTIFACTORY_ORIGINATED, originatedHeader); } String hostId = ContextHelper.get().beanForType(AddonsManager.class).addonByType(HaCommonAddon.class) .getHostId(); // Add the extra headers to the remote request if (extraHeaders != null) { for (Map.Entry<String, String> entry : extraHeaders.entrySet()) { boolean isReservedKey = ARTIFACTORY_ORIGINATED.equals(entry.getKey()) || ORIGIN_ARTIFACTORY.equals(entry.getKey()) || ACCEPT_ENCODING.equals(entry.getKey()); if (!isReservedKey) { method.addHeader(entry.getKey(), entry.getValue()); } } } // Add the default artifactory headers, those headers will always override the existing headers if they already exist method.addHeader(ARTIFACTORY_ORIGINATED, hostId); //For backwards compatibility method.setHeader(ORIGIN_ARTIFACTORY, hostId); //Set gzip encoding if (handleGzipResponse) { method.addHeader(ACCEPT_ENCODING, "gzip"); } // Set custom query params String queryParams = getDescriptor().getQueryParams(); if (StringUtils.isNotBlank(queryParams)) { String url = method.getURI().toString(); if (url.contains("?")) { url += "&"; } else { url += "?"; } url += HttpUtils.encodeQuery(queryParams); method.setURI(URI.create(url)); } }
From source file:org.dasein.cloud.aws.storage.S3Method.java
S3Response invoke(@Nullable String bucket, @Nullable String object, @Nullable String temporaryEndpoint) throws S3Exception, CloudException, InternalException { if (wire.isDebugEnabled()) { wire.debug(""); wire.debug("----------------------------------------------------------------------------------"); }//from www. ja v a2 s .co m HttpClient client = null; boolean leaveOpen = false; try { StringBuilder url = new StringBuilder(); HttpRequestBase method; int status; // Sanitise the parameters as they may have spaces and who knows what else if (bucket != null) { bucket = AWSCloud.encode(bucket, false); } if (object != null && !"?location".equalsIgnoreCase(object) && !"?acl".equalsIgnoreCase(object) && !"?tagging".equalsIgnoreCase(object)) { object = AWSCloud.encode(object, false); } if (temporaryEndpoint != null) { temporaryEndpoint = AWSCloud.encode(temporaryEndpoint, false); } if (provider.getEC2Provider().isAWS()) { url.append("https://"); String regionId = provider.getContext().getRegionId(); if (temporaryEndpoint == null) { boolean validDomainName = isValidDomainName(bucket); if (bucket != null && validDomainName) { url.append(bucket); if (regionId != null && !regionId.isEmpty() && !"us-east-1".equals(regionId)) { url.append(".s3-"); url.append(regionId); url.append(".amazonaws.com/"); } else { url.append(".s3.amazonaws.com/"); } } else { if (regionId != null && !regionId.isEmpty() && !"us-east-1".equals(regionId)) { url.append("s3-"); url.append(regionId); url.append(".amazonaws.com/"); } else { url.append("s3.amazonaws.com/"); } } if (bucket != null && !validDomainName) { url.append(bucket); url.append("/"); } } else { url.append(temporaryEndpoint); url.append("/"); } } else if (provider.getEC2Provider().isStorage() && "google".equalsIgnoreCase(provider.getProviderName())) { url.append("https://"); if (temporaryEndpoint == null) { if (bucket != null) { url.append(bucket); url.append("."); } url.append("commondatastorage.googleapis.com/"); } else { url.append(temporaryEndpoint); url.append("/"); } } else { int idx = 0; if (!provider.getContext().getEndpoint().startsWith("http")) { url.append("https://"); } else { idx = provider.getContext().getEndpoint().indexOf("https://"); if (idx == -1) { idx = "http://".length(); url.append("http://"); } else { idx = "https://".length(); url.append("https://"); } } String service = ""; if (provider.getEC2Provider().isEucalyptus()) { service = "Walrus/"; } if (temporaryEndpoint == null) { url.append(provider.getContext().getEndpoint().substring(idx)); if (!provider.getContext().getEndpoint().endsWith("/")) { url.append("/").append(service); } else { url.append(service); } } else { url.append(temporaryEndpoint); url.append("/"); url.append(service); } if (bucket != null) { url.append(bucket); url.append("/"); } } if (object != null) { url.append(object); } else if (parameters != null) { boolean first = true; if (object != null && object.indexOf('?') != -1) { first = false; } for (Map.Entry<String, String> entry : parameters.entrySet()) { String key = entry.getKey(); String val = entry.getValue(); if (first) { url.append("?"); first = false; } else { url.append("&"); } if (val != null) { url.append(AWSCloud.encode(key, false)); url.append("="); url.append(AWSCloud.encode(val, false)); } else { url.append(AWSCloud.encode(key, false)); } } } if (provider.getEC2Provider().isStorage() && provider.getProviderName().equalsIgnoreCase("Google")) { headers.put(AWSCloud.P_GOOG_DATE, getDate()); } else { headers.put(AWSCloud.P_AWS_DATE, provider.getV4HeaderDate(null)); } if (contentType == null && body != null) { contentType = "application/xml"; headers.put("Content-Type", contentType); } else if (contentType != null) { headers.put("Content-Type", contentType); } method = action.getMethod(url.toString()); String host = method.getURI().getHost(); headers.put("host", host); if (action.equals(S3Action.PUT_BUCKET_TAG)) try { headers.put("Content-MD5", toBase64(computeMD5Hash(body))); } catch (NoSuchAlgorithmException e) { logger.error(e); } catch (IOException e) { logger.error(e); } if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { method.addHeader(entry.getKey(), entry.getValue()); } } if (body != null) { ((HttpEntityEnclosingRequestBase) method).setEntity(new StringEntity(body, APPLICATION_XML)); } else if (uploadFile != null) { ((HttpEntityEnclosingRequestBase) method).setEntity(new FileEntity(uploadFile, contentType)); } try { String hash = null; if (method instanceof HttpEntityEnclosingRequestBase) { try { hash = provider.getRequestBodyHash( EntityUtils.toString(((HttpEntityEnclosingRequestBase) method).getEntity())); } catch (IOException e) { throw new InternalException(e); } } else { hash = provider.getRequestBodyHash(""); } String signature; if (provider.getEC2Provider().isAWS()) { // Sign v4 for AWS signature = provider.getV4Authorization(new String(provider.getAccessKey()[0]), new String(provider.getAccessKey()[1]), method.getMethod(), url.toString(), SERVICE_ID, headers, hash); if (hash != null) { method.addHeader(AWSCloud.P_AWS_CONTENT_SHA256, hash); } } else { // Eucalyptus et al use v2 signature = provider.signS3(new String(provider.getAccessKey()[0], "utf-8"), provider.getAccessKey()[1], method.getMethod(), null, contentType, headers, bucket, object); } method.addHeader(AWSCloud.P_CFAUTH, signature); } catch (UnsupportedEncodingException e) { logger.error(e); } if (wire.isDebugEnabled()) { wire.debug("[" + url.toString() + "]"); wire.debug(method.getRequestLine().toString()); for (Header header : method.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); if (body != null) { try { wire.debug(EntityUtils.toString(((HttpEntityEnclosingRequestBase) method).getEntity())); } catch (IOException ignore) { } wire.debug(""); } else if (uploadFile != null) { wire.debug("-- file upload --"); wire.debug(""); } } attempts++; client = provider.getClient(body == null && uploadFile == null); S3Response response = new S3Response(); HttpResponse httpResponse; try { APITrace.trace(provider, action.toString()); httpResponse = client.execute(method); if (wire.isDebugEnabled()) { wire.debug(httpResponse.getStatusLine().toString()); for (Header header : httpResponse.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); } status = httpResponse.getStatusLine().getStatusCode(); } catch (IOException e) { logger.error(url + ": " + e.getMessage()); throw new InternalException(e); } response.headers = httpResponse.getAllHeaders(); HttpEntity entity = httpResponse.getEntity(); InputStream input = null; if (entity != null) { try { input = entity.getContent(); } catch (IOException e) { throw new CloudException(e); } } try { if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_ACCEPTED) { Header clen = httpResponse.getFirstHeader("Content-Length"); long len = -1L; if (clen != null) { len = Long.parseLong(clen.getValue()); } if (len != 0L) { try { Header ct = httpResponse.getFirstHeader("Content-Type"); if ((ct != null && (ct.getValue().startsWith("application/xml") || ct.getValue().startsWith("text/xml"))) || (action.equals(S3Action.GET_BUCKET_TAG) && input != null)) { try { response.document = parseResponse(input); return response; } finally { input.close(); } } else if (ct != null && ct.getValue().startsWith("application/octet-stream") && len < 1) { return null; } else { response.contentLength = len; if (ct != null) { response.contentType = ct.getValue(); } response.input = input; response.method = method; leaveOpen = true; return response; } } catch (IOException e) { logger.error(e); throw new CloudException(e); } } else { return response; } } else if (status == HttpStatus.SC_NO_CONTENT) { return response; } if (status == HttpStatus.SC_FORBIDDEN) { throw new S3Exception(status, "", "AccessForbidden", "Access was denied : " + (url != null ? url.toString() : "")); } else if (status == HttpStatus.SC_NOT_FOUND) { throw new S3Exception(status, null, null, "Object not found."); } else { if (status == HttpStatus.SC_SERVICE_UNAVAILABLE || status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { if (attempts >= 5) { String msg; if (status == HttpStatus.SC_SERVICE_UNAVAILABLE) { msg = "Cloud service is currently unavailable."; } else { msg = "The cloud service encountered a server error while processing your request."; } logger.error(msg); throw new CloudException(msg); } else { leaveOpen = true; if (input != null) { try { input.close(); } catch (IOException ignore) { } } try { Thread.sleep(5000L); } catch (InterruptedException ignore) { } return invoke(bucket, object); } } try { Document doc; try { logger.warn("Received error code: " + status); doc = parseResponse(input); } finally { if (input != null) { input.close(); } } if (doc != null) { String endpoint = null, code = null, message = null, requestId = null; NodeList blocks = doc.getElementsByTagName("Error"); if (blocks.getLength() > 0) { Node error = blocks.item(0); NodeList attrs; attrs = error.getChildNodes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); if (attr.getNodeName().equals("Code") && attr.hasChildNodes()) { code = attr.getFirstChild().getNodeValue().trim(); } else if (attr.getNodeName().equals("Message") && attr.hasChildNodes()) { message = attr.getFirstChild().getNodeValue().trim(); } else if (attr.getNodeName().equals("RequestId") && attr.hasChildNodes()) { requestId = attr.getFirstChild().getNodeValue().trim(); } else if (attr.getNodeName().equals("Endpoint") && attr.hasChildNodes()) { endpoint = attr.getFirstChild().getNodeValue().trim(); } } } if (endpoint != null && code.equals("TemporaryRedirect")) { if (temporaryEndpoint != null) { throw new CloudException("Too deep redirect to " + endpoint); } else { return invoke(bucket, object, endpoint); } } else { if (message == null) { throw new CloudException("Unable to identify error condition: " + status + "/" + requestId + "/" + code); } throw new S3Exception(status, requestId, code, message); } } else { throw new CloudException("Unable to parse error."); } } catch (IOException e) { if (status == HttpStatus.SC_FORBIDDEN) { throw new S3Exception(status, "", "AccessForbidden", "Access was denied without explanation."); } throw new CloudException(e); } catch (RuntimeException e) { throw new CloudException(e); } catch (Error e) { throw new CloudException(e); } } } finally { if (!leaveOpen) { if (input != null) { try { input.close(); } catch (IOException ignore) { } } } } } finally { if (!leaveOpen && client != null) { client.getConnectionManager().shutdown(); } if (wire.isDebugEnabled()) { wire.debug("----------------------------------------------------------------------------------"); wire.debug(""); } } }
From source file:org.opencastproject.kernel.security.TrustedHttpClientImpl.java
/** * Perform a request, and extract the realm and nonce values * // ww w. j a v a 2 s .co m * @param request * The request to execute in order to obtain the realm and nonce * @return A String[] containing the {realm, nonce} */ protected String[] getRealmAndNonce(HttpRequestBase request) throws TrustedHttpClientException { HttpClient httpClient = makeHttpClient(); HttpResponse response; try { response = httpClient.execute(request); } catch (IOException e) { httpClient.getConnectionManager().shutdown(); throw new TrustedHttpClientException(e); } Header[] headers = response.getHeaders("WWW-Authenticate"); if (headers == null || headers.length == 0) { logger.warn("URI {} does not support digest authentication", request.getURI()); httpClient.getConnectionManager().shutdown(); return null; } Header authRequiredResponseHeader = headers[0]; String nonce = null; String realm = null; for (HeaderElement element : authRequiredResponseHeader.getElements()) { if ("nonce".equals(element.getName())) { nonce = element.getValue(); } else if ("Digest realm".equals(element.getName())) { realm = element.getValue(); } } httpClient.getConnectionManager().shutdown(); return new String[] { realm, nonce }; }
From source file:org.opencastproject.serviceregistry.api.RemoteBase.java
/** * Makes a request to all available remote services and returns the response as soon as the first of them returns the * expected http status code.// w w w .ja v a 2 s . c om * * @param httpRequest * the http request. If the URI is specified, it should include only the path beyond the service endpoint. * For example, a request intended for http://{host}/{service}/extra/path/info.xml should include the URI * "/extra/path/info.xml". * @param expectedHttpStatus * any expected status codes to include in the return. * @return the response object, or null if we can not connect to any services */ protected HttpResponse getResponse(HttpRequestBase httpRequest, Integer... expectedHttpStatus) { boolean warnedUnavailability = false; // Try forever while (true) { List<ServiceRegistration> remoteServices = null; List<String> servicesInWarningState = new ArrayList<String>(); // Find available services while (remoteServices == null || remoteServices.size() == 0) { boolean warned = false; try { remoteServices = remoteServiceManager.getServiceRegistrationsByLoad(serviceType); if (remoteServices == null || remoteServices.size() == 0) { if (!warned) { logger.warn("No services of type '{}' found, waiting...", serviceType); warned = true; } logger.debug("Still no services of type '{}' found, waiting...", serviceType); try { Thread.sleep(TIMEOUT); } catch (InterruptedException e) { logger.warn("Interrupted while waiting for remote service of type '{}'", serviceType); return null; } } } catch (ServiceRegistryException e) { logger.warn("Unable to obtain a list of remote services", e); return null; } } URI originalUri = httpRequest.getURI(); String uriSuffix = null; if (originalUri != null && StringUtils.isNotBlank(originalUri.toString())) { uriSuffix = originalUri.toString(); } // Try each available service for (ServiceRegistration remoteService : remoteServices) { HttpResponse response = null; try { String fullUrl = null; if (uriSuffix == null) { fullUrl = UrlSupport.concat(remoteService.getHost(), remoteService.getPath()); } else { fullUrl = UrlSupport.concat( new String[] { remoteService.getHost(), remoteService.getPath(), uriSuffix }); } logger.debug("Connecting to remote service of type '{}' at {}", serviceType, fullUrl); URI uri = new URI(fullUrl); httpRequest.setURI(uri); response = client.execute(httpRequest); StatusLine status = response.getStatusLine(); if (Arrays.asList(expectedHttpStatus).contains(status.getStatusCode())) { if (servicesInWarningState.contains(fullUrl)) { logger.warn("Service at {} is back to normal with expected status code {}", fullUrl, status.getStatusCode()); } return response; } else { if (!knownHttpStatuses.contains(status.getStatusCode()) && !servicesInWarningState.contains(fullUrl)) { logger.warn("Service at {} returned unexpected response code {}", fullUrl, status.getStatusCode()); servicesInWarningState.add(fullUrl); } closeConnection(response); } } catch (Exception e) { closeConnection(response); } } // Reset Original URI httpRequest.setURI(originalUri); // If none of them accepted the request, let's wait and retry if (!warnedUnavailability) { logger.warn("No service of type '{}' is currently readily available", serviceType); warnedUnavailability = true; } else { logger.debug("All services of type '{}' are still unavailable", serviceType); } try { Thread.sleep(TIMEOUT); } catch (InterruptedException e) { logger.warn("Interrupted while waiting for remote service of type '{}'", serviceType); return null; } } }
From source file:org.openecomp.sdc.be.dao.rest.HttpRestClient.java
private RestResponse execute(HttpRequestBase httpRequestBase, Properties headers) { RestResponse restResponse = null;/*from ww w .ja v a2 s . c om*/ CloseableHttpResponse httpResponse = null; try { addHeadersToRequest(httpRequestBase, headers); httpResponse = this.httpClient.execute(httpRequestBase); restResponse = buildRestResponseFromResult(httpResponse); logger.debug("After executing uri {}. response = {}.", httpRequestBase.getURI().toString(), restResponse); } catch (Exception exception) { httpRequestBase.abort(); String description = "Failed executing http request " + httpRequestBase.getURI().toString() + "(" + httpRequestBase.getMethod() + ")"; BeEcompErrorManager.getInstance().logInternalFlowError("ExecuteRestRequest", description, ErrorSeverity.ERROR); restResponse = errorRestResponse; } finally { // ensure the connection gets released to the manager releaseResource(httpResponse); } return restResponse; }
From source file:org.openecomp.sdc.be.dao.rest.HttpRestClient.java
private RestResponseAsByteArray executeAndReturnByteArray(HttpRequestBase httpRequestBase, Properties headers) { RestResponseAsByteArray restResponse = null; CloseableHttpResponse httpResponse = null; try {/*from w w w .j av a2 s. co m*/ addHeadersToRequest(httpRequestBase, headers); httpResponse = this.httpClient.execute(httpRequestBase); restResponse = buildRestResponseByteArrayFromResult(httpResponse); if (restResponse != null) { logger.debug("After executing uri {}. Response: {}.", httpRequestBase.getURI().toString(), restResponse.toPrettyString()); } } catch (Exception exception) { httpRequestBase.abort(); String description = "Failed executing http request " + httpRequestBase.getURI().toString() + "(" + httpRequestBase.getMethod() + ")"; BeEcompErrorManager.getInstance().logInternalFlowError("ExecuteRestRequest", description, ErrorSeverity.ERROR); logger.debug(description, exception); restResponse = errorRestResponseAsByteArray; } finally { // ensure the connection gets released to the manager releaseResource(httpResponse); } return restResponse; }
From source file:org.openrepose.nodeservice.httpcomponent.RequestProxyServiceImpl.java
private ServiceClientResponse execute(HttpRequestBase base) { HttpClientResponse httpClientResponse = getClient(); try {/*w w w. j av a 2s . co m*/ HttpResponse httpResponse = httpClientResponse.getHttpClient().execute(base); HttpEntity entity = httpResponse.getEntity(); HttpComponentResponseCodeProcessor responseCode = new HttpComponentResponseCodeProcessor( httpResponse.getStatusLine().getStatusCode()); InputStream stream = null; if (entity != null) { stream = new ByteArrayInputStream(RawInputStreamReader.instance().readFully(entity.getContent())); EntityUtils.consume(entity); } return new ServiceClientResponse(responseCode.getCode(), stream); } catch (IOException ex) { LOG.error("Error executing request to {}", base.getURI().toString(), ex); } finally { base.releaseConnection(); httpClientService.releaseClient(httpClientResponse); } return new ServiceClientResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null); }
From source file:org.structr.rest.common.HttpHelper.java
private static void configure(final HttpRequestBase req, final String username, final String password, final String proxyUrlParameter, final String proxyUsernameParameter, final String proxyPasswordParameter, final String cookieParameter, final Map<String, String> headers, final boolean followRedirects) { if (StringUtils.isBlank(proxyUrlParameter)) { proxyUrl = Settings.HttpProxyUrl.getValue(); } else {//w w w . j a va2s . c o m proxyUrl = proxyUrlParameter; } if (StringUtils.isBlank(proxyUsernameParameter)) { proxyUsername = Settings.HttpProxyUser.getValue(); } else { proxyUsername = proxyUsernameParameter; } if (StringUtils.isBlank(proxyPasswordParameter)) { proxyPassword = Settings.HttpProxyPassword.getValue(); } else { proxyPassword = proxyPasswordParameter; } if (!StringUtils.isBlank(cookieParameter)) { cookie = cookieParameter; } //final HttpHost target = HttpHost.create(url.getHost()); HttpHost proxy = null; final CredentialsProvider credsProvider = new BasicCredentialsProvider(); if (StringUtils.isNoneBlank(username, password)) { credsProvider.setCredentials(new AuthScope(new HttpHost(req.getURI().getHost())), new UsernamePasswordCredentials(username, password)); } if (StringUtils.isNotBlank(proxyUrl)) { proxy = HttpHost.create(proxyUrl); if (StringUtils.isNoneBlank(proxyUsername, proxyPassword)) { credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(proxyUsername, proxyPassword)); } } client = HttpClients.custom().setDefaultConnectionConfig(ConnectionConfig.DEFAULT) .setUserAgent("curl/7.35.0").setDefaultCredentialsProvider(credsProvider).build(); reqConfig = RequestConfig.custom().setProxy(proxy).setRedirectsEnabled(followRedirects) .setCookieSpec(CookieSpecs.DEFAULT).build(); req.setConfig(reqConfig); if (StringUtils.isNotBlank(cookie)) { req.addHeader("Cookie", cookie); req.getParams().setParameter("http.protocol.single-cookie-header", true); } req.addHeader("Connection", "close"); // add request headers from context for (final Map.Entry<String, String> header : headers.entrySet()) { req.addHeader(header.getKey(), header.getValue()); } }
From source file:org.wso2.carbon.apimgt.hybrid.gateway.common.util.HttpRequestUtil.java
/** * Get URI from the http method//from w w w . ja va 2 s. co m * * @param httpMethod http method * @return URI string */ private static String getURI(HttpRequestBase httpMethod) { return httpMethod.getURI().toString(); }