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.fujitsu.dc.engine.rs.AbstractService.java

/**
 * ??.//from w w w . ja  v  a 2  s.  com
 * @param req Request
 * @param res Response
 * @return URL
 * @throws MalformedURLException 
 */
public final String parseRequestUri(final HttpServletRequest req, final HttpServletResponse res)
        throws MalformedURLException {
    String baseUrl = "";

    // URI??URI????????
    //  /dc-engine/engine-test/ds-engine-test/service/hello?a=b&c=d
    // DC??????URI?????????
    // ?DC-Engine???URI???
    String requestUri = req.getHeader(KEY_HEADER_REQUEST_URI);
    if (requestUri == null || requestUri.length() == 0) {
        requestUri = req.getRequestURI();
        String query = req.getQueryString();
        if (query != null && query.length() > 0) {
            requestUri += "?" + query;
        }
    }

    // URI??????????
    // /dc-engine/engine-test/ds-engine-test/service/hello
    int indexQ = requestUri.indexOf("?");
    String scriptName = requestUri;
    if (indexQ > 0) {
        scriptName = requestUri.substring(0, indexQ);
    }

    // ?????JSGI????
    req.setAttribute("env.requestUri", requestUri);
    req.setAttribute("scriptName", scriptName);

    // ?????URL??
    // DC??????URL?????????
    // ??????
    String defaultBaseUrl = DcEngineConfig.getDefaultBaseUrl();
    baseUrl = req.getHeader(KEY_HEADER_BASEURL);
    if (baseUrl == null || baseUrl.length() == 0) {
        baseUrl = defaultBaseUrl;
    }

    // ?????JSGI????
    URL baseUrlObj = new URL(baseUrl);
    int port = baseUrlObj.getPort();
    String proto = baseUrlObj.getProtocol();
    String host = baseUrlObj.getHost();
    String hostHeader = host;
    if (port < 0) {
        if ("http".endsWith(proto)) {
            port = PORT_HTTP;
        }
        if ("https".endsWith(proto)) {
            port = PORT_HTTPS;
        }
    } else {
        hostHeader += ":" + port;
    }
    req.setAttribute("HostHeader", hostHeader);
    req.setAttribute("host", baseUrlObj.getHost());
    req.setAttribute("port", port);
    req.setAttribute("scheme", proto);
    return baseUrl;
}

From source file:com.cladonia.xngreditor.URLUtilities.java

/**
 * Encrypts the password in this URL.//from  w ww . ja  v a 2  s  .co  m
 * 
 * @param url the url.
 * 
 * @return the encrypted string.
 */
public static String encrypt(URL url) {
    String password = getPassword(url);
    String username = getUsername(url);

    if (password != null) {
        password = StringUtilities.encrypt(password);
    }

    //      System.out.println( "URLUtilities.encrypt( "+url+") ["+password+"]");

    return createURL(url.getProtocol(), username, password, url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef()).toString();
}

From source file:org.spiffyui.server.AuthServlet.java

/**
 * The JDK will return -1 for the port number if no port is specified, but the URLs can 
 * also have a default port.  This method handles getting the port number including the
 * default ports for the URL./*from  w w w. j  ava2  s .  co m*/
 * 
 * @param url    the URL to get the port for
 * 
 * @return the port from the URL
 */
private int getPort(URL url) {
    if (url.getPort() == -1) {
        return url.getDefaultPort();
    } else {
        return url.getPort();
    }
}

From source file:com.norconex.commons.lang.url.HttpURL.java

/**
 * Constructor.//from  w  w  w.j a va 2  s  .co m
 * @param url a URL
 */
public HttpURL(String url) {
    if (url.startsWith("http")) {
        URL urlwrap;
        try {
            urlwrap = new URL(url);
        } catch (MalformedURLException e) {
            throw new URLException("Could not interpret URL: " + url, e);
        }
        protocol = urlwrap.getProtocol();
        host = urlwrap.getHost();
        port = urlwrap.getPort();
        path = urlwrap.getPath();
    }

    // Parameters
    if (StringUtils.contains(url, "?")) {
        queryString = new QueryString(url);
    }
}

From source file:com.frentix.restapi.RestConnection.java

/**
 * Creates a new RestConnection instance.
 * /*from   w w  w .j  a  v a2s.co m*/
 * @param url URL to remote location.
 */
public RestConnection(URL url) {
    this(url.getHost(), url.getPort(), url.getProtocol(), url.getPath(), LOGIN, PASSWORD);
}

From source file:jhttpp2.Jhttpp2ClientInputStream.java

/**
 * Parser for the first (!) line from the HTTP request<BR>
 * Sets up the URL, method and remote hostname.
 * //from  ww w.  j a v a2 s .  c  o  m
 * @return an InetAddress for the hostname, null on errors with a
 *         statuscode!=SC_OK
 */
