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.allblacks.utils.web.HttpUtil.java

/**
 * Gets data from URL as byte[] throws {@link RuntimeException} If anything
 * goes wrong/* w  ww .ja va2s . c  o  m*/
 * 
 * @return The content of the URL as a byte[]
 * @throws java.io.IOException
 */
public byte[] getDataAsByteArray(String url, CredentialsProvider cp) throws IOException {
    HttpClient httpClient = getNewHttpClient();

    URL urlObj = new URL(url);
    HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());

    HttpContext credContext = new BasicHttpContext();
    credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp);

    HttpGet job = new HttpGet(url);
    HttpResponse response = httpClient.execute(host, job, credContext);

    HttpEntity entity = response.getEntity();
    return EntityUtils.toByteArray(entity);
}

From source file:com.allblacks.utils.web.HttpUtil.java

/**
 * Gets data from URL as char[] throws {@link RuntimeException} If anything
 * goes wrong/*from www .ja  v  a2  s  .  c o  m*/
 * 
 * @param url the url from which to retrieve the data.
 * @param cp the credential provider
 * 
 * @return The content of the URL as a char[]
 * @throws java.io.IOException
 */
public synchronized char[] getDataAsCharArray(String url, CredentialsProvider cp) throws IOException {
    HttpClient httpClient = getNewHttpClient();

    URL urlObj = new URL(url);
    HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());

    HttpContext credContext = new BasicHttpContext();
    credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp);

    HttpGet job = new HttpGet(url);
    HttpResponse response = httpClient.execute(host, job, credContext);

    HttpEntity entity = response.getEntity();
    return EntityUtils.toString(entity).toCharArray();
}

From source file:com.snowplowanalytics.refererparser.Parser.java

public Referer parse(URL refererUrl, String pageHost) {
    if (refererUrl == null) {
        return null;
    }//from www  .  j av  a2s.c  o  m
    return parse(refererUrl.getProtocol(), refererUrl.getHost(), refererUrl.getPath(), refererUrl.getQuery(),
            pageHost);
}

From source file:org.talend.librariesmanager.deploy.ArtifactsDeployer.java

