List of usage examples for java.net URL getProtocol
public String getProtocol()
From source file:org.sonatype.nexus.proxy.maven.routing.internal.AbstractHttpRemoteStrategy.java
protected String getRemoteUrlOf(final MavenProxyRepository mavenProxyRepository) throws MalformedURLException { final String remoteRepositoryRootUrl = mavenProxyRepository.getRemoteUrl(); final URL remoteUrl = new URL(remoteRepositoryRootUrl); if (!"http".equalsIgnoreCase(remoteUrl.getProtocol()) && !"https".equalsIgnoreCase(remoteUrl.getProtocol())) { throw new MalformedURLException("URL protocol unsupported: " + remoteRepositoryRootUrl); }/*from ww w . j av a2s . c o m*/ return remoteRepositoryRootUrl; }
From source file:com.ehsy.solr.util.SimplePostTool.java
/** * Appends to the path of the URL/* w ww .j a v a 2 s .co m*/ * @param url the URL * @param append the path to append * @return the final URL version */ protected static URL appendUrlPath(URL url, String append) throws MalformedURLException { return new URL(url.getProtocol() + "://" + url.getAuthority() + url.getPath() + append + (url.getQuery() != null ? "?" + url.getQuery() : "")); }
From source file:cz.zcu.kiv.eeg.mobile.base.ws.ssl.SSLSimpleClientHttpRequestFactory.java
@Override protected HttpURLConnection openConnection(URL url, Proxy proxy) throws IOException { final HttpURLConnection httpUrlConnection = super.openConnection(url, proxy); if (url.getProtocol().toLowerCase().equals("https")) { try {//from w w w . j a v a 2 s . c om SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) { } public void checkServerTrusted(X509Certificate[] chain, String authType) { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } }, null); ((HttpsURLConnection) httpUrlConnection).setSSLSocketFactory(ctx.getSocketFactory()); ((HttpsURLConnection) httpUrlConnection).setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); } catch (Exception e) { } } return httpUrlConnection; }
From source file:com.cisco.cta.taxii.adapter.httpclient.CredentialsProviderFactory.java
private void addProxyCredentials(CredentialsProvider credsProvider) { if (proxySettings.getUrl() == null) { return;//from w w w. j a v a 2s.c om } if (proxySettings.getAuthenticationType() == null) { throw new IllegalStateException("Both proxy url and proxy authentication type have to be specified."); } ProxyAuthenticationType authType = proxySettings.getAuthenticationType(); URL proxyUrl = proxySettings.getUrl(); HttpHost proxyHost = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort(), proxyUrl.getProtocol()); Credentials credentials; switch (authType) { case NONE: break; case BASIC: credentials = new UsernamePasswordCredentials(proxySettings.getUsername(), proxySettings.getPassword()); credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), credentials); break; case NTLM: credentials = new NTCredentials(proxySettings.getUsername(), proxySettings.getPassword(), proxySettings.getWorkstation(), proxySettings.getDomain()); credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), credentials); break; default: throw new IllegalStateException("Unsupported authentication type: " + authType); } }
From source file:com.predic8.membrane.core.interceptor.rewrite.ReverseProxyingInterceptor.java
private boolean isSameSchemeHostAndPort(String location2, String location) throws MalformedURLException { try {/* w w w . jav a2 s . c o m*/ if (location.startsWith("/") || location2.startsWith("/")) return false; // no host info available URL loc2 = new URL(location2); URL loc1 = new URL(location); if (loc2.getProtocol() != null && !loc2.getProtocol().equals(loc1.getProtocol())) return false; if (loc2.getHost() != null && !loc2.getHost().equals(loc1.getHost())) return false; int loc2Port = loc2.getPort() == -1 ? loc2.getDefaultPort() : loc2.getPort(); int loc1Port = loc1.getPort() == -1 ? loc1.getDefaultPort() : loc1.getPort(); if (loc2Port != loc1Port) return false; return true; } catch (MalformedURLException e) { log.warn("", e); // TODO: fix these cases return false; } }
From source file:fx.browser.Window.java
public void setLocation(String location) throws URISyntaxException { System.out.println("# " + this.toString() + "-Classloader: " + getClass().getClassLoader().toString() + " setLocation()"); this.location = location; HttpGet httpGet = new HttpGet(new URI(location)); try (CloseableHttpResponse response = Browser.getHttpClient().execute(httpGet)) { switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: FXMLLoader loader = new FXMLLoader(); Header header = response.getFirstHeader("class-loader-url"); if (header != null) { URL url = new URL(location); url = new URL(url.getProtocol(), url.getHost(), url.getPort(), header.getValue()); if (logger.isLoggable(Level.INFO)) { logger.log(Level.INFO, "Set up remote classloader: {0}", url); }/*ww w . j a v a 2s . c om*/ loader.setClassLoader(HttpClassLoader.getInstance(url, getClass().getClassLoader())); } try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); response.getEntity().writeTo(buffer); response.close(); setContent(loader.load(new ByteArrayInputStream(buffer.toByteArray()))); } catch (Exception e) { response.close(); logger.log(Level.INFO, e.toString(), e); Node node = loader.load(getClass().getResourceAsStream("/fxml/webview.fxml")); WebViewController controller = (WebViewController) loader.getController(); controller.view(location); setContent(node); } break; case HttpStatus.SC_UNAUTHORIZED: response.close(); Optional<Pair<String, String>> result = new LoginDialog().showAndWait(); if (result.isPresent()) { URL url = new URL(location); Browser.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(result.get().getKey(), result.get().getValue())); setLocation(location); } break; } } catch (IOException e) { e.printStackTrace(); } }
From source file:com.apigee.sdk.apm.http.impl.client.cache.URIExtractor.java
public String canonicalizeUri(String uri) { try {/*from ww w. jav a2 s . c o m*/ URL u = new URL(uri); String protocol = u.getProtocol().toLowerCase(); String hostname = u.getHost().toLowerCase(); int port = canonicalizePort(u.getPort(), protocol); String path = canonicalizePath(u.getPath()); if ("".equals(path)) path = "/"; String query = u.getQuery(); String file = (query != null) ? (path + "?" + query) : path; URL out = new URL(protocol, hostname, port, file); return out.toString(); } catch (MalformedURLException e) { return uri; } }
From source file:com.chinamobile.bcbsp.util.BSPJob.java
/** * Find the BC-BSP application jar of the job. * @param myClass// w w w.java 2 s . c om * The BC-BSP application jarFileClass of the job. * @return The name of the jar. */ private static String findContainingJar(Class<?> myClass) { ClassLoader loader = myClass.getClassLoader(); String classFile = myClass.getName().replaceAll("\\.", "/") + ".class"; try { for (Enumeration<URL> itr = loader.getResources(classFile); itr.hasMoreElements();) { URL url = itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { LOG.error("[findContainingJar]", e); throw new RuntimeException(e); } return null; }
From source file:com.icesoft.faces.webapp.parser.JspPageToDocument.java
/** * @param context/*w ww. ja v a 2 s .c om*/ * @param namespaceURL * @return InputStream * @throws IOException */ public static InputStream getTldInputStream(ExternalContext context, String namespaceURL) throws IOException { // InputStream in = null; JarFile jarFile = null; String[] location = null; namespaceURL = String.valueOf(namespaceURL); // "jsp" is only a placeholder for standard JSP tags that are // not supported, so just return null if ("jsp".equals(namespaceURL)) { return null; } if ("http://java.sun.com/JSP/Page".equals(namespaceURL)) { return null; } // TldLocationsCache may fail esp. with SecurityException on SUN app server TldLocationsCache tldCache = new TldLocationsCache(context); try { location = tldCache.getLocation(namespaceURL); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(e.getMessage(), e); } } if (null == location) { if (namespaceURL.startsWith("/") && namespaceURL.endsWith(".tld")) { location = new String[] { namespaceURL }; } } if (null == location) { location = scanJars(context, namespaceURL); } if (null == location) { //look for Sun implementation URL tagURL = JspPageToDocument.class.getClassLoader().getResource(SUN_TAG_CLASS); if (null != tagURL) { // Bug 876 // Not all app servers (ie WebLogic 8.1) return // an actual JarURLConnection. URLConnection conn = tagURL.openConnection(); //ICE-3683: Special processing for JBoss 5 micro-container with VFS if (tagURL.getProtocol().equals("vfszip")) { String tagPath = tagURL.toExternalForm(); String jarPath = tagPath.substring(0, tagPath.indexOf(SUN_TAG_CLASS)); String tldPath = jarPath; if (namespaceURL.endsWith("html")) { tldPath += HTML_TLD_SUFFIX; } else if (namespaceURL.endsWith("core")) { tldPath += CORE_TLD_SUFFIX; } URL tldURL = new URL(tldPath); return tldURL.openConnection().getInputStream(); } if (conn instanceof JarURLConnection) { location = scanJar((JarURLConnection) conn, namespaceURL); } else { //OSGi-based servers (such as GlassFishv3 and WebSphere7) //do not provide JarURLConnection to their resources so //we handle the JSF TLDs as a special case. if (namespaceURL.endsWith("html")) { location = getBundleLocation(tagURL, HTML_TLD_SUFFIX); } else if (namespaceURL.endsWith("core")) { location = getBundleLocation(tagURL, CORE_TLD_SUFFIX); } } } } if (null == location) { try { // scan WebSphere dirs for JSF jars String separator = System.getProperty("path.separator"); String wsDirs = System.getProperty("ws.ext.dirs"); String[] dirs = null; if (null != wsDirs) { dirs = wsDirs.split(separator); } else { dirs = new String[] {}; } Iterator theDirs = Arrays.asList(dirs).iterator(); while (theDirs.hasNext()) { String dir = (String) theDirs.next(); try { location = scanJars(dir, namespaceURL); } catch (Exception e) { //catch all possible exceptions including runtime exception //so that the rest of jars still can be scanned. } if (null != location) { break; } } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(e.getMessage(), e); } } } if (null == location) { //look for MyFaces implementation URL tagURL = JspPageToDocument.class.getClassLoader().getResource(MYFACES_TAG_CLASS); if (null != tagURL) { URLConnection conn = tagURL.openConnection(); if (conn instanceof JarURLConnection) { location = scanJar((JarURLConnection) conn, namespaceURL); } else { //OSGi-based servers (such as GlassFishv3 and WebSphere7) //do not provide JarURLConnection to their resources so //we handle the JSF TLDs as a special case. if (namespaceURL.endsWith("html")) { location = getBundleLocation(tagURL, HTML_TLD_SUFFIX); } else if (namespaceURL.endsWith("core")) { location = getBundleLocation(tagURL, CORE_TLD_SUFFIX); } } } } if (null == location) { String msg = "Can't find TLD for location [" + namespaceURL + "]. JAR containing the TLD may not be in the classpath"; log.error(msg); return null; } else { if (log.isTraceEnabled()) { for (int i = 0; i < location.length; i++) { log.trace("Found TLD location for " + namespaceURL + " = " + location[i]); } } } if (!location[0].endsWith("jar")) { InputStream tldStream = context.getResourceAsStream(location[0]); if (null == tldStream) { tldStream = (new URL(location[0])).openConnection().getInputStream(); } return tldStream; } else { // Tag library is packaged in JAR file URL jarFileUrl = new URL("jar:" + location[0] + "!/"); JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection(); conn.setUseCaches(false); conn.connect(); jarFile = conn.getJarFile(); ZipEntry jarEntry = jarFile.getEntry(location[1]); return jarFile.getInputStream(jarEntry); } }
From source file:juzu.impl.plugin.amd.AmdMaxAgeTestCase.java
@Test @RunAsClient/* w w w. j av a2s . c o m*/ public void test() throws Exception { URL applicationURL = applicationURL(); driver.get(applicationURL.toString()); WebElement elt = driver.findElement(By.id("Foo")); URL url = new URL(applicationURL.getProtocol(), applicationURL.getHost(), applicationURL.getPort(), elt.getText()); HttpGet get = new HttpGet(url.toURI()); HttpResponse response = HttpClientBuilder.create().build().execute(get); Header[] cacheControl = response.getHeaders("Cache-Control"); assertEquals(1, cacheControl.length); assertEquals("max-age=1000", cacheControl[0].getValue()); }