Example usage for java.net URI getPort

List of usage examples for java.net URI getPort

Introduction

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

Prototype

public int getPort() 

Source Link

Document

Returns the port number of this URI.

Usage

From source file:com.tasktop.c2c.server.ssh.server.commands.AbstractInteractiveProxyCommand.java

private HeaderGroup computeHeaders(URI targetUri) {
    HeaderGroup headers = new HeaderGroup();
    String host = targetUri.getHost();
    int port = targetUri.getPort();
    String hostHeaderValue = host;
    if (port != 80) {
        hostHeaderValue += ":" + port;
    }//from  ww  w. j av a  2 s.co m
    headers.addHeader(new Header("Host", hostHeaderValue));
    headers.addHeader(new Header("Transfer-Encoding", "chunked"));
    headers.addHeader(new Header("Content-Type", MIME_TYPE_APPLICATION_OCTET_STREAM));
    headers.addHeader(new Header("Accept", MIME_TYPE_APPLICATION_OCTET_STREAM));
    String remoteAddress = session.getIoSession().getRemoteAddress().toString();
    headers.addHeader(new Header("X-Forwarded-For", remoteAddress));
    // TODO: http://en.wikipedia.org/wiki/X-Forwarded-For
    return headers;
}

From source file:com.jaeksoft.searchlib.crawler.database.DatabaseCrawlMongoDb.java

MongoClient getMongoClient() throws URISyntaxException, UnknownHostException {
    String user = getUser();/*  w  w w . j  a  v a2 s.  c  o m*/
    String password = getPassword();
    URI uri = new URI(getUrl());
    MongoCredential credential = null;
    if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(password)) {
        credential = MongoCredential.createMongoCRCredential(user, databaseName, password.toCharArray());
        return new MongoClient(new ServerAddress(uri.getHost(), uri.getPort()), Arrays.asList(credential));
    }
    return new MongoClient(new ServerAddress(uri.getHost(), uri.getPort()));
}

From source file:edu.ucsb.nceas.ezid.EZIDService.java

/**
 * Log into the EZID service using account credentials provided by EZID. The cookie
 * returned by EZID is cached in a local CookieStore for the duration of the EZIDService,
 * and so subsequent calls uning this instance of the service will function as
 * fully authenticated. An exception is thrown if authentication fails.
 * @param username to identify the user account from EZID
 * @param password the secret password for this account
 * @throws EZIDException if authentication fails for any reason
 *//* w  ww . j  av  a 2  s  .com*/
public void login(String username, String password) throws EZIDException {
    try {
        URI serviceUri = new URI(loginServiceEndpoint);
        HttpHost targetHost = new HttpHost(serviceUri.getHost(), serviceUri.getPort(), serviceUri.getScheme());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials(username, password));
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        HttpClientContext localcontext = HttpClientContext.create();
        localcontext.setAuthCache(authCache);
        localcontext.setCredentialsProvider(credsProvider);

        ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
            public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toByteArray(entity);
                } else {
                    return null;
                }
            }
        };
        byte[] body = null;

        HttpGet httpget = new HttpGet(loginServiceEndpoint);
        body = httpclient.execute(httpget, handler, localcontext);
        String message = new String(body);
        String msg = parseIdentifierResponse(message);
    } catch (URISyntaxException e) {
        throw new EZIDException(e.getMessage());
    } catch (ClientProtocolException e) {
        throw new EZIDException(e.getMessage());
    } catch (IOException e) {
        throw new EZIDException(e.getMessage());
    }
}

From source file:com.box.restclientv2.httpclientsupport.HttpClientURIBuilder.java

private void digestURI(final URI uri) throws URISyntaxException {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), HttpClientConsts.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

From source file:com.helger.peppol.httpclient.AbstractGenericSMPClient.java

/**
 * Constructor with a direct SMP URL.<br>
 * Remember: must be HTTP and using port 80 only!
 *
 * @param aSMPHost/*from w  ww  .  j  a v  a2s .  co  m*/
 *        The address of the SMP service. Must be port 80 and basic http only
 *        (no https!). Example: http://smpcompany.company.org
 */
