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:io.seldon.external.ExternalPluginServer.java

@Override
public JsonNode execute(JsonNode payload, OptionsHolder options) {
    long timeNow = System.currentTimeMillis();
    URI uri = URI.create(options.getStringOption(URL_PROPERTY_NAME));
    try {/* www .ja v a  2 s .c  o  m*/
        URIBuilder builder = new URIBuilder().setScheme("http").setHost(uri.getHost()).setPort(uri.getPort())
                .setPath(uri.getPath()).setParameter("json", payload.toString());

        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new APIException(APIException.GENERIC_ERROR);
    }
    HttpContext context = HttpClientContext.create();
    HttpGet httpGet = new HttpGet(uri);
    try {
        if (logger.isDebugEnabled())
            logger.debug("Requesting " + httpGet.getURI().toString());
        CloseableHttpResponse resp = httpClient.execute(httpGet, context);
        if (resp.getStatusLine().getStatusCode() == 200) {

            JsonFactory factory = mapper.getJsonFactory();
            JsonParser parser = factory.createJsonParser(resp.getEntity().getContent());
            JsonNode actualObj = mapper.readTree(parser);
            if (logger.isDebugEnabled())
                logger.debug(
                        "External prediction server took " + (System.currentTimeMillis() - timeNow) + "ms");
            return actualObj;
        } else {
            logger.error(
                    "Couldn't retrieve prediction from external prediction server -- bad http return code: "
                            + resp.getStatusLine().getStatusCode());
            throw new APIException(APIException.GENERIC_ERROR);
        }
    } catch (IOException e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new APIException(APIException.GENERIC_ERROR);
    }

}

From source file:org.stem.ClusterManagerDaemon.java

private HttpServer createServer(URI uri, ResourceConfig resourceCfg) {
    final String host = uri.getHost();
    final int port = uri.getPort();
    final HttpServer server = new HttpServer();
    final NetworkListener listener = new NetworkListener("grizzly", host, port);
    listener.getTransport().setWorkerThreadPoolConfig(adjustThreadPool());
    server.addListener(listener);/*from  w w  w.  j av a  2s  . co  m*/

    final ServerConfiguration config = server.getServerConfiguration();
    final HttpHandler handler = ContainerFactory.createContainer(HttpHandler.class, resourceCfg);

    if (handler != null)
        config.addHttpHandler(handler, uri.getPath());

    return server;
}

From source file:org.fcrepo.server.utilities.ServerUtility.java

/**
 * Tell whether the given URL appears to be referring to somewhere within
 * the Fedora webapp./*ww  w  .  j a  v  a2  s .c om*/
 */
public static boolean isURLFedoraServer(String url) {

    // scheme must be http or https
    URI uri = URI.create(url);
    String scheme = uri.getScheme();
    if (!scheme.equals("http") && !scheme.equals("https")) {
        return false;
    }

    // host must be configured hostname or localhost
    String fHost = CONFIG.getParameter(FEDORA_SERVER_HOST, Parameter.class).getValue();
    String host = uri.getHost();
    if (!host.equals(fHost) && !host.equals("localhost")) {
        return false;
    }

    // path must begin with configured webapp context
    String path = uri.getPath();
    String fedoraContext = CONFIG.getParameter(FEDORA_SERVER_CONTEXT, Parameter.class).getValue();
    if (!path.startsWith("/" + fedoraContext + "/")) {
        return false;
    }

    // port specification must match http or https port as appropriate
    String httpPort = CONFIG.getParameter(FEDORA_SERVER_PORT, Parameter.class).getValue();
    String httpsPort = CONFIG.getParameter(FEDORA_REDIRECT_PORT, Parameter.class).getValue();
    if (uri.getPort() == -1) {
        // unspecified, so fedoraPort must be 80 (http), or 443 (https)
        if (scheme.equals("http")) {
            return httpPort.equals("80");
        } else {
            return httpsPort.equals("443");
        }
    } else {
        // specified, so must match appropriate http or https port
        String port = "" + uri.getPort();
        if (scheme.equals("http")) {
            return port.equals(httpPort);
        } else {
            return port.equals(httpsPort);
        }
    }

}

