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:org.everit.authentication.cas.ecm.tests.SampleApp.java

/**
 * Constructor.//  w  ww  .j a  v a 2  s  . co m
 */
public SampleApp(final String hostname, final Filter sessionAuthenticationFilter,
        final Servlet sessionLogoutServlet, final Filter casAuthenticationFilter,
        final EventListener casAuthenticationEventListener, final AuthenticationContext authenticationContext)
        throws Exception {
    super();
    this.hostname = hostname;
    server = new Server(0);

    // Initialize servlet context
    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setVirtualHosts(new String[] { hostname });

    servletContextHandler.addFilter(new FilterHolder(sessionAuthenticationFilter), "/*", null);
    servletContextHandler.addFilter(new FilterHolder(casAuthenticationFilter), "/*", null);
    servletContextHandler.addServlet(
            new ServletHolder("helloWorldServlet", new HelloWorldServlet(authenticationContext)),
            HELLO_SERVLET_ALIAS);
    servletContextHandler.addServlet(new ServletHolder("sessionLogoutServlet", sessionLogoutServlet),
            LOGOUT_SERVLET_ALIAS);

    servletContextHandler.addEventListener(casAuthenticationEventListener);
    server.setHandler(servletContextHandler);

    // Initialize session management
    HashSessionManager sessionManager = new HashSessionManager();
    String sessionStoreDirecotry = System.getProperty("jetty.session.store.directory");
    sessionManager.setStoreDirectory(new File(sessionStoreDirecotry));
    sessionManager.setLazyLoad(true); // required to initialize the servlet context before restoring
                                      // the sessions
    sessionManager.addEventListener(casAuthenticationEventListener);

    SessionHandler sessionHandler = servletContextHandler.getSessionHandler();
    sessionHandler.setSessionManager(sessionManager);

    start();

    URI serverUri = server.getURI();
    port = serverUri.getPort();

    String testServerURI = serverUri.toString();
    String testServerURL = testServerURI.substring(0, testServerURI.length() - 1);

    helloServiceUrl = testServerURL + HELLO_SERVLET_ALIAS;
    sessionLogoutUrl = testServerURL + "/logout";
    loggedOutUrl = testServerURL + "/logged-out.html";
    failedUrl = testServerURL + "/failed.html";
}

From source file:com.esri.geoportal.commons.http.BotsHttpClient.java

private URI updateURI(URI uri, PHP php) throws URISyntaxException {
    return new URI(php.protocol != null ? php.protocol : uri.getScheme(), uri.getUserInfo(),
            php.host != null ? php.host : uri.getHost(),
            php.host != null ? php.port != null ? php.port : -1 : uri.getPort(), uri.getPath(), uri.getQuery(),
            uri.getFragment());// ww w. j  av a2 s.c o  m
}

From source file:mobi.jenkinsci.server.core.net.ProxyUtil.java

private int getPort(final URI remoteURI) {
    final int port = remoteURI.getPort();
    if (port > 0) {
        return port;
    } else {//from ww w .  j  a v  a2 s.c o  m
        return remoteURI.getScheme().equalsIgnoreCase("http") ? 80
                : (remoteURI.getScheme().equalsIgnoreCase("https") ? 443 : 0);
    }
}

From source file:org.nextlets.erc.defaults.http.ERCHttpInvokerImpl.java

protected void addProxy(ERCConfiguration configuration, HttpRequestBase req) {
    if (configuration.getProxyUrl() != null && !configuration.getProxyUrl().isEmpty()) {
        log.debug("Proxy: {}", configuration.getProxyUrl());
        URI proxyUri;
        try {//  www  .j  av a 2 s .c  om
            proxyUri = new URI(configuration.getProxyUrl());
        } catch (URISyntaxException ex) {
            throw new ERCClientException(ex);
        }
        HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme());
        req.setConfig(RequestConfig.custom().setProxy(proxy).build());
    }

}

From source file:org.piraso.ui.api.views.URLTabView.java

