List of usage examples for java.net URI getAuthority
public String getAuthority()
From source file:com.buaa.cfs.fs.Path.java
/** Returns a qualified path object. */ public Path makeQualified(URI defaultUri, Path workingDir) { Path path = this; if (!isAbsolute()) { path = new Path(workingDir, this); }/* w w w. ja va 2s.c om*/ URI pathUri = path.toUri(); String scheme = pathUri.getScheme(); String authority = pathUri.getAuthority(); String fragment = pathUri.getFragment(); if (scheme != null && (authority != null || defaultUri.getAuthority() == null)) return path; if (scheme == null) { scheme = defaultUri.getScheme(); } if (authority == null) { authority = defaultUri.getAuthority(); if (authority == null) { authority = ""; } } URI newUri = null; try { newUri = new URI(scheme, authority, normalizePath(scheme, pathUri.getPath()), null, fragment); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } return new Path(newUri); }
From source file:org.deri.any23.http.DefaultHTTPClient.java
/** * * Opens an {@link java.io.InputStream} from a given URI. * It follows redirects.//from w w w .java 2 s . c o m * * @param uri to be opened * @return {@link java.io.InputStream} * @throws IOException */ public InputStream openInputStream(String uri) throws IOException { GetMethod method = null; try { ensureClientInitialized(); String uriStr = null; try { URI uriObj = new URI(uri); // [scheme:][//authority][path][?query][#fragment] final String path = uriObj.getPath(); final String query = uriObj.getQuery(); final String fragment = uriObj.getFragment(); uriStr = String .format("%s://%s%s%s%s%s%s", uriObj.getScheme(), uriObj.getAuthority(), path != null ? URLEncoder.encode(path, "UTF-8").replaceAll("%2F", "/") : "", query == null ? "" : "?", query != null ? URLEncoder.encode(query, "UTF-8").replaceAll("%3D", "=") .replaceAll("%26", "&") : "", fragment == null ? "" : "#", fragment != null ? URLEncoder.encode(fragment, "UTF-8") : ""); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid URI string.", e); } method = new GetMethod(uriStr); method.setFollowRedirects(true); client.executeMethod(method); _contentLength = method.getResponseContentLength(); final Header contentTypeHeader = method.getResponseHeader("Content-Type"); contentType = contentTypeHeader == null ? null : contentTypeHeader.getValue(); if (method.getStatusCode() != 200) { throw new IOException( "Failed to fetch " + uri + ": " + method.getStatusCode() + " " + method.getStatusText()); } actualDocumentURI = method.getURI().toString(); byte[] response = method.getResponseBody(); return new ByteArrayInputStream(response); } finally { if (method != null) { method.releaseConnection(); } } }
From source file:com.granita.icloudcalsync.resource.RemoteCollection.java
Resource.AssetDownloader getDownloader() { return new Resource.AssetDownloader() { @Override// w ww . j a v a 2s . c o m public byte[] download(URI uri) throws URISyntaxException, IOException, HttpException, DavException { if (!uri.isAbsolute()) throw new URISyntaxException(uri.toString(), "URI referenced from entity must be absolute"); if (uri.getScheme().equalsIgnoreCase(baseURI.getScheme()) && uri.getAuthority().equalsIgnoreCase(baseURI.getAuthority())) { // resource is on same server, send Authorization WebDavResource file = new WebDavResource(collection, uri); file.get("image/*"); return file.getContent(); } else { // resource is on an external server, don't send Authorization return IOUtils.toByteArray(uri); } } }; }
From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.RMWebAppFilter.java
private String appendOrReplaceParamter(String uri, String newQuery) { if (uri.contains(YarnWebParams.NEXT_REFRESH_INTERVAL + "=")) { return uri.replaceAll(YarnWebParams.NEXT_REFRESH_INTERVAL + "=[^&]+", newQuery); }//from ww w . j a v a2s . c o m try { URI oldUri = new URI(uri); String appendQuery = oldUri.getQuery(); if (appendQuery == null) { appendQuery = newQuery; } else { appendQuery += "&" + newQuery; } URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(), oldUri.getPath(), appendQuery, oldUri.getFragment()); return newUri.toString(); } catch (URISyntaxException e) { return null; } }
From source file:org.dkpro.lab.engine.impl.DefaultTaskContext.java
@Override public TaskContextMetadata resolve(URI aUri) { StorageService storage = getStorageService(); if (LATEST_CONTEXT_SCHEME.equals(aUri.getScheme())) { try {/*from w w w . j av a 2 s. c o m*/ return storage.getLatestContext(aUri.getAuthority(), extractConstraints(aUri)); } catch (TaskContextNotFoundException e) { throw new UnresolvedImportException(this, aUri.toString(), e); } } else if (CONTEXT_ID_SCHEME.equals(aUri.getScheme())) { try { return storage.getContext(aUri.getAuthority()); } catch (TaskContextNotFoundException e) { throw new UnresolvedImportException(this, aUri.toString(), e); } } else { throw new DataAccessResourceFailureException("Unknown scheme in import [" + aUri + "]"); } }
From source file:org.apache.any23.http.DefaultHTTPClient.java
/** * * Opens an {@link java.io.InputStream} from a given URI. * It follows redirects.//from w w w . j a v a 2s. c om * * @param uri to be opened * @return {@link java.io.InputStream} * @throws IOException */ public InputStream openInputStream(String uri) throws IOException { GetMethod method = null; try { ensureClientInitialized(); String uriStr; try { URI uriObj = new URI(uri); // [scheme:][//authority][path][?query][#fragment] final String path = uriObj.getPath(); final String query = uriObj.getQuery(); final String fragment = uriObj.getFragment(); uriStr = String .format("%s://%s%s%s%s%s%s", uriObj.getScheme(), uriObj.getAuthority(), path != null ? URLEncoder.encode(path, "UTF-8").replaceAll("%2F", "/") : "", query == null ? "" : "?", query != null ? URLEncoder.encode(query, "UTF-8").replaceAll("%3D", "=") .replaceAll("%26", "&") : "", fragment == null ? "" : "#", fragment != null ? URLEncoder.encode(fragment, "UTF-8") : ""); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid URI string.", e); } method = new GetMethod(uriStr); method.setFollowRedirects(true); client.executeMethod(method); _contentLength = method.getResponseContentLength(); final Header contentTypeHeader = method.getResponseHeader("Content-Type"); contentType = contentTypeHeader == null ? null : contentTypeHeader.getValue(); if (method.getStatusCode() != 200) { throw new IOException( "Failed to fetch " + uri + ": " + method.getStatusCode() + " " + method.getStatusText()); } actualDocumentURI = method.getURI().toString(); byte[] response = method.getResponseBody(); return new ByteArrayInputStream(response); } finally { if (method != null) { method.releaseConnection(); } } }
From source file:org.eclipse.orion.internal.server.servlets.xfer.TransferResourceDecorator.java
private void addTransferLinks(HttpServletRequest request, URI resource, JSONObject representation) throws URISyntaxException, JSONException, UnsupportedEncodingException { URI location = new URI(representation.getString(ProtocolConstants.KEY_LOCATION)); IPath targetPath = new Path(location.getPath()).removeFirstSegments(1).removeTrailingSeparator(); IPath path = new Path("/xfer/import").append(targetPath); //$NON-NLS-1$ URI link = new URI(resource.getScheme(), resource.getAuthority(), path.toString(), null, null); if (representation.has(ProtocolConstants.KEY_EXCLUDED_IN_IMPORT)) { String linkString = link.toString(); if (linkString.contains("?")) { linkString += "&" + ProtocolConstants.PARAM_EXCLUDE + "=" + URLEncoder .encode(representation.getString(ProtocolConstants.KEY_EXCLUDED_IN_IMPORT), "UTF-8"); } else {// w ww .j av a 2 s .c o m linkString += "?" + ProtocolConstants.PARAM_EXCLUDE + "=" + URLEncoder .encode(representation.getString(ProtocolConstants.KEY_EXCLUDED_IN_IMPORT), "UTF-8"); } link = new URI(linkString); representation.remove(ProtocolConstants.KEY_EXCLUDED_IN_IMPORT); } representation.put(ProtocolConstants.KEY_IMPORT_LOCATION, link); //Bug 348073: don't add export links for empty directories if (isEmptyDirectory(request, targetPath)) { return; } path = new Path("/xfer/export").append(targetPath).addFileExtension("zip"); //$NON-NLS-1$ //$NON-NLS-2$ link = new URI(resource.getScheme(), resource.getAuthority(), path.toString(), null, null); if (representation.has(ProtocolConstants.KEY_EXCLUDED_IN_EXPORT)) { String linkString = link.toString(); if (linkString.contains("?")) { linkString += "&" + ProtocolConstants.PARAM_EXCLUDE + "=" + URLEncoder .encode(representation.getString(ProtocolConstants.KEY_EXCLUDED_IN_EXPORT), "UTF-8"); } else { linkString += "?" + ProtocolConstants.PARAM_EXCLUDE + "=" + URLEncoder .encode(representation.getString(ProtocolConstants.KEY_EXCLUDED_IN_EXPORT), "UTF-8"); } link = new URI(linkString); representation.remove(ProtocolConstants.KEY_EXCLUDED_IN_EXPORT); } representation.put(ProtocolConstants.KEY_EXPORT_LOCATION, link); }
From source file:org.mcxiaoke.commons.http.util.URIUtilsEx.java
/** * Extracts target host from the given {@link URI}. * //from w w w. j ava2 s . c o m * @param uri * @return the target host if the URI is absolute or * <code>null</null> if the URI is * relative or does not contain a valid host name. * * @since 4.1 */ public static HttpHost extractHost(final URI uri) { if (uri == null) { return null; } HttpHost target = null; if (uri.isAbsolute()) { int port = uri.getPort(); // may be overridden later String host = uri.getHost(); if (host == null) { // normal parse failed; let's do it ourselves // authority does not seem to care about the valid character-set // for host names host = uri.getAuthority(); if (host != null) { // Strip off any leading user credentials int at = host.indexOf('@'); if (at >= 0) { if (host.length() > at + 1) { host = host.substring(at + 1); } else { host = null; // @ on its own } } // Extract the port suffix, if present if (host != null) { int colon = host.indexOf(':'); if (colon >= 0) { int pos = colon + 1; int len = 0; for (int i = pos; i < host.length(); i++) { if (Character.isDigit(host.charAt(i))) { len++; } else { break; } } if (len > 0) { try { port = Integer.parseInt(host.substring(pos, pos + len)); } catch (NumberFormatException ex) { } } host = host.substring(0, colon); } } } } String scheme = uri.getScheme(); if (host != null) { target = new HttpHost(host, port, scheme); } } return target; }
From source file:org.dthume.maven.xpom.impl.saxon.XPomUri.java
public XPomUri(final String uriString) throws URISyntaxException { if (null == uriString) throw new URISyntaxException(uriString, "Cannot parse null URI"); final URI jUri = new URI(uriString); if (!"xpom".equals(jUri.getScheme())) { throw new URISyntaxException(uriString, "Not in xpom://<coords>[/<path>] format"); }/* ww w. j ava 2 s . co m*/ final Matcher matcher = ARTIFACT_PATTERN.matcher(jUri.getPath()); if (!matcher.matches()) { throw new URISyntaxException(uriString, "XPOM scheme but not in xpom://<coords>[/<path>] format"); } final String groupId = jUri.getAuthority(); coords = toCoords(matcher, groupId); resource = toResource(matcher); params = unmodifiableMap(toParams(jUri.getQuery())); }
From source file:com.asakusafw.runtime.stage.launcher.LauncherOptionsParser.java
private void configureJobJar(List<Path> paths, String className, Map<Path, Path> cacheMap) throws IOException { if (configuration.get(KEY_CONF_JAR) != null) { return;//from w ww . j ava2 s. c om } for (Path path : paths) { Path remote = cacheMap.get(path); URI uri = path.toUri(); if (remote != null && uri.getScheme().equals("file")) { //$NON-NLS-1$ File file = new File(uri); if (isInclude(file, className)) { Path qualified = remote.getFileSystem(configuration).makeQualified(remote); if (LOG.isDebugEnabled()) { LOG.debug(MessageFormat.format("Application class is in: file={2} ({1}), class={0}", //$NON-NLS-1$ className, path, qualified)); } URI target = qualified.toUri(); if (target.getScheme() != null && (target.getScheme().equals("file") || target.getAuthority() != null)) { //$NON-NLS-1$ configuration.set(KEY_CONF_JAR, qualified.toString()); } break; } } } }