From source file:com.orange.cloud.servicebroker.filter.securitygroups.domain.Destination.java

public Destination(String uriString) {
    try {/* w  w  w  .ja v a2 s . c om*/
        URI uri = new URI(uriString);
        setHost(uri.getHost());
        if (noPort(uri)) {
            setDefaultPort(uri.getScheme());
        } else {
            setPort(ImmutablePort.of(uri.getPort()));
        }
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Cannot create connection info. Invalid URI " + uriString, e);
    }
}

From source file:com.ericsson.gerrit.plugins.syncindex.HttpClientProvider.java

private BasicCredentialsProvider buildCredentials() {
    URI uri = URI.create(cfg.getUrl());
    BasicCredentialsProvider creds = new BasicCredentialsProvider();
    creds.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(cfg.getUser(), cfg.getPassword()));
    return creds;
}

From source file:org.cloudfoundry.identity.uaa.login.saml.IdentityProviderConfigurator.java

protected String adjustURIForPort(String uri) throws URISyntaxException {
    URI metadataURI = new URI(uri);
    if (metadataURI.getPort() < 0) {
        switch (metadataURI.getScheme()) {
        case "https":
            return new URIBuilder(uri).setPort(443).build().toString();
        case "http":
            return new URIBuilder(uri).setPort(80).build().toString();
        default://from  w w  w . j  a va 2  s.c  o m
            return uri;
        }
    }
    return uri;
}

From source file:com.shazam.fork.reporter.gradle.JenkinsDownloader.java

@Nonnull
private URL getArtifactUrl(Build build, Artifact artifact) {
    try {//from   ww w  .  jav a  2s  . c  om
        URI uri = new URI(build.getUrl());
        String artifactPath = uri.getPath() + "artifact/" + artifact.getRelativePath();
        URI artifactUri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
                artifactPath, "", "");
        return artifactUri.toURL();
    } catch (URISyntaxException | MalformedURLException e) {
        throw new GradleException("Error when trying to construct artifact URL for: " + build.getUrl(), e);
    }
}

From source file:net.sf.ufsc.ftp.FtpSession.java

/**
 * Constructs a new FtpSession./*from   w w w.  j  av a2  s .  c  om*/
 * @param uri
 * @throws IOException
 */
public FtpSession(URI uri, FTPClient client) throws IOException {
    super(uri);

    this.client = client;

    UserInfo userInfo = new UserInfo(uri);

    String host = uri.getHost();

    int port = uri.getPort();

    if (port < 0) {
        this.client.connect(host);
    } else {
        this.client.connect(host, port);
    }

    String reply = this.client.getReplyString();

    if (!FTPReply.isPositiveCompletion(this.client.getReplyCode())) {
        this.close();

        throw new IOException(reply);
    }

    this.logger.info(reply);

    boolean loggedIn = this.client.login(userInfo.getUser(), userInfo.getPassword());

    reply = this.client.getReplyString();

    if (!loggedIn) {
        this.close();

        throw new IOException(reply);
    }

    this.logger.info(reply);

    this.client.enterLocalPassiveMode();
}

From source file:com.grendelscan.commons.http.apache_overrides.client.CustomHttpClient.java

public String getRequestHeadString(final HttpHost originalTarget, final HttpRequest request,
        final HttpContext originalContext) throws IOException, HttpException {
    HttpContext context = originalContext;
    HttpHost target = originalTarget;/*from ww w  .j  a v  a  2s  .  c  o m*/

    if (context == null) {
        context = createHttpContext();
    }

    if (target == null) {
        URI uri = ((HttpUriRequest) request).getURI();
        target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    }
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
    prepareRequest(request);

    getHttpProcessor().process(request, context);

    String requestString = request.getRequestLine().toString() + "\n";
    for (Header header : request.getAllHeaders()) {
        requestString += header.toString() + "\n";
    }
    return requestString;
}