/** * Builds an URI from an URI and a suffix. * * <p>This suffix can be an absolute URL, or a relative path with respect to the first URI. In * this case, the suffix is resolved with respect to the URI. * * <p>If the suffix is already an URL, its is returned.<br> * If the suffix is a relative file path and cannot be resolved, an exception is thrown. * * <p>The returned URI is normalized. * * @param referenceUri the reference URI (can be null) * @param uriSuffix the URI suffix (not null) * @return the new URI * @throws URISyntaxException if the resolution failed */ public static URI buildNewURI(URI referenceUri, String uriSuffix) throws URISyntaxException { if (uriSuffix == null) throw new NullPointerException("The URI suffix cannot be null."); uriSuffix = uriSuffix.replaceAll("\\\\", "/"); URI importUri = null; try { // Absolute URL ? importUri = urlToUri(new URL(uriSuffix)); } catch (Exception e) { try { // Relative URL ? if (!referenceUri.toString().endsWith("/") && !uriSuffix.startsWith("/")) referenceUri = new URI(referenceUri.toString() + "/"); importUri = referenceUri.resolve(new URI(null, uriSuffix, null)); } catch (Exception e2) { String msg = "An URI could not be built from the URI " + referenceUri.toString() + " and the suffix " + uriSuffix + "."; throw new URISyntaxException(msg, e2.getMessage()); } } return importUri.normalize(); }
/** * Builds an URI from an URL (with a handle for URLs not compliant with RFC 2396). * * @param url an URL * @return an URI * @throws URISyntaxException if the URI is invalid and could not be repaired */ public static URI urlToUri(URL url) throws URISyntaxException { URI uri; try { // Possible failing step. uri = url.toURI(); } catch (Exception e) { // URL did not comply with RFC 2396 => illegal non-escaped characters. try { uri = new URI( url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); } catch (Exception e1) { throw new URISyntaxException(String.valueOf(url), "Broken URL."); } } uri = uri.normalize(); return uri; }
/** * Add topic id-value pairs to idMap. * * @param id id * @param value value */ public void addId(final URI id, final String value) { if (id != null && value != null) { final URI localId = id.normalize(); final String localValue = value.trim(); idMap.put(localId, localValue); } }
/** * Return the value corresponding to the id. * * @param id id * @return value */ public String getIdValue(final URI id) { if (id == null) { return null; } final URI localId = id.normalize(); return idMap.get(localId); }
public static String join(String source, String fragment) { try { boolean isRelative = false; if (source.startsWith("/") || source.startsWith(".")) { isRelative = true; } URI uri = new URI(source); if (!source.endsWith("/") && (fragment.startsWith("./") && "".equals(uri.getPath()))) { uri = new URI(source + "/"); } else if ("".equals(uri.getPath()) && !fragment.startsWith("/")) { uri = new URI(source + "/"); } URI f = new URI(fragment); URI resolved = uri.resolve(f); URI normalized = resolved.normalize(); if (Character.isAlphabetic(normalized.toString().charAt(0)) && isRelative) { return "./" + normalized.toString(); } return normalized.toString(); } catch (Exception e) { return source; } }
/* * Analyze the schema at the given URI, the schema is parsed and stored in * XMLObjectInfos. */ private void analyzeXSD(URI uri) { uri = uri.normalize(); // already seen this xsd, skip it if (xmlObjectInfos.containsKey(uri.toString())) return; XSDSchema xsdSchema = XSDSchemaImpl.getSchemaForSchema(uri.toString()); String encoding = null; // if schema is not cached, parse it if (xsdSchema == null) { XSDParser p = new XSDParser(null); InputStream is = NetUtils.getURLInputStream(uri.toString()); if (is != null) { p.parse(is); xsdSchema = p.getSchema(); encoding = p.getEncoding(); } } else encoding = xsdSchema.getDocument().getXmlEncoding(); if (xsdSchema != null) { if (encoding == null) encoding = "UTF-8"; XMLObjectInfo info = new XMLObjectInfo(new Path(uri.getPath()), xsdSchema, encoding); xmlObjectInfos.put(uri.toString(), info); updatePathPrefix(info); // analyze its imports and includes analyzeXSD(uri, xsdSchema); } }
public static URI getLocalName(URI prefix, URI identifier) throws InvalidIdentifierException, NamingAuthorityConfigurationException { try { verifyPrefix(prefix); } catch (Exception e) { throw new NamingAuthorityConfigurationException(e.getMessage()); } String idStr = identifier.normalize().toString(); String prefixStr = prefix.normalize().toString(); if (!idStr.startsWith(prefixStr) || prefixStr.length() >= idStr.length()) { throw new InvalidIdentifierException( "Identifier (" + identifier + ") is not local to prefix (" + prefix + ")."); } return prefix.relativize(identifier); }
/** * Return a proper URL with lowercase scheme, lowercase domain, stripped hash - used to compare * against other previously seen URLs * * @param href A relative or absolute URL * @param context Null or an absolute URL to use when href is relative * @return */ public static URL getCanonicalURL(String href, String context) { try { URI normalized; if (context != null) { normalized = new URI(context); if (normalized.getPath().equals("")) context += "/"; normalized = new URI(context).resolve(href); } else { normalized = new URI(href); } normalized = normalized.normalize(); return new URI( normalized.getScheme().toLowerCase(), normalized.getUserInfo(), normalized.getHost(), normalized.getPort(), normalized.getPath().equals("") ? "/" : normalized.getPath(), normalized.getQuery(), null) .toURL(); } catch (URISyntaxException e) { LogFactory.getLog(URLCanonicalizer.class) .error("Unable to canonicalize href: " + href + ", context" + context, e); return null; } catch (MalformedURLException e) { LogFactory.getLog(URLCanonicalizer.class) .error("Unable to canonicalize href: " + href + ", context" + context, e); return null; } catch (IllegalArgumentException e) { LogFactory.getLog(URLCanonicalizer.class) .error("Unable to canonicalize href: " + href + ", context" + context, e); return null; } /*if (href.contains("#")) { href = href.substring(0, href.indexOf("#")); } href = href.replace(" ", "%20"); try { URL canonicalURL; if (context == null) { canonicalURL = new URL(href); } else { canonicalURL = new URL(new URL(context), href); } String path = canonicalURL.getPath(); if (path.startsWith("/../")) { path = path.substring(3); canonicalURL = new URL(canonicalURL.getProtocol(), canonicalURL.getHost(), canonicalURL.getPort(), path); } else if (path.contains("..")) { System.out.println("Found path with ..: " + path + " " + href + " " + context); } return canonicalURL; } catch (MalformedURLException ex) { return null; }*/ }
/** * Add topic id to the idMap. * * @param id topic id * @return updated topic id */ public String addId(final URI id) { if (id == null) { return null; } final URI localId = id.normalize(); index++; final String newId = "unique_" + Integer.toString(index); idMap.put(localId, newId); return newId; }
private static String normalize(String path) { try { URI uri = new URI(path); String normalized = uri.normalize().toString(); return normalized; } catch (URISyntaxException e) { e.printStackTrace(); return null; } }
// it compares the link if it is equal to .html then it will be added to the list. private void find(Path file) { Path name = file.getFileName(); if (name != null && matcher.matches(name)) { numMatches++; int i = -1; String shortPath = file.toString(); i = shortPath.indexOf("-lewis"); shortPath = shortPath.substring(i + 7, shortPath.length()); URI newLink = new File(file.toString()).toURI(); // System.out.println("Antes :" +newLink.toString() ); newLink = newLink.normalize(); linksMatches.add(newLink.toString().substring(6)); } }
/** * Create new Jersey container request context. * * @param baseUri base application URI. * @param requestUri request URI. * @param httpMethod request HTTP method name. * @param securityContext security context of the current request. Must not be {@code null}. The * {@link SecurityContext#getUserPrincipal()} must return {@code null} if the current request * has not been authenticated by the container. * @param propertiesDelegate custom {@link PropertiesDelegate properties delegate} to be used by * the context. */ public ContainerRequest( URI baseUri, URI requestUri, String httpMethod, SecurityContext securityContext, PropertiesDelegate propertiesDelegate) { super(true); this.baseUri = baseUri == null ? DEFAULT_BASE_URI : baseUri.normalize(); this.requestUri = requestUri; this.httpMethod = httpMethod; this.securityContext = securityContext; this.propertiesDelegate = new TracingAwarePropertiesDelegate(propertiesDelegate); this.uriRoutingContext = new UriRoutingContext(this); }
@Override public Collection<Message> doUpgrade() { if (whitelistManager.getRules().isEmpty()) { final Map<URI, List<ExternalGadgetSpec>> externalGadgets = gadgetApplinkUpgradeUtil.getExternalGadgetsRequiringUpgrade(); final List<String> rules = new ArrayList<String>(); // always add WAC to ensure we get the marketing gadget rules.add("http://www.atlassian.com/*"); if (!externalGadgets.isEmpty()) { for (URI uri : externalGadgets.keySet()) { rules.add(uri.normalize().toASCIIString().toLowerCase() + "/*"); } } updateWhitelist(rules); } return Collections.emptySet(); }
/** Construct a path from a URI */ public Path(URI aUri) { uri = aUri.normalize(); }
@Override public InputSource resolveEntity(String name, String publicId, String base, String systemId) throws SAXException, IOException { try { // First try resolving using the publicId String resolved = publicIds.get(publicId); if (resolved != null) { InputSource is = new InputSource(resolved); is.setPublicId(publicId); return is; } // If there is no systemId, can't try anything else if (systemId == null) { throw new FileNotFoundException( sm.getString("localResolver.unresolvedEntity", name, publicId, systemId, base)); } // Try resolving with the supplied systemId resolved = systemIds.get(systemId); if (resolved != null) { InputSource is = new InputSource(resolved); is.setPublicId(publicId); return is; } // Work-around for XML documents that use just the file name for the // location to refer to a JavaEE schema for (String javaEENamespace : JAVA_EE_NAMESPACES) { String javaEESystemId = javaEENamespace + '/' + systemId; resolved = systemIds.get(javaEESystemId); if (resolved != null) { InputSource is = new InputSource(resolved); is.setPublicId(publicId); return is; } } // Resolve the supplied systemId against the base URI systemUri; try { if (base == null) { systemUri = new URI(systemId); } else { // Can't use URI.resolve() because "jar:..." URLs are not valid // hierarchical URIs so resolve() does not work. new URL() // delegates to the jar: stream handler and it manages to figure // it out. URI baseUri = new URI(base); systemUri = new URL(baseUri.toURL(), systemId).toURI(); } systemUri = systemUri.normalize(); } catch (URISyntaxException e) { // May be caused by a | being used instead of a : in an absolute // file URI on Windows. if (blockExternal) { // Absolute paths aren't allowed so block it throw new MalformedURLException(e.getMessage()); } else { // See if the URLHandler can resolve it return new InputSource(systemId); } } if (systemUri.isAbsolute()) { // Try the resolved systemId resolved = systemIds.get(systemUri.toString()); if (resolved != null) { InputSource is = new InputSource(resolved); is.setPublicId(publicId); return is; } if (!blockExternal) { InputSource is = new InputSource(systemUri.toString()); is.setPublicId(publicId); return is; } } throw new FileNotFoundException( sm.getString("localResolver.unresolvedEntity", name, publicId, systemId, base)); } catch (Exception excp) { excp.printStackTrace(); } return null; }
/** * Gets the canonical url, starting from a relative or absolute url found in a given context * (baseURL). * * @param url the url string defining the reference * @param baseURL the context in which this url was found * @return the canonical url */ public static String getCanonicalURL(String url, String baseURL) { try { /* Build the absolute URL, from the url and the baseURL */ String resolvedURL = URLResolver.resolveUrl(baseURL == null ? "" : baseURL, url); log.debug("Resolved URL: " + resolvedURL); URI canonicalURI = new URI(resolvedURL); /* Some checking. */ if (canonicalURI.getScheme() == null) { throw new MalformedURLException( "Protocol could not be reliably evaluated from uri: " + canonicalURI + " and base url: " + baseURL); } if (canonicalURI.getHost() == null) { throw new MalformedURLException( "Host could not be reliably evaluated from: " + canonicalURI); } /* * Normalize: no empty segments (i.e., "//"), no segments equal to ".", and no segments equal to * ".." that are preceded by a segment not equal to "..". */ String path = canonicalURI.normalize().getRawPath(); /* Convert '//' -> '/' */ int idx = path.indexOf("//"); while (idx >= 0) { path = path.replace("//", "/"); idx = path.indexOf("//"); } /* Drop starting '/../' */ while (path.startsWith("/../")) { path = path.substring(3); } /* Trim */ path = path.trim(); /* Process parameters and sort them. */ final SortedMap<String, String> params = createParameterMap(canonicalURI.getRawQuery()); final String queryString; String canonicalParams = canonicalize(params); queryString = (canonicalParams.isEmpty() ? "" : "?" + canonicalParams); /* Add starting slash if needed */ if (path.length() == 0) { path = "/" + path; } /* Drop default port: example.com:80 -> example.com */ int port = canonicalURI.getPort(); if (port == 80) { port = -1; } /* Lowercasing protocol and host */ String protocol = canonicalURI.getScheme().toLowerCase(); String host = canonicalURI.getHost().toLowerCase(); String pathAndQueryString = normalizePath(path) + queryString; URL result = new URL(protocol, host, port, pathAndQueryString); return result.toExternalForm(); } catch (MalformedURLException ex) { log.warn( "Error while Processing URL in the spidering process (on base " + baseURL + "): " + ex.getMessage()); return null; } catch (URISyntaxException ex) { log.warn( "Error while Processing URI in the spidering process (on base " + baseURL + "): " + ex.getMessage()); return null; } }
/* (non-Javadoc) * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistableURIProvider#getURI(java.lang.Object) */ @Override public URI getURI(final Object context) { Assert.isNotNull(context); URI uri = null; final IPeer peer = getPeer(context); if (peer != null) { // Get the URI the peer model has been created from final AtomicReference<URI> nodeURI = new AtomicReference<URI>(); final AtomicReference<Version> version = new AtomicReference<Version>(); Runnable runnable = new Runnable() { @Override public void run() { String value = peer.getAttributes().get(IPersistableNodeProperties.PROPERTY_URI); if (value != null && !"".equals(value.trim())) { // $NON-NLS-1$ nodeURI.set(URI.create(value.trim())); } value = peer.getAttributes().get(IPeerProperties.PROP_VERSION); version.set(value != null ? new Version(value.trim()) : null); } }; if (Protocol.isDispatchThread()) { runnable.run(); } else { Protocol.invokeAndWait(runnable); } if (nodeURI.get() != null) { uri = nodeURI.get(); } if (uri == null) { String baseName = peer.getName(); if (baseName == null) { baseName = peer.getID(); } String name = makeValidFileSystemName(baseName); // Get the URI from the name uri = getRoot().append(name + ".peer").toFile().toURI(); // $NON-NLS-1$ try { File file = new File(uri.normalize()); int i = 0; while (file.exists()) { name = makeValidFileSystemName( baseName + (version.get() != null ? "_" + version.get().toString() : "") + //$NON-NLS-1$ //$NON-NLS-2$ (i > 0 ? " (" + i + ")" : "")); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ uri = getRoot().append(name + ".peer").toFile().toURI(); // $NON-NLS-1$ file = new File(uri.normalize()); i++; } } catch (Exception e) { } } } return uri; }
@Override protected void doParse( Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { Map<String, ParsingModuleInfo> scripts = parseSpecificBeans(element, parserContext, builder.getRawBeanDefinition(), ns(LANG_URI)); ElementSelector searchFolders = and(sameNs(element), name("search-folders")); ElementSelector searchFiles = and(sameNs(element), name("search-files")); for (Element subElement : subElements(element)) { String prefix = null; String typeName = null; String moduleName = null; Pattern scriptNamePattern = null; String scriptResourceName = null; String language = null; if (searchFolders.accept(subElement)) { String folderName = assertNotNull( normalizePathName(subElement.getAttribute("folders")), "no folder name provided for search-folders"); // 取prefix prefix = getPrefix(folderName); if (prefix != null) { folderName = folderName.substring(prefix.length() + 1); } // folderName不以/开始 if (folderName.startsWith("/")) { folderName = folderName.substring(1); } scriptNamePattern = compilePathName(folderName); typeName = assertNotNull(trimToNull(subElement.getAttribute("type")), "no type name provided"); language = trimToNull(subElement.getAttribute("language")); scriptResourceName = folderName + "/**/*.*"; log.trace( "Searching in folders: {}, moduleType={}, language={}", new Object[] {folderName, typeName, language == null ? "auto" : language}); } else if (searchFiles.accept(subElement)) { String fileName = assertNotNull( normalizePathName(subElement.getAttribute("files")), "no script file name provided for search-files"); // fileName不以/结尾 assertTrue(!fileName.endsWith("/"), "invalid script file name: %s", fileName); // 取prefix prefix = getPrefix(fileName); if (prefix != null) { fileName = fileName.substring(prefix.length() + 1); } // fileName不以/开始 if (fileName.startsWith("/")) { fileName = fileName.substring(1); } scriptNamePattern = compilePathName(fileName); typeName = assertNotNull(trimToNull(subElement.getAttribute("type")), "no type name provided"); moduleName = assertNotNull(trimToNull(subElement.getAttribute("name")), "no module name provided"); language = trimToNull(subElement.getAttribute("language")); scriptResourceName = fileName; log.trace( "Searching for script files: {}, moduleType={}, moduleName={}, language={}", new Object[] {fileName, typeName, moduleName, language == null ? "auto" : language}); } if (scriptResourceName != null) { scriptResourceName = prefix == null ? scriptResourceName : prefix + ":" + scriptResourceName; // 扫描scripts ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver( parserContext.getReaderContext().getResourceLoader()); int found = 0; try { Resource[] resources = resolver.getResources(scriptResourceName.replace('?', '*')); BeanDefinitionDefaults defaults = getBeanDefinitionDefaults(subElement, parserContext); ParsingModuleMatcher matcher = new ParsingModuleMatcher(scripts, scriptNamePattern, typeName, moduleName) { @Override protected String getName(String name, String itemName) { String ext = getExt(itemName); if (ext != null && name.endsWith("." + ext)) { return name.substring(0, name.length() - ext.length() - 1); } return name; } }; for (Resource resource : resources) { if (resource.isReadable()) { URI uri = resource.getURI(); if (uri == null) { continue; } String resourceName = uri.normalize().toString(); if (matcher.doMatch(resourceName)) { BeanDefinition scriptBean = createScriptBean(subElement, parserContext, resourceName, language, defaults); String beanName = matcher.generateBeanName(resourceName, parserContext.getRegistry()); parserContext.getRegistry().registerBeanDefinition(beanName, scriptBean); found++; } } } } catch (IOException e) { parserContext .getReaderContext() .error("Failed to scan resources: " + scriptResourceName, subElement, e); return; } log.debug("Found {} module scripts with pattern: {}", found, scriptResourceName); } } postProcessItems(element, parserContext, builder, scripts, "search-folders or search-files"); }
/* * Analyze the wsdl document at the given URI, and traverse any relative files that * it imports. Can optionally pass in a parsed Definition if one's available so * we don't have to parse the wsdl again (otherwise just pass in null). */ private void analyzeWSDL(URI uri, Definition definition) throws MalformedURLException, IOException, WSDLException, WWWAuthenticationException { uri = uri.normalize(); // already seen this wsdl, skip if (xmlObjectInfos.containsKey(uri.toString())) return; // need to parse the wsdl ourselves if (definition == null) definition = parser.getWSDLDefinitionVerbose(uri.toString()); // save a reference to the starting wsdl if (this.definition == null) this.definition = definition; IPath path = new Path(uri.getPath()); // a target filename was given, so we need to modify the path with the new name if (definition == this.definition && targetFilename != null) path = path.removeLastSegments(1).append(targetFilename); XMLObjectInfo info = new XMLObjectInfo(path, definition); xmlObjectInfos.put(uri.toString(), info); updatePathPrefix(info); // now look at wsdl imports for (Iterator it = definition.getImports().values().iterator(); it.hasNext(); ) { List list = (List) it.next(); for (Iterator listIt = list.iterator(); listIt.hasNext(); ) { Import wsdlImport = (Import) listIt.next(); String wsdlImportLocation = wsdlImport.getLocationURI(); // analyze any relative imports we find if (wsdlImportLocation != null && isRelative(wsdlImportLocation)) { // bad form, importing xsd with wsdl:import, but need to handle if (wsdlImportLocation.toLowerCase().endsWith(XSD)) analyzeXSD(uri.resolve(uriCreate(wsdlImportLocation))); else analyzeWSDL(uri.resolve(uriCreate(wsdlImportLocation)), null); } } } // now look at xsd imports Types types = definition.getTypes(); // there's no wsdl:types, we're done if (types == null) return; for (Iterator it = types.getExtensibilityElements().iterator(); it.hasNext(); ) { ExtensibilityElement extElement = (ExtensibilityElement) it.next(); Element element; // we'll try to parse any UnknownExtensibilityElements and // XSDSchemaExtensibilityElements into an XSD schema if (extElement instanceof UnknownExtensibilityElement) element = ((UnknownExtensibilityElement) extElement).getElement(); else if (extElement instanceof XSDSchemaExtensibilityElement) element = ((XSDSchemaExtensibilityElement) extElement).getElement(); else if (extElement instanceof Schema) element = ((Schema) extElement).getElement(); else continue; try { XSDSchema xsdSchema = XSDSchemaImpl.createSchema(element); // analyze the inlined schema at the current uri analyzeXSD(uri, xsdSchema); } catch (Exception t) { // ignore any extensibility elements that cannot be parsed into a // XSDSchema instance } } }
/** * Find the topic id from idMap. * * @param id topic id * @return true if find and false otherwise */ public boolean findId(final URI id) { return id != null && idMap.containsKey(id.normalize()); }