@Override
protected void populateMessage(MessageAwareEntry m) throws Exception {
    List<URI> urls = URLParser.parseUrls(m.getMessage());

    int i = 0;//from   w w w.j  a  v  a2 s. com
    for (URI uri : urls) {
        try {
            if (i != 0) {
                insertKeyword(txtEditor, "\n\n");
            }

            insertKeyword(txtEditor, String.format("[%d] Scheme: ", ++i));
            insertCode(txtEditor, uri.getScheme());

            insertKeyword(txtEditor, "\n    Host: ");
            insertCode(txtEditor, uri.getHost());

            if (uri.getPort() > 80) {
                insertKeyword(txtEditor, "\n    Port: ");
                insertCode(txtEditor, String.valueOf(uri.getPort()));
            }

            insertKeyword(txtEditor, "\n    Path: ");
            insertCode(txtEditor, uri.getPath());

            String queryString = uri.getQuery();

            if (StringUtils.isNotBlank(queryString)) {
                List<NameValuePair> params = URLEncodedUtils.parse(uri, "UTF-8");

                insertKeyword(txtEditor, "\n    Query String: ");

                for (NameValuePair nvp : params) {
                    insertCode(txtEditor, "\n       ");
                    insertIdentifier(txtEditor, nvp.getName() + ": ");
                    insertCode(txtEditor, nvp.getValue());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.microsoft.services.sharepoint.http.FroyoHttpConnection.java

@Override
public ListenableFuture<Response> execute(final Request request) {

    final SettableFuture<Response> future = SettableFuture.create();

    final RequestTask requestTask = new RequestTask() {

        AndroidHttpClient mClient;/*from  w ww .ja  v a2  s.  c o m*/
        InputStream mResponseStream;

        @Override
        protected Void doInBackground(Void... voids) {
            if (request == null) {
                future.setException(new IllegalArgumentException("request"));
            }

            mClient = AndroidHttpClient.newInstance(Platform.getUserAgent());
            mResponseStream = null;
            URI uri;

            try {
                HttpRequest realRequest = createRealRequest(request);
                assert request != null;
                uri = new URI(request.getUrl());

                HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());

                HttpResponse response;

                try {
                    response = mClient.execute(host, realRequest);
                } catch (SocketTimeoutException timeoutException) {
                    closeStreamAndClient();
                    future.setException(timeoutException);
                    return null;
                }

                mResponseStream = response.getEntity().getContent();
                Header[] headers = response.getAllHeaders();
                Map<String, List<String>> headersMap = new HashMap<String, List<String>>();
                for (Header header : headers) {
                    String headerName = header.getName();
                    if (headersMap.containsKey(headerName)) {
                        headersMap.get(headerName).add(header.getValue());
                    } else {
                        List<String> headerValues = new ArrayList<String>();
                        headerValues.add(header.getValue());
                        headersMap.put(headerName, headerValues);
                    }
                }

                future.set(new StreamResponse(mResponseStream, response.getStatusLine().getStatusCode(),
                        headersMap));
                closeStreamAndClient();
            } catch (Exception e) {
                closeStreamAndClient();

                future.setException(e);
            }

            return null;
        }

        protected void closeStreamAndClient() {
            if (mResponseStream != null) {
                try {
                    mResponseStream.close();
                } catch (IOException e) {
                }
            }

            if (mClient != null) {
                mClient.close();
            }
        }
    };

    Futures.addCallback(future, new FutureCallback<Response>() {
        @Override
        public void onFailure(Throwable arg0) {
            requestTask.closeStreamAndClient();

        }

        @Override
        public void onSuccess(Response response) {
        }
    });

    executeTask(requestTask);

    return future;
}

From source file:com.subgraph.vega.internal.http.requests.HttpRequestBuilder.java

@Override
public synchronized void setFromRequestLine(RequestLine requestLine) throws URISyntaxException {
    method = requestLine.getMethod();// www .ja  v a2 s .c o m

    final URI requestUri = new URI(requestLine.getUri());
    scheme = requestUri.getScheme();
    if (scheme == null) {
        scheme = "http";
    }

    host = requestUri.getHost();
    hostPort = requestUri.getPort();
    if (hostPort == -1) {
        if (scheme.equals("https")) {
            hostPort = 443;
        } else {
            hostPort = 80;
        }
    }

    setPathFromUri(requestUri);
    setProtocolVersion(requestLine.getProtocolVersion());
}

From source file:com.vmware.photon.controller.housekeeper.xenon.SubnetIPLeaseSyncServiceTest.java

private void createDhcpSubnet(TestEnvironment env) throws Throwable {
    String cidr = "192.168.0.0/16";
    SubnetUtils subnetUtils = new SubnetUtils(cidr);
    SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();

    DhcpSubnetService.State subnetState = new DhcpSubnetService.State();
    subnetState.subnetId = SUBNET_ID;/*w ww. j a  v  a 2  s .c om*/
    subnetState.version = 1L;
    subnetState.lowIp = IpHelper.ipStringToLong(subnetInfo.getLowAddress());
    subnetState.highIp = IpHelper.ipStringToLong(subnetInfo.getHighAddress());
    subnetState.lowIpDynamic = subnetState.lowIp + 1;
    subnetState.highIpDynamic = subnetState.highIp - 1;
    subnetState.cidr = cidr;
    URI hostUri = env.getHosts()[0].getUri();
    subnetState.dhcpAgentEndpoint = hostUri.getScheme() + "://" + hostUri.getHost() + ":" + hostUri.getPort();

    env.sendPostAndWaitForReplication(DhcpSubnetService.FACTORY_LINK, subnetState);
}

From source file:com.sap.core.odata.testutil.tool.core.AbstractTestClient.java

private void call(final URI baseUri, final TestRequest testPath) {
    HttpRequestBase request = null;//from   w w w.  j  a v a2 s.c om
    try {
        request = createRequest(baseUri, testPath);
        testPath.applyHeaders(request);

        LOG.debug("Call for request line '{}'", request.getRequestLine());

        final HttpHost targetHost = new HttpHost(baseUri.getHost(), baseUri.getPort());
        final HttpResponse response = httpClient.execute(targetHost, request);

        //
        handleSuccessCall(baseUri, testPath, request, response);
    } catch (Exception e) {
        handleFailureCall(testPath, request, e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}