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:gov.loc.ndmso.proxyfilter.RequestProxy.java

/**
 * This method performs the proxying of the request to the target address.
 *
 * @param target     The target address. Has to be a fully qualified address. The request is send as-is to this address.
 * @param hsRequest  The request data which should be send to the
 * @param hsResponse The response data which will contain the data returned by the proxied request to target.
 * @throws java.io.IOException Passed on from the connection logic.
 *//*ww  w .  j a  v  a2  s .com*/
public static void execute(final String target, final String collection, final HttpServletRequest hsRequest,
        final HttpServletResponse hsResponse, MultiThreadedHttpConnectionManager connManager)
        throws IOException {
    // log.info("execute, target is " + target);
    // log.info("response commit state: " + hsResponse.isCommitted());

    if (target == null || "".equals(target) || "".equals(target.trim())) {
        log.error("The target address is not given. Please provide a target address.");
        return;
    }

    // log.info("checking url");
    final URL url;
    try {
        url = new URL(target);
    } catch (MalformedURLException e) {
        // log.error("The provided target url is not valid.", e);
        return;
    }

    // log.info("setting up the host configuration");

    final HostConfiguration config = new HostConfiguration();

    ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy"));
    if (proxyHost != null)
        config.setProxyHost(proxyHost);

    final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
    config.setHost(url.getHost(), port, "http");

    // log.info("config is " + config.toString());

    final HttpMethod targetRequest = setupProxyRequest(hsRequest, url);
    if (targetRequest == null) {
        // log.error("Unsupported request method found: " + hsRequest.getMethod());
        return;
    }

    //perform the request to the target server
    final HttpClient client = new HttpClient(connManager);
    //if (log.isInfoEnabled()) {
    // log.info("client state" + client.getState());
    // log.info("client params" + client.getParams().toString());
    // log.info("executeMethod / fetching data ...");
    //}

    final int result = client.executeMethod(config, targetRequest);

    //copy the target response headers to our response
    setupResponseHeaders(targetRequest, hsResponse);

    String binRegex = ".*\\.(?i)(jpg|tif|png|gif|bmp|mp3|mpg)(.*$)*";
    String binRegexRedux = ".*(?i)(\\/thumb)(.*$)*";

    if (target.matches(binRegex) || target.matches(binRegexRedux)) {
        // log.info("binRegex matched: " + target);
        InputStream originalResponseStream = targetRequest.getResponseBodyAsStream();

        if (originalResponseStream != null) {

            if (targetRequest.getResponseHeaders().toString().matches("(?i).*content-type.*")) {
                PrintWriter responseStream = hsResponse.getWriter();
                copyStreamText(targetRequest.getResponseBodyAsString(), responseStream);
            } else {
                OutputStream responseStream = hsResponse.getOutputStream();
                copyStreamBinary(originalResponseStream, responseStream);
            }
        }

    } else {
        // log.info("binRegex NOT matched: " + target);
        String proxyResponseStr = targetRequest.getResponseBodyAsString();
        // the body might be null, i.e. for responses with cache-headers which leave out the body

        if (proxyResponseStr != null) {
            //proxyResponseStr = proxyResponseStr.replaceAll("xqy", "jsp");

            proxyResponseStr = proxyResponseStr.replaceAll("National Library Catalog",
                    "Library of Congress Data Service");
            proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress collections",
                    "Library of Congress bibliographic data");
            proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress Collections",
                    "Library of Congress Bibliographic Data");

            proxyResponseStr = proxyResponseStr.replaceAll("action=\"/", "action=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("href=\"/", "href=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("href=\"/diglib/loc\\.",
                    "href=\"/diglib/" + collection + "/loc.");
            proxyResponseStr = proxyResponseStr.replaceAll("src=\"/", "src=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("value=\"/", "value=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("url\\(/", "url\\(/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("url\\(\"/", "url\\(\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("src'\\) == \"/", "src'\\) == \"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("src\", \"/", "src\", \"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("natlibcat@loc.gov", "ndmso@loc.gov");

            proxyResponseStr = proxyResponseStr.replaceAll("/nlc/", "/lcds/");
            proxyResponseStr = proxyResponseStr.replaceAll("/lcwa/", "/lcwanew/");
            //proxyResponseStr = proxyResponseStr.replaceAll("/tohap/", "/x-tohap/");

            proxyResponseStr = proxyResponseStr.replaceAll(".xqy", ".jsp");

            PrintWriter responseStream = hsResponse.getWriter();
            copyStreamText(proxyResponseStr, responseStream);
        }
    }

    // log.info("set up response, result code was " + result);
    targetRequest.releaseConnection();

    // SimpleHttpConnectionManager connManager = (SimpleHttpConnectionManager) client.getHttpConnectionManager();
    // connManager.closeIdleConnections(1000);

    // HttpConnection httpConn = connManager.getConnection(config);
    // httpConn.releaseConnection();

}

From source file:com.adavr.player.media.XVClient.java

@Override
public boolean isSupported(URL url) {
    return url.getHost().endsWith("xvideos.com");
}

From source file:com.wavemaker.tools.ws.WebServiceToolsManager.java

/**
 * Constructs a service name for the given URL.
 * /*from w  w  w. j  a  v a  2  s.  c  om*/
 * @param url
 * @return
 */
private static String constructServiceName(URL url) {
    String host = url.getHost();
    int i = host.indexOf('.');
    if (i > -1) {
        String s1 = host.substring(i + 1, host.length());
        int j = s1.indexOf('.');
        String s2 = null;
        if (j > -1) {
            s2 = s1.substring(0, j);
        } else {
            s2 = host.substring(0, i);
        }
        return s2;
    }
    return host;
}

From source file:com.adito.core.RequestParameterMap.java

private static URL parseProxiedURL(String location, MultiMap map) {
    try {//www  . ja  va  2s .c  o  m
        URL proxiedURL = new URL(location);
        URL proxiedURLBase = proxiedURL;

        // Extract parameters from the proxied URL
        String query = proxiedURL.getQuery();
        if (query != null) {
            proxiedURLBase = new URL(proxiedURL.getProtocol(), proxiedURL.getHost(),
                    proxiedURL.getPort() < 1 ? -1 : proxiedURL.getPort(), proxiedURL.getPath());
            parseQuery(map, query);
        }
        return proxiedURLBase;
    } catch (MalformedURLException murle) {
        log.error("Invalid proxied URL '" + location + "'");
    }
    return null;
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

/**
 * build proxy for cloud foundry http connection
 * @param targetURL - target API URL/*from  w  ww . j  a  v  a2  s  .  c o  m*/
 * @return the full target URL
 */
private static HttpProxyConfiguration buildProxyConfiguration(URL targetURL) {
    ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy;
    if (proxyConfig == null) {
        return null;
    }

    String host = targetURL.getHost();
    for (Pattern p : proxyConfig.getNoProxyHostPatterns()) {
        if (p.matcher(host).matches()) {
            return null;
        }
    }

    return new HttpProxyConfiguration(proxyConfig.name, proxyConfig.port);
}

From source file:com.cisco.cta.taxii.adapter.httpclient.HttpClientFactory.java

public HttpClient create() {
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (proxySettings.getUrl() != null) {
        URL proxyUrl = proxySettings.getUrl();
        HttpHost proxyHost = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort(), proxyUrl.getProtocol());
        clientBuilder.setProxy(proxyHost);
    }/*  ww  w  . j  av a 2  s  .  co m*/
    return clientBuilder.build();
}

From source file:org.commonjava.indy.client.core.auth.BasicAuthenticator.java

public HttpClientContext decoratePrototypeContext(final URL url, final HttpClientContext ctx) {
    final AuthScope as = new AuthScope(url.getHost(), url.getPort() < 0 ? url.getDefaultPort() : url.getPort());
    return decoratePrototypeContext(as, null, null, ctx);
}

From source file:TextUtils.java

/**
 * Using the java API URL class, extract the http/https
 * hostname.//from  w w w .  ja  v  a2s. c  o  m
 * 
 * e.g: http://www.google.com/search will return http://www.google.com
 * 
 * @return
 */
public String getHTTPHostname(final String urlStr) {
    try {
        URL url = new URL(urlStr);
        String curHostname = url.getHost();
        String scheme = url.getProtocol();
        String fullNewURL = "";
        if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
            fullNewURL = scheme + "://" + curHostname;
            return fullNewURL;
        } else {
            throw new MalformedURLException();
        }
    } catch (MalformedURLException e) {
        return "invalid-hostname";
    }
}

From source file:br.bireme.tb.URLS.java

static URL withDomain(final URL furl, final String url) throws IOException {
    assert furl != null;
    assert url != null;

    final String url2 = url.trim();
    final URL ret;

    if (url2.startsWith("http://") || (url2.startsWith("www"))) {
        ret = new URL(url2);
    } else {//from   www  . j a va 2s .  c o m
        if (url2.charAt(0) == '/') {
            ret = new URL(furl.getProtocol() + "://" + furl.getHost() + url2);
        } else {
            final String path0 = furl.getPath();
            final String path = path0.substring(0, path0.lastIndexOf('/'));
            ret = new URL(furl.getProtocol() + "://" + furl.getHost() + path + "/" + url2);
        }
    }
    return ret;
}

From source file:de.unikassel.android.sdcframework.transmission.BasicAuthHttpProtocol.java

@Override
protected final void configureForAuthentication(DefaultHttpClient client, URL url) {
    client.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), -1),
            new UsernamePasswordCredentials(getUserName(), getMd5Password()));
}