Example usage for java.net URL getHost

List of usage examples for java.net URL getHost

Introduction

In this page you can find the example usage for java.net URL getHost.

Prototype

public String getHost() 

Source Link

Document

Gets the host name of this URL , if applicable.

Usage

From source file:org.apache.nutch.tools.CommonCrawlDataDumper.java

public static String reverseUrl(String urlString) {
    URL url;/*from ww  w. j av  a2 s . c o  m*/
    String reverseKey = null;
    try {
        url = new URL(urlString);

        String[] hostPart = url.getHost().replace('.', '/').split("/");

        StringBuilder sb = new StringBuilder();
        sb.append(hostPart[hostPart.length - 1]);
        for (int i = hostPart.length - 2; i >= 0; i--) {
            sb.append("/" + hostPart[i]);
        }

        reverseKey = sb.toString();

    } catch (MalformedURLException e) {
        LOG.error("Failed to parse URL: {}", urlString);
    }

    return reverseKey;
}

From source file:com.xpn.xwiki.stats.impl.StatsUtil.java

/**
 * @param context the XWiki context./*from w  w w.ja  v a2s  . c  om*/
 * @return the referer.
 * @since 1.4M1
 */
public static String getReferer(XWikiContext context) {
    String referer = context.getRequest().getHeader(REQPROP_REFERER);

    try {
        URL url = new URL(referer);
        URL baseurl = context.getURL();
        if (baseurl.getHost().equals(url.getHost())) {
            referer = null;
        }
    } catch (MalformedURLException e) {
        referer = null;
    }

    return referer;
}

From source file:ca.sqlpower.wabit.enterprise.client.ServerInfoProvider.java

private static void init(String host, String port, String path, String username, String password)
        throws IOException {

    URL serverInfoUrl = toServerInfoURL(host, port, path);
    if (version.containsKey(generateServerKey(host, port, path, username, password)))
        return;//from www  .ja va 2 s . c  o  m

    try {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 2000);
        DefaultHttpClient httpClient = new DefaultHttpClient(params);
        httpClient.setCookieStore(WabitClientSession.getCookieStore());
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(serverInfoUrl.getHost(), AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(username, password));

        HttpUriRequest request = new HttpOptions(serverInfoUrl.toURI());
        String responseBody = httpClient.execute(request, new BasicResponseHandler());

        // Decode the message
        String serverVersion;
        Boolean licensedServer;
        final String watermarkMessage;
        try {
            JSONObject jsonObject = new JSONObject(responseBody);
            serverVersion = jsonObject.getString(ServerProperties.SERVER_VERSION.toString());
            licensedServer = jsonObject.getBoolean(ServerProperties.SERVER_LICENSED.toString());
            watermarkMessage = jsonObject.getString(ServerProperties.SERVER_WATERMARK_MESSAGE.toString());
        } catch (JSONException e) {
            throw new IOException(e.getMessage());
        }

        // Save found values
        version.put(generateServerKey(host, port, path, username, password), new Version(serverVersion));
        licenses.put(generateServerKey(host, port, path, username, password), licensedServer);
        watermarkMessages.put(generateServerKey(host, port, path, username, password), watermarkMessage);

        // Notify the user if the server is not licensed.
        if (!licensedServer) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JOptionPane.showMessageDialog(null, watermarkMessage, "SQL Power Wabit Server License",
                            JOptionPane.WARNING_MESSAGE);
                }
            });
        }

        // Now get the available fonts.
        URL serverFontsURL = toServerFontsURL(host, port, path);
        HttpUriRequest fontsRequest = new HttpGet(serverFontsURL.toURI());
        String fontsResponseBody = httpClient.execute(fontsRequest, new BasicResponseHandler());
        try {
            JSONArray fontsArray = new JSONArray(fontsResponseBody);
            List<String> fontNames = new ArrayList<String>();
            for (int i = 0; i < fontsArray.length(); i++) {
                fontNames.add(fontsArray.getString(i));
            }
            // Sort the list.
            Collections.sort(fontNames);
            fonts.put(generateServerKey(host, port, path, username, password), fontNames);
        } catch (JSONException e) {
            throw new IOException(e.getMessage());
        }

    } catch (URISyntaxException e) {
        throw new IOException(e.getLocalizedMessage());
    }
}

From source file:com.arjuna.qa.junit.HttpUtils.java

