Example usage for java.net URL getPort

List of usage examples for java.net URL getPort

Introduction

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

Prototype

public int getPort() 

Source Link

Document

Gets the port number of this URL .

Usage

From source file:com.esri.geoportal.commons.utils.HttpClientContextBuilder.java

/**
 * Creates client context./*www.j  a v  a  2 s  .  c  o  m*/
 * @param url url
 * @param cred credentials
 * @return client context
 */
public static HttpClientContext createHttpClientContext(URL url, SimpleCredentials cred) {
    HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(cred.getUserName(), cred.getPassword()));

    // 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(targetHost, basicAuth);

    // Add AuthCache to the execution context
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);

    return context;
}

From source file:Main.java

public static String encodeDocumentUrl(String urlString) {
    try {//from   www  .j a va 2s .c  o  m

        URL url = new URL(urlString);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());

        return uri.toASCIIString();

    } catch (MalformedURLException e) {
        return null;
    } catch (URISyntaxException e) {
        return null;
    }

}

From source file:de.fraunhofer.iosb.ilt.tests.Constants.java

public static SensorThingsService createService(URL serviceUrl)
        throws MalformedURLException, URISyntaxException {
    SensorThingsService service = new SensorThingsService(serviceUrl);
    if (USE_OPENID_CONNECT) {
        service.setTokenManager(new TokenManagerOpenIDConnect().setTokenServerUrl(TOKEN_SERVER_URL)
                .setClientId(CLIENT_ID).setUserName(USERNAME).setPassword(PASSWORD));
    }//w  w w  .j  av a 2  s  . co  m
    if (USE_BASIC_AUTH) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        URL url = new URL(BASE_URL);
        credsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()),
                new UsernamePasswordCredentials(USERNAME, PASSWORD));
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
                .build();
        service.setClient(httpclient);
    }
    return service;
}

From source file:com.cloudera.sqoop.util.JdbcUrl.java

/**
 * @return the port from the connect string, or -1 if we can't.
 *///w  ww  .  ja  va 2s  .  c  o m
public static int getPort(String connectString) {
    try {
        String sanitizedString = null;
        int schemeEndOffset = connectString.indexOf("://");
        if (-1 == schemeEndOffset) {
            // Couldn't find one? ok, then there's no problem, it should work as a
            // URL.
            sanitizedString = connectString;
        } else {
            sanitizedString = "http" + connectString.substring(schemeEndOffset);
        }

        URL connectUrl = new URL(sanitizedString);
        return connectUrl.getPort();
    } catch (MalformedURLException mue) {
        LOG.error("Malformed connect string URL: " + connectString + "; reason is " + mue.toString());
        return -1;
    }
}

From source file:com.rovemonteux.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static InputStream simpleAction(final URL url) throws IOException {
    final int port = url.getPort() < 0 ? 80 : url.getPort();
    final TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    final Map<String, Object> localProperties = new HashMap<String, Object>();
    final NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /* localAddress */null,
            httpServerNetAddress);//  w ww  .  ja va 2s  . c  o  m
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    final long timeoutInMs = 10L * 1000L;

    return HttpUtil.getInstance().getReponseBodyInputStream(lowerLayerNetSocket, httpServerNetAddress,
            pathOnHttpServer, timeoutInMs);
}

From source file:com.rovemonteux.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static byte[] simpleBytesAction(final URL url) throws IOException {
    final int port = url.getPort() < 0 ? 80 : url.getPort();
    final TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    final Map<String, Object> localProperties = new HashMap<String, Object>();
    final NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /* localAddress */null,
            httpServerNetAddress);/*from  w  ww . ja  v  a2 s.  c  o  m*/
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    final long timeoutInMs = 10L * 1000L;

    HttpUtil.getInstance();
    return HttpUtil.get(lowerLayerNetSocket, httpServerNetAddress, pathOnHttpServer, timeoutInMs);
}

From source file:com.moss.appkeep.tools.cache.SimpleAppkeepComponentCache.java

private static String dirNameForUrl(String url) {
    try {/*from  w w  w  .ja  v a  2  s  .c o m*/
        URL u = new URL(url);
        return u.getProtocol() + "_" + u.getHost() + "_" + u.getPort() + "_"
                + u.getPath().replaceAll(Pattern.quote("/"), "_");
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static byte[] simpleBytesAction(URL url) throws IOException {
    int port = (url.getPort() < 0) ? 80 : url.getPort();
    TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    Map<String, Object> localProperties = new HashMap<String, Object>();
    NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /*localAddress*/ null,
            httpServerNetAddress);/*  w ww  .  ja v  a  2 s  .  co  m*/
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    long timeoutInMs = 10L * 1000L;

    return HttpUtil.getInstance().get(lowerLayerNetSocket, httpServerNetAddress, pathOnHttpServer, timeoutInMs);
}

From source file:jp.go.nict.langrid.management.logic.service.HttpClientUtil.java

/**
 * //from   ww w  .  ja v  a  2  s. c  om
 * 
 */
public static HttpClient createHttpClientWithHostConfig(URL url) {
    HttpClient client = new HttpClient();
    int port = url.getPort();
    if (port == -1) {
        port = url.getDefaultPort();
        if (port == -1) {
            port = 80;
        }
    }
    if ((url.getProtocol().equalsIgnoreCase("https")) && (sslSocketFactory != null)) {
        Protocol https = new Protocol("https",
                (ProtocolSocketFactory) new SSLSocketFactorySSLProtocolSocketFactory(sslSocketFactory), port);
        client.getHostConfiguration().setHost(url.getHost(), url.getPort(), https);
    } else {
        Protocol http = new Protocol("http", new SocketFactoryProtocolSocketFactory(SocketFactory.getDefault()),
                port);
        client.getHostConfiguration().setHost(url.getHost(), url.getPort(), http);
    }
    try {
        List<Proxy> proxies = ProxySelector.getDefault().select(url.toURI());
        for (Proxy p : proxies) {
            if (p.equals(Proxy.NO_PROXY))
                continue;
            if (!p.type().equals(Proxy.Type.HTTP))
                continue;
            InetSocketAddress addr = (InetSocketAddress) p.address();
            client.getHostConfiguration().setProxy(addr.getHostName(), addr.getPort());
            break;
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return client;
}

From source file:org.silvertunnel.netlib.adapter.httpclient.HttpClientUtil.java

public static InputStream simpleAction(URL url) throws IOException {
    int port = (url.getPort() < 0) ? 80 : url.getPort();
    TcpipNetAddress httpServerNetAddress = new TcpipNetAddress(url.getHost(), port);
    Map<String, Object> localProperties = new HashMap<String, Object>();
    NetSocket lowerLayerNetSocket = lowerNetLayer.createNetSocket(localProperties, /*localAddress*/ null,
            httpServerNetAddress);/*from  w  ww.  ja va2  s .c  o  m*/
    String pathOnHttpServer = url.getPath();
    if (pathOnHttpServer == null || pathOnHttpServer.length() < 1) {
        pathOnHttpServer = "/";
    }
    long timeoutInMs = 10L * 1000L;

    return HttpUtil.getInstance().getReponseBodyInputStream(lowerLayerNetSocket, httpServerNetAddress,
            pathOnHttpServer, timeoutInMs);
}