public InetAddress parseRequest(String a, int method_index) {
    log.debug(a);
    String f;
    int pos;
    url = "";
    if (ssl) {
        f = a.substring(8);
    } else {
        method = a.substring(0, a.indexOf(" ")); // first word in the line
        pos = a.indexOf(":"); // locate first :
        if (pos == -1) { // occours with "GET / HTTP/1.1"
            url = a.substring(a.indexOf(" ") + 1, a.lastIndexOf(" "));
            if (method_index == 0) { // method_index==0 --> GET
                if (url.indexOf(server.WEB_CONFIG_FILE) != -1)
                    statuscode = Jhttpp2HTTPSession.SC_CONFIG_RQ;
                else
                    statuscode = Jhttpp2HTTPSession.SC_FILE_REQUEST;
            } else {
                if (method_index == 1 && url.indexOf(server.WEB_CONFIG_FILE) != -1) { // allow
                    // "POST"
                    // for
                    // admin
                    // log
                    // in
                    statuscode = Jhttpp2HTTPSession.SC_CONFIG_RQ;
                } else {
                    statuscode = Jhttpp2HTTPSession.SC_INTERNAL_SERVER_ERROR;
                    errordescription = "This WWW proxy supports only the \"GET\" method while acting as webserver.";
                }
            }
            return null;
        }
        f = a.substring(pos + 3); // removes "http://"
    }
    pos = f.indexOf(" "); // locate space, should be the space before
    // "HTTP/1.1"
    if (pos == -1) { // buggy request
        statuscode = Jhttpp2HTTPSession.SC_CLIENT_ERROR;
        errordescription = "Your browser sent an invalid request: \"" + a + "\"";
        return null;
    }
    f = f.substring(0, pos); // removes all after space
    // if the url contains a space... it's not our mistake...(url's must
    // never contain a space character)
    pos = f.indexOf("/"); // locate the first slash
    if (pos != -1) {
        url = f.substring(pos); // saves path without hostname
        f = f.substring(0, pos); // reduce string to the hostname
    } else
        url = "/"; // occurs with this request:
    // "GET http://localhost HTTP/1.1"
    pos = f.indexOf(":"); // check for the portnumber
    if (pos != -1) {
        String l_port = f.substring(pos + 1);
        l_port = l_port.indexOf(" ") != -1 ? l_port.substring(0, l_port.indexOf(" ")) : l_port;
        int i_port = 80;
        try {
            i_port = Integer.parseInt(l_port);
        } catch (NumberFormatException e_get_host) {
            server.writeLog("get_Host :" + e_get_host + " !!!!");
        }
        f = f.substring(0, pos);
        remote_port = i_port;
    } else
        remote_port = 80;
    remote_host_name = f;

    SortedMap<String, URL> map = server.getHostRedirects();
    for (String redirectHost : map.keySet()) {
        if (redirectHost.equals(remote_host_name)) {
            URL u = map.get(redirectHost);
            f = u.getHost();
            if (u.getPort() != -1) {
                remote_port = u.getPort();
            }
            log.debug("Redirecting host " + redirectHost + " to " + f + " port " + remote_port);
        }
    }
    InetAddress address = null;
    if (server.log_access)
        server.logAccess(connection.getLocalSocket().getInetAddress().getHostAddress() + " " + method + " "
                + getFullURL());
    try {
        address = InetAddress.getByName(f);
        if (remote_port == server.port && address.equals(InetAddress.getLocalHost())) {
            if (url.indexOf(server.WEB_CONFIG_FILE) != -1 && (method_index == 0 || method_index == 1))
                statuscode = Jhttpp2HTTPSession.SC_CONFIG_RQ;
            else if (method_index > 0) {
                statuscode = Jhttpp2HTTPSession.SC_INTERNAL_SERVER_ERROR;
                errordescription = "This WWW proxy supports only the \"GET\" method while acting as webserver.";
            } else
                statuscode = Jhttpp2HTTPSession.SC_FILE_REQUEST;
        }
    } catch (UnknownHostException e_u_host) {
        if (!server.use_proxy)
            statuscode = Jhttpp2HTTPSession.SC_HOST_NOT_FOUND;
    }
    return address;
}

From source file:io.personium.engine.rs.AbstractService.java

/**
 * ??./*w ww. ja v  a 2s .c  o  m*/
 * @param req Request
 * @param res Response
 * @return URL
 * @throws MalformedURLException 
 */
