List of usage examples for java.net URL getDefaultPort
public int getDefaultPort()
From source file:org.fcrepo.localservices.saxon.SaxonServlet.java
/** * Return a URL string in which the port is always specified. *///w w w . j av a 2 s . co m private static String normalizeURL(String urlString) throws MalformedURLException { URL url = new URL(urlString); if (url.getPort() == -1) { return url.getProtocol() + "://" + url.getHost() + ":" + url.getDefaultPort() + url.getFile() + (url.getRef() != null ? "#" + url.getRef() : ""); } else { return urlString; } }
From source file:sit.web.client.HttpHelper.java
public static void apacheDownload(String downloadUrl, File targetFile) throws IOException { URL url = new URL(downloadUrl); HttpClient httpclient;/*from w w w. j ava 2 s . c o m*/ if (isHTTPS(downloadUrl)) { httpclient = HTTPTrustHelper.getNewHttpClient(Charset.defaultCharset(), (url.getPort() != -1) ? url.getPort() : url.getDefaultPort()); } else { httpclient = new DefaultHttpClient(); } HttpGet request = new HttpGet(downloadUrl); HttpResponse response = httpclient.execute(request); InputStream is = response.getEntity().getContent(); FileOutputStream writer = new FileOutputStream(targetFile); try { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; while ((bytesRead = is.read(buffer, 0, buffer.length)) >= 0) { writer.write(buffer, 0, bytesRead); } } finally { writer.close(); is.close(); httpclient.getConnectionManager().shutdown(); } }
From source file:com.digitalpebble.storm.crawler.protocol.http.HttpRobotRulesParser.java
/** * Compose unique key to store and access robot rules in cache for given URL *///from w w w. ja va 2 s . c o m protected static String getCacheKey(URL url) { String protocol = url.getProtocol().toLowerCase(Locale.ROOT); // normalize // to // lower // case String host = url.getHost().toLowerCase(Locale.ROOT); // normalize to // lower case int port = url.getPort(); if (port == -1) { port = url.getDefaultPort(); } /* * Robot rules apply only to host, protocol, and port where robots.txt * is hosted (cf. NUTCH-1752). Consequently */ String cacheKey = protocol + ":" + host + ":" + port; return cacheKey; }
From source file:com.evilisn.DAO.CertMapper.java
public static byte[] httpGetBin(URI uri, boolean bActiveCheckUnknownHost) throws Exception { InputStream is = null;//from ww w . j av a2 s. c o m InputStream is_temp = null; try { if (uri == null) return null; URL url = uri.toURL(); if (bActiveCheckUnknownHost) { url.getProtocol(); String host = url.getHost(); int port = url.getPort(); if (port == -1) port = url.getDefaultPort(); InetSocketAddress isa = new InetSocketAddress(host, port); if (isa.isUnresolved()) { //fix JNLP popup error issue throw new UnknownHostException("Host Unknown:" + isa.toString()); } } HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setDoInput(true); uc.setAllowUserInteraction(false); uc.setInstanceFollowRedirects(true); setTimeout(uc); String contentEncoding = uc.getContentEncoding(); int len = uc.getContentLength(); // is = uc.getInputStream(); if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("gzip") != -1) { is_temp = uc.getInputStream(); is = new GZIPInputStream(is_temp); } else if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("deflate") != -1) { is_temp = uc.getInputStream(); is = new InflaterInputStream(is_temp); } else { is = uc.getInputStream(); } if (len != -1) { int ch = 0, i = 0; byte[] res = new byte[len]; while ((ch = is.read()) != -1) { res[i++] = (byte) (ch & 0xff); } return res; } else { ArrayList<byte[]> buffer = new ArrayList<byte[]>(); int buf_len = 1024; byte[] res = new byte[buf_len]; int ch = 0, i = 0; while ((ch = is.read()) != -1) { res[i++] = (byte) (ch & 0xff); if (i == buf_len) { //rotate buffer.add(res); i = 0; res = new byte[buf_len]; } } int total_len = buffer.size() * buf_len + i; byte[] buf = new byte[total_len]; for (int j = 0; j < buffer.size(); j++) { System.arraycopy(buffer.get(j), 0, buf, j * buf_len, buf_len); } if (i > 0) { System.arraycopy(res, 0, buf, buffer.size() * buf_len, i); } return buf; } } catch (Exception e) { e.printStackTrace(); return null; } finally { closeInputStream(is_temp); closeInputStream(is); } }
From source file:org.tuckey.web.filters.urlrewrite.RequestProxy.java
/** * This method performs the proxying of the request to the target address. * * @param target The target address. Has to be a fully qualified address. The request is send as-is to this address. * @param hsRequest The request data which should be send to the * @param hsResponse The response data which will contain the data returned by the proxied request to target. * @throws java.io.IOException Passed on from the connection logic. */// www .ja va 2s . com public static void execute(final String target, final HttpServletRequest hsRequest, final HttpServletResponse hsResponse) throws IOException { if (log.isInfoEnabled()) { log.info("execute, target is " + target); log.info("response commit state: " + hsResponse.isCommitted()); } if (StringUtils.isBlank(target)) { log.error("The target address is not given. Please provide a target address."); return; } log.info("checking url"); final URL url; try { url = new URL(target); } catch (MalformedURLException e) { log.error("The provided target url is not valid.", e); return; } log.info("seting up the host configuration"); final HostConfiguration config = new HostConfiguration(); ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy")); if (proxyHost != null) config.setProxyHost(proxyHost); final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort(); config.setHost(url.getHost(), port, url.getProtocol()); if (log.isInfoEnabled()) log.info("config is " + config.toString()); final HttpMethod targetRequest = setupProxyRequest(hsRequest, url); if (targetRequest == null) { log.error("Unsupported request method found: " + hsRequest.getMethod()); return; } //perform the reqeust to the target server final HttpClient client = new HttpClient(new SimpleHttpConnectionManager()); if (log.isInfoEnabled()) { log.info("client state" + client.getState()); log.info("client params" + client.getParams().toString()); log.info("executeMethod / fetching data ..."); } final int result; if (targetRequest instanceof EntityEnclosingMethod) { final RequestProxyCustomRequestEntity requestEntity = new RequestProxyCustomRequestEntity( hsRequest.getInputStream(), hsRequest.getContentLength(), hsRequest.getContentType()); final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) targetRequest; entityEnclosingMethod.setRequestEntity(requestEntity); result = client.executeMethod(config, entityEnclosingMethod); } else { result = client.executeMethod(config, targetRequest); } //copy the target response headers to our response setupResponseHeaders(targetRequest, hsResponse); InputStream originalResponseStream = targetRequest.getResponseBodyAsStream(); //the body might be null, i.e. for responses with cache-headers which leave out the body if (originalResponseStream != null) { OutputStream responseStream = hsResponse.getOutputStream(); copyStream(originalResponseStream, responseStream); } log.info("set up response, result code was " + result); }
From source file:gov.loc.ndmso.proxyfilter.RequestProxy.java
/** * This method performs the proxying of the request to the target address. * * @param target The target address. Has to be a fully qualified address. The request is send as-is to this address. * @param hsRequest The request data which should be send to the * @param hsResponse The response data which will contain the data returned by the proxied request to target. * @throws java.io.IOException Passed on from the connection logic. *//*from w w w.j a v a 2 s. com*/ public static void execute(final String target, final String collection, final HttpServletRequest hsRequest, final HttpServletResponse hsResponse, MultiThreadedHttpConnectionManager connManager) throws IOException { // log.info("execute, target is " + target); // log.info("response commit state: " + hsResponse.isCommitted()); if (target == null || "".equals(target) || "".equals(target.trim())) { log.error("The target address is not given. Please provide a target address."); return; } // log.info("checking url"); final URL url; try { url = new URL(target); } catch (MalformedURLException e) { // log.error("The provided target url is not valid.", e); return; } // log.info("setting up the host configuration"); final HostConfiguration config = new HostConfiguration(); ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy")); if (proxyHost != null) config.setProxyHost(proxyHost); final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort(); config.setHost(url.getHost(), port, "http"); // log.info("config is " + config.toString()); final HttpMethod targetRequest = setupProxyRequest(hsRequest, url); if (targetRequest == null) { // log.error("Unsupported request method found: " + hsRequest.getMethod()); return; } //perform the request to the target server final HttpClient client = new HttpClient(connManager); //if (log.isInfoEnabled()) { // log.info("client state" + client.getState()); // log.info("client params" + client.getParams().toString()); // log.info("executeMethod / fetching data ..."); //} final int result = client.executeMethod(config, targetRequest); //copy the target response headers to our response setupResponseHeaders(targetRequest, hsResponse); String binRegex = ".*\\.(?i)(jpg|tif|png|gif|bmp|mp3|mpg)(.*$)*"; String binRegexRedux = ".*(?i)(\\/thumb)(.*$)*"; if (target.matches(binRegex) || target.matches(binRegexRedux)) { // log.info("binRegex matched: " + target); InputStream originalResponseStream = targetRequest.getResponseBodyAsStream(); if (originalResponseStream != null) { if (targetRequest.getResponseHeaders().toString().matches("(?i).*content-type.*")) { PrintWriter responseStream = hsResponse.getWriter(); copyStreamText(targetRequest.getResponseBodyAsString(), responseStream); } else { OutputStream responseStream = hsResponse.getOutputStream(); copyStreamBinary(originalResponseStream, responseStream); } } } else { // log.info("binRegex NOT matched: " + target); String proxyResponseStr = targetRequest.getResponseBodyAsString(); // the body might be null, i.e. for responses with cache-headers which leave out the body if (proxyResponseStr != null) { //proxyResponseStr = proxyResponseStr.replaceAll("xqy", "jsp"); proxyResponseStr = proxyResponseStr.replaceAll("National Library Catalog", "Library of Congress Data Service"); proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress collections", "Library of Congress bibliographic data"); proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress Collections", "Library of Congress Bibliographic Data"); proxyResponseStr = proxyResponseStr.replaceAll("action=\"/", "action=\"/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("href=\"/", "href=\"/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("href=\"/diglib/loc\\.", "href=\"/diglib/" + collection + "/loc."); proxyResponseStr = proxyResponseStr.replaceAll("src=\"/", "src=\"/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("value=\"/", "value=\"/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("url\\(/", "url\\(/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("url\\(\"/", "url\\(\"/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("src'\\) == \"/", "src'\\) == \"/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("src\", \"/", "src\", \"/diglib/"); proxyResponseStr = proxyResponseStr.replaceAll("natlibcat@loc.gov", "ndmso@loc.gov"); proxyResponseStr = proxyResponseStr.replaceAll("/nlc/", "/lcds/"); proxyResponseStr = proxyResponseStr.replaceAll("/lcwa/", "/lcwanew/"); //proxyResponseStr = proxyResponseStr.replaceAll("/tohap/", "/x-tohap/"); proxyResponseStr = proxyResponseStr.replaceAll(".xqy", ".jsp"); PrintWriter responseStream = hsResponse.getWriter(); copyStreamText(proxyResponseStr, responseStream); } } // log.info("set up response, result code was " + result); targetRequest.releaseConnection(); // SimpleHttpConnectionManager connManager = (SimpleHttpConnectionManager) client.getHttpConnectionManager(); // connManager.closeIdleConnections(1000); // HttpConnection httpConn = connManager.getConnection(config); // httpConn.releaseConnection(); }
From source file:org.commonjava.indy.client.core.auth.BasicAuthenticator.java
public HttpClientContext decoratePrototypeContext(final URL url, final HttpClientContext ctx) { final AuthScope as = new AuthScope(url.getHost(), url.getPort() < 0 ? url.getDefaultPort() : url.getPort()); return decoratePrototypeContext(as, null, null, ctx); }
From source file:com.macdonst.ftpclient.FtpClient.java
/** * Extracts the port of the FTP server. Returns 21 by default. * @param url//from w ww . j ava2 s . c o m * @return */ private int extractPort(URL url) { if (url.getPort() == -1) { return url.getDefaultPort(); } else { return url.getPort(); } }
From source file:opendap.threddsHandler.ThreddsCatalogUtil.java
public static String getUrlInfo(URL url) throws InterruptedException { String info = "URL:\n"; info += " getHost(): " + url.getHost() + "\n"; info += " getAuthority(): " + url.getAuthority() + "\n"; info += " getFile(): " + url.getFile() + "\n"; info += " getSystemPath(): " + url.getPath() + "\n"; info += " getDefaultPort(): " + url.getDefaultPort() + "\n"; info += " getPort(): " + url.getPort() + "\n"; info += " getProtocol(): " + url.getProtocol() + "\n"; info += " getQuery(): " + url.getQuery() + "\n"; info += " getRef(): " + url.getRef() + "\n"; info += " getUserInfo(): " + url.getUserInfo() + "\n"; return info;/*from ww w . j a v a 2 s . co m*/ }
From source file:com.woonoz.proxy.servlet.UrlRewriterImpl.java
private int getPortOrDefault(URL url) { final int port = url.getPort(); if (port == -1) { return url.getDefaultPort(); } else {/*from w w w. j av a 2s.c om*/ return port; } }