List of usage examples for java.net URL getProtocol
public String getProtocol()
From source file:com.amazonaws.mws.MarketplaceWebServiceOrdersClient.java
private static int extractPortNumber(String url, boolean usesHttps) { try {//from ww w . j av a 2 s.c om URL u = new URL(url); int ret = u.getPort(); if (ret == -1) { if (u.getProtocol().equalsIgnoreCase("https")) { ret = 443; } else { ret = 80; } } return ret; } catch (MalformedURLException e) { throw new RuntimeException(url + " is not a URL", e); } }
From source file:net.siegmar.japtproxy.fetcher.FetcherPool.java
/** * Initializes an fetcher based on the given targetResource. * * @param targetResource the target resource * @return a fetcher instance, or {@code null} if no fetcher * was found for the specivied targetResource *///from w w w.j a va2 s. com public Fetcher getInstance(final URL targetResource) { final String protocol = targetResource.getProtocol(); final Fetcher fetcher = fetchers.get(protocol); if (fetcher != null) { LOG.debug("Looking up fetcher for protocol '{}', found '{}'", protocol, fetcher.getClass().getName()); } else { LOG.debug("Looking up fetcher for protocol '{}', found none", protocol); } return fetcher; }
From source file:com.mashape.unirest.request.HttpRequest.java
private URL parseUrl(String s) throws Exception { URL u = new URL(s); return new URI(u.getProtocol(), u.getAuthority(), u.getPath(), u.getQuery(), u.getRef()).toURL(); }
From source file:com.none.tom.simplerssreader.net.FeedDownloader.java
@SuppressWarnings("ConstantConditions") public static InputStream getInputStream(final Context context, final String feedUrl) { final ConnectivityManager manager = context.getSystemService(ConnectivityManager.class); final NetworkInfo info = manager.getActiveNetworkInfo(); if (info == null || !info.isConnected()) { ErrorHandler.setErrno(ErrorHandler.ERROR_NETWORK); LogUtils.logError("No network connection"); return null; }// w w w . j a v a 2 s . c o m URL url; try { url = new URL(feedUrl); } catch (final MalformedURLException e) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_BAD_REQUEST, e); return null; } final boolean[] networkPrefs = DefaultSharedPrefUtils.getNetworkPreferences(context); final boolean useHttpTorProxy = networkPrefs[0]; final boolean httpsOnly = networkPrefs[1]; final boolean followRedir = networkPrefs[2]; for (int nrRedirects = 0;; nrRedirects++) { if (nrRedirects >= HTTP_NR_REDIRECTS_MAX) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_TOO_MANY_REDIRECTS); LogUtils.logError("Too many redirects"); return null; } HttpURLConnection conn = null; try { if (httpsOnly && url.getProtocol().equalsIgnoreCase("http")) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTPS_ONLY); LogUtils.logError("Attempting insecure connection"); return null; } if (useHttpTorProxy) { conn = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName(HTTP_PROXY_TOR_IP), HTTP_PROXY_TOR_PORT))); } else { conn = (HttpURLConnection) url.openConnection(); } conn.setRequestProperty("Accept-Charset", StandardCharsets.UTF_8.name()); conn.setConnectTimeout(HTTP_URL_DEFAULT_TIMEOUT); conn.setReadTimeout(HTTP_URL_DEFAULT_TIMEOUT); conn.connect(); final int responseCode = conn.getResponseCode(); switch (responseCode) { case HttpURLConnection.HTTP_OK: return getInputStream(conn.getInputStream()); case HttpURLConnection.HTTP_MOVED_PERM: case HttpURLConnection.HTTP_MOVED_TEMP: case HttpURLConnection.HTTP_SEE_OTHER: case HTTP_TEMP_REDIRECT: if (followRedir) { final String location = conn.getHeaderField("Location"); url = new URL(url, location); if (responseCode == HttpURLConnection.HTTP_MOVED_PERM) { SharedPrefUtils.updateSubscriptionUrl(context, url.toString()); } continue; } ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_FOLLOW_REDIRECT_DENIED); LogUtils.logError("Couldn't follow redirect"); return null; default: if (responseCode < 0) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_RESPONSE); LogUtils.logError("No valid HTTP response"); return null; } else if (responseCode >= 400 && responseCode <= 500) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_CLIENT); } else if (responseCode >= 500 && responseCode <= 600) { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_SERVER); } else { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_UNHANDLED); } LogUtils.logError("Error " + responseCode + ": " + conn.getResponseMessage()); return null; } } catch (final IOException e) { if ((e instanceof ConnectException && e.getMessage().equals("Network is unreachable")) || e instanceof SocketTimeoutException || e instanceof UnknownHostException) { ErrorHandler.setErrno(ErrorHandler.ERROR_NETWORK, e); } else { ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_UNHANDLED, e); } return null; } finally { if (conn != null) { conn.disconnect(); } } } }
From source file:com.thoughtworks.go.util.Log4jDirectConfigurer.java
private boolean log4JExistInClassPath(URL resource) { return resource != null && resource.getProtocol().equalsIgnoreCase("file"); }
From source file:cn.cuizuoli.appranking.http.client.TrustClientHttpRequestFactory.java
@Override protected HttpURLConnection openConnection(URL url, Proxy proxy) throws IOException { if (StringUtils.equals(url.getProtocol(), "https")) { HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory); HttpsURLConnection urlConnection = (HttpsURLConnection) (proxy != null ? url.openConnection(proxy) : url.openConnection()); urlConnection.setHostnameVerifier(DO_NOT_VERIFY); return urlConnection; } else {//from w w w.ja v a 2s . c o m HttpURLConnection urlConnection = (HttpURLConnection) (proxy != null ? url.openConnection(proxy) : url.openConnection()); return urlConnection; } }
From source file:com.digitalpebble.storm.crawler.protocol.ProtocolFactory.java
/** Returns an instance of the protocol to use for a given URL */ public synchronized Protocol getProtocol(URL url) { // get the protocol String protocol = url.getProtocol(); return cache.get(protocol); }
From source file:com.orange.oidc.tim.service.HttpOpenidConnect.java
static public HttpURLConnection getHUC(String address) { HttpURLConnection http = null; try {//from ww w .ja va 2 s . c om URL url = new URL(address); if (url.getProtocol().equalsIgnoreCase("https")) { // only use trustAllHosts and DO_NOT_VERIFY in development process trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); https.setHostnameVerifier(DO_NOT_VERIFY); http = https; } else { http = (HttpURLConnection) url.openConnection(); } } catch (Exception e) { e.printStackTrace(); } return http; }
From source file:com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.SamlConfig.java
public SamlConfig(Security security) { if (!security.getAuthn().getSaml().isEnabled()) { return;/*w w w. java 2 s . co m*/ } Saml saml = security.getAuthn().getSaml(); this.enabled = saml.isEnabled(); this.issuerId = saml.getIssuerId(); this.metadataUrl = "file:" + saml.getMetadataLocal(); if (StringUtils.isNotEmpty(saml.getMetadataRemote())) { this.metadataUrl = saml.getMetadataRemote(); } this.keyStore = "file:" + saml.getKeyStore(); this.keyStoreAliasName = saml.getKeyStoreAliasName(); this.keyStorePassword = saml.getKeyStorePassword(); URL u = saml.getServiceAddress(); this.redirectProtocol = u.getProtocol(); this.redirectHostname = u.getHost(); if (u.getPort() != -1) { this.redirectHostname += ":" + u.getPort(); } if (StringUtils.isNotEmpty(u.getPath())) { this.redirectBasePath = u.getPath(); } }
From source file:com.esri.gpt.framework.http.crawl.HttpCrawlRequest.java
private String getProtocolHostPort() throws MalformedURLException { URL u = new URL(getUrl()); return String.format("%s://%s%s", u.getProtocol(), u.getHost(), u.getPort() >= 0 ? ":" + u.getPort() : ""); }