List of usage examples for java.net URL getRef
public String getRef()
From source file:org.hypertopic.RESTDatabase.java
public URL getURL(String path) throws Exception { URL url = new URL(this.baseUrl + path); return new URL(new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()).toASCIIString()); }
From source file:cn.openwatch.internal.http.loopj.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 *//*from w w w .ja v 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 } } 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.si.services.sidora.rest.batch.beans.BatchRequestControllerBean.java
/** * Check Resource MimeType using Apache Tika * @param exchange/*from w w w . ja va 2 s. c om*/ * @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.digitalpebble.stormcrawler.filtering.basic.BasicURLNormalizer.java
@Override public String filter(URL sourceUrl, Metadata sourceMetadata, String urlToFilter) { urlToFilter = urlToFilter.trim();/*from w w w . ja va 2s .c o m*/ if (removeAnchorPart) { try { URL theURL = new URL(urlToFilter); String anchor = theURL.getRef(); if (anchor != null) urlToFilter = urlToFilter.replace("#" + anchor, ""); } catch (MalformedURLException e) { return null; } } if (unmangleQueryString) { urlToFilter = unmangleQueryString(urlToFilter); } if (!queryElementsToRemove.isEmpty()) { urlToFilter = filterQueryElements(urlToFilter); } try { URL theURL = new URL(urlToFilter); String file = theURL.getFile(); String protocol = theURL.getProtocol(); String host = theURL.getHost(); boolean hasChanged = false; // lowercased protocol if (!urlToFilter.startsWith(protocol)) { hasChanged = true; } if (host != null) { String newHost = host.toLowerCase(Locale.ROOT); if (!host.equals(newHost)) { host = newHost; hasChanged = true; } } int port = theURL.getPort(); // properly encode characters in path/file using percent-encoding String file2 = unescapePath(file); file2 = escapePath(file2); if (!file.equals(file2)) { hasChanged = true; } if (hasChanged) { urlToFilter = new URL(protocol, host, port, file2).toString(); } } catch (MalformedURLException e) { return null; } if (checkValidURI) { try { URI uri = URI.create(urlToFilter); urlToFilter = uri.normalize().toString(); } catch (java.lang.IllegalArgumentException e) { LOG.info("Invalid URI {}", urlToFilter); return null; } } return urlToFilter; }
From source file:org.talend.datatools.xml.utils.SchemaPopulationUtil.java
/** * Return the root node of a schema tree. * //from w w w .j a va2s. c o m * @param fileName * @return * @throws OdaException * @throws MalformedURLException * @throws URISyntaxException */ public static ATreeNode getSchemaTree(String fileName, boolean incAttr) throws OdaException, MalformedURLException, URISyntaxException { includeAttribute = incAttr; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); URI uri = null; File f = new File(fileName); if (f.exists()) { uri = f.toURI(); } else { URL url = new URL(fileName); uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); } // Then try to parse the input string as a url in web. if (uri == null) { uri = new URI(fileName); } // fixed a bug when parse one file contians Franch ,maybe need modification XMLSchemaLoader xsLoader = new XMLSchemaLoader(); XSModel xsModel = xsLoader.loadURI(uri.toString()); if (xsModel == null) { try { Grammar loadGrammar = xsLoader.loadGrammar( new XMLInputSource(null, uri.toString(), null, new FileInputStream(f), "ISO-8859-1")); xsModel = ((XSGrammar) loadGrammar).toXSModel(); } catch (XNIException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } ATreeNode complexTypesRoot = populateComplexTypeTree(xsModel); XSNamedMap map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION); ATreeNode root = new ATreeNode(); root.setValue("ROOT"); for (int i = 0; i < map.getLength(); i++) { ATreeNode node = new ATreeNode(); XSElementDecl element = (XSElementDecl) map.item(i); String namespace = element.getNamespace(); XSObject unique = element.getIdentityConstraints().itemByName(namespace, element.getName()); if (unique instanceof UniqueOrKey) { node.getUniqueNames().clear(); UniqueOrKey uniqueOrKey = (UniqueOrKey) unique; String uniqueName = ""; StringList fieldStrs = uniqueOrKey.getFieldStrs(); for (int j = 0; j < fieldStrs.getLength(); j++) { uniqueName = fieldStrs.item(j); if (uniqueName != null && !"".equals(uniqueName)) { uniqueName = uniqueName.replace("/", "").replace(".", ""); node.getUniqueNames().add(uniqueName); } } } ATreeNode namespaceNode = null; if (namespace != null) { namespaceNode = new ATreeNode(); namespaceNode.setDataType(namespace); namespaceNode.setType(ATreeNode.NAMESPACE_TYPE); namespaceNode.setValue(namespace); } node.setValue(element.getName()); node.setType(ATreeNode.ELEMENT_TYPE); node.setDataType(element.getName()); if (element.getTypeDefinition() instanceof XSComplexTypeDecl) { XSComplexTypeDecl complexType = (XSComplexTypeDecl) element.getTypeDefinition(); // If the complex type is explicitly defined, that is, it has name. if (complexType.getName() != null) { node.setDataType(complexType.getName()); ATreeNode n = findComplexElement(complexTypesRoot, complexType.getName()); if (namespaceNode != null) { node.addChild(namespaceNode); } if (n != null) { node.addChild(n.getChildren()); } } // If the complex type is implicitly defined, that is, it has no name. else { addParticleAndAttributeInfo(node, complexType, complexTypesRoot, new VisitingRecorder()); } } root.addChild(node); } // if no base element, display all complex types / attributes directly. if (map.getLength() == 0) { root.addChild(complexTypesRoot.getChildren()); } populateRoot(root); return root; }
From source file:eionet.cr.web.action.source.EditSourceActionBean.java
/** * Validates source for editing./*from ww w .j a v a 2 s. com*/ * * @return */ private boolean validateEditSource() { String urlString = harvestSource.getUrl(); if (!isUserOwner(harvestSource)) { addGlobalValidationError("Only owner can edit this source."); } if (StringUtils.isBlank(urlString)) { addGlobalValidationError(new SimpleError("URL missing!")); } else { try { URL url = new URL(urlString); if (url.getRef() != null) { addGlobalValidationError(new SimpleError("URL with a fragment part not allowed!")); } if (!StringUtils.equals(uri, urlString) && URLUtil.isNotExisting(urlString, harvestSource.isSparqlEndpoint())) { addGlobalValidationError(new SimpleError("There is no resource existing behind this URL!")); } Integer intervalMinutes = harvestSource.getIntervalMinutes(); if (intervalMinutes != null) { if (intervalMinutes.intValue() < 0 || intervalMultiplier < 0) { addGlobalValidationError(new SimpleError("Harvest interval must be >= 0")); } else { harvestSource.setIntervalMinutes( Integer.valueOf(intervalMinutes.intValue() * intervalMultiplier)); } } else { harvestSource.setIntervalMinutes(Integer.valueOf(0)); } } catch (MalformedURLException e) { addGlobalValidationError(new SimpleError("Invalid URL!")); } } return getContext().getValidationErrors() == null || getContext().getValidationErrors().isEmpty(); }
From source file:com.gargoylesoftware.htmlunit.WebRequest.java
private URL buildUrlWithNewFile(URL url, String newFile) { try {//from ww w. ja v a 2 s . c o m if (url.getRef() != null) { newFile += '#' + url.getRef(); } url = new URL(url.getProtocol(), url.getHost(), url.getPort(), newFile); } catch (final Exception e) { throw new RuntimeException("Cannot set URL: " + url.toExternalForm(), e); } return url; }
From source file:com.k42b3.aletheia.protocol.http.Request.java
private void parseUrl(URL url) { this.host = url.getHost(); this.path = url.getPath().isEmpty() ? "/" : url.getPath(); if (url.getQuery() != null) { this.path += "?" + url.getQuery(); }// w ww . ja v a 2s . co m if (url.getRef() != null) { this.path += "#" + url.getRef(); } }
From source file:org.talend.datatools.xml.utils.SchemaPopulationUtil.java
/** * Return the root node of a schema tree. * /*from ww w .jav a 2 s . com*/ * @param fileName * @return * @throws OdaException * @throws MalformedURLException * @throws URISyntaxException */ public static ATreeNode getSchemaTree(String fileName, boolean incAttr, boolean forMDM, List<String> attList) throws OdaException, MalformedURLException, URISyntaxException { includeAttribute = incAttr; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); URI uri = null; File f = new File(fileName); if (f.exists()) { uri = f.toURI(); } else { URL url = new URL(fileName); uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); } // Then try to parse the input string as a url in web. if (uri == null) { uri = new URI(fileName); } // fixed a bug when parse one file contians Franch ,maybe need modification XMLSchemaLoader xsLoader = new XMLSchemaLoader(); XSModel xsModel = xsLoader.loadURI(uri.toString()); if (xsModel == null) { try { Grammar loadGrammar = xsLoader.loadGrammar( new XMLInputSource(null, uri.toString(), null, new FileInputStream(f), "ISO-8859-1")); xsModel = ((XSGrammar) loadGrammar).toXSModel(); } catch (XNIException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } ATreeNode complexTypesRoot = populateComplexTypeTree(xsModel); XSNamedMap map = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION); ATreeNode root = new ATreeNode(); root.setValue("ROOT"); for (int i = 0; i < map.getLength(); i++) { ATreeNode node = new ATreeNode(); XSElementDecl element = (XSElementDecl) map.item(i); String namespace = element.getNamespace(); XSObject unique = element.getIdentityConstraints().itemByName(namespace, element.getName()); if (unique instanceof UniqueOrKey) { node.getUniqueNames().clear(); UniqueOrKey uniqueOrKey = (UniqueOrKey) unique; String uniqueName = ""; StringList fieldStrs = uniqueOrKey.getFieldStrs(); for (int j = 0; j < fieldStrs.getLength(); j++) { uniqueName = fieldStrs.item(j); if (uniqueName != null && !"".equals(uniqueName)) { uniqueName = uniqueName.replace("/", "").replace(".", ""); node.getUniqueNames().add(uniqueName); } } } ATreeNode namespaceNode = null; if (namespace != null) { namespaceNode = new ATreeNode(); namespaceNode.setDataType(namespace); namespaceNode.setType(ATreeNode.NAMESPACE_TYPE); namespaceNode.setValue(namespace); } node.setValue(element.getName()); node.setType(ATreeNode.ELEMENT_TYPE); node.setDataType(element.getName()); if (element.getTypeDefinition() instanceof XSComplexTypeDecl) { XSComplexTypeDecl complexType = (XSComplexTypeDecl) element.getTypeDefinition(); // If the complex type is explicitly defined, that is, it has name. if (complexType.getName() != null) { node.setDataType(complexType.getName()); ATreeNode n = findComplexElement(complexTypesRoot, complexType.getName()); if (namespaceNode != null) { node.addChild(namespaceNode); } if (n != null) { node.addChild(n.getChildren()); } } // If the complex type is implicitly defined, that is, it has no name. else { // If the complex type is implicitly defined , no need to check it in explicitly complex type addParticleAndAttributeInfo(node, complexType, complexTypesRoot, new VisitingRecorder()); } } String nodeType = node.getOriginalDataType(); if (forMDM) { Object nodeValue = node.getValue(); if (nodeValue != null && !attList.contains(nodeValue) && nodeType != null && !attList.contains(nodeType)) { continue; } } else { if (nodeType != null && !attList.contains(nodeType)) { continue; } } if (forMDM && attList.size() == 1 && node.getValue().equals(attList.get(0))) { root = node; } else { root.addChild(node); } } // if no base element, display all complex types / attributes directly. if (map.getLength() == 0) { root.addChild(complexTypesRoot.getChildren()); } populateRoot(root); return root; }
From source file:opendap.threddsHandler.ThreddsCatalogUtil.java
public static String getUrlInfo(URL url) throws InterruptedException { String info = "URL:\n"; info += " getHost(): " + url.getHost() + "\n"; info += " getAuthority(): " + url.getAuthority() + "\n"; info += " getFile(): " + url.getFile() + "\n"; info += " getSystemPath(): " + url.getPath() + "\n"; info += " getDefaultPort(): " + url.getDefaultPort() + "\n"; info += " getPort(): " + url.getPort() + "\n"; info += " getProtocol(): " + url.getProtocol() + "\n"; info += " getQuery(): " + url.getQuery() + "\n"; info += " getRef(): " + url.getRef() + "\n"; info += " getUserInfo(): " + url.getUserInfo() + "\n"; return info;/*from w w w . jav a2 s. c o m*/ }