List of usage examples for java.net URL getHost
public String getHost()
From source file:com.manning.blogapps.chapter10.atomclient.AtomBlogConnection.java
/** * Create Atom blog client instance for specified URL and user account. * @param url End-point URL of Atom service * @param username Username of account * @param password Password of account *//* w ww. j a va 2 s .c om*/ public AtomBlogConnection(String url, String username, String password) throws BlogClientException { Document doc = null; try { URL feedURL = new URL(url); Credentials creds = new UsernamePasswordCredentials(username, password); httpClient.getState().setAuthenticationPreemptive(true); httpClient.getState().setCredentials(null, feedURL.getHost(), creds); GetMethod method = new GetMethod(url); addAuthentication(method, username, password); httpClient.executeMethod(method); SAXBuilder builder = new SAXBuilder(); doc = builder.build(method.getResponseBodyAsStream()); } catch (Throwable t) { throw new BlogClientException("Error connecting to blog server", t); } AtomService service = AtomService.documentToService(doc); Iterator iter = service.getWorkspaces().iterator(); int count = 0; while (iter.hasNext()) { AtomService.Workspace workspace = (AtomService.Workspace) iter.next(); Blog blog = new AtomBlog(httpClient, username, password, workspace); blogSites.put(blog.getToken(), blog); } }
From source file:com.cladonia.xngreditor.URLUtilities.java
/** * Returns a String representation without the Username and Password. * /*from w w w. j ava2s . co m*/ * @param url the url to convert to a String... * * @return the string representation. */ public static String toString(URL url) { if (url != null) { StringBuffer result = new StringBuffer(url.getProtocol()); result.append(":"); if (url.getHost() != null && url.getHost().length() > 0) { result.append("//"); result.append(url.getHost()); } if (url.getPort() > 0) { result.append(":"); result.append(url.getPort()); } if (url.getPath() != null) { result.append(url.getPath()); } if (url.getQuery() != null) { result.append('?'); result.append(url.getQuery()); } if (url.getRef() != null) { result.append("#"); result.append(url.getRef()); } return result.toString(); } return null; }
From source file:com.romeikat.datamessie.core.base.service.download.AbstractDownloader.java
protected String getHost(final String url) { final URL urlAsUrl; try {/* w ww .j a va2s.co m*/ urlAsUrl = new URL(url); } catch (final Exception e) { return null; } final String host = urlAsUrl.getHost(); return host; }
From source file:edu.si.services.sidora.rest.batch.beans.BatchRequestControllerBean.java
/** * Check Resource MimeType using Apache Tika * @param exchange//ww w . j av a 2 s .com * @throws URISyntaxException * @throws MalformedURLException */ public void getMIMEType(Exchange exchange) throws URISyntaxException, MalformedURLException { /** * TODO: * * Need to make sure that mimetypes are consistent with what's used in workbench. * See link for workbench mimetype list * * https://github.com/Smithsonian/sidora-workbench/blob/master/workbench/includes/utils.inc#L1119 * */ out = exchange.getIn(); URL url = new URL(out.getHeader("resourceFile", String.class)); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); String resourceFile = uri.toASCIIString(); String resourceFileExt = FilenameUtils.getExtension(resourceFile); String mimeType = null; if (resourceFileExt.equalsIgnoreCase("nef")) { mimeType = "image/x-nikon-nef"; } else if (resourceFileExt.equalsIgnoreCase("dng")) { mimeType = "image/x-adobe-dng"; } else { LOG.debug("Checking {} for MIME Type", resourceFile); mimeType = new Tika().detect(resourceFile); } LOG.debug("Batch Process " + resourceFile + " || MIME=" + mimeType); out.setHeader("dsMIME", mimeType); }
From source file:com.predic8.membrane.core.interceptor.balancer.NodeOnlineChecker.java
public Node getNodeFromExchange(Exchange exc, int destination) { URL destUrl = getUrlObjectFromDestination(exc, destination); return new Node(destUrl.getProtocol() + "://" + destUrl.getHost(), destUrl.getPort()); }
From source file:com.twinsoft.convertigo.engine.admin.services.mobiles.GetBuildStatus.java
@Override protected void getServiceResult(HttpServletRequest request, Document document) throws Exception { String project = Keys.project.value(request); MobileApplication mobileApplication = getMobileApplication(project); if (mobileApplication == null) { throw new ServiceException("no such mobile application"); } else {//from ww w . ja v a2s.co m boolean bAdminRole = Engine.authenticatedSessionManager.hasRole(request.getSession(), Role.WEB_ADMIN); if (!bAdminRole && mobileApplication.getAccessibility() == Accessibility.Private) { throw new AuthenticationException("Authentication failure: user has not sufficient rights!"); } } String platformName = Keys.platform.value(request); String mobileBuilderPlatformURL = EnginePropertiesManager .getProperty(PropertyName.MOBILE_BUILDER_PLATFORM_URL); URL url = new URL(mobileBuilderPlatformURL + "/getstatus"); HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setHost(url.getHost()); HttpState httpState = new HttpState(); Engine.theApp.proxyManager.setProxy(hostConfiguration, httpState, url); PostMethod method = new PostMethod(url.toString()); JSONObject jsonResult; try { HeaderName.ContentType.setRequestHeader(method, MimeType.WwwForm.value()); method.setRequestBody(new NameValuePair[] { new NameValuePair("application", mobileApplication.getComputedApplicationName()), new NameValuePair("platformName", platformName), new NameValuePair("auth_token", mobileApplication.getComputedAuthenticationToken()), new NameValuePair("endpoint", mobileApplication.getComputedEndpoint(request)) }); int methodStatusCode = Engine.theApp.httpClient.executeMethod(hostConfiguration, method, httpState); InputStream methodBodyContentInputStream = method.getResponseBodyAsStream(); byte[] httpBytes = IOUtils.toByteArray(methodBodyContentInputStream); String sResult = new String(httpBytes, "UTF-8"); if (methodStatusCode != HttpStatus.SC_OK) { throw new ServiceException( "Unable to get building status for application '" + project + "' (final app name: '" + mobileApplication.getComputedApplicationName() + "').\n" + sResult); } jsonResult = new JSONObject(sResult); } finally { method.releaseConnection(); } Element statusElement = document.createElement("build"); statusElement.setAttribute(Keys.project.name(), project); statusElement.setAttribute(Keys.platform.name(), platformName); if (jsonResult.has(platformName + "_status")) { statusElement.setAttribute("status", jsonResult.getString(platformName + "_status")); } else { statusElement.setAttribute("status", "none"); } if (jsonResult.has(platformName + "_error")) { statusElement.setAttribute("error", jsonResult.getString(platformName + "_error")); } statusElement.setAttribute("version", jsonResult.has("version") ? jsonResult.getString("version") : "n/a"); statusElement.setAttribute("phonegap_version", jsonResult.has("phonegap_version") ? jsonResult.getString("phonegap_version") : "n/a"); statusElement.setAttribute("revision", jsonResult.has("revision") ? jsonResult.getString("revision") : "n/a"); statusElement.setAttribute("endpoint", jsonResult.has("endpoint") ? jsonResult.getString("endpoint") : "n/a"); document.getDocumentElement().appendChild(statusElement); }
From source file:com.esri.gpt.control.webharvest.client.waf.FtpClientRequest.java
/** * Creates instance of the request./*w w w . java 2 s . c om*/ * @param url url * @param cp credential provider */ public FtpClientRequest(URL url, CredentialProvider cp) { this.protocol = Val.chkStr(url.getProtocol()); this.host = Val.chkStr(url.getHost()); this.port = url.getPort() >= 0 ? url.getPort() : 21; this.root = Val.chkStr(url.getPath()).replaceAll("/$", ""); this.cp = cp; }
From source file:de.escidoc.core.http.HttpClientBuilderImpl.java
@Override public HttpClientBuilder withUsernamePasswordCredentials(final String urlString, final String username, final String password) { final URL url; try {//from ww w .j a v a2s .c om url = new URL(urlString); } catch (final MalformedURLException e) { throw new IllegalArgumentException("Invalid url '" + urlString + "'.", e); } final AuthScope authScope = new AuthScope(url.getHost(), AuthScope.ANY_PORT, AuthScope.ANY_REALM); final Credentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password); this.httpClient.getCredentialsProvider().setCredentials(authScope, usernamePasswordCredentials); return this; }
From source file:com.lazerycode.ebselen.customhandlers.FileDownloader.java
public String downloader(WebElement element, String attribute) throws Exception { //Assuming that getAttribute does some magic to return a fully qualified URL String downloadLocation = element.getAttribute(attribute); if (downloadLocation.trim().equals("")) { throw new Exception("The element you have specified does not link to anything!"); }/*from w ww . ja v a2s . c om*/ URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.RFC_2965); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadURL.getPath()); FileHandler downloadedFile = new FileHandler( downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true); try { int status = client.executeMethod(getRequest); LOGGER.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm()); BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream()); int offset = 0; int len = 4096; int bytes = 0; byte[] block = new byte[len]; while ((bytes = in.read(block, offset, len)) > -1) { downloadedFile.getWritableFileOutputStream().write(block, 0, bytes); } downloadedFile.close(); in.close(); LOGGER.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile()); } catch (Exception Ex) { LOGGER.error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return downloadedFile.getAbsoluteFile(); }
From source file:io.fabric8.api.registry.rules.CamelEndpointFinder.java
protected String switchToUseProxyLink(String uriText) { try {/*from w w w. ja v a 2 s . co m*/ URL url = new URL(uriText); String host = url.getHost(); if (Strings.isNotBlank(host)) { int port = url.getPort(); if (port <= 0) { port = 80; } return urlPathJoin("/hawtio/proxy/" + host + "/" + port, url.getPath()); } } catch (MalformedURLException e) { LOG.warn("Failed to parse URL " + uriText + ". " + e, e); } return uriText; }