public DefaultEndpoint(String serviceUrl) { this.serviceUrl = serviceUrl; try { URL url = new URL(serviceUrl); this.networkAddress = url.getHost(); this.port = url.getPort(); this.isSecure = "https".equals(url.getProtocol()); this.relativeUri = url.getPath(); } catch (Exception e) { throw new IllegalArgumentException("Malformed serviceUrl: " + serviceUrl); } }
private String getPoolKey(URL url) { return StringUtils.format( "%s://%s:%s", url.getProtocol(), url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort() ); } }
@Override public boolean matches(WebRequest request) { URL url = request.getUrl(); String host = url.getHost(); if (this.hosts.contains(host)) { return true; } int port = url.getPort(); if (port == -1) { port = url.getDefaultPort(); } String hostAndPort = host + ":" + port; return this.hosts.contains(hostAndPort); }
/** * Method to validate if the given String represents a hostname:port. * * <p>Works also for ipv6. * * <p>See: http://stackoverflow.com/questions/2345063/java-common-way-to-validate-and-convert-hostport-to-inetsocketaddress * * @return URL object for accessing host and Port */ public static URL getCorrectHostnamePort(String hostPort) { try { URL u = new URL("http://" + hostPort); if (u.getHost() == null) { throw new IllegalArgumentException("The given host:port ('" + hostPort + "') doesn't contain a valid host"); } if (u.getPort() == -1) { throw new IllegalArgumentException("The given host:port ('" + hostPort + "') doesn't contain a valid port"); } return u; } catch (MalformedURLException e) { throw new IllegalArgumentException("The given host:port ('" + hostPort + "') is invalid", e); } }
public static String getClientIp(String serviceUrl) { try { URL url = new URL(serviceUrl); int port = url.getPort(); if (port == -1) { port = url.getDefaultPort(); } try (Socket socket = new Socket(url.getHost(), port)) { return socket.getLocalAddress().getHostAddress(); } } catch (Exception e) { return SystemUtil.getFirstLocalNonLoopbackIpAddress(); } } }
@Override public Permission getPermission() throws IOException { URL url = getURL(); String hostname = url.getHost(); int hostPort = url.getPort() != -1 ? url.getPort() : HttpUrl.defaultPort(url.getProtocol()); if (usingProxy()) { InetSocketAddress proxyAddress = (InetSocketAddress) client.proxy().address(); hostname = proxyAddress.getHostName(); hostPort = proxyAddress.getPort(); } return new SocketPermission(hostname + ":" + hostPort, "connect, resolve"); }
/** * Constructs an object importer. * * <p>Remote objects are imported from the web server that the given * applet has been loaded from. * * @param applet the applet loaded from the <code>Webserver</code>. */ public ObjectImporter(Applet applet) { URL codebase = applet.getCodeBase(); orgServername = servername = codebase.getHost(); orgPort = port = codebase.getPort(); }
private String getRootUrl(String string) { try { final URL url = new URL(string); return new URL(url.getProtocol(), url.getHost(), url.getPort(), "").toString(); } catch (MalformedURLException e) { throw new RuntimeException(e); } }
private HttpResponse execute(HttpUriRequest hr, DefaultHttpClient client, HttpContext context) throws ClientProtocolException, IOException{ HttpResponse response = null; if(hr.getURI().getAuthority().contains("_")) { URL urlObj = hr.getURI().toURL(); HttpHost host; if(urlObj.getPort() == -1) { host = new HttpHost(urlObj.getHost(), 80, urlObj.getProtocol()); } else { host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol()); } response = client.execute(host, hr, context); } else { response = client.execute(hr, context); } return response; }
private CookieOrigin cookieOriginFromUri(String uri) { try { URL parsedUrl = new URL(uri); int port = parsedUrl.getPort() != -1 ? parsedUrl.getPort() : 80; return new CookieOrigin( parsedUrl.getHost(), port, parsedUrl.getPath(), "https".equals(parsedUrl.getProtocol())); } catch (MalformedURLException e) { throw new IllegalArgumentException(e); } } }
private String getHost(URL url) { int port = url.getPort(); if (port == -1) { final String protocol = url.getProtocol(); if ("http".equalsIgnoreCase(protocol)) { port = 80; } else if ("https".equalsIgnoreCase(protocol)) { port = 443; } else { throw new IAE("Cannot figure out default port for protocol[%s], please set Host header.", protocol); } } return StringUtils.format("%s:%s", url.getHost(), port); }
/** * Re-establish broker connection using the next available seed broker address. */ private void useNextAddressAsNewContactSeedBroker() { if (++currentContactSeedBrokerIndex == seedBrokerAddresses.length) { currentContactSeedBrokerIndex = 0; } URL newContactUrl = NetUtils.getCorrectHostnamePort(seedBrokerAddresses[currentContactSeedBrokerIndex]); this.consumer = new SimpleConsumer(newContactUrl.getHost(), newContactUrl.getPort(), soTimeout, bufferSize, dummyClientId); }
/** * Appends a trailing '/' to a {@link URL} object. Does not append a slash if one is already present. * * @param originalURL The URL to append a slash to * @return a new URL object that ends in a slash */ public static URL appendTrailingSlash(URL originalURL) { try { return originalURL.getPath().endsWith("/") ? originalURL : new URL(originalURL.getProtocol(), originalURL.getHost(), originalURL.getPort(), originalURL.getFile() + '/'); } catch (MalformedURLException ignored) { // shouldn't happen throw new IllegalArgumentException("Invalid resource URL: " + originalURL); } }