List of usage examples for java.net URL getHost
public String getHost()
From source file:com.kagilum.plugins.icescrum.IceScrumSession.java
private void setAuthentication() throws MalformedURLException { int port;/*from w w w. j ava 2 s . c o m*/ URL url = new URL(settings.getUrl() + "/version/"); if (url.getPort() == -1) { port = url.getDefaultPort(); } else { port = url.getPort(); } client.getState().setCredentials(new AuthScope(url.getHost(), port), new UsernamePasswordCredentials(settings.getUsername(), settings.getPassword())); }
From source file:org.apache.ambari.server.view.ViewURLStreamProvider.java
/** * Is it allowed to make calls to the supplied url * @param spec the URL/*from w w w . ja v a 2 s .c o m*/ * @return */ protected boolean isProxyCallAllowed(String spec) { if (StringUtils.isNotBlank(spec) && this.getHostPortRestrictionHandler().proxyCallRestricted()) { try { URL url = new URL(spec); return this.getHostPortRestrictionHandler().allowProxy(url.getHost(), Integer.toString(url.getPort() == -1 ? url.getDefaultPort() : url.getPort())); } catch (MalformedURLException ex) { } } return true; }
From source file:net.java.sip.communicator.service.httputil.HttpUtils.java
/** * Posting form to <tt>address</tt>. For submission we use POST method * which is "application/x-www-form-urlencoded" encoded. * @param httpClient the http client// w ww.j a v a 2 s . co m * @param postMethod the post method * @param address HTTP address. * @param formParamNames the parameter names to include in post. * @param formParamValues the corresponding parameter values to use. * @param usernameParamIx the index of the username parameter in the * <tt>formParamNames</tt> and <tt>formParamValues</tt> * if any, otherwise -1. * @param passwordParamIx the index of the password parameter in the * <tt>formParamNames</tt> and <tt>formParamValues</tt> * if any, otherwise -1. * @param headerParamNames additional header name to include * @param headerParamValues corresponding header value to include * @return the result or null if send was not possible or * credentials ask if any was canceled. */ private static HttpEntity postForm(DefaultHttpClient httpClient, HttpPost postMethod, String address, ArrayList<String> formParamNames, ArrayList<String> formParamValues, int usernameParamIx, int passwordParamIx, RedirectHandler redirectHandler, List<String> headerParamNames, List<String> headerParamValues) throws Throwable { // if we have username and password in the parameters, lets // retrieve their values // if there are already filled skip asking the user Credentials creds = null; if (usernameParamIx != -1 && usernameParamIx < formParamNames.size() && passwordParamIx != -1 && passwordParamIx < formParamNames.size() && (formParamValues.get(usernameParamIx) == null || formParamValues.get(usernameParamIx).length() == 0) && (formParamValues.get(passwordParamIx) == null || formParamValues.get(passwordParamIx).length() == 0)) { URL url = new URL(address); HTTPCredentialsProvider prov = (HTTPCredentialsProvider) httpClient.getCredentialsProvider(); // don't allow empty username while (creds == null || creds.getUserPrincipal() == null || StringUtils.isNullOrEmpty(creds.getUserPrincipal().getName())) { creds = prov.getCredentials(new AuthScope(url.getHost(), url.getPort())); // it was user canceled lets stop processing if (creds == null && !prov.retry()) { return null; } } } // construct the name value pairs we will be sending List<NameValuePair> parameters = new ArrayList<NameValuePair>(); // there can be no params if (formParamNames != null) { for (int i = 0; i < formParamNames.size(); i++) { // we are on the username index, insert retrieved username value if (i == usernameParamIx && creds != null) { parameters .add(new BasicNameValuePair(formParamNames.get(i), creds.getUserPrincipal().getName())); } // we are on the password index, insert retrieved password val else if (i == passwordParamIx && creds != null) { parameters.add(new BasicNameValuePair(formParamNames.get(i), creds.getPassword())); } else // common name value pair, all info is present { parameters.add(new BasicNameValuePair(formParamNames.get(i), formParamValues.get(i))); } } } // our custom strategy, will check redirect handler should we redirect // if missing will use the default handler httpClient.setRedirectStrategy(new CustomRedirectStrategy(redirectHandler, parameters)); // Uses String UTF-8 to keep compatible with android version and // older versions of the http client libs, as the one used // in debian (4.1.x) String s = URLEncodedUtils.format(parameters, "UTF-8"); StringEntity entity = new StringEntity(s, "UTF-8"); // set content type to "application/x-www-form-urlencoded" entity.setContentType(URLEncodedUtils.CONTENT_TYPE); // insert post values encoded. postMethod.setEntity(entity); if (headerParamNames != null) { for (int i = 0; i < headerParamNames.size(); i++) { postMethod.addHeader(headerParamNames.get(i), headerParamValues.get(i)); } } // execute post return executeMethod(httpClient, postMethod, redirectHandler, parameters); }
From source file:cn.vlabs.duckling.vwb.PromitionLogFilter.java
private String getRefererHost(String referer) { URL refererUrl = null; try {/*from w ww. j a v a 2 s . c o m*/ refererUrl = new URL(referer); } catch (MalformedURLException e) { refererUrl = null; } if (refererUrl == null) { return PROMOTION_HOST_EMPTY; } return refererUrl.getHost(); }
From source file:org.picketbox.test.authentication.http.jetty.DelegatingSecurityFilterHTTPBasicUnitTestCase.java
@Test public void testBasicAuth() throws Exception { URL url = new URL(urlStr); DefaultHttpClient httpclient = null; try {//w ww .j av a2 s .c o m String user = "Aladdin"; String pass = "Open Sesame"; httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass)); HttpGet httpget = new HttpGet(url.toExternalForm()); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } assertEquals(200, statusLine.getStatusCode()); EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:org.uiautomation.ios.server.grid.RegistrationRequest.java
public void registerToHub() { HttpClient client = new DefaultHttpClient(); try {//from w ww. ja v a 2s . co m URL registration = new URL(hubURL); BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST", registration.toExternalForm()); String json = getJSONRequest().toString(); r.setEntity(new StringEntity(json)); HttpHost host = new HttpHost(registration.getHost(), registration.getPort()); HttpResponse response = client.execute(host, r); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Error sending the registration request."); } } catch (Exception e) { throw new WebDriverException("Error sending the registration request.", e); } }
From source file:eu.markov.jenkins.plugin.mvnmeta.MavenMetadataParameterDefinitionBackwardCompatibility.java
private String createRepoBaseUrlWithImplicitUser() throws MalformedURLException { StringBuilder credentialsIdBuilder = new StringBuilder(); URL url = new URL(this.getRepoBaseUrl()); credentialsIdBuilder.append(url.getProtocol()); credentialsIdBuilder.append("://"); credentialsIdBuilder.append(this.username); credentialsIdBuilder.append("@"); credentialsIdBuilder.append(url.getHost()); int port = url.getPort(); if (port > -1) { credentialsIdBuilder.append(":"); credentialsIdBuilder.append(port); }/* w ww .j a v a2s . c om*/ credentialsIdBuilder.append(url.getPath()); return credentialsIdBuilder.toString(); }
From source file:Main.java
/** * Checks, whether the URL points to the same service. A service is equal * if the protocol, host and port are equal. * * @param url a url// w w w.jav a2 s . c o m * @param baseUrl an other url, that should be compared. * @return true, if the urls point to the same host and port and use the * same protocol, false otherwise. */ private boolean isSameService(final URL url, final URL baseUrl) { if (!url.getProtocol().equals(baseUrl.getProtocol())) { return false; } if (!url.getHost().equals(baseUrl.getHost())) { return false; } if (url.getPort() != baseUrl.getPort()) { return false; } return true; }