public static HttpMethodBase accessURL(URL url, String realm, int expectedHttpCode, Header[] hdrs, int type)
        throws Exception {
    HttpClient httpConn = new HttpClient();
    HttpMethodBase request = createMethod(url, type);

    int hdrCount = hdrs != null ? hdrs.length : 0;
    for (int n = 0; n < hdrCount; n++)
        request.addRequestHeader(hdrs[n]);
    try {//from w  w w .ja  va 2s. co  m
        System.err.println("Connecting to: " + url);
        String userInfo = url.getUserInfo();

        if (userInfo != null) {
            UsernamePasswordCredentials auth = new UsernamePasswordCredentials(userInfo);
            httpConn.getState().setCredentials(realm, url.getHost(), auth);
        }
        System.err.println("RequestURI: " + request.getURI());
        int responseCode = httpConn.executeMethod(request);
        String response = request.getStatusText();
        System.err.println("responseCode=" + responseCode + ", response=" + response);
        String content = request.getResponseBodyAsString();
        System.err.println(content);
        // Validate that we are seeing the requested response code
        if (responseCode != expectedHttpCode) {
            throw new IOException("Expected reply code:" + expectedHttpCode + ", actual=" + responseCode);
        }
    } catch (IOException e) {
        throw e;
    }
    return request;
}

From source file:edu.stanford.muse.slant.CustomSearchHelper.java

/** returns domain from given httpurl (must start with http://)
 * removes www. from the beginning of the domain if its present
 * e.g. http://www.stanford.edu/~hangal1 returns stanford.edu
 *///from   w  w  w  . ja v  a 2 s .  co m
public static String getDomainFromURL(String httpurl) {
    String domain = null;
    try {
        URL url = new URL(httpurl);
        domain = url.getHost();
        if (domain.startsWith("www."))
            domain = domain.substring("www.".length()); // strip the "www."         

        return domain;
    } catch (MalformedURLException mfe) {
        log.warn("Bad URL: " + httpurl);
    }
    return domain;
}

From source file:fr.eolya.utils.http.HttpLoader.java

private static HttpClient setProxy(DefaultHttpClient httpClient, String url, String proxyHost, String proxyPort,
        String proxyExclude, String proxyUser, String proxyPassword) {

    boolean exclude = false;

    if (StringUtils.isNotEmpty(proxyExclude)) {
        String[] aProxyExclude = proxyExclude.split(",");
        for (int i = 0; i < aProxyExclude.length; i++)
            aProxyExclude[i] = aProxyExclude[i].trim();

        try {// w  w w .  ja v a2 s.  c  o m
            URL u = new URL(url);
            if (Arrays.asList(aProxyExclude).contains(u.getHost())) {
                exclude = true;
            }
        } catch (MalformedURLException e) {
            exclude = true;
        }
    }

    if (!exclude && StringUtils.isNotEmpty(proxyHost) && StringUtils.isNotEmpty(proxyPort)) {
        if (StringUtils.isNotEmpty(proxyUser) && StringUtils.isNotEmpty(proxyPassword)) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(proxyHost, Integer.valueOf(proxyPort)),
                    new UsernamePasswordCredentials(proxyUser, proxyPassword));
        }
        HttpHost proxy = new HttpHost(proxyHost, Integer.valueOf(proxyPort));
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    } else {
        httpClient.getParams().removeParameter(ConnRoutePNames.DEFAULT_PROXY);
    }
    return httpClient;
}

From source file:es.uvigo.ei.sing.jarvest.core.HTTPUtils.java

public synchronized static InputStream doPost(String urlstring, String queryString, String separator,
        Map<String, String> additionalHeaders, StringBuffer charsetb) throws HttpException, IOException {
    System.err.println("posting to: " + urlstring + ". query string: " + queryString);
    HashMap<String, String> query = parseQueryString(queryString, separator);
    HttpClient client = getClient();//from w w w .  j  a va2 s .c om

    URL url = new URL(urlstring);
    int port = url.getPort();
    if (port == -1) {
        if (url.getProtocol().equalsIgnoreCase("http")) {
            port = 80;
        }
        if (url.getProtocol().equalsIgnoreCase("https")) {
            port = 443;
        }
    }

    client.getHostConfiguration().setHost(url.getHost(), port, url.getProtocol());
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    final PostMethod post = new PostMethod(url.getFile());
    addHeaders(additionalHeaders, post);
    post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    post.setRequestHeader("Accept", "*/*");
    // Prepare login parameters
    NameValuePair[] valuePairs = new NameValuePair[query.size()];

    int counter = 0;

    for (String key : query.keySet()) {
        //System.out.println("Adding pair: "+key+": "+query.get(key));
        valuePairs[counter++] = new NameValuePair(key, query.get(key));

    }

    post.setRequestBody(valuePairs);
    //authpost.setRequestEntity(new StringRequestEntity(requestEntity));

    client.executeMethod(post);

    int statuscode = post.getStatusCode();
    InputStream toret = null;
    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
            || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
        Header header = post.getResponseHeader("location");
        if (header != null) {
            String newuri = header.getValue();
            if ((newuri == null) || (newuri.equals(""))) {
                newuri = "/";
            }

        } else {
            System.out.println("Invalid redirect");
            System.exit(1);
        }
    } else {
        charsetb.append(post.getResponseCharSet());
        final InputStream in = post.getResponseBodyAsStream();

        toret = new InputStream() {

            @Override
            public int read() throws IOException {
                return in.read();
            }

            @Override
            public void close() {
                post.releaseConnection();
            }

        };

    }

    return toret;
}

