List of usage examples for java.net URL getProtocol
public String getProtocol()
From source file:dk.netarkivet.common.distribute.HTTPRemoteFileRegistry.java
/** * Get the url for cleaning up after a remote file registered under some * URL./*from w ww. ja v a 2 s . c om*/ * @param url some URL * * @return the cleanup url. * @throws MalformedURLException If unable to construct the cleanup url */ URL getCleanupUrl(URL url) throws MalformedURLException { return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + UNREGISTER_URL_POSTFIX); }
From source file:edu.si.services.sidora.rest.batch.beans.BatchRequestControllerBean.java
/** * Check Resource MimeType using Apache Tika * @param exchange/* w w w. ja va 2s . co m*/ * @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.liferay.httpservice.internal.http.DefaultHttpContextTest.java
protected void getResourceAndVerify(String path) { URL url = _defaultHttpContext.getResource(path); Assert.assertNotNull(url);/*from w w w. j ava2 s . c om*/ Assert.assertEquals(_FILE, url.getFile()); Assert.assertEquals(_HOST, url.getHost()); Assert.assertEquals(_PROTOCOL, url.getProtocol()); }
From source file:wsattacker.plugin.intelligentdos.requestSender.Http4RequestSenderImpl.java
public String sendRequestHttpClient(RequestObject requestObject) { String strUrl = requestObject.getEndpoint(); String strXml = requestObject.getRequestContent(); StringBuilder result = new StringBuilder(); BufferedReader rd = null;/*from w w w . j a v a 2 s. c o m*/ try { URL url = new URL(strUrl); String protocol = url.getProtocol(); HttpClient client; if (protocol.equalsIgnoreCase("https")) { SSLSocketFactory sf = get(); Scheme httpsScheme = new Scheme("https", url.getPort(), sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); // apache HttpClient version >4.2 should use // BasicClientConnectionManager ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry); client = new DefaultHttpClient(cm); } else { client = new DefaultHttpClient(); } setParamsToClient(client); HttpPost post = new HttpPost(strUrl); setHeader(requestObject, post); ByteArrayEntity entity = new ByteArrayEntity(strXml.getBytes("UTF-8")) { @Override public void writeTo(OutputStream outstream) throws IOException { super.writeTo(outstream); sendLastByte = System.nanoTime(); } }; post.setEntity(entity); HttpResponse response = client.execute(post); receiveFirstByte = System.nanoTime(); rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent(), Charset.defaultCharset())); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } } catch (IOException e) { return e.getMessage(); } catch (RuntimeException e) { return e.getMessage(); } finally { if (rd != null) { try { rd.close(); } catch (IOException e) { e.printStackTrace(); } } } return result.toString(); }
From source file:fr.eolya.utils.http.HttpUtils.java
/** * Encode url//from ww w.j av a 2 s .c o m * * @param url url to be encoded * @return */ public static String urlEncode(String url) { try { URL u = new URL(url); String host = u.getHost(); int indexFile = url.indexOf("/", url.indexOf(host)); if (indexFile == -1) return url; String urlFile = u.getFile(); urlFile = URLDecoder.decode(urlFile, "UTF-8"); String protocol = u.getProtocol(); int port = u.getPort(); if (port != -1 && port != 80 && "http".equals(protocol)) host += ":".concat(String.valueOf(port)); if (port != -1 && port != 443 && "https".equals(protocol)) host += ":".concat(String.valueOf(port)); URI uri = new URI(u.getProtocol(), host, urlFile, null); String ret = uri.toASCIIString(); ret = ret.replaceAll("%3F", "?"); return ret; } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:it.wingstech.csslesser.LessEngine.java
public String compile(URL input) throws LessException { try {//from ww w . ja v a2s. c o m long time = System.currentTimeMillis(); logger.debug("Compiling URL: " + input.getProtocol() + ":" + input.getFile()); String result = call(cf, new Object[] { input.getProtocol() + ":" + input.getFile(), getClass().getClassLoader() }); logger.debug("The compilation of '" + input + "' took " + (System.currentTimeMillis() - time) + " ms."); return result; } catch (Exception e) { throw parseLessException(e); } }
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 . j a va 2 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; }
From source file:com.sonymobile.tools.gerrit.gerritevents.workers.rest.AbstractRestCommandJob.java
@Override public void run() { ReviewInput reviewInput = createReview(); String reviewEndpoint = resolveEndpointURL(); HttpPost httpPost = createHttpPostEntity(reviewInput, reviewEndpoint); if (httpPost == null) { return;//from w w w . ja va2 s .c o m } DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), config.getHttpCredentials()); if (config.getGerritProxy() != null && !config.getGerritProxy().isEmpty()) { try { URL url = new URL(config.getGerritProxy()); HttpHost proxy = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } catch (MalformedURLException e) { logger.error("Could not parse proxy URL, attempting without proxy.", e); if (altLogger != null) { altLogger.print("ERROR Could not parse proxy URL, attempting without proxy. " + e.getMessage()); } } } try { HttpResponse httpResponse = httpclient.execute(httpPost); String response = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8"); if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { logger.error("Gerrit response: {}", httpResponse.getStatusLine().getReasonPhrase()); if (altLogger != null) { altLogger.print("ERROR Gerrit response: " + httpResponse.getStatusLine().getReasonPhrase()); } } } catch (Exception e) { logger.error("Failed to submit result to Gerrit", e); if (altLogger != null) { altLogger.print("ERROR Failed to submit result to Gerrit" + e.toString()); } } }
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:eionet.webq.web.interceptor.CdrAuthorizationInterceptor.java
/** * Gets host from url string./*w ww .j a va 2 s . c o m*/ * Result will be in format protocol://host:port. * * @param url url to be checked * @return host or null if provided string is malformed URL */ private String getUrlHost(String url) { try { URL parsedUrl = new URL(url); return parsedUrl.getProtocol() + "://" + parsedUrl.getAuthority(); } catch (MalformedURLException e) { return null; } }