List of usage examples for org.apache.http.client.methods HttpHead HttpHead
public HttpHead(final String uri)
From source file:org.graphity.core.util.jena.HttpOp.java
/** * Executes a HTTP HEAD operation//w ww. j a v a 2 s .c o m * * @param url * URL * @param acceptString * Accept Header * @param handler * Response Handler * @param httpClient * HTTP Client * @param httpContext * HTTP Context * @param authenticator * HTTP Authenticator */ public static void execHttpHead(String url, String acceptString, HttpResponseHandler handler, HttpClient httpClient, HttpContext httpContext, HttpAuthenticator authenticator) { String requestURI = determineRequestURI(url); HttpHead httpHead = new HttpHead(requestURI); exec(url, httpHead, acceptString, handler, httpClient, httpContext, authenticator); }
From source file:com.vuze.plugin.azVPN_Helper.CheckerCommon.java
protected boolean canReach(InetAddress addressToReach, URI uri) { InetAddress[] resolve = null; try {//from w w w. ja va 2 s. c om String domain = uri.getHost(); // If Vuze has a proxy set up (Tools->Options->Connection->Proxy), then // we'll need to disable it for the URL AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null) { resolve = SystemDefaultDnsResolver.INSTANCE.resolve(domain); for (InetAddress address : resolve) { selector.setProxy(new InetSocketAddress(address, 443), Proxy.NO_PROXY); } } HttpHead getHead = new HttpHead(uri); RequestConfig requestConfig = RequestConfig.custom().setLocalAddress(addressToReach) .setConnectTimeout(12000).build(); getHead.setConfig(requestConfig); CloseableHttpResponse response = HttpClients.createDefault().execute(getHead); response.close(); } catch (Throwable t) { t.printStackTrace(); return false; } finally { AEProxySelector selector = AEProxySelectorFactory.getSelector(); if (selector != null && resolve != null) { for (InetAddress address : resolve) { AEProxySelectorFactory.getSelector().removeProxy(new InetSocketAddress(address, 443)); } } } return true; }
From source file:com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient.java
private int headRequestStatus(String path) throws IOException, InterruptedException { HttpHead httpHead = new HttpHead(path); try (CloseableHttpResponse response = executeMethod(httpHead)) { EntityUtils.consume(response.getEntity()); return response.getStatusLine().getStatusCode(); } catch (IOException e) { throw new IOException("Communication error for url: " + path, e); } finally {//from w w w . java 2 s.com release(httpHead); } }
From source file:lucee.runtime.tag.Http.java
private void _doEndTag() throws PageException, IOException { long start = System.nanoTime(); HttpClientBuilder builder = HTTPEngine4Impl.getHttpClientBuilder(); ssl(builder);//from w ww . j a va 2 s .c o m // redirect if (redirect) builder.setRedirectStrategy(new DefaultRedirectStrategy()); else builder.disableRedirectHandling(); // cookies BasicCookieStore cookieStore = new BasicCookieStore(); builder.setDefaultCookieStore(cookieStore); ConfigWeb cw = pageContext.getConfig(); HttpRequestBase req = null; HttpContext httpContext = null; CacheHandler cacheHandler = null; String cacheId = null; // HttpRequestBase req = init(pageContext.getConfig(),this,client,params,url,port); { if (StringUtil.isEmpty(charset, true)) charset = ((PageContextImpl) pageContext).getWebCharset().name(); else charset = charset.trim(); // check if has fileUploads boolean doUploadFile = false; for (int i = 0; i < this.params.size(); i++) { if ((this.params.get(i)).getType() == HttpParamBean.TYPE_FILE) { doUploadFile = true; break; } } // parse url (also query string) int len = this.params.size(); StringBuilder sbQS = new StringBuilder(); for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); int type = param.getType(); // URL if (type == HttpParamBean.TYPE_URL) { if (sbQS.length() > 0) sbQS.append('&'); sbQS.append(param.getEncoded() ? urlenc(param.getName(), charset) : param.getName()); sbQS.append('='); sbQS.append(param.getEncoded() ? urlenc(param.getValueAsString(), charset) : param.getValueAsString()); } } String host = null; HttpHost httpHost; try { URL _url = HTTPUtil.toURL(url, port, encoded); httpHost = new HttpHost(_url.getHost(), _url.getPort()); host = _url.getHost(); url = _url.toExternalForm(); if (sbQS.length() > 0) { // no existing QS if (StringUtil.isEmpty(_url.getQuery())) { url += "?" + sbQS; } else { url += "&" + sbQS; } } } catch (MalformedURLException mue) { throw Caster.toPageException(mue); } // cache if (cachedWithin != null) { cacheId = createCacheId(); cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_HTTP, null) .getInstanceMatchingObject(cachedWithin, null); if (cacheHandler instanceof CacheHandlerPro) { CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin); if (cacheItem instanceof HTTPCacheItem) { pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData()); return; } } else if (cacheHandler != null) { // TODO this else block can be removed when all cache handlers implement CacheHandlerPro CacheItem cacheItem = cacheHandler.get(pageContext, cacheId); if (cacheItem instanceof HTTPCacheItem) { pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData()); return; } } } // cache not found, process and cache result if needed // select best matching method (get,post, post multpart (file)) boolean isBinary = false; boolean doMultiPart = doUploadFile || this.multiPart; HttpEntityEnclosingRequest eeReqPost = null; HttpEntityEnclosingRequest eeReq = null; if (this.method == METHOD_GET) { req = new HttpGetWithBody(url); eeReq = (HttpEntityEnclosingRequest) req; } else if (this.method == METHOD_HEAD) { req = new HttpHead(url); } else if (this.method == METHOD_DELETE) { isBinary = true; req = new HttpDeleteWithBody(url); eeReq = (HttpEntityEnclosingRequest) req; } else if (this.method == METHOD_PUT) { isBinary = true; HttpPut put = new HttpPut(url); eeReqPost = put; req = put; eeReq = put; } else if (this.method == METHOD_TRACE) { isBinary = true; req = new HttpTrace(url); } else if (this.method == METHOD_OPTIONS) { isBinary = true; req = new HttpOptions(url); } else if (this.method == METHOD_PATCH) { isBinary = true; eeReq = HTTPPatchFactory.getHTTPPatch(url); req = (HttpRequestBase) eeReq; } else { isBinary = true; eeReqPost = new HttpPost(url); req = (HttpPost) eeReqPost; eeReq = eeReqPost; } boolean hasForm = false; boolean hasBody = false; boolean hasContentType = false; // Set http params ArrayList<FormBodyPart> parts = new ArrayList<FormBodyPart>(); StringBuilder acceptEncoding = new StringBuilder(); java.util.List<NameValuePair> postParam = eeReqPost != null ? new ArrayList<NameValuePair>() : null; for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); int type = param.getType(); // URL if (type == HttpParamBean.TYPE_URL) { // listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(), // http.charset))); } // Form else if (type == HttpParamBean.TYPE_FORM) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam with type formfield can only be used when the method attribute of the parent http tag is set to post"); if (eeReqPost != null) { if (doMultiPart) { parts.add(new FormBodyPart(param.getName(), new StringBody(param.getValueAsString(), CharsetUtil.toCharset(charset)))); } else { postParam.add(new BasicNameValuePair(param.getName(), param.getValueAsString())); } } // else if(multi!=null)multi.addParameter(param.getName(),param.getValueAsString()); } // CGI else if (type == HttpParamBean.TYPE_CGI) { if (param.getEncoded()) req.addHeader(urlenc(param.getName(), charset), urlenc(param.getValueAsString(), charset)); else req.addHeader(param.getName(), param.getValueAsString()); } // Header else if (type == HttpParamBean.TYPE_HEADER) { if (param.getName().equalsIgnoreCase("content-type")) hasContentType = true; if (param.getName().equalsIgnoreCase("Content-Length")) { } else if (param.getName().equalsIgnoreCase("Accept-Encoding")) { acceptEncoding.append(headerValue(param.getValueAsString())); acceptEncoding.append(", "); } else req.addHeader(param.getName(), headerValue(param.getValueAsString())); } // Cookie else if (type == HttpParamBean.TYPE_COOKIE) { HTTPEngine4Impl.addCookie(cookieStore, host, param.getName(), param.getValueAsString(), "/", charset); } // File else if (type == HttpParamBean.TYPE_FILE) { hasForm = true; if (this.method == METHOD_GET) throw new ApplicationException( "httpparam type file can't only be used, when method of the tag http equal post"); // if(param.getFile()==null) throw new ApplicationException("httpparam type file can't only be used, when method of the tag http equal // post"); String strCT = getContentType(param); ContentType ct = HTTPUtil.toContentType(strCT, null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); if (doMultiPart) { try { Resource res = param.getFile(); parts.add(new FormBodyPart(param.getName(), new ResourceBody(res, mt, res.getName(), cs))); // parts.add(new ResourcePart(param.getName(),new ResourcePartSource(param.getFile()),getContentType(param),_charset)); } catch (FileNotFoundException e) { throw new ApplicationException("can't upload file, path is invalid", e.getMessage()); } } } // XML else if (type == HttpParamBean.TYPE_XML) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = "text/xml"; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; hasContentType = true; req.addHeader("Content-type", mt + "; charset=" + cs); if (eeReq == null) throw new ApplicationException( "type xml is only supported for methods get, delete, post, and put"); HTTPEngine4Impl.setBody(eeReq, param.getValueAsString(), mt, cs); } // Body else if (type == HttpParamBean.TYPE_BODY) { ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null); String mt = null; if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true)) mt = ct.getMimeType(); String cs = charset; if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true)) cs = ct.getCharset(); hasBody = true; if (eeReq == null) throw new ApplicationException( "type body is only supported for methods get, delete, post, and put"); HTTPEngine4Impl.setBody(eeReq, param.getValue(), mt, cs); } else { throw new ApplicationException("invalid type [" + type + "]"); } } // post params if (postParam != null && postParam.size() > 0) eeReqPost.setEntity(new org.apache.http.client.entity.UrlEncodedFormEntity(postParam, charset)); if (compression) { acceptEncoding.append("gzip"); } else { acceptEncoding.append("deflate;q=0"); req.setHeader("TE", "deflate;q=0"); } req.setHeader("Accept-Encoding", acceptEncoding.toString()); // multipart if (doMultiPart && eeReq != null) { hasContentType = true; boolean doIt = true; if (!this.multiPart && parts.size() == 1) { ContentBody body = parts.get(0).getBody(); if (body instanceof StringBody) { StringBody sb = (StringBody) body; try { org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType .create(sb.getMimeType(), sb.getCharset()); String str = IOUtil.toString(sb.getReader()); StringEntity entity = new StringEntity(str, ct); eeReq.setEntity(entity); } catch (IOException e) { throw Caster.toPageException(e); } doIt = false; } } if (doIt) { MultipartEntityBuilder mpeBuilder = MultipartEntityBuilder.create().setStrictMode(); // enabling the line below will append charset=... to the Content-Type header // if (!StringUtil.isEmpty(charset, true)) // mpeBuilder.setCharset(CharsetUtil.toCharset(charset)); Iterator<FormBodyPart> it = parts.iterator(); while (it.hasNext()) { FormBodyPart part = it.next(); mpeBuilder.addPart(part); } eeReq.setEntity(mpeBuilder.build()); } // eem.setRequestEntity(new MultipartRequestEntityFlex(parts.toArray(new Part[parts.size()]), eem.getParams(),http.multiPartType)); } if (hasBody && hasForm) throw new ApplicationException("mixing httpparam type file/formfield and body/XML is not allowed"); if (!hasContentType) { if (isBinary) { if (hasBody) req.addHeader("Content-type", "application/octet-stream"); else req.addHeader("Content-type", "application/x-www-form-urlencoded; charset=" + charset); } else { if (hasBody) req.addHeader("Content-type", "text/html; charset=" + charset); } } // set User Agent if (!hasHeaderIgnoreCase(req, "User-Agent")) req.setHeader("User-Agent", this.useragent); // set timeout setTimeout(builder, checkRemainingTimeout()); // set Username and Password if (this.username != null) { if (this.password == null) this.password = ""; if (AUTH_TYPE_NTLM == this.authType) { if (StringUtil.isEmpty(this.workStation, true)) throw new ApplicationException( "attribute workstation is required when authentication type is [NTLM]"); if (StringUtil.isEmpty(this.domain, true)) throw new ApplicationException( "attribute domain is required when authentication type is [NTLM]"); HTTPEngine4Impl.setNTCredentials(builder, this.username, this.password, this.workStation, this.domain); } else httpContext = HTTPEngine4Impl.setCredentials(builder, httpHost, this.username, this.password, preauth); } // set Proxy ProxyData proxy = null; if (!StringUtil.isEmpty(this.proxyserver)) { proxy = ProxyDataImpl.getInstance(this.proxyserver, this.proxyport, this.proxyuser, this.proxypassword); } if (pageContext.getConfig().isProxyEnableFor(host)) { proxy = pageContext.getConfig().getProxyData(); } HTTPEngine4Impl.setProxy(builder, req, proxy); } CloseableHttpClient client = null; try { if (httpContext == null) httpContext = new BasicHttpContext(); Struct cfhttp = new StructImpl(); cfhttp.setEL(ERROR_DETAIL, ""); pageContext.setVariable(result, cfhttp); /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// client = builder.build(); Executor4 e = new Executor4(pageContext, this, client, httpContext, req, redirect); HTTPResponse4Impl rsp = null; if (timeout == null || timeout.getMillis() <= 0) { try { rsp = e.execute(httpContext); } catch (Throwable t) { ExceptionUtil.rethrowIfNecessary(t); if (!throwonerror) { if (t instanceof SocketTimeoutException) setRequestTimeout(cfhttp); else setUnknownHost(cfhttp, t); return; } throw toPageException(t, rsp); } } else { e.start(); try { synchronized (this) {// print.err(timeout); this.wait(timeout.getMillis()); } } catch (InterruptedException ie) { throw Caster.toPageException(ie); } if (e.t != null) { if (!throwonerror) { setUnknownHost(cfhttp, e.t); return; } throw toPageException(e.t, rsp); } rsp = e.response; if (!e.done) { req.abort(); if (throwonerror) throw new HTTPException("408 Request Time-out", "a timeout occurred in tag http", 408, "Time-out", rsp == null ? null : rsp.getURL()); setRequestTimeout(cfhttp); return; // throw new ApplicationException("timeout"); } } /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset()); int statCode = 0; // Write Response Scope // String rawHeader=httpMethod.getStatusLine().toString(); String mimetype = null; String contentEncoding = null; // status code cfhttp.set(STATUSCODE, ((rsp.getStatusCode() + " " + rsp.getStatusText()).trim())); cfhttp.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode())); cfhttp.set(STATUS_TEXT, (rsp.getStatusText())); cfhttp.set(HTTP_VERSION, (rsp.getProtocolVersion())); // responseHeader lucee.commons.net.http.Header[] headers = rsp.getAllHeaders(); StringBuffer raw = new StringBuffer(rsp.getStatusLine() + " "); Struct responseHeader = new StructImpl(); Struct cookie; Array setCookie = new ArrayImpl(); Query cookies = new QueryImpl( new String[] { "name", "value", "path", "domain", "expires", "secure", "httpOnly" }, 0, "cookies"); for (int i = 0; i < headers.length; i++) { lucee.commons.net.http.Header header = headers[i]; // print.ln(header); raw.append(header.toString() + " "); if (header.getName().equalsIgnoreCase("Set-Cookie")) { setCookie.append(header.getValue()); parseCookie(cookies, header.getValue()); } else { // print.ln(header.getName()+"-"+header.getValue()); Object value = responseHeader.get(KeyImpl.getInstance(header.getName()), null); if (value == null) responseHeader.set(KeyImpl.getInstance(header.getName()), header.getValue()); else { Array arr = null; if (value instanceof Array) { arr = (Array) value; } else { arr = new ArrayImpl(); responseHeader.set(KeyImpl.getInstance(header.getName()), arr); arr.appendEL(value); } arr.appendEL(header.getValue()); } } // Content-Type if (header.getName().equalsIgnoreCase("Content-Type")) { mimetype = header.getValue(); if (mimetype == null) mimetype = NO_MIMETYPE; } // Content-Encoding if (header.getName().equalsIgnoreCase("Content-Encoding")) { contentEncoding = header.getValue(); } } cfhttp.set(RESPONSEHEADER, responseHeader); cfhttp.set(KeyConstants._cookies, cookies); responseHeader.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode())); responseHeader.set(EXPLANATION, (rsp.getStatusText())); if (setCookie.size() > 0) responseHeader.set(SET_COOKIE, setCookie); // is text boolean isText = mimetype == null || mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype); // is multipart boolean isMultipart = MultiPartResponseUtils.isMultipart(mimetype); cfhttp.set(KeyConstants._text, Caster.toBoolean(isText)); // mimetype charset // boolean responseProvideCharset=false; if (!StringUtil.isEmpty(mimetype, true)) { if (isText) { String[] types = HTTPUtil.splitMimeTypeAndCharset(mimetype, null); if (types[0] != null) cfhttp.set(KeyConstants._mimetype, types[0]); if (types[1] != null) cfhttp.set(CHARSET, types[1]); } else cfhttp.set(KeyConstants._mimetype, mimetype); } else cfhttp.set(KeyConstants._mimetype, NO_MIMETYPE); // File Resource file = null; if (strFile != null && strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile); } else if (strFile != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strFile); } else if (strPath != null) { file = ResourceUtil.toResourceNotExisting(pageContext, strPath); // Resource dir = file.getParentResource(); if (file.isDirectory()) { file = file.getRealResource(req.getURI().getPath());// TODO was getName() // ->http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html#getName() } } if (file != null) pageContext.getConfig().getSecurityManager().checkFileLocation(file); // filecontent InputStream is = null; if (isText && getAsBinary != GET_AS_BINARY_YES) { String str; try { // read content if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); if (is != null && isGzipEncoded(contentEncoding)) is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { str = is == null ? "" : IOUtil.toString(is, responseCharset, checkRemainingTimeout().getMillis()); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) { str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset, checkRemainingTimeout().getMillis()); } else throw eof; } } catch (UnsupportedEncodingException uee) { str = IOUtil.toString(is, (Charset) null, checkRemainingTimeout().getMillis()); } } catch (IOException ioe) { throw Caster.toPageException(ioe); } finally { IOUtil.closeEL(is); } if (str == null) str = ""; if (resolveurl) { // if(e.redirectURL!=null)url=e.redirectURL.toExternalForm(); str = new URLResolver().transform(str, e.response.getTargetURL(), false); } cfhttp.set(KeyConstants._filecontent, str); try { if (file != null) { IOUtil.write(file, str, ((PageContextImpl) pageContext).getWebCharset(), false); } } catch (IOException e1) { } if (name != null) { Query qry = CSVParser.toQuery(str, delimiter, textqualifier, columns, firstrowasheaders); pageContext.setVariable(name, qry); } } // Binary else { byte[] barr = null; if (isGzipEncoded(contentEncoding)) { if (method != METHOD_HEAD) { is = rsp.getContentAsStream(); is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { barr = is == null ? new byte[0] : IOUtil.toBytes(is); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) barr = IOUtil.toBytes(((CachingGZIPInputStream) is).getRawData()); else throw eof; } } catch (IOException t) { throw Caster.toPageException(t); } finally { IOUtil.closeEL(is); } } else { try { if (method != METHOD_HEAD) barr = rsp.getContentAsByteArray(); else barr = new byte[0]; } catch (IOException t) { throw Caster.toPageException(t); } } // IF Multipart response get file content and parse parts if (barr != null) { if (isMultipart) { cfhttp.set(KeyConstants._filecontent, MultiPartResponseUtils.getParts(barr, mimetype)); } else { cfhttp.set(KeyConstants._filecontent, barr); } } else cfhttp.set(KeyConstants._filecontent, ""); if (file != null) { try { if (barr != null) IOUtil.copy(new ByteArrayInputStream(barr), file, true); } catch (IOException ioe) { throw Caster.toPageException(ioe); } } } // header cfhttp.set(KeyConstants._header, raw.toString()); if (!isStatusOK(rsp.getStatusCode())) { String msg = rsp.getStatusCode() + " " + rsp.getStatusText(); cfhttp.setEL(ERROR_DETAIL, msg); if (throwonerror) { throw new HTTPException(msg, null, rsp.getStatusCode(), rsp.getStatusText(), rsp.getURL()); } } // TODO: check if we can use statCode instead of rsp.getStatusCode() everywhere and cleanup the code if (cacheHandler != null && rsp.getStatusCode() == 200) { // add to cache cacheHandler.set(pageContext, cacheId, cachedWithin, new HTTPCacheItem(cfhttp, url, System.nanoTime() - start)); } } finally { if (client != null) client.close(); } }
From source file:ch.iterate.openstack.swift.Client.java
/** * Gets current CDN sharing status of the container * * @param container Container// w w w . ja v a2s. c o m * @return Information on the container * @throws GenericException Unexpected response * @throws ch.iterate.openstack.swift.exception.NotFoundException * The Container has never been CDN enabled */ public CDNContainer getCDNContainerInfo(Region region, String container) throws IOException { HttpHead method = new HttpHead(region.getCDNManagementUrl(container)); return this.execute(method, new CdnContainerInfoHandler(region, container)); }
From source file:cn.com.loopj.android.http.AsyncHttpClient.java
/** * Perform a HTTP HEAD request and track the Android Context which initiated the request. * * @param context the Android Context which initiated the request. * @param url the URL to send the request to. * @param params additional HEAD parameters to send with the request. * @param responseHandler the response handler instance that should handle the response. * @return RequestHandle of future request process *//*w w w.j ava 2 s . co m*/ public RequestHandle head(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) { return sendRequest(httpClient, httpContext, new HttpHead(getUrlWithQueryString(isUrlEncodingEnabled, url, params)), null, responseHandler, context); }
From source file:cn.com.loopj.android.http.AsyncHttpClient.java
/** * Perform a HTTP HEAD request and track the Android Context which initiated the request with * customized headers//w ww . jav a 2 s. c o m * * @param context Context to execute request against * @param url the URL to send the request to. * @param headers set headers only for this request * @param params additional HEAD parameters to send with the request. * @param responseHandler the response handler instance that should handle the response. * @return RequestHandle of future request process */ public RequestHandle head(Context context, String url, Header[] headers, RequestParams params, ResponseHandlerInterface responseHandler) { HttpUriRequest request = new HttpHead(getUrlWithQueryString(isUrlEncodingEnabled, url, params)); if (headers != null) request.setHeaders(headers); return sendRequest(httpClient, httpContext, request, null, responseHandler, context); }
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 . j av a2 s .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:illab.nabal.proxy.AbstractContext.java
/** * Get HTTP HEAD object.//from w ww.j av a2s . c o m * * @param uri * @return HttpHead */ protected synchronized HttpHead getHttpHead(URI uri) { return new HttpHead(uri); }
From source file:com.archivas.clienttools.arcutils.impl.adapter.HCAPAdapter.java
protected boolean doTestConnection() throws ConnectionTestException { boolean isValid; HttpUriRequest request;/*from w ww. j a v a2 s . c o m*/ HCAPProfile hcapProfile = (HCAPProfile) getProfile(); HttpHost httpHost = null; try { httpHost = new HttpHost(getHost(), hcapProfile.getPort(), hcapProfile.getProtocol()); } catch (StorageAdapterException e) { throw new ConnectionTestException(e, null, null, getProfile().getName()); } String root = "/"; String resolvedPath = hcapProfile.resolvePath(root); request = new HttpHead(resolvedPath); HcapAdapterCookie cookie = new HcapAdapterCookie(request, httpHost); // Eventually we will just return this cookie which will be passed back to the caller. synchronized (savingCookieLock) { if (savedCookie != null) { throw new RuntimeException( "This adapter already has a current connection to host -- cannot create two at once."); } savedCookie = cookie; } Throwable cause = null; Integer statusCode = null; try { executeMethod(cookie); } catch (IOException e) { LOG.log(Level.INFO, "IOException during testConnection", e); cause = e; } finally { close(); } if (cookie.getResponse() != null) { statusCode = cookie.getResponse().getStatusLine().getStatusCode(); } if (statusCode != null && statusCode == HttpStatus.SC_OK) { isValid = true; } else if (cause != null) { throw new ConnectionTestException(cause, statusCode, null, getProfile().getName()); } else { throw new ConnectionTestException(statusCode, null, getProfile().getName()); } return isValid; }