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); } }
@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 PreSignedUrlParser(String preSignedUrl) { try { URL url = new URL(preSignedUrl); this.bucket = parseBucketFromHost(url.getHost()); String path = url.getPath(); String[] pathParts = path.split("/"); if (pathParts.length < 2) { throw new IllegalArgumentException("pre-signed url " + preSignedUrl + " must point to a file within a bucket"); } if (pathParts.length > 3) { throw new IllegalArgumentException("pre-signed url " + preSignedUrl + " may only have only subdirectory under a bucket"); } if (pathParts.length > 2) { this.prefix = pathParts[1]; } } catch (MalformedURLException ex) { throw new IllegalArgumentException("pre-signed url " + preSignedUrl + " is not a valid url"); } }
/** * If the first URL we try to access with a HTTP proxy is HTTPS then the authentication cache will not have been * pre-populated, so we try to access at least one HTTP URL before the very first HTTPS url. * @param proxy * @param url the actual URL being opened. */ private void jenkins48775workaround(Proxy proxy, URL url) { if ("https".equals(url.getProtocol()) && !authCacheSeeded && proxy != Proxy.NO_PROXY) { HttpURLConnection preAuth = null; try { // We do not care if there is anything at this URL, all we care is that it is using the proxy preAuth = (HttpURLConnection) new URL("http", url.getHost(), -1, "/").openConnection(proxy); preAuth.setRequestMethod("HEAD"); preAuth.connect(); } catch (IOException e) { // ignore, this is just a probe we don't care at all } finally { if (preAuth != null) { preAuth.disconnect(); } } authCacheSeeded = true; } else if ("https".equals(url.getProtocol())){ // if we access any http url using a proxy then the auth cache will have been seeded authCacheSeeded = authCacheSeeded || proxy != Proxy.NO_PROXY; } }
private String getPoolKey(URL url) { return StringUtils.format( "%s://%s:%s", url.getProtocol(), url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort() ); } }
/** * Helper methods used for constructing an optimal stream for * parsers to use, when input is to be read from an URL. * This helps when reading file content via URL. */ protected InputStream _optimizedStreamFromURL(URL url) throws IOException { if ("file".equals(url.getProtocol())) { /* Can not do this if the path refers * to a network drive on windows. This fixes the problem; * might not be needed on all platforms (NFS?), but should not * matter a lot: performance penalty of extra wrapping is more * relevant when accessing local file system. */ String host = url.getHost(); if (host == null || host.length() == 0) { // [core#48]: Let's try to avoid probs with URL encoded stuff String path = url.getPath(); if (path.indexOf('%') < 0) { return new FileInputStream(url.getPath()); } // otherwise, let's fall through and let URL decoder do its magic } } return url.openStream(); }
PermissionCollection pc = super.getPermissions(codesource); URL u = codesource.getLocation(); if (u.getProtocol().equals("jar")) { try { if (u.getProtocol().equals("file")) { String path = u.getFile(); String host = u.getHost(); if (host != null && host.length() > 0) { path = "//" + host + path; String host = u.getHost(); if (host.length() == 0) { host = "localhost";
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(); } } }
/** * Creates a new instance * @param request the current {@link HttpServletRequest} to obtain the * {@link #getServiceUrl()} from. * @param artifactPattern the {@link Pattern} that will be used to clean up the query * string from containing the artifact name and value. This can be created using * {@link #createArtifactPattern(String)}. */ DefaultServiceAuthenticationDetails(String casService, HttpServletRequest request, Pattern artifactPattern) throws MalformedURLException { super(request); URL casServiceUrl = new URL(casService); int port = getServicePort(casServiceUrl); final String query = getQueryString(request, artifactPattern); this.serviceUrl = UrlUtils.buildFullRequestUrl(casServiceUrl.getProtocol(), casServiceUrl.getHost(), port, request.getRequestURI(), query); }
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); }
/** * Helper methods used for constructing an optimal stream for * parsers to use, when input is to be read from an URL. * This helps when reading file content via URL. */ protected InputStream _optimizedStreamFromURL(URL url) throws IOException { if ("file".equals(url.getProtocol())) { /* Can not do this if the path refers * to a network drive on windows. This fixes the problem; * might not be needed on all platforms (NFS?), but should not * matter a lot: performance penalty of extra wrapping is more * relevant when accessing local file system. */ String host = url.getHost(); if (host == null || host.length() == 0) { // [core#48]: Let's try to avoid probs with URL encoded stuff String path = url.getPath(); if (path.indexOf('%') < 0) { return new FileInputStream(url.getPath()); } // otherwise, let's fall through and let URL decoder do its magic } } return url.openStream(); }
/** {@inheritDoc} */ @Override public void connect() throws IOException { URL url = getURL(); // Gets class loader UUID. IgniteUuid ldrId = IgniteUuid.fromString(url.getHost()); // Gets resource name. String name = url.getPath(); GridDeployment dep = mgr.getDeployment(ldrId); if (dep != null) { in = dep.classLoader().getParent().getResourceAsStream(name); // If resource exists connected = true; } }
/** * 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(); }
/** * 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); } }