public final String parseRequestUri(final HttpServletRequest req, final HttpServletResponse res)
        throws MalformedURLException {
    String baseUrl = "";

    // URI??URI????????
    //  /personium-engine/engine-test/ds-engine-test/service/hello?a=b&c=d
    // DC??????URI?????????
    // ?Personium-Engine???URI???
    String requestUri = req.getHeader(KEY_HEADER_REQUEST_URI);
    if (requestUri == null || requestUri.length() == 0) {
        requestUri = req.getRequestURI();
        String query = req.getQueryString();
        if (query != null && query.length() > 0) {
            requestUri += "?" + query;
        }
    }

    // URI??????????
    // /personium-engine/engine-test/personium-engine-test/service/hello
    int indexQ = requestUri.indexOf("?");
    String scriptName = requestUri;
    if (indexQ > 0) {
        scriptName = requestUri.substring(0, indexQ);
    }

    // ?????JSGI????
    req.setAttribute("env.requestUri", requestUri);
    req.setAttribute("scriptName", scriptName);

    // ?????URL??
    // DC??????URL?????????
    baseUrl = req.getHeader(KEY_HEADER_BASEURL);

    // ?????JSGI????
    URL baseUrlObj = new URL(baseUrl);
    int port = baseUrlObj.getPort();
    String proto = baseUrlObj.getProtocol();
    String host = baseUrlObj.getHost();
    String hostHeader = host;
    if (port < 0) {
        if ("http".endsWith(proto)) {
            port = PORT_HTTP;
        }
        if ("https".endsWith(proto)) {
            port = PORT_HTTPS;
        }
    } else {
        hostHeader += ":" + port;
    }
    req.setAttribute("HostHeader", hostHeader);
    req.setAttribute("host", baseUrlObj.getHost());
    req.setAttribute("port", port);
    req.setAttribute("scheme", proto);
    return baseUrl;
}

From source file:cm.aptoide.pt.RssHandler.java

private void getIcon(String uri, String name) {
    String url = mserver + "/" + uri;
    String file = mctx.getString(R.string.icons_path) + name;

    /*File exists = new File(file);
    if(exists.exists()){/*from w w  w . ja  v a 2s .co  m*/
       return;
    }*/

    try {
        FileOutputStream saveit = new FileOutputStream(file);
        DefaultHttpClient mHttpClient = new DefaultHttpClient();
        HttpGet mHttpGet = new HttpGet(url);

        if (requireLogin) {
            URL mUrl = new URL(url);
            mHttpClient.getCredentialsProvider().setCredentials(new AuthScope(mUrl.getHost(), mUrl.getPort()),
                    new UsernamePasswordCredentials(usern, passwd));
        }

        HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
        if (mHttpResponse.getStatusLine().getStatusCode() == 401) {
            return;
        } else if (mHttpResponse.getStatusLine().getStatusCode() == 403) {
            return;
        } else {
            byte[] buffer = EntityUtils.toByteArray(mHttpResponse.getEntity());
            saveit.write(buffer);
        }
    } catch (IOException e) {
    } catch (IllegalArgumentException e) {
    }

}

From source file:com.github.mike10004.jenkinsbbhook.WebhookHandler.java

protected CredentialsProvider asCredentialsProvider(URL jenkinsCrumbUrl, AppParams appParams, String username,
        String apiToken) {/*w  w  w  .  ja v  a 2  s  .  co m*/
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    String jenkinsHost = jenkinsCrumbUrl.getHost();
    int jenkinsPort = jenkinsCrumbUrl.getPort();
    if (jenkinsPort == -1) {
        jenkinsPort = jenkinsCrumbUrl.getDefaultPort();
    }
    AuthScope scope = new AuthScope(jenkinsHost, jenkinsPort);
    credentialsProvider.setCredentials(scope, new UsernamePasswordCredentials(username, apiToken));
    return credentialsProvider;
}

From source file:fr.itldev.koya.services.impl.UserServiceImpl.java

/**
 * Authenticated RestTemplate Factory./*from ww w . j  a  va2  s .co m*/
 *
 * @return
 */
private RestTemplate getAuthenticatedRestTemplate(String login, String password) throws MalformedURLException {
    URL url = new URL(getAlfrescoServerUrl());

    RestTemplate userRestTemplate = new RestClient(login, password, url.getHost(), url.getPort(),
            url.getProtocol());
    List<HttpMessageConverter<?>> msgConverters = new ArrayList<>();
    msgConverters.add((HttpMessageConverter<?>) beanFactory.getBean("stringHttpMessageConverter"));
    msgConverters.add((HttpMessageConverter<?>) beanFactory.getBean("jsonHttpMessageConverter"));
    msgConverters.add((HttpMessageConverter<?>) beanFactory.getBean("formHttpMessageConverter"));
    userRestTemplate.setMessageConverters(msgConverters);
    userRestTemplate.setErrorHandler((ResponseErrorHandler) beanFactory.getBean("alfrescoRestErrorHandler"));

    return userRestTemplate;
}