List of usage examples for java.net URL getRef
public String getRef()
From source file:org.apache.nutch.net.urlnormalizer.querystring.QuerystringURLNormalizer.java
public String normalize(String urlString, String scope) throws MalformedURLException { URL url = new URL(urlString); String queryString = url.getQuery(); if (queryString == null) { return urlString; }/*from www . j a v a2 s . c o m*/ List<String> queryStringParts = Arrays.asList(queryString.split("&")); Collections.sort(queryStringParts); StringBuilder sb = new StringBuilder(); sb.append(url.getProtocol()); sb.append("://"); sb.append(url.getHost()); if (url.getPort() > -1) { sb.append(":"); sb.append(url.getPort()); } sb.append(url.getPath()); sb.append("?"); sb.append(StringUtils.join(queryStringParts, "&")); if (url.getRef() != null) { sb.append("#"); sb.append(url.getRef()); } return sb.toString(); }
From source file:edu.cornell.mannlib.vitro.webapp.filters.VitroURL.java
public VitroURL(String urlStr, String characterEncoding) { this.characterEncoding = characterEncoding; if (urlStr.indexOf("&") > -1) { wasXMLEscaped = true;//from w ww .ja va 2s .c om urlStr = StringEscapeUtils.unescapeXml(urlStr); } try { URL url = new URL(urlStr); this.protocol = url.getProtocol(); this.host = url.getHost(); this.port = Integer.toString(url.getPort()); this.pathParts = splitPath(url.getPath()); this.pathBeginsWithSlash = beginsWithSlash(url.getPath()); this.pathEndsInSlash = endsInSlash(url.getPath()); this.queryParams = parseQueryParams(url.getQuery()); this.fragment = url.getRef(); } catch (Exception e) { // Under normal circumstances, this is because the urlStr is relative // We'll assume that we just have a path and possibly a query string. // This is likely to be a bad assumption, but let's roll with it. Matcher m = pathPattern.matcher(urlStr); String[] urlParts = new String[2]; if (m.matches()) { urlParts[0] = m.group(1); if (m.groupCount() == 2) urlParts[1] = m.group(2); } else { //??? } try { this.pathParts = splitPath(URLDecoder.decode(getPath(urlStr), characterEncoding)); this.pathBeginsWithSlash = beginsWithSlash(urlParts[0]); this.pathEndsInSlash = endsInSlash(urlParts[0]); if (urlParts.length > 1) { this.queryParams = parseQueryParams(URLDecoder.decode(urlParts[1], characterEncoding)); } } catch (UnsupportedEncodingException uee) { log.error("Unable to use character encoding " + characterEncoding, uee); } } }
From source file:de.innovationgate.utils.URLBuilder.java
/** * Creates a URLBuilder that parses an existing URL * @param url The URL to parse//from w ww . j a v a 2s. c om * @param encoding The encoding of URL parameters. The URLBuilder will decode them. */ public URLBuilder(URL url, String encoding) { this(url.getProtocol(), url.getPort(), url.getHost(), url.getPath(), url.getQuery(), url.getRef(), encoding); }
From source file:org.apache.taverna.component.profile.ComponentProfileImpl.java
public ComponentProfileImpl(Registry registry, URL profileURL, BaseProfileLocator base) throws ComponentException { logger.info("Loading profile in " + identityHashCode(this) + " from " + profileURL); this.base = base; try {// www .ja va2 s. c om URL url = profileURL; if (url.getProtocol().startsWith("http")) url = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), url.getRef()) .toURL(); loadProfile(this, url, base); } catch (MalformedURLException e) { logger.warn("Malformed URL? " + profileURL); } catch (URISyntaxException e) { logger.warn("Malformed URL? " + profileURL); } parentRegistry = registry; }
From source file:com.all.facebook.impl.FacebookServiceImpl.java
@Override public String authorizeUrl(String responseUrl) throws FacebookServiceException { URL url; try {//from w w w. j av a 2s. c o m url = new URL(responseUrl); } catch (MalformedURLException e) { throw new FacebookServiceException(e); } if (!responseUrl.contains(REDIRECT_URI)) { throw new FacebookServiceException( "The url does not correpond with the redirect_uri parameter: " + responseUrl); } String ref = url.getRef(); if (ref == null) { Map<String, String> error = extractKeyValuePairs(url.getQuery()); String errorType = error.get("error"); throw new FacebookServiceException(error.toString(), errorType); } Map<String, String> keyValuePairs = extractKeyValuePairs(ref); if (!keyValuePairs.containsKey("access_token")) { throw new FacebookServiceException( "Could not find the access token in the responseUrl: " + responseUrl); } authorize(keyValuePairs.get("access_token")); return accessToken; }
From source file:com.microsoft.aad.adal.Discovery.java
@Override public boolean isValidAuthority(URL authorizationEndpoint) { // For comparison purposes, convert to lowercase Locale.US // getProtocol returns scheme and it is available if it is absolute url // Authority is in the form of https://Instance/tenant/somepath if (authorizationEndpoint != null && !StringExtensions.IsNullOrBlank(authorizationEndpoint.getHost()) && authorizationEndpoint.getProtocol().equals("https") && StringExtensions.IsNullOrBlank(authorizationEndpoint.getQuery()) && StringExtensions.IsNullOrBlank(authorizationEndpoint.getRef()) && !StringExtensions.IsNullOrBlank(authorizationEndpoint.getPath())) { if (UrlExtensions.isADFSAuthority(authorizationEndpoint)) { Logger.e(TAG, "Instance validation returned error", "", ADALError.DEVELOPER_AUTHORITY_CAN_NOT_BE_VALIDED, new AuthenticationException(ADALError.DISCOVERY_NOT_SUPPORTED)); return false; } else if (sValidHosts.contains(authorizationEndpoint.getHost().toLowerCase(Locale.US))) { // host can be the instance or inside the validated list. // Valid hosts will help to skip validation if validated before // call Callback and skip the look up return true; } else {/*from ww w .j a v a 2s.co m*/ // Only query from Prod instance for now, not all of the // instances in the list return queryInstance(authorizationEndpoint); } } return false; }
From source file:com.netspective.commons.lang.JavaDocs.java
/** * Return the DOM document that represents the output from JavaDoc for the given class. The XML file is located * using Java resource loading mechanism using the following search order: * <ol>//from w w w. java 2s .co m * <li>Looks for fully qualified name (FQN) of class plus ".xml" (x.y.z.xml) * <li>Looks for java-doc-xml/x.y.z.xml * <li>Looks for resources/java-doc-xml/x.y.z.xml * </ol> * * @return The DOM document for the given class or null if x.y.z.xml resource could not be located. */ public Document getXmlDocForClass(Class cls) throws IOException { Document javaDoc = (Document) xmlDocsByClass.get(cls); if (javaDoc != null) return javaDoc; String javaDocXmlFileName = (cls.getName() + ".xml").replace('$', '.'); URL javaDocUrl = ResourceLoader.getFirstResource(new String[] { javaDocXmlFileName, "java-doc-xml/" + javaDocXmlFileName, "resources/java-doc-xml/" + javaDocXmlFileName }); if (javaDocUrl != null) { InputStream is = javaDocUrl.openStream(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); javaDoc = factory.newDocumentBuilder().parse(is); xmlDocsByClass.put(cls, javaDoc); } catch (SAXException e) { log.error("A parsing error occurred; the xml input is not valid: " + javaDocUrl.getRef(), e); } catch (ParserConfigurationException e) { log.error("A parser configuration error occurred; the xml input is not valid", e); } finally { is.close(); } } return javaDoc; }
From source file:nya.miku.wishmaster.chans.dobrochan.DobroModule.java
private String sanitizeUrl(String urlStr) { if (urlStr == null) return null; try {// w w w.ja v a2 s. c o m URL url = new URL(urlStr); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return uri.toURL().toString(); } catch (Exception e) { Logger.e(TAG, "sanitize url", e); return urlStr; } }
From source file:cn.com.loopj.android.http.AsyncHttpClient.java
/** * Will encode url, if not disabled, and adds params on the end of it * * @param url String with URL, should be valid URL without params * @param params RequestParams to be appended on the end of URL * @param shouldEncodeUrl whether url should be encoded (replaces spaces with %20) * @return encoded url if requested with params appended if any available *///ww w.j av a 2s.c o m public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url, RequestParams params) { if (url == null) return null; if (shouldEncodeUrl) { try { String decodedURL = URLDecoder.decode(url, "UTF-8"); URL _url = new URL(decodedURL); URI _uri = new URI(_url.getProtocol(), _url.getUserInfo(), _url.getHost(), _url.getPort(), _url.getPath(), _url.getQuery(), _url.getRef()); url = _uri.toASCIIString(); } catch (Exception ex) { // Should not really happen, added just for sake of validity log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex); } } if (params != null) { // Construct the query string and trim it, in case it // includes any excessive white spaces. String paramString = params.getParamString().trim(); // Only add the query string if it isn't empty and it // isn't equal to '?'. if (!paramString.equals("") && !paramString.equals("?")) { url += url.contains("?") ? "&" : "?"; url += paramString; } } return url; }
From source file:edu.isi.wings.portal.controllers.RunController.java
public String publishRun(String runid) { HashMap<String, String> retmap = new HashMap<String, String>(); ExecutionMonitorAPI monitor = config.getDomainExecutionMonitor(); RuntimePlan plan = monitor.getRunDetails(runid); if (plan.getRuntimeInfo().getStatus() != Status.SUCCESS) { retmap.put("error", "Can only publish successfully completed runs"); } else {//from w w w .ja va 2 s.c om try { Mapper opmm = new Mapper(); Publisher publisher = config.getPublisher(); String tstoreurl = publisher.getTstoreUrl(); String puburl = publisher.getUrl(); String upurl = publisher.getUploadServer().getUrl(); opmm.setPublishExportPrefix(puburl); String rname = runid.substring(runid.indexOf('#') + 1); String runurl = opmm.getRunUrl(rname); String tid = plan.getOriginalTemplateID(); String tmpname = tid.substring(tid.indexOf('#') + 1); String tmd5 = getTemplateMD5(plan.getOriginalTemplateID()); String tname = tmpname + "-" + tmd5; String turl = opmm.getTemplateUrl(tname); // Check if run already published if (graphExists(tstoreurl, runurl)) { retmap.put("url", runurl); retmap.put("error", "Run already published"); return json.toJson(retmap); } // Fetch expanded template (to get data binding ids) TemplateCreationAPI tc = TemplateFactory.getCreationAPI(props); Template xtpl = tc.getTemplate(plan.getExpandedTemplateID()); HashMap<String, String> varBindings = new HashMap<String, String>(); for (Variable var : xtpl.getVariables()) { varBindings.put(var.getID(), var.getBinding().getID()); } // Create a temporary directory to upload/move File _tmpdir = File.createTempFile("temp", ""); File tempdir = new File(_tmpdir.getParent() + "/" + rname); FileUtils.deleteQuietly(tempdir); if (!_tmpdir.delete() || !tempdir.mkdirs()) throw new Exception("Cannot create temp directory"); File datadir = new File(tempdir.getAbsolutePath() + "/data"); File codedir = new File(tempdir.getAbsolutePath() + "/code"); File dcontdir = new File(tempdir.getAbsolutePath() + "/ont/data"); File acontdir = new File(tempdir.getAbsolutePath() + "/ont/components"); File wflowdir = new File(tempdir.getAbsolutePath() + "/ont/workflows"); File execsdir = new File(tempdir.getAbsolutePath() + "/ont/executions"); datadir.mkdirs(); codedir.mkdirs(); dcontdir.mkdirs(); acontdir.mkdirs(); wflowdir.mkdirs(); execsdir.mkdirs(); String tupurl = upurl + "/" + tempdir.getName(); String dataurl = tupurl + "/data"; String codeurl = tupurl + "/code"; String dconturl = tupurl + "/ont/data"; String aconturl = tupurl + "/ont/components"; String wflowurl = tupurl + "/ont/workflows"; String execsurl = tupurl + "/ont/executions"; Properties props = config.getProperties(); String dclib = props.getProperty("lib.domain.data.url"); String dcont = props.getProperty("ont.domain.data.url"); String aclib = props.getProperty("lib.concrete.url"); String acabs = props.getProperty("lib.abstract.url"); String wfpfx = props.getProperty("domain.workflows.dir.url"); String expfx = props.getProperty("domain.executions.dir.url"); String cdir = props.getProperty("lib.domain.code.storage"); String ddir = props.getProperty("lib.domain.data.storage"); // Get files to upload && modify "Locations" to point to uploaded urls HashSet<ExecutionFile> uploadFiles = new HashSet<ExecutionFile>(); HashSet<ExecutionCode> uploadCodes = new HashSet<ExecutionCode>(); for (ExecutionStep step : plan.getPlan().getAllExecutionSteps()) { for (ExecutionFile file : step.getInputFiles()) uploadFiles.add(file); for (ExecutionFile file : step.getOutputFiles()) uploadFiles.add(file); uploadCodes.add(step.getCodeBinding()); } for (ExecutionFile file : uploadFiles) { File copyfile = new File(file.getLocation()); // Only upload files below a threshold file size long maxsize = publisher.getUploadServer().getMaxUploadSize(); if (copyfile.length() > 0 && (maxsize == 0 || copyfile.length() < maxsize)) { // Copy over file to temp directory FileUtils.copyFileToDirectory(copyfile, datadir); // Change file path in plan to the web accessible one file.setLocation(file.getLocation().replace(ddir, dataurl)); } else { String bindingid = varBindings.get(file.getID()); file.setLocation(config.getServerUrl() + this.dataUrl + "/fetch?data_id=" + URLEncoder.encode(bindingid, "UTF-8")); } } for (ExecutionCode code : uploadCodes) { File copydir = null; if (code.getCodeDirectory() != null) { copydir = new File(code.getCodeDirectory()); // Change path in plan to the web accessible one code.setCodeDirectory(code.getCodeDirectory().replace(cdir, codeurl)); } else { File f = new File(code.getLocation()); copydir = f.getParentFile(); } // Copy over directory to temp directory FileUtils.copyDirectoryToDirectory(copydir, codedir); // Change path in plan to the web accessible one code.setLocation(code.getLocation().replace(cdir, codeurl)); } String dcontdata = IOUtils.toString(new URL(dcont)); dcontdata = dcontdata.replace(dcont, dconturl + "/ontology.owl"); FileUtils.write(new File(dcontdir.getAbsolutePath() + "/ontology.owl"), dcontdata); String dclibdata = IOUtils.toString(new URL(dclib)); dclibdata = dclibdata.replace(dcont, dconturl + "/ontology.owl"); dclibdata = dclibdata.replace(dclib, dconturl + "/library.owl"); dclibdata = dclibdata.replace(ddir, dataurl); FileUtils.write(new File(dcontdir.getAbsolutePath() + "/library.owl"), dclibdata); String aclibdata = IOUtils.toString(new URL(aclib)); aclibdata = aclibdata.replace(dcont, aconturl + "/ontology.owl"); aclibdata = aclibdata.replace(aclib, aconturl + "/library.owl"); aclibdata = aclibdata.replace(cdir, codeurl); FileUtils.write(new File(acontdir.getAbsolutePath() + "/library.owl"), aclibdata); String acabsdata = IOUtils.toString(new URL(acabs)); acabsdata = acabsdata.replace(dcont, dconturl + "/ontology.owl"); acabsdata = acabsdata.replace(aclib, aconturl + "/library.owl"); acabsdata = acabsdata.replace(acabs, aconturl + "/abstract.owl"); FileUtils.write(new File(acontdir.getAbsolutePath() + "/abstract.owl"), acabsdata); File planfile = new File(execsdir.getAbsolutePath() + "/" + plan.getPlan().getName() + ".owl"); String plandata = plan.getPlan().serialize(); plandata = plandata.replace("\"" + wfpfx, "\"" + wflowurl); plandata = plandata.replace("\"" + expfx, "\"" + execsurl); plandata = plandata.replace(dclib, dconturl + "/library.owl"); plandata = plandata.replace(dcont, dconturl + "/ontology.owl"); plandata = plandata.replace(aclib, aconturl + "/library.owl"); plandata = plandata.replace(acabs, aconturl + "/abstract.owl"); FileUtils.write(planfile, plandata); String rplanurl = execsurl + "/" + plan.getName() + ".owl"; File rplanfile = new File(execsdir.getAbsolutePath() + "/" + plan.getName() + ".owl"); String rplandata = IOUtils.toString(new URL(runid)); rplandata = rplandata.replace(wfpfx, wflowurl); rplandata = rplandata.replace(expfx, execsurl); rplandata = rplandata.replace(tmpname + ".owl", tname + ".owl"); rplandata = rplandata.replace("#" + tmpname + "\"", "#" + tname + "\""); rplandata = rplandata.replace(dclib, dconturl + "/library.owl"); rplandata = rplandata.replace(dcont, dconturl + "/ontology.owl"); rplandata = rplandata.replace(aclib, aconturl + "/library.owl"); rplandata = rplandata.replace(acabs, aconturl + "/abstract.owl"); FileUtils.write(rplanfile, rplandata); URL otplurl = new URL(plan.getOriginalTemplateID()); String otmplurl = wflowurl + "/" + otplurl.getRef() + ".owl"; File otplfile = new File(wflowdir.getAbsolutePath() + "/" + otplurl.getRef() + ".owl"); String otpldata = IOUtils.toString(otplurl); otpldata = otpldata.replace(wfpfx, wflowurl); otpldata = otpldata.replace(expfx, execsurl); otpldata = otpldata.replace(dclib, dconturl + "/library.owl"); otpldata = otpldata.replace(dcont, dconturl + "/ontology.owl"); otpldata = otpldata.replace(aclib, aconturl + "/library.owl"); otpldata = otpldata.replace(acabs, aconturl + "/abstract.owl"); FileUtils.write(otplfile, otpldata); URL xtplurl = new URL(plan.getExpandedTemplateID()); File xtplfile = new File(execsdir.getAbsolutePath() + "/" + xtplurl.getRef() + ".owl"); String xtpldata = IOUtils.toString(xtplurl); xtpldata = xtpldata.replace(wfpfx, wflowurl); xtpldata = xtpldata.replace(expfx, execsurl); xtpldata = xtpldata.replace(dclib, dconturl + "/library.owl"); xtpldata = xtpldata.replace(dcont, dconturl + "/ontology.owl"); xtpldata = xtpldata.replace(aclib, aconturl + "/library.owl"); xtpldata = xtpldata.replace(acabs, aconturl + "/abstract.owl"); FileUtils.write(xtplfile, xtpldata); // TODO: Change base url to an opmw.url + "/resource/WorkflowTemplate/ ?? uploadDirectory(publisher.getUploadServer(), tempdir); FileUtils.deleteQuietly(tempdir); // Convert results into prov and opmw File opmwfile = File.createTempFile("opmw-", ".owl"); File provfile = File.createTempFile("prov-", ".owl"); File tmplfile = File.createTempFile("tmpl-", ".owl"); String liburl = props.getProperty("lib.domain.execution.url"); runurl = opmm.transformWINGSResultsToOPMW(rplanurl, liburl, "RDF/XML", opmwfile.getAbsolutePath(), provfile.getAbsolutePath(), rname); turl = opmm.transformWINGSElaboratedTemplateToOPMW(otmplurl, "RDF/XML", tmplfile.getAbsolutePath(), tname); // Publish run opmw data publishFile(tstoreurl, runurl, opmwfile.getAbsolutePath()); // Publish provenance data to the default graph publishFile(tstoreurl, "default", provfile.getAbsolutePath()); // Publish template if it doesn't already exist if (!graphExists(tstoreurl, turl)) publishFile(tstoreurl, turl, tmplfile.getAbsolutePath()); opmwfile.delete(); provfile.delete(); tmplfile.delete(); retmap.put("url", runurl); } catch (Exception e) { e.printStackTrace(); retmap.put("error", e.getMessage()); } } return json.toJson(retmap); }