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:be.fedict.eid.idp.model.bean.AccountingServiceBean.java

private String normalize(String domain) {
    URI uri;
    try {/*from   ww  w.ja  va 2  s.  c o  m*/
        uri = new URI(domain);
    } catch (URISyntaxException e) {
        return domain;
    }
    String scheme = uri.getScheme();
    if ("http".equals(scheme) || "https".equals(scheme)) {
        return uri.getScheme() + "://" + uri.getHost() + uri.getPath();
    }
    return domain;
}

From source file:com.jgoetsch.eventtrader.source.SocketIOWebSocketMsgSource.java

protected String getTokenUrl() throws IOException, URISyntaxException {
    URI base = new URI(getUrl());
    BufferedReader tokenReader = new BufferedReader(
            new InputStreamReader(new URI("http", base.getUserInfo(), base.getHost(), base.getPort(),
                    base.getPath(), base.getQuery(), base.getFragment()).toURL().openStream()));
    String token = tokenReader.readLine();
    tokenReader.close();//  ww  w. j  ava2 s  .c  om

    String r[] = token.split(":");
    String comp[] = getUrl().split("\\?");
    if (!comp[0].endsWith("/"))
        comp[0] += '/';
    return comp[0] + "websocket/" + r[0] + (comp.length > 0 ? "?" + comp[1] : "");
}

From source file:com.netscape.certsrv.client.PKIClient.java

public <T> T createProxy(String subsystem, Class<T> clazz) throws URISyntaxException {

    if (subsystem == null) {
        // by default use the subsystem specified in server URI
        subsystem = getSubsystem();// w ww . ja va2 s  .com
    }

    if (subsystem == null) {
        throw new PKIException("Missing subsystem name.");
    }

    URI serverURI = config.getServerURI();
    URI resourceURI = new URI(serverURI.getScheme(), serverURI.getUserInfo(), serverURI.getHost(),
            serverURI.getPort(), "/" + subsystem + "/rest", serverURI.getQuery(), serverURI.getFragment());

    return connection.createProxy(resourceURI, clazz);
}

From source file:com.subgraph.vega.impl.scanner.urls.ResponseAnalyzer.java

private URI stripQuery(URI uri) {
    try {//w w w .  j  a  v a  2s  .  c o m
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null,
                null);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade.java

private void appendAccessTokenToQuery(HttpRequestBase request, OAuthBearerClientRequest oAuthClientRequest)
        throws OAuthSystemException {
    String queryString = getQueryStringFromOAuthClientRequest(oAuthClientRequest);
    URI oldUri = request.getURI();
    String requestQueryString = oldUri.getQuery() != null ? oldUri.getQuery() + "&" + queryString : queryString;

    try {/*  w  ww .  ja v a 2 s.  c  o  m*/
        request.setURI(URIUtils.createURI(oldUri.getScheme(), oldUri.getHost(), oldUri.getPort(),
                oldUri.getRawPath(), requestQueryString, oldUri.getFragment()));
    } catch (URISyntaxException e) {
        throw new OAuthSystemException(e);
    }
}

From source file:org.sonatype.nexus.testsuite.security.nexus4383.Nexus4383LogoutResourceIT.java

/**
 * 1.) Make a get request to set a cookie </BR>
 * 2.) verify cookie works (do not send basic auth) </BR>
 * 3.) do logout  </BR>/*from   www  . j  ava  2  s  .c  o m*/
 * 4.) repeat step 2 and expect failure.
 */
@Test
public void testLogout() throws Exception {
    TestContext context = TestContainer.getInstance().getTestContext();
    String username = context.getAdminUsername();
    String password = context.getPassword();
    String url = this.getBaseNexusUrl() + RequestFacade.SERVICE_LOCAL + "status";
    String logoutUrl = this.getBaseNexusUrl() + RequestFacade.SERVICE_LOCAL + "authentication/logout";

    Header userAgentHeader = new BasicHeader("User-Agent", "Something Stateful");

    // default useragent is: Jakarta Commons-HttpClient/3.1[\r][\n]
    DefaultHttpClient httpClient = new DefaultHttpClient();
    URI nexusBaseURI = new URI(url);
    final BasicHttpContext localcontext = new BasicHttpContext();
    final HttpHost targetHost = new HttpHost(nexusBaseURI.getHost(), nexusBaseURI.getPort(),
            nexusBaseURI.getScheme());
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(username, password));
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

    // HACK: Disable CSRFGuard support for now, its too problematic
    //String owaspQueryParams = null;
    HttpGet getMethod = new HttpGet(url);
    getMethod.addHeader(userAgentHeader);
    try {
        CloseableHttpResponse response = httpClient.execute(getMethod, localcontext);
        // HACK: Disable CSRFGuard support for now, its too problematic
        //Header owaspCsrfToken = response.getFirstHeader("OWASP_CSRFTOKEN");
        //assertThat(owaspCsrfToken, is(notNullValue()));
        //owaspQueryParams = "?" + owaspCsrfToken.getName() + "=" + owaspCsrfToken.getValue();
        Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
    } finally {
        getMethod.reset();
    }

    Cookie sessionCookie = this.getSessionCookie(httpClient.getCookieStore().getCookies());
    Assert.assertNotNull("Session Cookie not set", sessionCookie);

    httpClient.getCookieStore().clear(); // remove cookies
    httpClient.getCredentialsProvider().clear(); // remove auth

    // now with just the cookie
    httpClient.getCookieStore().addCookie(sessionCookie);
    // HACK: Disable CSRFGuard support for now, its too problematic
    //getMethod = new HttpGet(url + owaspQueryParams);
    getMethod = new HttpGet(url);
    try {
        Assert.assertEquals(httpClient.execute(getMethod).getStatusLine().getStatusCode(), 200);
    } finally {
        getMethod.reset();
    }

    // do logout
    // HACK: Disable CSRFGuard support for now, its too problematic
    //HttpGet logoutGetMethod = new HttpGet(logoutUrl + owaspQueryParams);
    HttpGet logoutGetMethod = new HttpGet(logoutUrl);
    try {
        final HttpResponse response = httpClient.execute(logoutGetMethod);
        Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
        Assert.assertEquals("OK", EntityUtils.toString(response.getEntity()));
    } finally {
        logoutGetMethod.reset();
    }

    // set cookie again
    httpClient.getCookieStore().clear(); // remove cookies
    httpClient.getCredentialsProvider().clear(); // remove auth

    httpClient.getCookieStore().addCookie(sessionCookie);
    HttpGet failedGetMethod = new HttpGet(url);
    try {
        final HttpResponse response = httpClient.execute(failedGetMethod);
        Assert.assertEquals(response.getStatusLine().getStatusCode(), 401);
    } finally {
        failedGetMethod.reset();
    }
}

