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:com.spectralogic.ds3client.NetworkClientImpl.java

private static HttpHost buildHost(final ConnectionDetails connectionDetails) throws MalformedURLException {
    final URI proxyUri = connectionDetails.getProxy();
    if (proxyUri != null) {
        return new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme());
    } else {//from   w  w  w . ja v a 2s.  c om
        final URL url = NetUtils.buildUrl(connectionDetails, "/");
        return new HttpHost(url.getHost(), NetUtils.getPort(url), url.getProtocol());
    }
}

From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java

private static long getLastModified(URL url) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {//www  .  jav  a2 s.  c o  m
        HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        Builder builder = RequestConfig.custom();
        HttpHost proxy = getProxy(target);
        if (proxy != null) {
            builder = builder.setProxy(proxy);
        }
        RequestConfig config = builder.build();
        HttpHead request = new HttpHead(url.toURI());
        request.setConfig(config);
        response = httpclient.execute(target, request);
        Header[] s = response.getHeaders("last-modified");
        if (s != null && s.length > 0) {
            String lastModified = s[0].getValue();
            return new Date(lastModified).getTime();
        }
    } catch (Exception e) {
        logWarning(e);
        return -1;
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        try {
            httpclient.close();
        } catch (IOException e) {
        }
    }
    return -1;
}

From source file:net.daporkchop.porkselfbot.util.HTTPUtils.java

/**
 * Concatenates the given {@link java.net.URL} and query.
 *
 * @param url URL to base off/*w  w  w  . ja  v  a  2 s . c  o  m*/
 * @param query Query to append to URL
 * @return URL constructed
 */
public static URL concatenateURL(final URL url, final String query) {
    try {
        if (url.getQuery() != null && url.getQuery().length() > 0) {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "&" + query);
        } else {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + query);
        }
    } catch (final MalformedURLException ex) {
        throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
    }
}

From source file:com.doculibre.constellio.utils.ConnectorManagerRequestUtils.java

