List of usage examples for java.net URL toExternalForm
public String toExternalForm()
From source file:com.digitalpebble.storm.crawler.bolt.ParserBolt.java
private List<Outlink> toOutlinks(String parentURL, List<Link> links, Metadata parentMetadata) { List<Outlink> outlinks = new ArrayList<Outlink>(links.size()); URL url_;/*from www. jav a 2 s.co m*/ try { url_ = new URL(parentURL); } catch (MalformedURLException e1) { // we would have known by now as previous // components check whether the URL is valid LOG.error("MalformedURLException on {}", parentURL); eventCounter.scope("error_invalid_source_url").incrBy(1); return outlinks; } for (Link l : links) { if (StringUtils.isBlank(l.getUri())) { continue; } String urlOL = null; // build an absolute URL try { URL tmpURL = URLUtil.resolveURL(url_, l.getUri()); urlOL = tmpURL.toExternalForm(); } catch (MalformedURLException e) { LOG.debug("MalformedURLException on {}", l.getUri()); eventCounter.scope("error_outlink_parsing_" + e.getClass().getSimpleName()).incrBy(1); continue; } // applies the URL filters if (urlFilters != null) { urlOL = urlFilters.filter(url_, parentMetadata, urlOL); if (urlOL == null) { eventCounter.scope("outlink_filtered").incrBy(1); continue; } } eventCounter.scope("outlink_kept").incrBy(1); Outlink ol = new Outlink(urlOL); // add the anchor ol.setAnchor(l.getText()); // get the metadata for the outlink from the parent ones ol.setMetadata(metadataTransfer.getMetaForOutlink(urlOL, parentURL, parentMetadata)); outlinks.add(ol); } return outlinks; }
From source file:org.apache.juddi.v3.client.mapping.wsdl.WSDLLocatorImpl.java
private InputSource getImportFromFile(String url) { InputSource inputSource = null; try {/*from w w w. j a v a2 s. c o m*/ URL importUrl = new URL(url); inputStream = importUrl.openStream(); inputSource = new InputSource(inputStream); latestImportURI = importUrl.toExternalForm(); } catch (Exception e) { log.error(e.getMessage()); log.debug(e.getMessage(), e); lastException = e; } return inputSource; }
From source file:org.motechproject.server.osgi.OsgiFrameworkService.java
/** * Initialize and start the OSGi framework *///from w ww . j a v a 2s.co m public void start() { try { ServletContext servletContext = ((WebApplicationContext) applicationContext).getServletContext(); osgiFramework.init(); BundleContext bundleContext = osgiFramework.getBundleContext(); // This is mandatory for Felix http servlet bridge servletContext.setAttribute(BundleContext.class.getName(), bundleContext); // install bundles ArrayList<Bundle> bundles = new ArrayList<Bundle>(); for (URL url : findBundles(servletContext)) { logger.debug("Installing bundle [" + url + "]"); Bundle bundle = bundleContext.installBundle(url.toExternalForm()); bundles.add(bundle); storeClassCloader(bundle); } for (Bundle bundle : bundles) { // custom bundle loaders if (bundleLoaders != null) { for (BundleLoader loader : bundleLoaders) { loader.loadBundle(bundle); } } bundle.start(); } osgiFramework.start(); logger.info("OSGi framework started"); } catch (Throwable e) { logger.error("Failed to start OSGi framework", e); throw new RuntimeException(e); } }
From source file:net.sf.ehcache.config.Configurator.java
/** * Configures a bean from an XML file available as an URL. *///from w w w .j ava2 s .c om public void configure(final Object bean, final URL url) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("Configuring ehcache from URL: " + url); } final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); final BeanHandler handler = new BeanHandler(bean); parser.parse(url.toExternalForm(), handler); }
From source file:org.jboss.ejb3.packagemanager.retriever.impl.HttpPackageRetriever.java
/** * @see org.jboss.ejb3.packagemanager.retriever.PackageRetriever#retrievePackage(PackageManagerContext, URL) *///from w w w . j a va 2s.c om @Override public File retrievePackage(PackageManagerContext pkgMgrCtx, URL packagePath) throws PackageRetrievalException { if (!packagePath.getProtocol().equals("http")) { throw new PackageRetrievalException("Cannot handle " + packagePath); } HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(packagePath.toExternalForm()); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); } catch (Exception e) { throw new PackageRetrievalException("Exception while retrieving package " + packagePath, e); } if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new PackageRetrievalException("Http retrieval wasn't successful, returned status code " + httpResponse.getStatusLine().getStatusCode()); } HttpEntity httpEntity = httpResponse.getEntity(); try { // TODO: should this tmp be deleted on exit? File tmpPkgFile = File.createTempFile("tmp", ".jar", pkgMgrCtx.getPackageManagerEnvironment().getPackageManagerTmpDir()); FileOutputStream fos = new FileOutputStream(tmpPkgFile); BufferedOutputStream bos = null; BufferedInputStream bis = null; try { bos = new BufferedOutputStream(fos); InputStream is = httpEntity.getContent(); bis = new BufferedInputStream(is); byte[] content = new byte[4096]; int length; while ((length = bis.read(content)) != -1) { bos.write(content, 0, length); } bos.flush(); } finally { if (bos != null) { bos.close(); } if (bis != null) { bis.close(); } } return tmpPkgFile; } catch (IOException ioe) { throw new PackageRetrievalException("Could not process the retrieved package", ioe); } // TODO: I need to read the HttpClient 4.x javadocs to figure out the API for closing the // Http connection }
From source file:gov.nih.nci.caintegrator.web.action.analysis.GenePatternAnalysisForm.java
/** * Returns the URL where information on the currently selected analysis method may be found. * * @return the method information URL.//from w w w . j a v a 2 s .co m */ public String getAnalysisMethodInformationUrl() { try { URL serviceUrl = new URL(server.getUrl()); URL infoUrl = new URL(serviceUrl.getProtocol(), serviceUrl.getHost(), serviceUrl.getPort(), "/gp/getTaskDoc.jsp"); return infoUrl.toExternalForm(); } catch (MalformedURLException e) { throw new IllegalStateException("Server URL should already have been validated.", e); } }
From source file:org.bitrepository.protocol.http.HttpFileExchange.java
/** * Method for putting data on the HTTP-server of a given url. * //from www . j a v a 2s.co m * TODO perhaps make it synchronized around the URL, to prevent data from * trying to uploaded several times to the same location simultaneously. * * @param in The data to put into the url. * @param url The place to put the data. * @throws IOException If a problem with the connection occurs during the * transaction. Also if the response code is 300 or above, which indicates * that the transaction has not been successful. */ private void performUpload(InputStream in, URL url) throws IOException { HttpClient httpClient = null; try { httpClient = getHttpClient(); HttpPut httpPut = new HttpPut(url.toExternalForm()); InputStreamEntity reqEntity = new InputStreamEntity(in, -1); reqEntity.setChunked(true); httpPut.setEntity(reqEntity); HttpResponse response = httpClient.execute(httpPut); // HTTP code >= 300 means error! if (response.getStatusLine().getStatusCode() >= HTTP_ERROR_CODE_BARRIER) { throw new IOException("Could not upload file to URL '" + url.toExternalForm() + "'. got status code '" + response.getStatusLine() + "'"); } log.debug("Uploaded datastream to url '" + url.toString() + "' and " + "received the response line '" + response.getStatusLine() + "'."); } finally { if (httpClient != null) { httpClient.getConnectionManager().shutdown(); } } }
From source file:org.apache.jmeter.protocol.http.control.HC4CookieHandler.java
@Override public void addCookieFromHeader(CookieManager cookieManager, boolean checkCookies, String cookieHeader, URL url) { boolean debugEnabled = log.isDebugEnabled(); if (debugEnabled) { log.debug("Received Cookie: " + cookieHeader + " From: " + url.toExternalForm()); }/*from w ww . j a v a2s . co m*/ String protocol = url.getProtocol(); String host = url.getHost(); int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort()); String path = url.getPath(); boolean isSecure = HTTPSamplerBase.isSecure(protocol); List<org.apache.http.cookie.Cookie> cookies = null; CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, isSecure); BasicHeader basicHeader = new BasicHeader(HTTPConstants.HEADER_SET_COOKIE, cookieHeader); try { cookies = cookieSpec.parse(basicHeader, cookieOrigin); } catch (MalformedCookieException e) { log.error("Unable to add the cookie", e); } if (cookies == null) { return; } for (org.apache.http.cookie.Cookie cookie : cookies) { try { if (checkCookies) { cookieSpec.validate(cookie, cookieOrigin); } Date expiryDate = cookie.getExpiryDate(); long exp = 0; if (expiryDate != null) { exp = expiryDate.getTime(); } Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.isSecure(), exp / 1000, ((BasicClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR), ((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR), cookie.getVersion()); // Store session cookies as well as unexpired ones if (exp == 0 || exp >= System.currentTimeMillis()) { cookieManager.add(newCookie); // Has its own debug log; removes matching cookies } else { cookieManager.removeMatchingCookies(newCookie); if (debugEnabled) { log.info("Dropping expired Cookie: " + newCookie.toString()); } } } catch (MalformedCookieException e) { // This means the cookie was wrong for the URL log.warn("Not storing invalid cookie: <" + cookieHeader + "> for URL " + url + " (" + e.getLocalizedMessage() + ")"); } catch (IllegalArgumentException e) { log.warn(cookieHeader + e.getLocalizedMessage()); } } }
From source file:com.digitalpebble.storm.crawler.indexing.AbstractIndexerBolt.java
/** * Returns the value to be used as the URL for indexing purposes, if present * the canonical value is used instead//from www . j av a2 s . com */ protected String valueForURL(Tuple tuple) { String url = tuple.getStringByField("url"); Metadata metadata = (Metadata) tuple.getValueByField("metadata"); // functionality deactivated if (StringUtils.isBlank(canonicalMetadataParamName)) { return url; } String canonicalValue = metadata.getFirstValue(canonicalMetadataName); // no value found? if (StringUtils.isBlank(canonicalValue)) { return url; } try { URL sURL = new URL(url); URL canonical = URLUtil.resolveURL(sURL, canonicalValue); // check is the same host if (sURL.getHost().equals(canonical.getHost())) { return canonical.toExternalForm(); } else { LOG.info("Canonical URL references a different host, ignoring in {} ", url); } } catch (MalformedURLException e) { LOG.error("Malformed canonical URL {} was found in {} ", canonicalValue, url); } return url; }
From source file:org.jboss.as.test.manualmode.web.valve.authenticator.GlobalAuthenticatorTestCase.java
/** * Testing that if authenticator valve is disabled then it is not used *//* ww w . java2 s . co m*/ @Test @InSequence(10) @OperateOnDeployment(value = DEPLOYMENT_NAME_2) public void testValveAuthDisable(@ArquillianResource URL url, @ArquillianResource ManagementClient client) throws Exception { ValveUtil.activateValve(client, CUSTOM_AUTHENTICATOR_2, false); ValveUtil.reload(client); String appUrl = url.toExternalForm() + WEB_APP_URL_2; Header[] valveHeaders = ValveUtil.hitValve(new URL(appUrl), HttpURLConnection.HTTP_NOT_FOUND); assertEquals("Auth valve is disabled => expecting no valve headers: " + Arrays.toString(valveHeaders), 0, valveHeaders.length); }