From source file:com.couchbase.client.ClusterManager.java

/**
 * Connects to a given server if a connection has not been made to at least
 * one of the servers in the server list already.
 * @param uri/* w  ww  . j ava2  s. c  om*/
 * @return
 */
private boolean connect(URI uri) {
    host = new HttpHost(uri.getHost(), uri.getPort());
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
    try {
        if (!conn.isOpen()) {
            Socket socket = new Socket(host.getHostName(), host.getPort());
            conn.bind(socket, new SyncBasicHttpParams());
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:eionet.cr.util.virtuoso.VirtuosoJdbcDriverTest.java

/**
 * Test if CR uses correct Virtuoso JDBC driver. It shouldn't get "Too many open statements" error.
 *
 * @throws SQLException When problem with connecting to Virtuoso.
 *//*from   ww w.j  a v a2s  .co m*/
@Test
public void testTooManyOpenStmts() throws SQLException {

    VirtuosoConnectionPoolDataSource dbsource = new VirtuosoConnectionPoolDataSource();

    String testDbURI = GeneralConfig.getRequiredProperty(GeneralConfig.VIRTUOSO_DB_URL);
    URI uri = URI.create(testDbURI.substring(5));

    dbsource.setServerName(uri.getHost());
    dbsource.setPortNumber(uri.getPort());
    dbsource.setPassword(GeneralConfig.getRequiredProperty(GeneralConfig.VIRTUOSO_DB_PWD));
    dbsource.setUser(GeneralConfig.getRequiredProperty(GeneralConfig.VIRTUOSO_DB_USR));
    dbsource.setCharset("UTF-8");
    VirtuosoPooledConnection pooledConnection = (VirtuosoPooledConnection) dbsource.getPooledConnection();
    virtuoso.jdbc4.VirtuosoConnection con = pooledConnection.getVirtuosoConnection();
    String jdbcComp = "DB.DBA.TTLP (?, ?, ?, ?)";
    CallableStatement stmt = null;
    int MAXIT = 10000;
    for (int i = 0; i < MAXIT; i++) {
        try {
            stmt = con.prepareCall(jdbcComp);
            stmt.setString(1, "");
            stmt.setString(2, "");
            stmt.setString(3, DUMMY_GRAPH_URI);
            stmt.setInt(4, 256);
            stmt.execute();
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("can't add data to virtuoso. ");
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
    }
}

From source file:net.i2cat.netconf.NetconfSession.java

public NetconfSession(SessionContext sessionContext, TransportFactory transportFactory)
        throws TransportNotRegisteredException, ConfigurationException {
    this.transportFactory = transportFactory;
    this.sessionContext = sessionContext;

    URI uri = sessionContext.getURI();

    if (uri.getScheme() == null || uri.getHost() == null || uri.getUserInfo() == null)
        throw new ConfigurationException("Insufficient information in session context's URI: " + uri);

    if (!transportFactory.isAwareOfScheme(uri.getScheme())) {
        throw new TransportNotRegisteredException(
                "Scheme '" + uri.getScheme() + "' given in URI has not been registered with TransportFactory.");
    }//from w  ww. j  a v  a  2s.  c  o  m
}

From source file:com.codeabovelab.dm.gateway.proxy.common.HttpProxyContext.java

public HttpProxyContext(HttpServletRequest request, HttpServletResponse response, URI target, String uid) {
    this.request = request;
    Assert.notNull(request, "request is null");
    this.response = response;
    Assert.notNull(response, "response is null");
    this.target = target;
    Assert.notNull(target, "target is null");
    this.targetHost = new HttpHost(target.getHost(), target.getPort(), target.getScheme());
    this.uid = uid;

}