From source file:cdr.forms.SwordDepositHandler.java

/**
 * Generates a limited authentication scope for the supplied URL, so that an HTTP client will not send username and
 * passwords to other URLs.//  w  w w. j  a  v a  2  s . c o m
 * 
 * @param queryURL
 *           the URL for the query.
 * @return an authentication scope tuned to the requested URL.
 * @throws IllegalArgumentException
 *            if <code>queryURL</code> is not a well-formed URL.
 */
public static AuthScope getAuthenticationScope(String queryURL) {
    if (queryURL == null) {
        throw new NullPointerException("Cannot derive authentication scope for null URL");
    }
    try {
        URL url = new URL(queryURL);
        // port defaults to 80 unless the scheme is https
        // or the port is explicitly set in the URL.
        int port = 80;
        if (url.getPort() == -1) {
            if ("https".equals(url.getProtocol())) {
                port = 443;
            }
        } else {
            port = url.getPort();
        }
        return new AuthScope(url.getHost(), port);
    } catch (MalformedURLException mue) {
        throw new IllegalArgumentException("supplied URL <" + queryURL + "> is ill-formed:" + mue.getMessage());
    }
}

From source file:com.cloud.utils.SwiftUtil.java

public static URL generateTempUrl(SwiftClientCfg cfg, String container, String object, String tempKey,
        int urlExpirationInterval) {

    int currentTime = (int) (System.currentTimeMillis() / 1000L);
    int expirationSeconds = currentTime + urlExpirationInterval;

    try {/*w  w  w  . j a v a2  s.  com*/

        URL endpoint = new URL(cfg.getEndPoint());
        String method = "GET";
        String path = String.format("/v1/AUTH_%s/%s/%s", cfg.getAccount(), container, object);

        //sign the request
        String hmacBody = String.format("%s\n%d\n%s", method, expirationSeconds, path);
        String signature = calculateRFC2104HMAC(hmacBody, tempKey);
        path += String.format("?temp_url_sig=%s&temp_url_expires=%d", signature, expirationSeconds);

        //generate the temp url
        URL tempUrl = new URL(endpoint.getProtocol(), endpoint.getHost(), endpoint.getPort(), path);

        return tempUrl;

    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new CloudRuntimeException(e.getMessage());
    }

}

From source file:com.icloud.framework.http.URLUtil.java

/**
 * <p>// ww w .  j  av  a 2 s  . co  m
 * Given two urls, a src and a destination of a redirect, it returns the
 * representative url.
 * <p>
 * 
 * <p>
 * This method implements an extended version of the algorithm used by the
 * Yahoo! Slurp crawler described here:<br>
 * <a href=
 * "http://help.yahoo.com/l/nz/yahooxtra/search/webcrawler/slurp-11.html">
 * How does the Yahoo! webcrawler handle redirects?</a> <br>
 * <br>
 * <ol>
 * <li>Choose target url if either url is malformed.</li>
 * <li>If different domains the keep the destination whether or not the
 * redirect is temp or perm</li>
 * <ul>
 * <li>a.com -> b.com*</li>
 * </ul>
 * <li>If the redirect is permanent and the source is root, keep the source.
 * </li>
 * <ul>
 * <li>*a.com -> a.com?y=1 || *a.com -> a.com/xyz/index.html</li>
 * </ul>
 * <li>If the redirect is permanent and the source is not root and the
 * destination is root, keep the destination</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com*</li>
 * </ul>
 * <li>If the redirect is permanent and neither the source nor the
 * destination is root, then keep the destination</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com/abc/page.html*</li>
 * </ul>
 * <li>If the redirect is temporary and source is root and destination is
 * not root, then keep the source</li>
 * <ul>
 * <li>*a.com -> a.com/xyz/index.html</li>
 * </ul>
 * <li>If the redirect is temporary and source is not root and destination
 * is root, then keep the destination</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com*</li>
 * </ul>
 * <li>If the redirect is temporary and neither the source or the
 * destination is root, then keep the shortest url. First check for the
 * shortest host, and if both are equal then check by path. Path is first by
 * length then by the number of / path separators.</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com/abc/page.html*</li>
 * <li>*www.a.com/xyz/index.html -> www.news.a.com/xyz/index.html</li>
 * </ul>
 * <li>If the redirect is temporary and both the source and the destination
 * are root, then keep the shortest sub-domain</li>
 * <ul>
 * <li>*www.a.com -> www.news.a.com</li>
 * </ul>
 * <br>
 * While not in this logic there is a further piece of representative url
 * logic that occurs during indexing and after scoring. During creation of
 * the basic fields before indexing, if a url has a representative url
 * stored we check both the url and its representative url (which should
 * never be the same) against their linkrank scores and the highest scoring
 * one is kept as the url and the lower scoring one is held as the orig url
 * inside of the index.
 * 
 * @param src
 *            The source url.
 * @param dst
 *            The destination url.
 * @param temp
 *            Is the redirect a temporary redirect.
 * 
 * @return String The representative url.
 */
