List of usage examples for java.net URI getFragment
public String getFragment()
From source file:org.rssowl.core.util.URIUtils.java
/** * A helper to convert custom schemes (like feed://) to the HTTP counterpart. * * @param uri the uri to get as HTTP/HTTPS {@link URI}. * @return the converted {@link URI} if necessary. *///from ww w . j av a 2 s . c om public static URI toHTTP(URI uri) { if (uri == null) return uri; String scheme = uri.getScheme(); if (HTTP_SCHEME.equals(scheme) || HTTPS_SCHEME.equals(scheme)) return uri; String newScheme = HTTP_SCHEME; if (SyncUtils.READER_HTTPS_SCHEME.equals(scheme)) newScheme = HTTPS_SCHEME; try { return new URI(newScheme, uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); } catch (URISyntaxException e) { return uri; } }
From source file:Main.java
/** * Removes dot segments according to RFC 3986, section 5.2.4 * * @param uri the original URI/*from www . j a v a2s . c o m*/ * @return the URI without dot segments */ private static URI removeDotSegments(URI uri) { String path = uri.getPath(); if ((path == null) || (path.indexOf("/.") == -1)) { // No dot segments to remove return uri; } String[] inputSegments = path.split("/"); Stack<String> outputSegments = new Stack<String>(); for (int i = 0; i < inputSegments.length; i++) { if ((inputSegments[i].length() == 0) || (".".equals(inputSegments[i]))) { // Do nothing } else if ("..".equals(inputSegments[i])) { if (!outputSegments.isEmpty()) { outputSegments.pop(); } } else { outputSegments.push(inputSegments[i]); } } StringBuilder outputBuffer = new StringBuilder(); for (String outputSegment : outputSegments) { outputBuffer.append('/').append(outputSegment); } try { return new URI(uri.getScheme(), uri.getAuthority(), outputBuffer.toString(), uri.getQuery(), uri.getFragment()); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } }
From source file:org.apache.oozie.action.hadoop.SparkMain.java
/** * Spark compares URIs based on scheme, host and port. Here we convert URIs * into the default format so that Spark won't think those belong to * different file system. This will avoid an extra copy of files which * already exists on same hdfs.// w ww . j av a 2 s .com * * @param fs * @param fileUri * @return fixed uri * @throws URISyntaxException */ private static URI getFixedUri(FileSystem fs, URI fileUri) throws URISyntaxException { if (fs.getUri().getScheme().equals(fileUri.getScheme()) && (fs.getUri().getHost().equals(fileUri.getHost()) || fileUri.getHost() == null) && (fs.getUri().getPort() == -1 || fileUri.getPort() == -1 || fs.getUri().getPort() == fileUri.getPort())) { return new URI(fs.getUri().getScheme(), fileUri.getUserInfo(), fs.getUri().getHost(), fs.getUri().getPort(), fileUri.getPath(), fileUri.getQuery(), fileUri.getFragment()); } return fileUri; }
From source file:com.ctriposs.r2.message.rest.QueryTunnelUtil.java
/** * @param request a RestRequest object to be encoded as a tunneled POST * @param threshold the size of the query params above which the request will be encoded * * @return an encoded RestRequest/*from w w w .ja v a 2s .com*/ */ public static RestRequest encode(final RestRequest request, int threshold) throws URISyntaxException, MessagingException, IOException { URI uri = request.getURI(); // Check to see if we should tunnel this request by testing the length of the query // if the query is NULL, we won't bother to encode. // 0 length is a special case that could occur with a url like http://www.foo.com? // which we don't want to encode, because we'll lose the "?" in the process // Otherwise only encode queries whose length is greater than or equal to the // threshold value. String query = uri.getRawQuery(); if (query == null || query.length() == 0 || query.length() < threshold) { return request; } RestRequestBuilder requestBuilder = new RestRequestBuilder(request); // reconstruct URI without query uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, uri.getFragment()); // If there's no existing body, just pass the request as x-www-form-urlencoded ByteString entity = request.getEntity(); if (entity == null || entity.length() == 0) { requestBuilder.setHeader(HEADER_CONTENT_TYPE, FORM_URL_ENCODED); requestBuilder.setEntity(ByteString.copyString(query, Data.UTF_8_CHARSET)); } else { // If we have a body, we must preserve it, so use multipart/mixed encoding MimeMultipart multi = createMultiPartEntity(entity, request.getHeader(HEADER_CONTENT_TYPE), query); requestBuilder.setHeader(HEADER_CONTENT_TYPE, multi.getContentType()); ByteArrayOutputStream os = new ByteArrayOutputStream(); multi.writeTo(os); requestBuilder.setEntity(ByteString.copy(os.toByteArray())); } // Set the base uri, supply the original method in the override header, and change method to POST requestBuilder.setURI(uri); requestBuilder.setHeader(HEADER_METHOD_OVERRIDE, requestBuilder.getMethod()); requestBuilder.setMethod(RestMethod.POST); return requestBuilder.build(); }
From source file:gate.tagger.tagme.TaggerTagMeWS.java
public static String recodeForDbp38(String uriString) { String ret;// w ww .j a v a 2 s . c om URI uri = null; if (uriString.startsWith("http://") || uriString.startsWith("https://")) { // First try to parse the string as an URI so that any superfluous // percent-encodings can get decoded later try { uri = new URI(uriString); } catch (Exception ex) { throw new GateRuntimeException("Could not parse URI " + uriString, ex); } // now use this constructor to-recode only the necessary parts try { String path = uri.getPath(); path = path.trim(); path = path.replaceAll(" +", "_"); uri = new URI(uri.getScheme(), null, uri.getHost(), -1, path, uri.getQuery(), uri.getFragment()); } catch (Exception ex) { throw new GateRuntimeException("Could not re-construct URI: " + uri); } ret = uri.toString(); } else { if (uriString.contains("\\u")) { uriString = StringEscapeUtils.unescapeJava(uriString); } uriString = uriString.trim(); uriString = uriString.replaceAll(" +", "_"); // We need to %-encode colons, otherwise the getPath() method will return // null ... uriString = uriString.replaceAll(":", "%3A"); try { uri = new URI(uriString); // decode and prepare for minimal percent encoding uriString = uri.getPath(); } catch (URISyntaxException ex) { // do nothing: the uriString must already be ready for percent-encoding } uriString = uriString.replaceAll(" +", "_"); try { uri = new URI(null, null, null, -1, "/" + uriString, null, null); } catch (Exception ex) { throw new GateRuntimeException("Could not re-construct URI part: " + uriString); } ret = uri.toString().substring(1); } return ret; }
From source file:ddf.catalog.resourceretriever.LocalResourceRetriever.java
private boolean uriContainsFragment(URI uri, String fragment) { return uri != null && uri.getFragment().equals(fragment); }
From source file:com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration.java
/** * Fix a serverUrl.//from ww w . j av a 2s. co m * * @param serverUrl the server URL. * @return the normalized server URL. */ @NonNull public static String normalizeServerUrl(@CheckForNull String serverUrl) { serverUrl = StringUtils.defaultIfBlank(serverUrl, BitbucketCloudEndpoint.SERVER_URL); try { URI uri = new URI(serverUrl).normalize(); String scheme = uri.getScheme(); if ("http".equals(scheme) || "https".equals(scheme)) { // we only expect http / https, but also these are the only ones where we know the authority // is server based, i.e. [userinfo@]server[:port] // DNS names must be US-ASCII and are case insensitive, so we force all to lowercase String host = uri.getHost() == null ? null : uri.getHost().toLowerCase(Locale.ENGLISH); int port = uri.getPort(); if ("http".equals(scheme) && port == 80) { port = -1; } else if ("https".equals(scheme) && port == 443) { port = -1; } serverUrl = new URI(scheme, uri.getUserInfo(), host, port, uri.getPath(), uri.getQuery(), uri.getFragment()).toASCIIString(); } } catch (URISyntaxException e) { // ignore, this was a best effort tidy-up } return serverUrl.replaceAll("/$", ""); }
From source file:org.archive.wayback.util.htmllex.ParseContextTest.java
/** * Test method for {@link org.archive.wayback.util.htmllex.ParseContext#contextualizeUrl(java.lang.String)}. *///from w w w .j av a 2 s . co m public void testContextualizeUrl() { ParseContext pc = new ParseContext(); try { URI tmp = new URI("http://base.com/foo.html#REF"); String ref = tmp.getFragment(); assertEquals("REF", ref); tmp = new URI("http://base.com/foo.html"); assertNull(tmp.getFragment()); pc.setBaseUrl(new URL("http://base.com/")); assertEquals("http://base.com/images.gif", pc.contextualizeUrl("/images.gif")); assertEquals("http://base.com/images.gif", pc.contextualizeUrl("../images.gif")); assertEquals("http://base.com/images.gif", pc.contextualizeUrl("../../images.gif")); assertEquals("http://base.com/image/1s.gif", pc.contextualizeUrl("/image/1s.gif")); assertEquals("http://base.com/image/1s.gif", pc.contextualizeUrl("../../image/1s.gif")); assertEquals("http://base.com/image/1s.gif", pc.contextualizeUrl("/../../image/1s.gif")); assertEquals("http://base.com/image/1.html#REF", pc.contextualizeUrl("/../../image/1.html#REF")); assertEquals("http://base.com/image/1.html#REF FOO", pc.contextualizeUrl("/../../image/1.html#REF FOO")); assertEquals("http://base.com/image/foo?boo=baz", pc.contextualizeUrl("/image/foo?boo=baz")); assertEquals("http://base.com/image/foo?boo=baz%3A&gar=war", pc.contextualizeUrl("/image/foo?boo=baz%3A&gar=war")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); fail(e.getLocalizedMessage()); } }
From source file:com.vmware.thinapp.common.util.AfUtil.java
/** * Checks whether the given URL string begins with a protocol (http://, * ftp://, etc.) If it does, the string is returned unchanged. If it does * not, full URL is returned and is constructed as parentUrl "/" url. * * @param url input URL string in absolute or relative form * @param parentUrl base URL to use if the given URL is in relative form * @return an absolute URL/*from w w w .j a va2 s. c om*/ */ public static URI relToAbs(String url, URI parentUrl) throws URISyntaxException { if (!StringUtils.hasLength(url)) { throw new URISyntaxException(url, "The input url was empty!"); } URI parent2 = new URI(parentUrl.getScheme(), parentUrl.getUserInfo(), parentUrl.getAuthority(), parentUrl.getPort(), parentUrl.getPath() + "/", // Parent URL path must end with "/" for // this to work. resolve() removes any // duplicates. parentUrl.getQuery(), parentUrl.getFragment()); return parent2.resolve(url.trim()); }
From source file:org.apache.pig.backend.hadoop.executionengine.tez.TezJobCompiler.java
private void addCacheResources(String[] fileNames) throws Exception { for (String fileName : fileNames) { fileName = fileName.trim();/*from w w w .j a va2 s . co m*/ if (fileName.length() > 0) { URI resourceURI = new URI(fileName); String fragment = resourceURI.getFragment(); Path remoteFsPath = new Path(resourceURI); String resourceName = (fragment != null && fragment.length() > 0) ? fragment : remoteFsPath.getName(); TezResourceManager.getInstance().addTezResource(resourceName, remoteFsPath); } } }