public AbstractGenericSMPClient(@Nonnull final URI aSMPHost) {
    ValueEnforcer.notNull(aSMPHost, "SMPHost");

    if (!"http".equals(aSMPHost.getScheme()))
        s_aLogger.warn("SMP URI " + aSMPHost + " does not use the expected http scheme!");
    // getPort () returns -1 if none was explicitly specified
    if (aSMPHost.getPort() != 80 && aSMPHost.getPort() != -1)
        s_aLogger.warn("SMP URI " + aSMPHost + " is not running on port 80!");

    // Build string and ensure it ends with a "/"
    final String sSMPHost = aSMPHost.toString();
    m_sSMPHost = sSMPHost.endsWith("/") ? sSMPHost : sSMPHost + '/';

    // Set default proxy from configuration file
    m_aProxy = SMPClientConfiguration.getHttpProxy();
}

From source file:com.sample.amqp.RabbitConfiguration.java

@Bean
public ConnectionFactory connectionFactory() {
    final URI ampqUrl;
    try {/*  w  w  w.  j  a v  a2 s .co  m*/
        ampqUrl = new URI(getEnvOrThrow("CLOUDAMQP_URL"));
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    final CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setUsername(ampqUrl.getUserInfo().split(":")[0]);
    factory.setPassword(ampqUrl.getUserInfo().split(":")[1]);
    factory.setHost(ampqUrl.getHost());
    factory.setPort(ampqUrl.getPort());
    factory.setVirtualHost(ampqUrl.getPath().substring(1));
    factory.setPublisherReturns(true);

    return factory;
}

From source file:com.adaptris.core.ftp.FileTransferConnection.java

private FileTransferClient create(String hostUrl) throws FileTransferException, IOException, PasswordException {
    String remoteHost = hostUrl;/*from   www.  jav  a2 s .c  o  m*/
    UserInfo ui = createUserInfo();
    int port = defaultControlPort();
    try {
        URI uri = new URI(hostUrl);
        if (acceptProtocol(uri.getScheme())) {
            remoteHost = uri.getHost();
            port = uri.getPort() != -1 ? uri.getPort() : defaultControlPort();
            ui.parse(uri.getRawUserInfo());
        }
    } catch (URISyntaxException e) {
        ;
    }
    FileTransferClient client = create(remoteHost, port, ui);
    return client;
}

From source file:com.mellanox.jxio.ServerPortal.java

private URI replaceIPinURI(URI uriForForward, String uriIPAddress) {
    URI newUri = null;/*from   ww w .j  ava2 s. c o  m*/
    try {
        newUri = new URI(uriForForward.getScheme(), uriForForward.getUserInfo(),
                new URI(uriIPAddress).getHost(), uriForForward.getPort(), uriForForward.getPath(),
                uriForForward.getQuery(), uriForForward.getFragment());
    } catch (URISyntaxException e) {
        e.printStackTrace();
        LOG.error(this.toLogString() + "URISyntaxException occured while trying to create a new URI");
    }
    return newUri;
}

From source file:org.mcxiaoke.commons.http.util.URIBuilderEx.java

private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), URIUtilsEx.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

From source file:com.intel.cosbench.api.httpauth.HttpAuth.java

@Override
public AuthContext login() {
    super.login();

    //        HttpHost host = new HttpHost();
    //        HttpHost targetHost = new HttpHost(host, port, protocol);

    URI uri;

    try {/*w w w. j  a  v  a  2  s . c  o  m*/
        uri = new URI(auth_url);
    } catch (URISyntaxException use) {
        throw new AuthException(use);
    }

    HttpGet method = new HttpGet(auth_url);
    method.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);

    client.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(this.username, this.password));

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(AuthPNames.TARGET_AUTH_PREF,
            Arrays.asList(new String[] { AuthPolicy.BASIC, AuthPolicy.DIGEST }));

    HttpResponse response = null;

    try {
        dumpClientSettings();
        response = client.execute(method, localContext);

        dumpClientSettings();
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            return createContext();
        }
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            throw new AuthException(response.getStatusLine().getReasonPhrase());
        }
    } catch (SocketTimeoutException ste) {
        throw new AuthTimeoutException(ste);
    } catch (ConnectTimeoutException cte) {
        throw new AuthTimeoutException(cte);
    } catch (InterruptedIOException ie) {
        throw new AuthInterruptedException(ie);
    } catch (Exception e) {
        throw new AuthException(e);
    } finally {
        if (response != null)
            try {
                dumpResponse(response);
                EntityUtils.consume(response.getEntity());
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }

        if (method != null)
            method.abort();
    }

    return createContext();
}