List of usage examples for java.net URL toExternalForm
public String toExternalForm()
From source file:org.archive.wayback.liveweb.StdRemoteLiveWebCache.java
/** * Gets resource object from the live web. Configure timeout to 10 seconds. * /*from www .j a va 2 s. com*/ * @param url to fetch from the live web. * @param maxCacheMS maximum age of resource to return - optionally honored * @param bUseOlder if true, return documents older than maxCacheMS if * a more recent copy is not available. * * @return Resource for url * * @throws LiveDocumentNotAvailableException if the resource cannot be * retrieved from the live web, but all proxying and caching * mechanisms functioned properly * @throws LiveWebCacheUnavailableException if there was a problem either * accessing the live web, in proxying to the live web, or in * maintaining the cache for the live web * @throws LiveWebTimeoutException if there is no response from the live * web cache before a timeout occurred. * @throws IOException for the usual reasons * * @see org.archive.wayback.liveweb.LiveWebCache#getCachedResource(java.net.URL, long, boolean) * @inheritDoc org.archive.wayback.liveweb.LiveWebCache#getCachedResource */ @Override public Resource getCachedResource(URL url, long maxCacheMS, boolean bUseOlder) throws LiveDocumentNotAvailableException, LiveWebCacheUnavailableException, LiveWebTimeoutException, IOException { String urlStr = url.toExternalForm(); if (requestPrefix != null) urlStr = requestPrefix + urlStr; HttpHost proxy = new HttpHost(hostConfiguration.getProxyHost(), hostConfiguration.getProxyPort()); // Set socketTimeout and connectionTimeout to 10 seconds. RequestConfig reqConf = RequestConfig.custom().setProxy(proxy).setSocketTimeout(10000) .setConnectTimeout(10000).setConnectionRequestTimeout(10000).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(reqConf).build(); HttpGet httpGet = new HttpGet(urlStr); try { // The following line gets robots.txt from live web response = httpclient.execute(httpGet); String httpHeaderStr = ""; String bodyStr = ""; /* If it fails to get robots.txt (http status code is 404), then display contents and don't throw exception (socketTimeOutException or connectTimeOutException) */ if (response.getStatusLine().getStatusCode() == 404) { httpHeaderStr = "HTTP/1.0 200 OK\n"; bodyStr = String.format("%s\n%s\n", "User-agent: *", "Allow: /"); } else if (response.getStatusLine().getStatusCode() == 200) { // The following line represents first line in http header httpHeaderStr = String.format("%s %d %s\n", response.getStatusLine().getProtocolVersion(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); // Get robots.txt contents and store it into bodyStr HttpEntity entity = response.getEntity(); bodyStr = EntityUtils.toString(entity); } // Get Http Header and store complete http header in httpHeaderStr for (Header header : response.getAllHeaders()) httpHeaderStr += header.toString() + "\n"; httpHeaderStr += "\n"; int length = httpHeaderStr.length() + bodyStr.length(); /* Using httpHeaderStr and bodyStr to construct responseStr. First line in responseStr should exist. */ // TODO: the following line should be enhanced, // especially the first line in responseStr. String responseStr = String.format("%s %s %d\n%s%s", urlStr, "0.0.0.0 10000000000000 text/plain", length, httpHeaderStr, bodyStr); ByteArrayInputStream bais = new ByteArrayInputStream(responseStr.getBytes()); // TODO: Should not use ARCRecord ARCRecord r = new ARCRecord(bais, "id", 0L, false, false, true); ar = (ArcResource) ResourceFactory.ARCArchiveRecordToResource(r, null); if (ar.getStatusCode() == 502) { throw new LiveDocumentNotAvailableException(urlStr); } else if (ar.getStatusCode() == 504) { throw new LiveWebTimeoutException("Timeout:" + urlStr); } return ar; } catch (ResourceNotAvailableException e) { throw new LiveDocumentNotAvailableException(urlStr); } catch (NoHttpResponseException e) { throw new LiveWebCacheUnavailableException("No Http Response for " + urlStr); } catch (ConnectException e) { throw new LiveWebCacheUnavailableException(e.getLocalizedMessage() + " : " + urlStr); } catch (SocketException e) { throw new LiveWebCacheUnavailableException(e.getLocalizedMessage() + " : " + urlStr); } catch (SocketTimeoutException e) { throw new LiveWebTimeoutException(e.getLocalizedMessage() + " : " + urlStr); } catch (ConnectTimeoutException e) { throw new LiveWebTimeoutException(e.getLocalizedMessage() + " : " + urlStr); } finally { response.close(); } }
From source file:cz.cas.lib.proarc.common.workflow.profile.WorkflowProfiles.java
private Unmarshaller getUnmarshaller() throws JAXBException { JAXBContext jctx = JAXBContext.newInstance(WorkflowDefinition.class); Unmarshaller unmarshaller = jctx.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL schemaUrl = WorkflowDefinition.class.getResource("workflow.xsd"); Schema schema = null;/*from www.j a v a 2 s .com*/ try { schema = sf.newSchema(new StreamSource(schemaUrl.toExternalForm())); } catch (SAXException ex) { throw new JAXBException("Missing schema workflow.xsd!", ex); } unmarshaller.setSchema(schema); ValidationEventCollector errors = new ValidationEventCollector() { @Override public boolean handleEvent(ValidationEvent event) { super.handleEvent(event); return true; } }; unmarshaller.setEventHandler(errors); return unmarshaller; }
From source file:com.elastica.browserfactory.RemoteDriverFactory.java
@Override public WebDriver createWebDriver() throws MalformedURLException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException { DriverConfig webDriverConfig = this.getWebDriverConfig(); DesiredCapabilities capability = null; URL url;/*from ww w. j av a 2s .com*/ url = new URL(webDriverConfig.getHubUrl()); switch (webDriverConfig.getBrowser()) { case FireFox: capability = new FirefoxCapabilitiesFactory().createCapabilities(webDriverConfig); break; case InternetExplore: capability = new IECapabilitiesFactory().createCapabilities(webDriverConfig); break; case Chrome: capability = new ChromeCapabilitiesFactory().createCapabilities(webDriverConfig); break; case HtmlUnit: capability = new HtmlUnitCapabilitiesFactory().createCapabilities(webDriverConfig); break; case Safari: capability = new SafariCapabilitiesFactory().createCapabilities(webDriverConfig); break; case Android: capability = new AndroidCapabilitiesFactory().createCapabilities(webDriverConfig); break; case IPhone: capability = ((ICapabilitiesFactory) Class .forName("com.elastica.browserfactory.IPhoneCapabilitiesFactory").getConstructor() .newInstance()).createCapabilities(webDriverConfig); break; case IPad: capability = ((ICapabilitiesFactory) Class .forName("com.elastica.browserfactory.IPadCapabilitiesFactory").getConstructor().newInstance()) .createCapabilities(webDriverConfig); break; case Opera: capability = new OperaCapabilitiesFactory().createCapabilities(webDriverConfig); break; case PhantomJS: capability = new PhantomJSCapabilitiesFactory().createCapabilities(webDriverConfig); break; default: break; } switch (webDriverConfig.getBrowser()) { case IPhone: case IPad: driver = (WebDriver) Class.forName("com.elastica.browserfactory.RemoteIOSBaseDriver") .getConstructor(URL.class, DesiredCapabilities.class).newInstance(url, capability); break; case FireFox: try { driver = new ScreenShotRemoteWebDriver(url, capability); } catch (RuntimeException e) { if (e.getMessage().contains( "Unable to connect to host 127.0.0.1 on port 7062 after 45000 ms. Firefox console output")) { TestLogging.log("Firefox Driver creation got port customexception, retry after 5 seconds"); WaitHelper.waitForSeconds(5); driver = new ScreenShotRemoteWebDriver(url, capability); } else { throw e; } } break; default: driver = new ScreenShotRemoteWebDriver(url, capability); } setImplicitWaitTimeout(webDriverConfig.getImplicitWaitTimeout()); if (webDriverConfig.getPageLoadTimeout() >= 0) { setPageLoadTimeout(webDriverConfig.getPageLoadTimeout(), webDriverConfig.getBrowser()); } this.setWebDriver(driver); String hub = url.getHost(); int port = url.getPort(); // logging node ip address: try { HttpHost host = new HttpHost(hub, port); DefaultHttpClient client = new DefaultHttpClient(); String sessionUrl = "http://" + hub + ":" + port + "/grid/api/testsession?session="; URL session = new URL(sessionUrl + ((RemoteWebDriver) driver).getSessionId()); BasicHttpEntityEnclosingRequest req; req = new BasicHttpEntityEnclosingRequest("POST", session.toExternalForm()); org.apache.http.HttpResponse response = client.execute(host, req); String responseContent = EntityUtils.toString(response.getEntity()); try { JSONObject object = new JSONObject(responseContent); String proxyId = (String) object.get("proxyId"); String node = (proxyId.split("//")[1].split(":")[0]); String browserName = ((RemoteWebDriver) driver).getCapabilities().getBrowserName(); String version = ((RemoteWebDriver) driver).getCapabilities().getVersion(); System.out.println("WebDriver is running on node " + node + ", " + browserName + version + ", session " + ((RemoteWebDriver) driver).getSessionId()); TestLogging.log("WebDriver is running on node " + node + ", " + browserName + version + ", session " + ((RemoteWebDriver) driver).getSessionId()); } catch (org.json.JSONException e) { } } catch (Exception ex) { } return driver; }
From source file:com.google.gwt.dev.cfg.ModuleDefLoader.java
/** * Loads a new module into <code>moduleDef</code> as an included module. * * @param parentLogger Logs the process. * @param moduleName The module to load. * @param moduleDef The module to add the new module to. * @throws UnableToCompleteException//from www. j av a 2s .com */ void nestedLoad(TreeLogger parentLogger, String moduleName, ModuleDef moduleDef) throws UnableToCompleteException { if (moduleDef.isInherited(moduleName)) { // No need to parse module again. return; } TreeLogger logger = parentLogger.branch(TreeLogger.DEBUG, "Loading inherited module '" + moduleName + "'", null); if (!ModuleDef.isValidModuleName(moduleName)) { logger.log(TreeLogger.ERROR, "Invalid module name: '" + moduleName + "'", null); throw new UnableToCompleteException(); } moduleDef.addInteritedModule(moduleName); // Find the specified module using the classpath. // String slashedModuleName = moduleName.replace('.', '/'); String resName = slashedModuleName + ModuleDefLoader.GWT_MODULE_XML_SUFFIX; URL moduleURL = classLoader.getResource(resName); if (moduleURL != null) { String externalForm = moduleURL.toExternalForm(); if (logger.isLoggable(TreeLogger.DEBUG)) { logger.log(TreeLogger.DEBUG, "Module location: " + externalForm, null); } try { if ((!(externalForm.startsWith("jar:file"))) && (!(externalForm.startsWith("zip:file"))) && (!(externalForm.startsWith("http://"))) && (!(externalForm.startsWith("ftp://")))) { File gwtXmlFile = new File(moduleURL.toURI()); moduleDef.addGwtXmlFile(gwtXmlFile); } } catch (URISyntaxException e) { logger.log(TreeLogger.ERROR, "Error parsing URI", e); throw new UnableToCompleteException(); } String compilationUnitArchiveName = slashedModuleName + ModuleDefLoader.COMPILATION_UNIT_ARCHIVE_SUFFIX; URL compiledModuleURL = classLoader.getResource(compilationUnitArchiveName); if (compiledModuleURL != null) { moduleDef.addCompilationUnitArchiveURL(compiledModuleURL); } } if (moduleURL == null) { logger.log(TreeLogger.ERROR, "Unable to find '" + resName + "' on your classpath; " + "could be a typo, or maybe you forgot to include a classpath entry for source?"); throw new UnableToCompleteException(); } // Extract just the directory containing the module. // String moduleDir = ""; int i = slashedModuleName.lastIndexOf('/'); if (i != -1) { moduleDir = slashedModuleName.substring(0, i) + "/"; } // Parse it. // Reader r = null; try { r = Util.createReader(logger, moduleURL); ModuleDefSchema schema = new ModuleDefSchema(logger, this, moduleName, moduleURL, moduleDir, moduleDef); ReflectiveParser.parse(logger, schema, r); } catch (Throwable e) { logger.log(TreeLogger.ERROR, "Unexpected error while processing XML", e); throw new UnableToCompleteException(); } finally { Utility.close(r); } }
From source file:fr.jayasoft.ivy.url.HttpClientHandler.java
private HeadMethod doHead(URL url, int timeout) throws IOException, HttpException { HttpClient client = getClient(url);// www . j a v a2s.c o m client.setTimeout(timeout); HeadMethod head = new HeadMethod(url.toExternalForm()); head.setDoAuthentication(useAuthentication(url) || useProxyAuthentication()); client.executeMethod(head); return head; }
From source file:uk.ac.manchester.cs.owl.semspreadsheets.repository.bioportal.BioPortalRepositoryAccessor.java
public String fetchOntologyFormat(String ontologyAcronym) { logger.debug("Fetching format for " + ontologyAcronym); BioPortalCache cache = BioPortalCache.getInstance(); String format = cache.getFormat(ontologyAcronym); if (format != null) { if (logger.isDebugEnabled()) { logger.debug("The ontology format for " + ontologyAcronym + " was found in the cache as " + format); }// ww w .j a va 2 s .co m } else { logger.info("The format for " + ontologyAcronym + " not found in cache, fetching"); try { URL url = new URL(BioPortalRepository.ONTOLOGY_LIST + "/" + ontologyAcronym + "/" + BioPortalRepository.LATEST_SUBMISSION + "?format=json&apikey=" + BioPortalRepository.readAPIKey()); logger.info("About to fetch more ontology information from " + url.toExternalForm()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setReadTimeout(CONNECT_TIMEOUT); int responseCode = connection.getResponseCode(); logger.info("BioPortal http response: " + responseCode); if (responseCode == 400 || responseCode == 403) { throw new BioPortalAccessDeniedException(); } JsonParser parser = new JsonFactory().createParser(connection.getInputStream()); while (format == null && parser.nextToken() != null) { String name = parser.getCurrentName(); if ("hasOntologyLanguage".equals(name)) { parser.nextToken(); format = parser.getText(); } } if (format == null) { format = "unknown"; } cache.storeFormat(ontologyAcronym, format); } catch (UnknownHostException e) { ErrorHandler.getErrorHandler().handleError(e); } catch (MalformedURLException e) { logger.error("Error with URL for BioPortal rest API", e); } catch (SocketTimeoutException e) { logger.error("Timeout connecting to BioPortal", e); ErrorHandler.getErrorHandler().handleError(e); } catch (IOException e) { logger.error("Error communiciating with BioPortal rest API", e); } catch (BioPortalAccessDeniedException e) { ErrorHandler.getErrorHandler().handleError(e); } } return format; }
From source file:net.sf.jsptest.compiler.jsp20.JasperCompiler.java
private String resolveWebRootFromClassPath() { String path = webRoot;/* w w w. j av a 2 s . c o m*/ if (path.startsWith("./")) { path = path.substring(2); } URL url = getClass().getClassLoader().getResource(path); if (url == null) { return webRoot; } if (!url.toExternalForm().startsWith("file:")) { log.info("Web root referenced a non-filesystem resource: " + url); return webRoot; } return new File(url.toExternalForm().substring("file:".length())).getAbsolutePath(); }
From source file:ste.travian.gui.WorldController.java
/** * Loads a travian map from the given URL. Note that if the store does not * have the world table, we create it.//from w ww. j a v a 2s . co m * * @param url the url of the map * * @throws ste.travian.TravianException in case of any error */ public void load(URL url) throws TravianException { // // Let's initialize the store so that if it does not contain the needed // tables they get created. // WorldStore store = new WorldStore(); store.initialize(); MapDownloader downloader = new MapDownloader(url.toExternalForm()); WorldUpdateWorker worker = new WorldUpdateWorker(mainWindow, store, downloader); worker.execute(); }
From source file:volker.streaming.music.gui.ApiPanel.java
private ImageIcon getIcon(URL url) { ImageIcon result = null;/*from ww w. j a va 2 s . c o m*/ if (url == null) { LOG.error("Couldn't find image"); } else { try { result = new ImageIcon(ImageIO.read(url)); } catch (IOException e) { LOG.error("Couldn't read image " + url.toExternalForm(), e); } } return result; }
From source file:at.newsagg.model.parser.hibernate.Item.java
public Item(Element itemElement, ChannelIF channel, String title, String description, URL link) { this.itemElement = itemElement; this.channel = channel; this.title = title; this.description = description; this.link = link.toExternalForm(); }