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.sonyericsson.hudson.plugins.gerrit.trigger.utils.HttpUtils.java

/**
 * @param config Gerrit Server Configuration.
 * @param url URL to get.//  w  w  w .  jav a  2s  .  co  m
 * @return httpresponse.
 * @throws IOException if found.
 */
public static HttpResponse performHTTPGet(IGerritHudsonTriggerConfig config, String url) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    if (config.getGerritProxy() != null && !config.getGerritProxy().isEmpty()) {
        try {
            URL proxyUrl = new URL(config.getGerritProxy());
            HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort(), proxyUrl.getProtocol());
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } catch (MalformedURLException e) {
            logger.error("Could not parse proxy URL, attempting without proxy.", e);
        }
    }

    httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), config.getHttpCredentials());
    HttpResponse execute;
    return httpclient.execute(httpGet);
}

From source file:com.threadswarm.imagefeedarchiver.FeedUtils.java

/**
 * Returns a hierarchical {@code URI} constructed from individual components 
 * of the supplied {@code urlString} argument.
 * <p>/*from www.  ja v  a 2s . c  o  m*/
 * The {@code urlString} argument is first used to instantiate a {@code URL} 
 * which in turn is used to construct a {@code URI} based on the individual 
 * components of the former.  This more robust then simply calling {@code URL.toURI()}.
 * 
 * @param urlString the {@code String} based representation of a URL
 * @return a {@code URI} constructed from the individual URL components
 * @throws URISyntaxException if a valid {@code URI} cannot be constructed from the supplied {@code urlString} argument
 * @throws MalformedURLException if the {@code urlString} cannot be used to instantiate a {@code URL}
 */
public static URI getUriFromUrlString(String urlString) throws URISyntaxException, MalformedURLException {
    URL url = new URL(urlString);
    return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef());
}

From source file:jp.go.nict.langrid.servicesupervisor.invocationprocessor.executor.intragrid.HttpClientUtil.java

/**
 * /*from  www.  j a  v  a 2 s . c o m*/
 * 
 */
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());
            client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
            break;
        }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return client;
}

From source file:com.jaeksoft.searchlib.util.LinkUtils.java

public final static URI newEncodedURI(String u) throws MalformedURLException, URISyntaxException {
    URL tmpUrl = new URL(u);
    return new URI(tmpUrl.getProtocol(), tmpUrl.getUserInfo(), tmpUrl.getHost(), tmpUrl.getPort(),
            tmpUrl.getPath(), tmpUrl.getQuery(), tmpUrl.getRef());
}

From source file:Main.java

public static String makeGETrequest(String url) throws MalformedURLException {
    String result = "";
    URL requestURL = new URL(url);
    result += "GET " + requestURL.getFile() + " HTTP/1.1";
    result += NEWLINE;/*from w  w w  . j a v a2s  .  c o m*/
    result += "Host: " + requestURL.getHost() + ":" + (requestURL.getPort() > 0 ? requestURL.getPort() : 80);
    result += NEWLINE;
    result += "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
    result += NEWLINE;
    result += "Connection: close";
    result += NEWLINE;
    result += NEWLINE;

    return result;
}

From source file:it.govpay.web.console.utils.HttpClientUtils.java

public static HttpResponse getEsitoPagamento(String urlToInvoke, Logger log) throws Exception {
    HttpResponse responseGET = null;/*  ww w .ja v  a2 s.c o m*/
    try {
        log.debug("Richiesta Esito del pagamento in corso...");

        URL urlObj = new URL(urlToInvoke);
        HttpHost target = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
        CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
        HttpGet richiestaPost = new HttpGet();

        richiestaPost.setURI(urlObj.toURI());

        log.debug("Invio tramite client Http in corso...");
        responseGET = client.execute(target, richiestaPost);

        if (responseGET == null)
            throw new NullPointerException("La Response HTTP e' null");

        log.debug("Invio tramite client Http completato.");
        return responseGET;
    } catch (Exception e) {
        log.error("Errore durante l'invio della richiesta di pagamento: " + e.getMessage(), e);
        throw e;
    }
}

From source file:Urls.java

public static String getNoRefForm(URL url) {
    String host = url.getHost();/*  www .  j  a  va 2s . c  o  m*/
    int port = url.getPort();
    String portText = port == -1 ? "" : ":" + port;
    String userInfo = url.getUserInfo();
    String userInfoText = userInfo == null || userInfo.length() == 0 ? "" : userInfo + "@";
    String hostPort = host == null || host.length() == 0 ? "" : "//" + userInfoText + host + portText;
    return url.getProtocol() + ":" + hostPort + url.getFile();
}

From source file:Main.java

public static void checkForBlacklistedURLs(URL url)

        throws IOException {
    for (int i = 0; i < BLACKLISTED_HOSTS.length; i++) {

        if (url.getHost().equalsIgnoreCase(BLACKLISTED_HOSTS[i]) && url.getPort() == BLACKLISTED_PORTS[i]) {

            throw (new IOException(
                    "http://" + BLACKLISTED_HOSTS[i] + ":" + BLACKLISTED_PORTS[i] + "/ is not a tracker"));
        }//ww  w .java  2  s  . co  m
    }
}

From source file:it.govpay.web.console.utils.HttpClientUtils.java

public static HttpResponse sendRichiestaPagamento(String urlToInvoke, RichiestaPagamento richiestaPagamento,
        Logger log) throws Exception {
    HttpResponse responseGET = null;/*from w w w.  j  a va2s . com*/
    try {
        log.debug("Invio del pagamento in corso...");

        URL urlObj = new URL(urlToInvoke);
        HttpHost target = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
        CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
        HttpPost richiestaPost = new HttpPost();

        richiestaPost.setURI(urlObj.toURI());

        log.debug("Serializzazione pagamento in corso...");
        byte[] bufferPagamento = JaxbUtils.toBytes(richiestaPagamento);
        log.debug("Serializzazione pagamento completata.");

        HttpEntity bodyEntity = new InputStreamEntity(new ByteArrayInputStream(bufferPagamento),
                ContentType.APPLICATION_XML);
        richiestaPost.setEntity(bodyEntity);
        richiestaPost.setHeader("Content-Type", ContentType.APPLICATION_XML.getMimeType());

        log.debug("Invio tramite client Http in corso...");
        responseGET = client.execute(target, richiestaPost);

        if (responseGET == null)
            throw new NullPointerException("La Response HTTP e' null");

        log.debug("Invio tramite client Http completato.");
        return responseGET;
    } catch (Exception e) {
        log.error("Errore durante l'invio della richiesta di pagamento: " + e.getMessage(), e);
        throw e;
    }
}

From source file:org.jboss.as.test.clustering.cluster.web.authentication.BasicAuthenticationWebFailoverTestCase.java

private static void setCredentials(CredentialsProvider provider, String user, String password, URL... urls) {
    for (URL url : urls) {
        provider.setCredentials(new AuthScope(url.getHost(), url.getPort()),
                new UsernamePasswordCredentials(user, password));
    }/*from w  w w .j a  v  a 2 s  .  com*/
}