private void installToRemote(HttpEntity entity, URL targetURL) throws BusinessException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {//from   w w  w .  j  a  v a2 s.com
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(targetURL.getHost(), targetURL.getPort()),
                new UsernamePasswordCredentials(nexusServer.getUserName(), nexusServer.getPassword()));

        HttpPut httpPut = new HttpPut(targetURL.toString());
        httpPut.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPut);
        StatusLine statusLine = response.getStatusLine();
        int responseCode = statusLine.getStatusCode();
        EntityUtils.consume(entity);
        if (responseCode > 399) {
            if (responseCode == 401) {
                throw new BusinessException("Authrity failed");
            } else {
                throw new BusinessException(
                        "Deploy failed: " + responseCode + ' ' + statusLine.getReasonPhrase());
            }
        }
    } catch (Exception e) {
        throw new BusinessException("softwareupdate.error.cannotupload", e.getMessage());
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.whispersystems.mmsmonster.mms.MmsConnection.java

protected CloseableHttpClient constructHttpClient() throws IOException {
    RequestConfig config = RequestConfig.custom().setConnectTimeout(20 * 1000)
            .setConnectionRequestTimeout(20 * 1000).setSocketTimeout(20 * 1000).setMaxRedirects(20).build();

    URL mmsc = new URL(apn.getMmsc());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    if (apn.hasAuthentication()) {
        credsProvider.setCredentials(//from  w ww. j  a v  a  2  s . c om
                new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
                new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
    }

    return HttpClients.custom().setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
            .setRedirectStrategy(new LaxRedirectStrategy()).setUserAgent("Android-Mms/2.0")
            .setConnectionManager(new BasicHttpClientConnectionManager()).setDefaultRequestConfig(config)
            .setDefaultCredentialsProvider(credsProvider).build();
}

From source file:com.conwet.wirecloud.ide.WirecloudAPI.java

public WirecloudAPI(URL deploymentServer) {
    try {/*from   w ww. j  a v a  2  s  .  co  m*/
        deploymentServer = new URL(deploymentServer.getProtocol(), deploymentServer.getHost(),
                deploymentServer.getPort(), deploymentServer.getPath());
    } catch (Exception e) {
        // Should not happen as the URL is build from a valid URL
        e.printStackTrace();
    }
    this.url = deploymentServer;
    this.AUTH_ENDPOINT = DEFAULT_AUTH_ENDPOINT;
    this.TOKEN_ENDPOINT = DEFAULT_TOKEN_ENDPOINT;
    try {
        this.UNIVERSAL_REDIRECT_URI = new URL(deploymentServer, DEFAULT_UNIVERSAL_REDIRECT_URI_PATH).toString();
    } catch (MalformedURLException e) {
        // Should not happen as the URL is build from a valid URL using a
        // constant
        e.printStackTrace();
    }
}

From source file:fr.gael.dhus.datastore.scanner.ScannerFactory.java

private String showPublicURL(URL url) {
    String protocol = url.getProtocol();
    String host = url.getHost();
    int port = url.getPort();
    String path = url.getFile();/*from   w  ww  .ja  v  a 2  s  .co  m*/
    if (protocol == null)
        protocol = "";
    else
        protocol += "://";

    String s_port = "";
    if (port != -1)
        s_port = ":" + port;

    return protocol + host + s_port + path;
}

From source file:WebCrawler.java

boolean robotSafe(URL url) {
    String strHost = url.getHost();

    // form URL of the robots.txt file
    String strRobot = "http://" + strHost + "/robots.txt";
    URL urlRobot;/*from   w  w  w .  j  ava  2s.c o  m*/
    try {
        urlRobot = new URL(strRobot);
    } catch (MalformedURLException e) {
        // something weird is happening, so don't trust it
        return false;
    }

    String strCommands;
    try {
        InputStream urlRobotStream = urlRobot.openStream();

        // read in entire file
        byte b[] = new byte[1000];
        int numRead = urlRobotStream.read(b);
        strCommands = new String(b, 0, numRead);
        while (numRead != -1) {
            if (Thread.currentThread() != searchThread)
                break;
            numRead = urlRobotStream.read(b);
            if (numRead != -1) {
                String newCommands = new String(b, 0, numRead);
                strCommands += newCommands;
            }
        }
        urlRobotStream.close();
    } catch (IOException e) {
        // if there is no robots.txt file, it is OK to search
        return true;
    }

    // assume that this robots.txt refers to us and 
    // search for "Disallow:" commands.
    String strURL = url.getFile();
    int index = 0;
    while ((index = strCommands.indexOf(DISALLOW, index)) != -1) {
        index += DISALLOW.length();
        String strPath = strCommands.substring(index);
        StringTokenizer st = new StringTokenizer(strPath);

        if (!st.hasMoreTokens())
            break;

        String strBadPath = st.nextToken();

        // if the URL starts with a disallowed path, it is not safe
        if (strURL.indexOf(strBadPath) == 0)
            return false;
    }

    return true;
}

From source file:com.stacksync.desktop.connection.plugins.swift.SwiftTransferManager.java

public void createContainer(String containerName) throws StorageException {

    try {/*from   w  ww .  j  a v  a 2 s .c  o  m*/
        connect();
        if (!client.containerExists(containerName)) {
            client.createContainer(containerName);
        }
    } catch (StorageConnectException ex) {

        String host = "";
        try {
            URL url = new URL(AUTH_URL);
            host = url.getHost();
        } catch (MalformedURLException ex1) {
            logger.warn("Incorrect URL: ", ex1);
        }

        // This error is from connect() function
        // It is necessary to differenciate between unauthorize, no connection or others...
        String message = ex.getCause().getMessage();
        if (message.contains("Incorrect")) {
            // Unauthorize
            throw new StorageUnauthorizeException("Incorrect user or password.");
        } else if (message.contains("not known") || message.contains("unreachable") || message.contains(host)) {
            // Network problems
            throw new StorageConnectException("Network problems.");
        } else {
            // Unknown problems...
            throw new StorageException(ex);
        }

    } catch (Exception ex) {
        throw new StorageException(ex);
    }

}

From source file:net.ychron.unirestinst.http.HttpClientHelper.java

private HttpRequestBase prepareRequest(HttpRequest request, boolean async) {

    Object defaultHeaders = options.getOption(Option.DEFAULT_HEADERS);
    if (defaultHeaders != null) {
        @SuppressWarnings("unchecked")
        Set<Entry<String, String>> entrySet = ((Map<String, String>) defaultHeaders).entrySet();
        for (Entry<String, String> entry : entrySet) {
            request.header(entry.getKey(), entry.getValue());
        }/*  w w w  .java  2s  . c  om*/
    }

    if (!request.getHeaders().containsKey(USER_AGENT_HEADER)) {
        request.header(USER_AGENT_HEADER, USER_AGENT);
    }
    if (!request.getHeaders().containsKey(ACCEPT_ENCODING_HEADER)) {
        request.header(ACCEPT_ENCODING_HEADER, "gzip");
    }

    HttpRequestBase reqObj = null;

    String urlToRequest = null;
    try {
        URL url = new URL(request.getUrl());
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(),
                URLDecoder.decode(url.getPath(), "UTF-8"), "", url.getRef());
        urlToRequest = uri.toURL().toString();
        if (url.getQuery() != null && !url.getQuery().trim().equals("")) {
            if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) {
                urlToRequest += "?";
            }
            urlToRequest += url.getQuery();
        } else if (urlToRequest.substring(urlToRequest.length() - 1).equals("?")) {
            urlToRequest = urlToRequest.substring(0, urlToRequest.length() - 1);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    switch (request.getHttpMethod()) {
    case GET:
        reqObj = new HttpGet(urlToRequest);
        break;
    case POST:
        reqObj = new HttpPost(urlToRequest);
        break;
    case PUT:
        reqObj = new HttpPut(urlToRequest);
        break;
    case DELETE:
        reqObj = new HttpDeleteWithBody(urlToRequest);
        break;
    case PATCH:
        reqObj = new HttpPatchWithBody(urlToRequest);
        break;
    case OPTIONS:
        reqObj = new HttpOptions(urlToRequest);
        break;
    case HEAD:
        reqObj = new HttpHead(urlToRequest);
        break;
    }

    Set<Entry<String, List<String>>> entrySet = request.getHeaders().entrySet();
    for (Entry<String, List<String>> entry : entrySet) {
        List<String> values = entry.getValue();
        if (values != null) {
            for (String value : values) {
                reqObj.addHeader(entry.getKey(), value);
            }
        }
    }

    // Set body
    if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) {
        if (request.getBody() != null) {
            HttpEntity entity = request.getBody().getEntity();
            if (async) {
                if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) {
                    reqObj.setHeader(entity.getContentType());
                }
                try {
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
                    entity.writeTo(output);
                    NByteArrayEntity en = new NByteArrayEntity(output.toByteArray());
                    ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity);
            }
        }
    }

    return reqObj;
}