List of usage examples for java.net URL getQuery
public String getQuery()
From source file:org.georchestra.security.Proxy.java
private URI buildUri(URL url) throws URISyntaxException { // Let URI constructor encode Path part URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), null, // Don't use query part because URI constructor will try to double encode it // (query part is already encoded in sURL) url.getRef());/*from w w w .j a v a 2 s . co m*/ // Reconstruct URL with encoded path from URI class and others parameters from URL class StringBuilder rawUrl = new StringBuilder(url.getProtocol() + "://" + url.getHost()); if (url.getPort() != -1) rawUrl.append(":" + String.valueOf(url.getPort())); rawUrl.append(uri.getRawPath()); // Use encoded version from URI class if (url.getQuery() != null) rawUrl.append("?" + url.getQuery()); // Use already encoded query part return new URI(rawUrl.toString()); }
From source file:org.cloudfoundry.identity.uaa.mock.token.TokenMvcMockTests.java
public static Map<String, List<String>> splitQuery(URL url) throws UnsupportedEncodingException { Map<String, List<String>> params = new LinkedHashMap<>(); String[] kv = url.getQuery().split("&"); for (String pair : kv) { int i = pair.indexOf("="); String key = i > 0 ? URLDecoder.decode(pair.substring(0, i), "UTF-8") : pair; if (!params.containsKey(key)) { params.put(key, new LinkedList<String>()); }/*w w w . jav a 2 s . co m*/ String value = i > 0 && pair.length() > i + 1 ? URLDecoder.decode(pair.substring(i + 1), "UTF-8") : null; params.get(key).add(value); } return params; }
From source file:com.stoutner.privacybrowser.MainWebViewActivity.java
private void loadUrlFromTextBox() throws UnsupportedEncodingException { // Get the text from urlTextBox and convert it to a string. String unformattedUrlString = urlTextBox.getText().toString(); URL unformattedUrl = null; Uri.Builder formattedUri = new Uri.Builder(); // Check to see if unformattedUrlString is a valid URL. Otherwise, convert it into a Duck Duck Go search. if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) { // Add http:// at the beginning if it is missing. Otherwise the app will segfault. if (!unformattedUrlString.startsWith("http")) { unformattedUrlString = "http://" + unformattedUrlString; }//from w w w .j a v a2 s.c o m // Convert unformattedUrlString to a URL, then to a URI, and then back to a string, which sanitizes the input and adds in any missing components. try { unformattedUrl = new URL(unformattedUrlString); } catch (MalformedURLException e) { e.printStackTrace(); } // The ternary operator (? :) makes sure that a null pointer exception is not thrown, which would happen if .get was called on a null value. final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null; final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null; final String path = unformattedUrl != null ? unformattedUrl.getPath() : null; final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null; final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null; formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment); formattedUrlString = formattedUri.build().toString(); } else { // Sanitize the search input and convert it to a DuckDuckGo search. final String encodedUrlString = URLEncoder.encode(unformattedUrlString, "UTF-8"); // Use the correct search URL based on javaScriptEnabled. if (javaScriptEnabled) { formattedUrlString = javaScriptEnabledSearchURL + encodedUrlString; } else { // JavaScript is disabled. formattedUrlString = javaScriptDisabledSearchURL + encodedUrlString; } } mainWebView.loadUrl(formattedUrlString); // Hides the keyboard so we can see the webpage. InputMethodManager inputMethodManager = (InputMethodManager) getSystemService( Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); }
From source file:net.www_eee.portal.channels.ProxyChannel.java
/** * Perform any desired modifications to a link which is <em>not</em> being * {@linkplain #rewriteProxiedFileLink(Page.Request, URL, URI, boolean, boolean) rewritten} to point back through this * channel./*from w ww . j a va 2s. co m*/ * * @param pageRequest The {@link net.www_eee.portal.Page.Request Request} currently being processed. * @param proxiedFileURL The {@linkplain #getProxiedFileURL(Page.Request, Channel.Mode, boolean) proxied file URL}. * @param linkURI The {@link URI} of the link to rewrite. * @param hyperlink Is the <code>linkURI</code> a hyperlink? * @param absoluteURLRequired Does the result need to be {@linkplain URI#isAbsolute() absolute}? * @param resolvedLinkURL The <code>linkURI</code> after being resolved to it's actual location. * @return The rewritten link {@link URI}. * @throws WWWEEEPortal.Exception If a problem occurred while determining the result. * @see #rewriteProxiedFileLink(Page.Request, URL, URI, boolean, boolean) */ protected static final URI rewriteProxiedFileLinkOutsideChannel(final Page.Request pageRequest, final URL proxiedFileURL, final @Nullable URI linkURI, final boolean hyperlink, final boolean absoluteURLRequired, final URL resolvedLinkURL) throws WWWEEEPortal.Exception { if ((linkURI != null) && (linkURI.isAbsolute())) { return linkURI; // If a document author includes an absolute link, we generally want to just leave that as-is. } try { if ((!absoluteURLRequired) && (equalHostAndPort(proxiedFileURL, pageRequest.getBaseURL()))) { // They didn't author an absolute link, we don't require one, and since the resolved link points to our host/port, we have the opportunity to return a nice short non-absolute link... final StringBuffer sb = new StringBuffer(); sb.append(resolvedLinkURL.getPath()); if (resolvedLinkURL.getQuery() != null) { sb.append('?'); sb.append(resolvedLinkURL.getQuery()); } if (resolvedLinkURL.getRef() != null) { sb.append('#'); sb.append(resolvedLinkURL.getRef()); } return new URI(sb.toString()); } return resolvedLinkURL.toURI(); } catch (URISyntaxException urise) { throw new ContentManager.ContentException("Error constructing resolved link URI", urise); } }
From source file:com.gargoylesoftware.htmlunit.WebClient.java
/** * Builds a WebResponse for a file URL.//from w w w. j av a 2 s .c om * This first implementation is basic. * It assumes that the file contains an HTML page encoded with the specified encoding. * @param url the file URL * @param charset encoding to use * @return the web response * @throws IOException if an IO problem occurs */ private WebResponse makeWebResponseForFileUrl(final WebRequest webRequest) throws IOException { URL cleanUrl = webRequest.getUrl(); if (cleanUrl.getQuery() != null) { // Get rid of the query portion before trying to load the file. cleanUrl = UrlUtils.getUrlWithNewQuery(cleanUrl, null); } if (cleanUrl.getRef() != null) { // Get rid of the ref portion before trying to load the file. cleanUrl = UrlUtils.getUrlWithNewRef(cleanUrl, null); } String fileUrl = cleanUrl.toExternalForm(); fileUrl = URLDecoder.decode(fileUrl, "UTF-8"); final File file = new File(fileUrl.substring(5)); if (!file.exists()) { // construct 404 final List<NameValuePair> compiledHeaders = new ArrayList<>(); compiledHeaders.add(new NameValuePair("Content-Type", "text/html")); final WebResponseData responseData = new WebResponseData( TextUtil.stringToByteArray("File: " + file.getAbsolutePath(), "UTF-8"), 404, "Not Found", compiledHeaders); return new WebResponse(responseData, webRequest, 0); } final String contentType = guessContentType(file); final DownloadedContent content = new DownloadedContent.OnFile(file, false); final List<NameValuePair> compiledHeaders = new ArrayList<>(); compiledHeaders.add(new NameValuePair("Content-Type", contentType)); final WebResponseData responseData = new WebResponseData(content, 200, "OK", compiledHeaders); return new WebResponse(responseData, webRequest, 0); }
From source file:de.innovationgate.wgpublisher.WGPRequestPath.java
public static URL determineRedirectionURL(URL currentURL, String redirectProtocol, String redirectHost, String redirectPort) throws MalformedURLException { // determine current protocol, host and port String currentProtocol = currentURL.getProtocol(); String currentHost = currentURL.getHost(); String currentPort = null;/*from ww w . j a v a 2s . c o m*/ if (currentURL.getPort() != -1) { currentPort = new Integer(currentURL.getPort()).toString(); } else if ("http".equals(currentProtocol)) { currentPort = "80"; } else if ("https".equals(currentProtocol)) { currentPort = "443"; } //build redirectURL boolean redirectNecessary = false; StringBuffer redirectURLBuffer = new StringBuffer(); if (redirectProtocol != null) { if (!currentProtocol.equalsIgnoreCase(redirectProtocol)) { redirectURLBuffer.append(redirectProtocol); redirectNecessary = true; } else { redirectURLBuffer.append(currentProtocol); } } else { redirectURLBuffer.append(currentProtocol); } redirectURLBuffer.append("://"); if (redirectHost != null) { if (!currentHost.equalsIgnoreCase(redirectHost)) { redirectURLBuffer.append(redirectHost); redirectNecessary = true; } else { redirectURLBuffer.append(currentHost); } } else { redirectURLBuffer.append(currentHost); } if (redirectPort != null && currentPort != null) { if (!currentPort.equalsIgnoreCase(redirectPort)) { redirectURLBuffer.append(":" + redirectPort); redirectNecessary = true; } else { redirectURLBuffer.append(":" + currentPort); } } else if (currentPort != null) { redirectURLBuffer.append(":" + currentPort); } redirectURLBuffer.append(currentURL.getPath()); if (currentURL.getQuery() != null) { redirectURLBuffer.append("?").append(currentURL.getQuery()); } if (redirectNecessary) { URL redirectURL = new URL(redirectURLBuffer.toString()); return redirectURL; } else { return null; } }
From source file:lucee.runtime.tag.Http4.java
private void _doEndTag(Struct cfhttp) throws PageException, IOException { BasicHttpParams params = new BasicHttpParams(); DefaultHttpClient client = HTTPEngine4Impl.createClient(params, redirect ? HTTPEngine.MAX_REDIRECT : 0); ConfigWeb cw = pageContext.getConfig(); HttpRequestBase req = null;/*from w ww. j a v a 2 s . c om*/ HttpContext httpContext = 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().equalsIgnoreCase("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); String type = param.getType(); // URL if (type.equals("url")) { if (sbQS.length() > 0) sbQS.append('&'); sbQS.append(param.getEncoded() ? HttpImpl.urlenc(param.getName(), charset) : param.getName()); sbQS.append('='); sbQS.append(param.getEncoded() ? HttpImpl.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); } // select best matching method (get,post, post multpart (file)) boolean isBinary = false; boolean doMultiPart = doUploadFile || this.multiPart; HttpPost post = null; HttpEntityEnclosingRequest eem = null; if (this.method == METHOD_GET) { req = new HttpGet(url); } else if (this.method == METHOD_HEAD) { req = new HttpHead(url); } else if (this.method == METHOD_DELETE) { isBinary = true; req = new HttpDelete(url); } else if (this.method == METHOD_PUT) { isBinary = true; HttpPut put = new HttpPut(url); req = put; eem = 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; eem = HTTPPatchFactory.getHTTPPatch(url); req = (HttpRequestBase) eem; } else { isBinary = true; post = new HttpPost(url); req = post; eem = post; } 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 = post != null ? new ArrayList<NameValuePair>() : null; for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); String type = param.getType(); // URL if (type.equals("url")) { //listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(), http.charset))); } // Form else if (type.equals("formfield") || type.equals("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 (post != 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.equals("cgi")) { if (param.getEncoded()) req.addHeader(HttpImpl.urlenc(param.getName(), charset), HttpImpl.urlenc(param.getValueAsString(), charset)); else req.addHeader(param.getName(), param.getValueAsString()); } // Header else if (type.startsWith("head")) { if (param.getName().equalsIgnoreCase("content-type")) hasContentType = true; if (param.getName().equalsIgnoreCase("Content-Length")) { } else if (param.getName().equalsIgnoreCase("Accept-Encoding")) { acceptEncoding.append(HttpImpl.headerValue(param.getValueAsString())); acceptEncoding.append(", "); } else req.addHeader(param.getName(), HttpImpl.headerValue(param.getValueAsString())); } // Cookie else if (type.equals("cookie")) { HTTPEngine4Impl.addCookie(client, host, param.getName(), param.getValueAsString(), "/", charset); } // File else if (type.equals("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"); String strCT = HttpImpl.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.equals("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 (eem == null) throw new ApplicationException("type xml is only supported for type post and put"); HTTPEngine4Impl.setBody(eem, param.getValueAsString(), mt, cs); } // Body else if (type.equals("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 (eem == null) throw new ApplicationException("type body is only supported for type post and put"); HTTPEngine4Impl.setBody(eem, param.getValue(), mt, cs); } else { throw new ApplicationException("invalid type [" + type + "]"); } } // post params if (postParam != null && postParam.size() > 0) post.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 && eem != 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); eem.setEntity(entity); } catch (IOException e) { throw Caster.toPageException(e); } doIt = false; } } if (doIt) { MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT); Iterator<FormBodyPart> it = parts.iterator(); while (it.hasNext()) { FormBodyPart part = it.next(); mpe.addPart(part.getName(), part.getBody()); } eem.setEntity(mpe); } //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 (!HttpImpl.hasHeaderIgnoreCase(req, "User-Agent")) req.setHeader("User-Agent", this.useragent); // set timeout if (this.timeout > 0L) HTTPEngine4Impl.setTimeout(params, (int) this.timeout); // 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(client, this.username, this.password, this.workStation, this.domain); } else httpContext = HTTPEngine4Impl.setCredentials(client, 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(client, req, proxy); } try { if (httpContext == null) httpContext = new BasicHttpContext(); /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Executor4 e = new Executor4(this, client, httpContext, req, redirect); HTTPResponse4Impl rsp = null; if (timeout < 0) { try { rsp = e.execute(httpContext); } catch (Throwable t) { if (!throwonerror) { setUnknownHost(cfhttp, t); return; } throw toPageException(t); } } else { e.start(); try { synchronized (this) {//print.err(timeout); this.wait(timeout); } } catch (InterruptedException ie) { throw Caster.toPageException(ie); } if (e.t != null) { if (!throwonerror) { setUnknownHost(cfhttp, e.t); return; } throw toPageException(e.t); } 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.getURL()); setRequestTimeout(cfhttp); return; //throw new ApplicationException("timeout"); } } /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset()); // 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(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(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 && HttpImpl.isGzipEncoded(contentEncoding)) is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { str = is == null ? "" : IOUtil.toString(is, responseCharset); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) { str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset); } else throw eof; } } catch (UnsupportedEncodingException uee) { str = IOUtil.toString(is, (Charset) null); } } 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(FILE_CONTENT, 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 (HttpImpl.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(FILE_CONTENT, MultiPartResponseUtils.getParts(barr, mimetype)); } else { cfhttp.set(FILE_CONTENT, barr); } } else cfhttp.set(FILE_CONTENT, ""); 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 (!HttpImpl.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()); } } } finally { //rsp.release(); } }
From source file:org.lnicholls.galleon.togo.ToGo.java
public ArrayList getRecordings(List tivos, ProgressListener progressIndicator) { ServerConfiguration serverConfiguration = Server.getServer().getServerConfiguration(); ArrayList videos = new ArrayList(); log.debug("getRecordings: " + tivos.size()); log.debug("mServerConfiguration.getMediaAccessKey()=" + serverConfiguration.getMediaAccessKey().length()); PersistentValue persistentValue = PersistentValueManager.loadPersistentValue("VideoServer.lastUpdate"); if (persistentValue != null) { try//from w w w . j a v a 2 s . c o m { String value = persistentValue.getValue(); Date date = new Date(value); Date now = new Date(); if (now.getTime() - date.getTime() < 60 * 1000) { log.debug("backoff: tivo busy with goback download"); return videos; } } catch (Throwable ex) { log.error("Could not retrieve video server last update", ex); } } if (serverConfiguration.getMediaAccessKey().length() > 0) { GetMethod get = null; Iterator tivosIterator = tivos.iterator(); while (tivosIterator.hasNext()) { TiVo tivo = (TiVo) tivosIterator.next(); HashMap shows = new HashMap(); try { Protocol protocol = new Protocol("https", new TiVoSSLProtocolSocketFactory(), 443); HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(tivo.getAddress(), 443, protocol); Credentials credentials = new UsernamePasswordCredentials("tivo", Tools.decrypt(serverConfiguration .getMediaAccessKey())); client.getState().setCredentials("TiVo DVR", tivo.getAddress(), credentials); int total = -1; int counter = 0; do { try { String url = QUERY_CONTAINER + RECURSE + ITEM_COUNT + MAX + ANCHOR_OFFSET + counter; log.debug(url); get = new GetMethod(url); client.executeMethod(get); // log.debug(get.getResponseBodyAsString()); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(get.getResponseBodyAsStream()); // Get the root element Element root = document.getRootElement(); // <TiVoContainer> Date lastChangedDate = new Date(); Element detailsElement = root.element("Details"); if (detailsElement != null) { Element totalItemsElement = detailsElement.element("TotalItems"); if (totalItemsElement != null) { try { total = Integer.parseInt(totalItemsElement.getText()); } catch (NumberFormatException ex) { } } Element lastChangeDateElement = detailsElement.element("LastChangeDate"); if (lastChangeDateElement != null) { try { lastChangedDate = Tools.hexDate(lastChangeDateElement.getText()); } catch (NumberFormatException ex) { } } } log.debug("lastChangedDate=" + lastChangedDate); log.debug("tivo.getLastChangedDate()=" + tivo.getLastChangedDate()); log.debug("total=" + total); log.debug("tivo.getNumShows()=" + tivo.getNumShows()); if (lastChangedDate.after(tivo.getLastChangedDate()) || total != tivo.getNumShows()) { tivo.setLastChangedDate(lastChangedDate); tivo.setNumShows(0); } else { // Nothing changed synchronized (this) { List recordings = VideoManager.listAll(); Iterator iterator = recordings.listIterator(); while (iterator.hasNext()) { Video video = (Video) iterator.next(); if (video.getUrl() != null && video.getUrl().indexOf(tivo.getAddress()) != -1) videos.add(video); } } break; } for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { Element child = (Element) iterator.next(); if (child.getName().equals("Item")) { Video video = new Video(); video.setMimeType("mpeg"); video.setDateModified(new Date()); video.setParentalControls(Boolean.FALSE); counter = counter + 1; if (progressIndicator != null) { if (total > 0) { progressIndicator.progress(counter + " of " + total); } else progressIndicator.progress(counter + ""); } Element details = child.element("Details"); if (details != null) { String value = Tools.getAttribute(details, "CopyProtected"); // <CopyProtected>Yes</CopyProtected> if (value != null) { if (value.equalsIgnoreCase("yes")) continue; } value = Tools.getAttribute(details, "Title"); if (value != null) video.setTitle(value); value = Tools.getAttribute(details, "ParentalControls"); if (value != null) { if (value.equalsIgnoreCase("yes")) video.setParentalControls(Boolean.TRUE); } value = Tools.getAttribute(details, "SourceSize"); if (value != null) video.setSize(Long.parseLong(value)); value = Tools.getAttribute(details, "Duration"); if (value != null) try { video.setDuration(Integer.parseInt(value)); } catch (NumberFormatException ex) { log.error("Could not set duration", ex); } value = Tools.getAttribute(details, "CaptureDate"); if (value != null) video.setDateRecorded(Tools.hexDate(value)); value = Tools.getAttribute(details, "EpisodeTitle"); if (value != null) video.setEpisodeTitle(value); value = Tools.getAttribute(details, "Description"); if (value != null) video.setDescription(value); value = Tools.getAttribute(details, "SourceChannel"); if (value != null) video.setChannel(value); value = Tools.getAttribute(details, "SourceStation"); if (value != null) video.setStation(value); value = Tools.getAttribute(details, "InProgress"); if (value != null) video.setStatus(Video.STATUS_RECORDING); video.setSource(tivo.getAddress()); video.setTivo(tivo.getName()); } String detailsUrl = null; Element links = child.element("Links"); if (links != null) { Element element = links.element("Content"); if (element != null) { String value = Tools.getAttribute(element, "Url"); if (value != null) video.setUrl(value); } element = links.element("CustomIcon"); if (element != null) { String value = Tools.getAttribute(element, "Url"); if (value != null) { video.setIcon(value.substring(value.lastIndexOf(":") + 1)); } } element = links.element("TiVoVideoDetails"); if (element != null) { String value = Tools.getAttribute(element, "Url"); if (value != null) { URL path = new URL(value); detailsUrl = path.getPath() + "?" + path.getQuery(); // getvideoDetails(client, // video, // path.getPath()+"?"+path.getQuery()); } } } if (detailsUrl != null) { shows.put(video, detailsUrl); tivo.setNumShows(tivo.getNumShows() + 1); } } } Thread.sleep(100); // give the CPU some breathing } finally { if (get != null) { get.releaseConnection(); get = null; } } } while (counter < total); // Get video details for (Iterator iterator = shows.keySet().iterator(); iterator.hasNext();) { Video video = (Video) iterator.next(); String url = (String) shows.get(video); getvideoDetails(client, video, url); videos.add(video); Thread.sleep(500); // give the CPU some breathing time } } catch (MalformedURLException ex) { Tools.logException(ToGo.class, ex); } catch (Exception ex) { Tools.logException(ToGo.class, ex); try { Thread.sleep(10000); // give the CPU some breathing time } catch (Exception ex2) { } } } } return videos; }
From source file:org.sakaiproject.content.tool.ResourcesHelperAction.java
public void doContinue(RunData data) { logger.debug(this + ".doContinue()"); SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid()); ParameterParser params = data.getParameters(); int requestStateId = params.getInt("requestStateId", 0); ResourcesAction.restoreRequestState(state, new String[] { ResourcesAction.PREFIX + ResourcesAction.REQUEST }, requestStateId); String content = params.getString("content"); if (content == null) { addAlert(state, rb.getString("text.notext")); return;// w ww.j a v a 2 s. co m } ToolSession toolSession = SessionManager.getCurrentToolSession(); // Tool tool = ToolManager.getCurrentTool(); // String url = (String) toolSession.getAttribute(tool.getId() + Tool.HELPER_DONE_URL); // toolSession.removeAttribute(tool.getId() + Tool.HELPER_DONE_URL); ResourceToolActionPipe pipe = (ResourceToolActionPipe) toolSession .getAttribute(ResourceToolAction.ACTION_PIPE); if (pipe == null) { return; } if (pipe != null) { String pipe_init_id = pipe.getInitializationId(); String response_init_id = params.getString(ResourcesAction.PIPE_INIT_ID); if (pipe_init_id == null || response_init_id == null || !response_init_id.equalsIgnoreCase(pipe_init_id)) { // in this case, prevent upload to wrong folder pipe.setErrorMessage(rb.getString("alert.try-again")); pipe.setActionCanceled(false); pipe.setErrorEncountered(true); pipe.setActionCompleted(false); return; } toolSession.setAttribute(ResourceToolAction.ACTION_PIPE, pipe); } String resourceType = pipe.getAction().getTypeId(); String mimetype = pipe.getMimeType(); ListItem item = new ListItem(pipe.getContentEntity()); // notification int noti = determineNotificationPriority(params, item.isDropbox, item.userIsMaintainer()); pipe.setRevisedMimeType(pipe.getMimeType()); if (ResourceType.TYPE_TEXT.equals(resourceType) || ResourceType.MIME_TYPE_TEXT.equals(mimetype)) { pipe.setRevisedMimeType(ResourceType.MIME_TYPE_TEXT); pipe.setRevisedResourceProperty(ResourceProperties.PROP_CONTENT_ENCODING, ResourcesAction.UTF_8_ENCODING); pipe.setNotification(noti); } else if (ResourceType.TYPE_HTML.equals(resourceType) || ResourceType.MIME_TYPE_HTML.equals(mimetype)) { StringBuilder alertMsg = new StringBuilder(); content = FormattedText.processHtmlDocument(content, alertMsg); pipe.setRevisedMimeType(ResourceType.MIME_TYPE_HTML); pipe.setRevisedResourceProperty(ResourceProperties.PROP_CONTENT_ENCODING, ResourcesAction.UTF_8_ENCODING); pipe.setNotification(noti); if (alertMsg.length() > 0) { addAlert(state, alertMsg.toString()); return; } } else if (ResourceType.TYPE_URL.equals(resourceType)) { // SAK-23587 - properly escape the URL where required try { URL url = new URL(content); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); content = uri.toString(); } catch (Exception e) { //ok to ignore, just use the original url logger.debug("URL can not be encoded: " + e.getClass() + ":" + e.getCause()); } pipe.setRevisedMimeType(ResourceType.MIME_TYPE_URL); pipe.setNotification(noti); } else if (ResourceType.TYPE_FOLDER.equals(resourceType)) { MultiFileUploadPipe mfp = (MultiFileUploadPipe) pipe; int count = params.getInt("folderCount"); mfp.setFileCount(count); List<ResourceToolActionPipe> pipes = mfp.getPipes(); for (int i = 0; i < pipes.size(); i++) { ResourceToolActionPipe fp = pipes.get(i); String folderName = params.getString("folder" + (i + 1)); fp.setFileName(folderName); fp.setNotification(noti); } } try { pipe.setRevisedContent(content.getBytes(ResourcesAction.UTF_8_ENCODING)); pipe.setActionCanceled(false); pipe.setErrorEncountered(false); pipe.setActionCompleted(true); } catch (UnsupportedEncodingException e) { logger.warn(this + ": " + e.toString()); addAlert(state, rb.getString("alert.utf8encoding")); pipe.setActionCanceled(false); pipe.setErrorEncountered(true); pipe.setActionCompleted(false); } toolSession.setAttribute(ResourceToolAction.DONE, Boolean.TRUE); }
From source file:lucee.runtime.tag.Http41.java
private void _doEndTag(Struct cfhttp) throws PageException, IOException { HttpClientBuilder builder = HttpClients.custom(); // redirect//w w w.j a va 2 s .c o m 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; //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().equalsIgnoreCase("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); String type = param.getType(); // URL if (type.equals("url")) { if (sbQS.length() > 0) sbQS.append('&'); sbQS.append(param.getEncoded() ? HttpImpl.urlenc(param.getName(), charset) : param.getName()); sbQS.append('='); sbQS.append(param.getEncoded() ? HttpImpl.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); } // select best matching method (get,post, post multpart (file)) boolean isBinary = false; boolean doMultiPart = doUploadFile || this.multiPart; HttpPost post = null; HttpEntityEnclosingRequest eem = null; if (this.method == METHOD_GET) { req = new HttpGet(url); } else if (this.method == METHOD_HEAD) { req = new HttpHead(url); } else if (this.method == METHOD_DELETE) { isBinary = true; req = new HttpDelete(url); } else if (this.method == METHOD_PUT) { isBinary = true; HttpPut put = new HttpPut(url); req = put; eem = 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; eem = HTTPPatchFactory.getHTTPPatch(url); req = (HttpRequestBase) eem; } else { isBinary = true; post = new HttpPost(url); req = post; eem = post; } 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 = post != null ? new ArrayList<NameValuePair>() : null; for (int i = 0; i < len; i++) { HttpParamBean param = this.params.get(i); String type = param.getType(); // URL if (type.equals("url")) { //listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(), http.charset))); } // Form else if (type.equals("formfield") || type.equals("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 (post != 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.equals("cgi")) { if (param.getEncoded()) req.addHeader(HttpImpl.urlenc(param.getName(), charset), HttpImpl.urlenc(param.getValueAsString(), charset)); else req.addHeader(param.getName(), param.getValueAsString()); } // Header else if (type.startsWith("head")) { if (param.getName().equalsIgnoreCase("content-type")) hasContentType = true; if (param.getName().equalsIgnoreCase("Content-Length")) { } else if (param.getName().equalsIgnoreCase("Accept-Encoding")) { acceptEncoding.append(HttpImpl.headerValue(param.getValueAsString())); acceptEncoding.append(", "); } else req.addHeader(param.getName(), HttpImpl.headerValue(param.getValueAsString())); } // Cookie else if (type.equals("cookie")) { HTTPEngineImpl.addCookie(cookieStore, host, param.getName(), param.getValueAsString(), "/", charset); } // File else if (type.equals("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"); String strCT = HttpImpl.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.equals("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 (eem == null) throw new ApplicationException("type xml is only supported for type post and put"); HTTPEngineImpl.setBody(eem, param.getValueAsString(), mt, cs); } // Body else if (type.equals("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 (eem == null) throw new ApplicationException("type body is only supported for type post and put"); HTTPEngineImpl.setBody(eem, param.getValue(), mt, cs); } else { throw new ApplicationException("invalid type [" + type + "]"); } } // post params if (postParam != null && postParam.size() > 0) post.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 && eem != 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); eem.setEntity(entity); } catch (IOException e) { throw Caster.toPageException(e); } doIt = false; } } if (doIt) { MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.STRICT); Iterator<FormBodyPart> it = parts.iterator(); while (it.hasNext()) { FormBodyPart part = it.next(); mpe.addPart(part.getName(), part.getBody()); } eem.setEntity(mpe); } //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 (!HttpImpl.hasHeaderIgnoreCase(req, "User-Agent")) req.setHeader("User-Agent", this.useragent); // set timeout if (this.timeout > 0L) { builder.setConnectionTimeToLive(this.timeout, TimeUnit.MILLISECONDS); } // 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]"); HTTPEngineImpl.setNTCredentials(builder, this.username, this.password, this.workStation, this.domain); } else httpContext = HTTPEngineImpl.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(); } HTTPEngineImpl.setProxy(builder, req, proxy); } CloseableHttpClient client = null; try { if (httpContext == null) httpContext = new BasicHttpContext(); /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// client = builder.build(); Executor41 e = new Executor41(this, client, httpContext, req, redirect); HTTPResponse4Impl rsp = null; if (timeout < 0) { try { rsp = e.execute(httpContext); } catch (Throwable t) { if (!throwonerror) { setUnknownHost(cfhttp, t); return; } throw toPageException(t); } } else { e.start(); try { synchronized (this) {//print.err(timeout); this.wait(timeout); } } catch (InterruptedException ie) { throw Caster.toPageException(ie); } if (e.t != null) { if (!throwonerror) { setUnknownHost(cfhttp, e.t); return; } throw toPageException(e.t); } 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.getURL()); setRequestTimeout(cfhttp); return; //throw new ApplicationException("timeout"); } } /////////////////////////////////////////// EXECUTE ///////////////////////////////////////////////// Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset()); // 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(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(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 && HttpImpl.isGzipEncoded(contentEncoding)) is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is); } try { try { str = is == null ? "" : IOUtil.toString(is, responseCharset); } catch (EOFException eof) { if (is instanceof CachingGZIPInputStream) { str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset); } else throw eof; } } catch (UnsupportedEncodingException uee) { str = IOUtil.toString(is, (Charset) null); } } 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(FILE_CONTENT, 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 (HttpImpl.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(FILE_CONTENT, MultiPartResponseUtils.getParts(barr, mimetype)); } else { cfhttp.set(FILE_CONTENT, barr); } } else cfhttp.set(FILE_CONTENT, ""); 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 (!HttpImpl.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()); } } } finally { if (client != null) client.close(); } }