Example usage for java.net URI getHost

List of usage examples for java.net URI getHost

Introduction

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

Prototype

public String getHost() 

Source Link

Document

Returns the host component of this URI.

Usage

From source file:org.soyatec.windowsazure.authenticate.HttpRequestAccessor.java

/**
 * Given the host suffix part, service name and account name, this method
 * constructs the account Uri/*w  w w.j  a  v  a  2  s. c o  m*/
 * 
 * @param hostSuffix
 * @param accountName
 * @return URI
 */
private static URI constructHostStyleAccountUri(URI hostSuffix, String accountName) {
    URI serviceUri = hostSuffix;
    String hostString = hostSuffix.toString();
    if (!hostString.startsWith(HttpHost.DEFAULT_SCHEME_NAME)) {
        hostString = HttpHost.DEFAULT_SCHEME_NAME + "://" + hostString;
        serviceUri = URI.create(hostString);
    }
    // Example:
    // Input: serviceEndpoint="http://blob.windows.net/",
    // accountName="youraccount"
    // Output: accountUri="http://youraccount.blob.windows.net/"
    // serviceUri in our example would be "http://blob.windows.net/"
    String accountUriString = null;
    if (serviceUri.getPort() == -1) {
        accountUriString = MessageFormat.format("{0}{1}{2}.{3}/",
                Utilities.isNullOrEmpty(serviceUri.getScheme()) ? HttpHost.DEFAULT_SCHEME_NAME
                        : serviceUri.getScheme(),
                SCHEME_DELIMITER, accountName, serviceUri.getHost());
    } else {
        accountUriString = MessageFormat.format("{0}{1}{2}.{3}:{4}/",
                Utilities.isNullOrEmpty(serviceUri.getScheme()) ? HttpHost.DEFAULT_SCHEME_NAME
                        : serviceUri.getScheme(),
                SCHEME_DELIMITER, accountName, serviceUri.getHost(), serviceUri.getPort());
    }

    return URI.create(accountUriString);
}

From source file:googleranking.processing.GoogleData.java

private String getDomain(String url) throws URISyntaxException {
    URI uri = new URI(url);
    String domain = uri.getHost();
    return domain;
}

From source file:com.github.brandtg.pantopod.crawler.FileBasedCrawlingEventHandler.java

@Override
protected boolean shouldExplore(URI url) {
    File outputRoot = new File(outputDir, url.getHost() + File.separator + url.getPath());
    File errFile = new File(outputRoot, ERR_FILE);
    return !outputRoot.exists() && !errFile.exists();
}

From source file:com.github.brandtg.pantopod.crawler.FileBasedCrawlingEventHandler.java

@Override
protected boolean hasError(URI url) {
    File outputRoot = new File(outputDir, url.getHost() + File.separator + url.getPath());
    File errFile = new File(outputRoot, ERR_FILE);
    return errFile.exists();
}

From source file:com.mycompany.projecta.JenkinsScraper.java

public String scrape(String urlString, String username, String password)
        throws ClientProtocolException, IOException {
    URI uri = URI.create(urlString);
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(username, password));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);//  w  w w .  j av a2s  .c  om
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    HttpGet httpGet = new HttpGet(uri);
    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    HttpResponse response = httpClient.execute(host, httpGet, localContext);

    String resp = response.toString();

    return EntityUtils.toString(response.getEntity());
}

From source file:org.apache.hadoop.gateway.hbase.HBaseCookieManager.java

protected HttpRequest createKerberosAuthenticationRequest(HttpUriRequest userRequest) {
    URI userUri = userRequest.getURI();
    try {/*w  ww .j av  a 2  s.  c o m*/
        URI authUri = new URI(userUri.getScheme(), null, userUri.getHost(), userUri.getPort(), "/version",
                userUri.getQuery(), null);
        HttpRequest authRequest = new HttpGet(authUri);
        return authRequest;
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(userUri.toString(), e);
    }
}

From source file:org.camunda.bpm.ext.sdk.ClientLogger.java

public void unableToConnect(URI uri, HttpHostConnectException e) {
    logError("005", "Unable to connect to host '{}'.", uri.getHost());
}

From source file:com.masstransitproject.crosstown.EndpointAddressImpl.java

protected boolean isLocal(URI uri) {
    String hostName = uri.getHost();
    boolean local = (".".equals(hostName) || "localhost".equals(hostName)
            || LOCAL_MACHINE_NAME.equals(hostName));

    return local;
}

From source file:org.piraso.client.net.HttpBasicAuthenticationTest.java

@Test
public void testAuthenticate() throws Exception {
    AbstractHttpClient client = mock(AbstractHttpClient.class);
    HttpContext context = mock(HttpContext.class);
    CredentialsProvider credentials = mock(CredentialsProvider.class);

    doReturn(credentials).when(client).getCredentialsProvider();

    HttpBasicAuthentication auth = new HttpBasicAuthentication(client, context);
    auth.setUserName("username");
    auth.setPassword("password");

    URI uri = URI.create("http://localhost:8080/test");

    auth.setTargetHost(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()));

    auth.execute();//from   w  w w. j  a v  a2s. c o m

    verify(credentials).setCredentials(Matchers.<AuthScope>any(), Matchers.<Credentials>any());
    verify(context).setAttribute(Matchers.<String>any(), any());
}

From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java

/**
 * For Basic auth, configure the context to do pre-emptive auth with the
 * credentials given./* ww w  .  j ava2 s. c o m*/
 * @param httpContext The context in use for the requests.
 * @param serverURI The RTC server we will be authenticating against
 * @param userId The userId to login with
 * @param password The password for the User
 * @param listener A listen to log messages to, Not required
 * @throws IOException Thrown if anything goes wrong
 */
private static void handleBasicAuthChallenge(HttpClientContext httpContext, String serverURI, String userId,
        String password, TaskListener listener) throws IOException {

    URI uri;
    try {
        uri = new URI(serverURI);
    } catch (URISyntaxException e) {
        throw new IOException(Messages.HttpUtils_invalid_server(serverURI), e);
    }
    HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials(userId, password));
    httpContext.setAttribute(HttpClientContext.CREDS_PROVIDER, credsProvider);

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(target, basicAuth);

    // Add AuthCache to the execution context
    httpContext.setAuthCache(authCache);
}