List of usage examples for java.net URL getHost
public String getHost()
From source file:com.cloudbees.jenkins.plugins.bitbucket.BitbucketBuildStatusNotifications.java
/** * Check if the build URL is compatible with Bitbucket API. * For example, Bitbucket API doesn't accept simple hostnames as URLs host value * Throws an IllegalStateException if it is not valid, or return the url otherwise * * @param url the URL of the build to check * @return the url if it is valid/*ww w. j av a2s. c om*/ */ static String checkURL(@NonNull String url) { if (url.startsWith("http://unconfigured-jenkins-location/")) { throw new IllegalStateException("Could not determine Jenkins URL."); } try { URL u = new URL(url); if (!u.getHost().contains(".")) { throw new IllegalStateException( "Please use a fully qualified name or an IP address for Jenkins URL"); } } catch (MalformedURLException e) { throw new IllegalStateException("Bad Jenkins URL"); } return url; }
From source file:ee.ria.xroad.common.util.CertHashBasedOcspResponderClient.java
/** * Creates an GET request to the internal cert hash based OCSP responder and expects an OCSP responses. * * @param providerAddress URL of the OCSP response provider * @param hashes certificate hashes for which to get the responses * @return list of OCSP response objects * @throws IOException if I/O errors occurred * @throws OCSPException if the response could not be parsed *///from w w w. jav a2 s.com public static List<OCSPResp> getOcspResponsesFromServer(String providerAddress, String[] hashes) throws IOException, OCSPException { URL url = createUrl(providerAddress, hashes); log.debug("Getting OCSP responses for hashes ({}) from: {}", Arrays.toString(hashes), url.getHost()); return getOcspResponsesFromServer(url); }
From source file:URLUtils.java
/** * normalize an URL,/*from w ww.jav a2s. c o m*/ * * @param u, * the URL to normalize * @return a new URL, the normalized version of the parameter, or the u URL, * if something failed in the process */ public static URL normalize(URL u) { String proto = u.getProtocol().toLowerCase(); String host = u.getHost().toLowerCase(); int port = u.getPort(); if (port != -1) { if (url_defport != null) { try { int udp; udp = ((Integer) url_defport.invoke(u, (Object[]) null)).intValue(); // we have the default, skip the port part if (udp == port) { port = -1; } } catch (InvocationTargetException ex) { } catch (IllegalAccessException iex) { } } else { switch (port) { case 21: if (proto.equals("ftp")) { port = -1; } break; case 80: if (proto.equals("http")) { port = -1; } break; case 443: if (proto.equals("https")) { port = -1; } break; } } } try { URL _nu; if (port == -1) { _nu = new URL(proto, host, u.getFile()); } else { _nu = new URL(proto, host, port, u.getFile()); } return _nu; } catch (MalformedURLException ex) { } return u; }
From source file:org.n52.web.common.RequestUtils.java
/** * Get the request {@link URL} without the query parameter * * @return Request {@link URL} without query parameter * @throws IOException// w w w . jav a2 s .c o m * @throws URISyntaxException */ public static String resolveQueryLessRequestUrl() throws IOException, URISyntaxException { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) .getRequest(); URL url = new URL(request.getRequestURL().toString()); String scheme = url.getProtocol(); String userInfo = url.getUserInfo(); String host = url.getHost(); int port = url.getPort(); String path = request.getRequestURI(); if (path != null && path.endsWith("/")) { path = path.substring(0, path.length() - 1); } URI uri = new URI(scheme, userInfo, host, port, path, null, null); return uri.toString(); }
From source file:org.n52.web.common.RequestUtils.java
/** * Get the full request {@link URL} including the query parameter * * @return Request {@link URL} with query parameter * @throws IOException// w w w .ja v a2 s . c o m * @throws URISyntaxException */ public static String resolveFullRequestUrl() throws IOException, URISyntaxException { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) .getRequest(); URL url = new URL(request.getRequestURL().toString()); String scheme = url.getProtocol(); String userInfo = url.getUserInfo(); String host = url.getHost(); int port = url.getPort(); String path = request.getRequestURI(); if (path != null && path.endsWith("/")) { path = path.substring(0, path.length() - 1); } String query = request.getQueryString(); URI uri = new URI(scheme, userInfo, host, port, path, query, null); return uri.toString(); }
From source file:com.mingsoft.weixin.http.HttpClientConnectionManager.java
/** * ?GET??/*from ww w .j a va 2s . com*/ * * @param url * @return */ public static HttpGet getGetMethod(String url) { URI uri = null; try { URL _url = new URL(url); uri = new URI(_url.getProtocol(), _url.getHost(), _url.getPath(), _url.getQuery(), null); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpGet pmethod = new HttpGet(uri); // ?? pmethod.addHeader("Connection", "keep-alive"); pmethod.addHeader("Cache-Control", "max-age=0"); pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "); pmethod.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8"); return pmethod; }
From source file:com.mingsoft.weixin.http.HttpClientConnectionManager.java
/** * ?post??/*from w ww . ja v a 2 s.co m*/ * * @param url * @return */ public static HttpPost getPostMethod(String url) { URI uri = null; try { URL _url = new URL(url); uri = new URI(_url.getProtocol(), _url.getHost(), _url.getPath(), _url.getQuery(), null); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpPost pmethod = new HttpPost(uri); // ?? pmethod.addHeader("Connection", "keep-alive"); pmethod.addHeader("Accept", "*/*"); pmethod.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); pmethod.addHeader("Host", "mp.weixin.qq.com"); pmethod.addHeader("X-Requested-With", "XMLHttpRequest"); pmethod.addHeader("Cache-Control", "max-age=0"); pmethod.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "); return pmethod; }
From source file:de.fhg.iais.asc.xslt.binaries.download.Downloader.java
private static URI createURI(URL url) throws URISyntaxException { return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); }
From source file:com.qwazr.utils.LinkUtils.java
public final static String urlHostPathWrapReduce(String url, int maxSize) { URL u; try {// w w w . ja v a 2 s. c o m u = new URL(url); } catch (MalformedURLException e) { return url; } String path = StringUtils.fastConcat(u.getHost(), '/', u.getPath()); String[] frags = StringUtils.split(path, '/'); if (frags.length < 2) return path; int startPos = 1; int endPos = frags.length - 2; StringBuilder sbStart = new StringBuilder(frags[0]); StringBuilder sbEnd = new StringBuilder(frags[frags.length - 1]); int length = sbStart.length() + sbEnd.length(); for (;;) { boolean bHandled = false; if (startPos != -1 && startPos < endPos) { if (frags[startPos].length() + length < maxSize) { sbStart.append('/'); sbStart.append(frags[startPos++]); bHandled = true; } } if (endPos != -1 && endPos > startPos) { if (frags[endPos].length() + length < maxSize) { sbEnd.insert(0, '/'); sbEnd.insert(0, frags[endPos--]); bHandled = true; } } if (!bHandled) break; } return StringUtils.fastConcat(sbStart, "//", sbEnd); }
From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java
public static String getApplicationId(HttpServletRequest request) throws MalformedURLException { StringBuffer appID = new StringBuffer(); URL url = new URL(request.getRequestURL().toString()); appID.append(url.getProtocol()).append("://").append(url.getHost()); /*if (! (url.getProtocol().equalsIgnoreCase("http") && (url.getPort() == 80 || url.getPort() <= 0)) || ! ( url.getProtocol().equalsIgnoreCase("https") && (url.getPort() == 443 || url.getPort() <= 0)) ) { appID.append(':').append(url.getPort()); }*///from w w w. j a v a2 s.c o m if (url.getPort() > 0) { appID.append(':').append(url.getPort()); } return appID.toString(); }