public static Element sendGet(ConnectorManager connectorManager, String servletPath,
        Map<String, String> paramsMap) {
    if (paramsMap == null) {
        paramsMap = new HashMap<String, String>();
    }/*from   w w w. ja  v  a  2 s . co m*/

    try {
        HttpParams params = new BasicHttpParams();
        for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) {
            String paramName = (String) it.next();
            String paramValue = (String) paramsMap.get(paramName);
            params.setParameter(paramName, paramValue);
        }

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
        HttpProtocolParams.setUseExpectContinue(params, true);

        BasicHttpProcessor httpproc = new BasicHttpProcessor();
        // Required protocol interceptors
        httpproc.addInterceptor(new RequestContent());
        httpproc.addInterceptor(new RequestTargetHost());
        // Recommended protocol interceptors
        httpproc.addInterceptor(new RequestConnControl());
        httpproc.addInterceptor(new RequestUserAgent());
        httpproc.addInterceptor(new RequestExpectContinue());

        HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

        HttpContext context = new BasicHttpContext(null);
        URL connectorManagerURL = new URL(connectorManager.getUrl());
        HttpHost host = new HttpHost(connectorManagerURL.getHost(), connectorManagerURL.getPort());

        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

        try {
            String target = connectorManager.getUrl() + servletPath;
            boolean firstParam = true;
            for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) {
                String paramName = (String) it.next();
                String paramValue = (String) paramsMap.get(paramName);

                if (firstParam) {
                    target += "?";
                    firstParam = false;
                } else {
                    target += "&";
                }
                target += paramName + "=" + paramValue;
            }

            if (!conn.isOpen()) {
                Socket socket = new Socket(host.getHostName(), host.getPort());
                conn.bind(socket, params);
            }
            BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("GET", target);
            LOGGER.fine(">> Request URI: " + request.getRequestLine().getUri());

            request.setParams(params);
            httpexecutor.preProcess(request, httpproc, context);
            HttpResponse response = httpexecutor.execute(request, conn, context);
            response.setParams(params);
            httpexecutor.postProcess(response, httpproc, context);

            LOGGER.fine("<< Response: " + response.getStatusLine());
            String entityText = EntityUtils.toString(response.getEntity());
            LOGGER.fine(entityText);
            LOGGER.fine("==============");
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else {
                LOGGER.fine("Connection kept alive...");
            }

            try {
                Document xml = DocumentHelper.parseText(entityText);
                return xml.getRootElement();
            } catch (Exception e) {
                LOGGER.severe("Error caused by text : " + entityText);
                throw e;
            }
        } finally {
            conn.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.trentorise.smartcampus.protocolcarrier.Communicator.java

private static HttpRequestBase buildRequest(MessageRequest msgRequest, String appToken, String authToken)
        throws URISyntaxException, UnsupportedEncodingException {
    String host = msgRequest.getTargetHost();
    if (host == null)
        throw new URISyntaxException(host, "null URI");
    if (!host.endsWith("/"))
        host += '/';
    String address = msgRequest.getTargetAddress();
    if (address == null)
        address = "";
    if (address.startsWith("/"))
        address = address.substring(1);//  w  w w  . j  a va 2 s . c  om
    String uriString = host + address;
    try {
        URL url = new URL(uriString);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        uriString = uri.toURL().toString();
    } catch (MalformedURLException e) {
        throw new URISyntaxException(uriString, e.getMessage());
    }
    if (msgRequest.getQuery() != null)
        uriString += "?" + msgRequest.getQuery();

    //      new URI(uriString);

    HttpRequestBase request = null;
    if (msgRequest.getMethod().equals(Method.POST)) {
        HttpPost post = new HttpPost(uriString);
        HttpEntity httpEntity = null;
        if (msgRequest.getRequestParams() != null) {
            // if body and requestparams are either not null there is an
            // exception
            if (msgRequest.getBody() != null && msgRequest != null) {
                throw new IllegalArgumentException("body and requestParams cannot be either populated");
            }
            httpEntity = new MultipartEntity();

            for (RequestParam param : msgRequest.getRequestParams()) {
                if (param.getParamName() == null || param.getParamName().trim().length() == 0) {
                    throw new IllegalArgumentException("paramName cannot be null or empty");
                }
                if (param instanceof FileRequestParam) {
                    FileRequestParam fileparam = (FileRequestParam) param;
                    ((MultipartEntity) httpEntity).addPart(param.getParamName(), new ByteArrayBody(
                            fileparam.getContent(), fileparam.getContentType(), fileparam.getFilename()));
                }
                if (param instanceof ObjectRequestParam) {
                    ObjectRequestParam objectparam = (ObjectRequestParam) param;
                    ((MultipartEntity) httpEntity).addPart(param.getParamName(),
                            new StringBody(convertObject(objectparam.getVars())));
                }
            }
            // mpe.addPart("file",
            // new ByteArrayBody(msgRequest.getFileContent(), ""));
            // post.setEntity(mpe);
        }
        if (msgRequest.getBody() != null) {
            httpEntity = new StringEntity(msgRequest.getBody(), Constants.CHARSET);
            ((StringEntity) httpEntity).setContentType(msgRequest.getContentType());
        }
        post.setEntity(httpEntity);
        request = post;
    } else if (msgRequest.getMethod().equals(Method.PUT)) {
        HttpPut put = new HttpPut(uriString);
        if (msgRequest.getBody() != null) {
            StringEntity se = new StringEntity(msgRequest.getBody(), Constants.CHARSET);
            se.setContentType(msgRequest.getContentType());
            put.setEntity(se);
        }
        request = put;
    } else if (msgRequest.getMethod().equals(Method.DELETE)) {
        request = new HttpDelete(uriString);
    } else {
        // default: GET
        request = new HttpGet(uriString);
    }

    Map<String, String> headers = new HashMap<String, String>();

    // default headers
    if (appToken != null) {
        headers.put(RequestHeader.APP_TOKEN.toString(), appToken);
    }
    if (authToken != null) {
        // is here for compatibility
        headers.put(RequestHeader.AUTH_TOKEN.toString(), authToken);
        headers.put(RequestHeader.AUTHORIZATION.toString(), "Bearer " + authToken);
    }
    headers.put(RequestHeader.ACCEPT.toString(), msgRequest.getContentType());

    if (msgRequest.getCustomHeaders() != null) {
        headers.putAll(msgRequest.getCustomHeaders());
    }

    for (String key : headers.keySet()) {
        request.addHeader(key, headers.get(key));
    }

    return request;
}

From source file:org.commonjava.redhat.maven.rv.util.InputUtils.java

public static File getFile(final String location, final File downloadsDir, final boolean deleteExisting)
        throws ValidationException {
    if (client == null) {
        final DefaultHttpClient hc = new DefaultHttpClient();
        hc.setRedirectStrategy(new DefaultRedirectStrategy());

        final String proxyHost = System.getProperty("http.proxyHost");
        final int proxyPort = Integer.parseInt(System.getProperty("http.proxyPort", "-1"));

        if (proxyHost != null && proxyPort > 0) {
            final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            hc.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
        }//from  ww  w.ja v  a2  s .  c om

        client = hc;
    }

    File result = null;

    if (location.startsWith("http")) {
        LOGGER.info("Downloading: '" + location + "'...");

        try {
            final URL url = new URL(location);
            final String userpass = url.getUserInfo();
            if (!isEmpty(userpass)) {
                final AuthScope scope = new AuthScope(url.getHost(), url.getPort());
                final Credentials creds = new UsernamePasswordCredentials(userpass);

                client.getCredentialsProvider().setCredentials(scope, creds);
            }
        } catch (final MalformedURLException e) {
            LOGGER.error("Malformed URL: '" + location + "'", e);
            throw new ValidationException("Failed to download: %s. Reason: %s", e, location, e.getMessage());
        }

        final File downloaded = new File(downloadsDir, new File(location).getName());
        if (deleteExisting && downloaded.exists()) {
            downloaded.delete();
        }

        if (!downloaded.exists()) {
            HttpGet get = new HttpGet(location);
            OutputStream out = null;
            try {
                HttpResponse response = client.execute(get);
                // Work around for scenario where we are loading from a server
                // that does a refresh e.g. gitweb
                if (response.containsHeader("Cache-control")) {
                    LOGGER.info("Waiting for server to generate cache...");
                    try {
                        Thread.sleep(5000);
                    } catch (final InterruptedException e) {
                    }
                    get.abort();
                    get = new HttpGet(location);
                    response = client.execute(get);
                }
                final int code = response.getStatusLine().getStatusCode();
                if (code == 200) {
                    final InputStream in = response.getEntity().getContent();
                    out = new FileOutputStream(downloaded);

                    copy(in, out);
                } else {
                    LOGGER.info(String.format("Received status: '%s' while downloading: %s",
                            response.getStatusLine(), location));

                    throw new ValidationException("Received status: '%s' while downloading: %s",
                            response.getStatusLine(), location);
                }
            } catch (final ClientProtocolException e) {
                throw new ValidationException("Failed to download: '%s'. Error: %s", e, location,
                        e.getMessage());
            } catch (final IOException e) {
                throw new ValidationException("Failed to download: '%s'. Error: %s", e, location,
                        e.getMessage());
            } finally {
                closeQuietly(out);
                get.abort();
            }
        }

        result = downloaded;
    } else {
        LOGGER.info("Using local file: '" + location + "'...");

        result = new File(location);
    }

    return result;
}

From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java

public static Element sendGet(String openSearchServerURLStr, Map<String, String> paramsMap) {
    if (paramsMap == null) {
        paramsMap = new HashMap<String, String>();
    }/*from  ww  w. j a  v a  2s.  c  o m*/

    try {
        HttpParams params = new BasicHttpParams();
        for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) {
            String paramName = (String) it.next();
            String paramValue = (String) paramsMap.get(paramName);
            params.setParameter(paramName, paramValue);
        }

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, CharSetUtils.UTF_8);
        HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
        HttpProtocolParams.setUseExpectContinue(params, true);

        BasicHttpProcessor httpproc = new BasicHttpProcessor();
        // Required protocol interceptors
        httpproc.addInterceptor(new RequestContent());
        httpproc.addInterceptor(new RequestTargetHost());
        // Recommended protocol interceptors
        httpproc.addInterceptor(new RequestConnControl());
        httpproc.addInterceptor(new RequestUserAgent());
        httpproc.addInterceptor(new RequestExpectContinue());

        HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

        HttpContext context = new BasicHttpContext(null);
        URL openSearchServerURL = new URL(openSearchServerURLStr);
        String host = openSearchServerURL.getHost();
        int port = openSearchServerURL.getPort();
        if (port == -1) {
            port = 80;
        }
        HttpHost httpHost = new HttpHost(host, port);

        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, httpHost);

        try {
            boolean firstParam = true;
            for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) {
                String paramName = (String) it.next();
                String paramValue = (String) paramsMap.get(paramName);
                if (paramValue != null) {
                    try {
                        paramValue = URLEncoder.encode(paramValue, CharSetUtils.ISO_8859_1);
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException(e);
                    }
                }

                if (firstParam) {
                    openSearchServerURLStr += "?";
                    firstParam = false;
                } else {
                    openSearchServerURLStr += "&";
                }
                openSearchServerURLStr += paramName + "=" + paramValue;
            }

            if (!conn.isOpen()) {
                Socket socket = new Socket(host, port);
                conn.bind(socket, params);
            }
            BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("GET",
                    openSearchServerURLStr);
            LOGGER.fine(">> Request URI: " + request.getRequestLine().getUri());

            request.setParams(params);
            httpexecutor.preProcess(request, httpproc, context);
            HttpResponse response = httpexecutor.execute(request, conn, context);
            response.setParams(params);
            httpexecutor.postProcess(response, httpproc, context);

            LOGGER.fine("<< Response: " + response.getStatusLine());
            String entityText = EntityUtils.toString(response.getEntity());
            LOGGER.fine(entityText);
            LOGGER.fine("==============");
            if (!connStrategy.keepAlive(response, context)) {
                conn.close();
            } else {
                LOGGER.fine("Connection kept alive...");
            }

            try {
                Document xml = DocumentHelper.parseText(entityText);
                return xml.getRootElement();
            } catch (RuntimeException e) {
                LOGGER.severe("Error caused by text : " + entityText);
                throw e;
            }
        } finally {
            conn.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.base.httpclient.HttpJsonClient.java

/**
 * get//  w  w  w  .  j av  a  2 s.  com
 * 
 * @param url
 *            URL
 * @param params
 *            getkey-value
 * @return JSON
 * @throws ClientProtocolException
 * @throws IOException
 * @throws URISyntaxException 
 */
public static String get(String url, Map<String, ?> params)
        throws ClientProtocolException, IOException, URISyntaxException {
    DefaultHttpClient httpclient = getHttpClient();
    try {
        if (params != null && !(params.isEmpty())) {
            List<NameValuePair> values = new ArrayList<NameValuePair>();
            for (Map.Entry<String, ?> entity : params.entrySet()) {
                BasicNameValuePair pare = new BasicNameValuePair(entity.getKey(), entity.getValue().toString());
                values.add(pare);

            }
            String str = URLEncodedUtils.format(values, "UTF-8");
            if (url.indexOf("?") > -1) {
                url += "&" + str;
            } else {
                url += "?" + str;
            }
        }
        URL pageURL = new URL(url);
        //pageUrl?httpget
        URI uri = new URI(pageURL.getProtocol(), pageURL.getHost(), pageURL.getPath(), pageURL.getQuery(),
                null);
        HttpGet httpget = createHttpUriRequest(HttpMethod.GET, uri);
        httpget.setHeader("Pragma", "no-cache");
        httpget.setHeader("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate");
        httpget.setHeader("Connection", "keep-alive");
        httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
        httpget.setHeader("Accept-Charset", "gbk,utf-8;q=0.7,*;q=0.7");
        httpget.setHeader("Referer", url);
        /*httpget.setHeader("Content-Encoding", "gzip");
        httpget.setHeader("Accept-Encoding", "gzip, deflate");*/
        //httpget.setHeader("Host", "s.taobao.com");
        /*int temp = Integer.parseInt(Math.round(Math.random()*(MingSpiderService.UserAgent.length-1))+"");//???
        httpget.setHeader("User-Agent", MingSpiderService.UserAgent[temp]);*/
        HttpResponse response = httpclient.execute(httpget);
        httpclient.getCookieStore().clear();
        int resStatu = response.getStatusLine().getStatusCode();
        String html = "";
        if (resStatu == HttpStatus.SC_OK) {//200 
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                html = EntityUtils.toString(entity, "GBK");
                EntityUtils.consume(entity);
            }
        }
        return html;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.dmsl.anyplace.utils.NetworkUtils.java

public static String encodeURL(String urlStr) throws URISyntaxException, MalformedURLException {
    URL url = new URL(urlStr);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());
    url = uri.toURL();/*from  w w  w  . j  a v  a 2  s .com*/
    return url.toString();
}

From source file:com.amazon.speech.speechlet.authentication.SpeechletRequestSignatureVerifier.java

/**
 * Verifies the signing certificate chain URL and returns a {@code URL} object.
 *
 * @param signingCertificateChainUrl//from   ww w.  j a  v a 2  s  .c  o  m
 *            the external form of the URL
 * @return the URL
 * @throws CertificateException
 *             if the URL is malformed or contains an invalid hostname, an unsupported protocol,
 *             or an invalid port (if specified)
 */
static URL getAndVerifySigningCertificateChainUrl(final String signingCertificateChainUrl)
        throws CertificateException {
    try {
        URL url = new URI(signingCertificateChainUrl).normalize().toURL();
        // Validate the hostname
        if (!VALID_SIGNING_CERT_CHAIN_URL_HOST_NAME.equalsIgnoreCase(url.getHost())) {
            throw new CertificateException(String.format(
                    "SigningCertificateChainUrl [%s] does not contain the required hostname" + " of [%s]",
                    signingCertificateChainUrl, VALID_SIGNING_CERT_CHAIN_URL_HOST_NAME));
        }

        // Validate the path prefix
        String path = url.getPath();
        if (!path.startsWith(VALID_SIGNING_CERT_CHAING_URL_PATH_PREFIX)) {
            throw new CertificateException(String.format(
                    "SigningCertificateChainUrl path [%s] is invalid. Expecting path to " + "start with [%s]",
                    signingCertificateChainUrl, VALID_SIGNING_CERT_CHAING_URL_PATH_PREFIX));
        }

        // Validate the protocol
        String urlProtocol = url.getProtocol();
        if (!VALID_SIGNING_CERT_CHAIN_PROTOCOL.equalsIgnoreCase(urlProtocol)) {
            throw new CertificateException(
                    String.format("SigningCertificateChainUrl [%s] contains an unsupported protocol [%s]",
                            signingCertificateChainUrl, urlProtocol));
        }

        // Validate the port uses the default of 443 for HTTPS if explicitly defined in the URL
        int urlPort = url.getPort();
        if ((urlPort != UNSPECIFIED_SIGNING_CERT_CHAIN_URL_PORT_VALUE) && (urlPort != url.getDefaultPort())) {
            throw new CertificateException(
                    String.format("SigningCertificateChainUrl [%s] contains an invalid port [%d]",
                            signingCertificateChainUrl, urlPort));
        }

        return url;
    } catch (IllegalArgumentException | MalformedURLException | URISyntaxException ex) {
        throw new CertificateException(
                String.format("SigningCertificateChainUrl [%s] is malformed", signingCertificateChainUrl), ex);
    }
}