public static String chooseRepr(String src, String dst, boolean temp) {

    // validate both are well formed urls
    URL srcUrl;
    URL dstUrl;
    try {
        srcUrl = new URL(src);
        dstUrl = new URL(dst);
    } catch (MalformedURLException e) {
        return dst;
    }

    // get the source and destination domain, host, and page
    String srcDomain = URLUtil.getDomainName(srcUrl);
    String dstDomain = URLUtil.getDomainName(dstUrl);
    String srcHost = srcUrl.getHost();
    String dstHost = dstUrl.getHost();
    String srcFile = srcUrl.getFile();
    String dstFile = dstUrl.getFile();

    // are the source and destination the root path url.com/ or url.com
    boolean srcRoot = (srcFile.equals("/") || srcFile.length() == 0);
    boolean destRoot = (dstFile.equals("/") || dstFile.length() == 0);

    // 1) different domain them keep dest, temp or perm
    // a.com -> b.com*
    //
    // 2) permanent and root, keep src
    // *a.com -> a.com?y=1 || *a.com -> a.com/xyz/index.html
    //
    // 3) permanent and not root and dest root, keep dest
    // a.com/xyz/index.html -> a.com*
    //
    // 4) permanent and neither root keep dest
    // a.com/xyz/index.html -> a.com/abc/page.html*
    //
    // 5) temp and root and dest not root keep src
    // *a.com -> a.com/xyz/index.html
    //
    // 7) temp and not root and dest root keep dest
    // a.com/xyz/index.html -> a.com*
    //
    // 8) temp and neither root, keep shortest, if hosts equal by path else
    // by
    // hosts. paths are first by length then by number of / separators
    // a.com/xyz/index.html -> a.com/abc/page.html*
    // *www.a.com/xyz/index.html -> www.news.a.com/xyz/index.html
    //
    // 9) temp and both root keep shortest sub domain
    // *www.a.com -> www.news.a.com

    // if we are dealing with a redirect from one domain to another keep the
    // destination
    if (!srcDomain.equals(dstDomain)) {
        return dst;
    }

    // if it is a permanent redirect
    if (!temp) {

        // if source is root return source, otherwise destination
        if (srcRoot) {
            return src;
        } else {
            return dst;
        }
    } else { // temporary redirect

        // source root and destination not root
        if (srcRoot && !destRoot) {
            return src;
        } else if (!srcRoot && destRoot) { // destination root and source
            // not
            return dst;
        } else if (!srcRoot && !destRoot && (srcHost.equals(dstHost))) {

            // source and destination hosts are the same, check paths, host
            // length
            int numSrcPaths = srcFile.split("/").length;
            int numDstPaths = dstFile.split("/").length;
            if (numSrcPaths != numDstPaths) {
                return (numDstPaths < numSrcPaths ? dst : src);
            } else {
                int srcPathLength = srcFile.length();
                int dstPathLength = dstFile.length();
                return (dstPathLength < srcPathLength ? dst : src);
            }
        } else {

            // different host names and both root take the shortest
            int numSrcSubs = srcHost.split("\\.").length;
            int numDstSubs = dstHost.split("\\.").length;
            return (numDstSubs < numSrcSubs ? dst : src);
        }
    }
}