List of usage examples for java.net URI resolve
public URI resolve(String str)
From source file:com.nesscomputing.migratory.loader.HttpLoader.java
@Override public Collection<URI> loadFolder(final URI folderUri, final String searchPattern) throws IOException { final List<URI> results = Lists.newArrayList(); final Pattern pattern = (searchPattern == null) ? null : Pattern.compile(searchPattern); final String path = folderUri.getPath(); final URI baseUri = (path.endsWith("/") ? folderUri : folderUri.resolve(path + "/")); final String content = loadFile(baseUri); if (content == null) { // File not found return results; }/* w w w. jav a2 s . c o m*/ // The folders are a list of file names in plain text. Split it up. final String[] filenames = StringUtils.split(content); for (String filename : filenames) { if (pattern.matcher(filename).matches()) { results.add(URI.create(baseUri + filename)); } } return results; }
From source file:stargate.commons.recipe.DataObjectPath.java
private void initialize(DataObjectPath parent, String child) { if (parent == null) { throw new IllegalArgumentException("parent is null"); }/* www . j av a 2s . c om*/ if (child == null || child.isEmpty()) { throw new IllegalArgumentException("child is empty or null"); } if (parent.uri.getPath().endsWith("/")) { this.uri = parent.uri.resolve(normalizePath(child)).normalize(); } else { try { URI uri = new URI(parent.uri.toASCIIString() + "/"); this.uri = uri.resolve(normalizePath(child)).normalize(); } catch (URISyntaxException ex) { throw new IllegalArgumentException(ex); } } }
From source file:org.orbeon.oxf.xforms.XFormsUtils.java
/** * Resolve a URI string against an element, taking into account ancestor xml:base attributes for * the resolution./*from ww w. j av a 2s .c o m*/ * * @param element element used to start resolution (if null, no resolution takes place) * @param baseURI optional base URI * @param uri URI to resolve * @return resolved URI */ public static URI resolveXMLBase(Element element, String baseURI, String uri) { try { // Allow for null Element if (element == null) return new URI(uri); final List<String> xmlBaseValues = new ArrayList<String>(); // Collect xml:base values Element currentElement = element; do { final String xmlBaseAttribute = currentElement.attributeValue(XMLConstants.XML_BASE_QNAME); if (xmlBaseAttribute != null) xmlBaseValues.add(xmlBaseAttribute); currentElement = currentElement.getParent(); } while (currentElement != null); // Append base if needed if (baseURI != null) xmlBaseValues.add(baseURI); // Go from root to leaf Collections.reverse(xmlBaseValues); xmlBaseValues.add(uri); // Resolve paths from root to leaf URI result = null; for (final String currentXMLBase : xmlBaseValues) { final URI currentXMLBaseURI = new URI(currentXMLBase); result = (result == null) ? currentXMLBaseURI : result.resolve(currentXMLBaseURI); } return result; } catch (URISyntaxException e) { throw new ValidationException("Error while resolving URI: " + uri, e, (element != null) ? (LocationData) element.getData() : null); } }
From source file:org.apache.taverna.scufl2.rdfxml.WorkflowParser.java
@SuppressWarnings("unchecked") protected void readWorkflow(URI wfUri, URI source) throws ReaderException, IOException { if (source.isAbsolute()) throw new ReaderException("Can't read external workflow source " + source); InputStream bundleStream = getParserState().getUcfPackage().getResourceAsInputStream(source.getRawPath()); JAXBElement<WorkflowDocument> elem; try {/* w w w . ja va 2s .co m*/ elem = (JAXBElement<WorkflowDocument>) unmarshaller.unmarshal(bundleStream); } catch (JAXBException e) { throw new ReaderException("Can't parse workflow document " + source, e); } URI base = getParserState().getLocation().resolve(source); if (elem.getValue().getBase() != null) base = base.resolve(elem.getValue().getBase()); if (elem.getValue().getAny().size() != 1) throw new ReaderException("Expects only a <Workflow> element in " + source); org.apache.taverna.scufl2.xml.Workflow workflow = (org.apache.taverna.scufl2.xml.Workflow) elem.getValue() .getAny().get(0); getParserState().setCurrentBase(base); parseWorkflow(workflow, wfUri); }
From source file:com.couchbase.client.vbucket.ConfigurationProviderHTTP.java
/** * Create a URL which has the appropriate headers to interact with the * service. Most exception handling is up to the caller. * * @param resource the URI either absolute or relative to the base for this * ClientManager//from w ww . j a v a 2s . c o m * @return * @throws java.io.IOException */ private URLConnection urlConnBuilder(URI base, URI resource) throws IOException { if (!resource.isAbsolute() && base != null) { resource = base.resolve(resource); } URL specURL = resource.toURL(); URLConnection connection = specURL.openConnection(); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("user-agent", "spymemcached vbucket client"); connection.setRequestProperty("X-memcachekv-Store-Client-" + "Specification-Version", CLIENT_SPEC_VER); if (restUsr != null) { try { connection.setRequestProperty("Authorization", buildAuthHeader(restUsr, restPwd)); } catch (UnsupportedEncodingException ex) { throw new IOException("Could not encode specified credentials for " + "HTTP request.", ex); } } return connection; }
From source file:org.mule.endpoint.MuleEndpointURI.java
public URI resolve(URI uri) { return uri.resolve(uri); }
From source file:org.callimachusproject.client.HttpClientFactoryTest.java
@Test public void testTarget() throws Exception { HttpContext localContext = new BasicHttpContext(); responses.add(new BasicHttpResponse(_200)); client.execute(new HttpGet("http://example.com/200"), new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_200.getStatusCode(), response.getStatusLine().getStatusCode()); return null; }// ww w.jav a2 s.co m }, localContext); HttpHost host = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); URI root = new URI(host.getSchemeName(), null, host.getHostName(), host.getPort(), "/", null, null); assertEquals("http://example.com/200", root.resolve(req.getURI()).toASCIIString()); }
From source file:org.geometerplus.android.fbreader.network.ActivityNetworkContext.java
private String url(URI base, Map<String, String> params, String key) { final String path = params.get(key); if (path == null) { return null; }//from ww w . ja v a 2 s . c om try { final URI relative = new URI(path); return relative.isAbsolute() ? null : base.resolve(relative).toString(); } catch (URISyntaxException e) { return null; } }
From source file:org.eclipse.orion.server.cf.commands.DeleteApplicationRoutesCommand.java
@Override protected ServerStatus _doIt() { MultiServerStatus status = new MultiServerStatus(); try {/*from ww w .j a v a 2s.c o m*/ URI targetURI = URIUtil.toURI(target.getUrl()); // get app details // TODO: it should be passed along with App object String appsUrl = target.getSpace().getCFJSON().getJSONObject("entity").getString("apps_url"); //$NON-NLS-1$//$NON-NLS-2$ URI appsURI = targetURI.resolve(appsUrl); GetMethod getAppsMethod = new GetMethod(appsURI.toString()); HttpUtil.configureHttpMethod(getAppsMethod, target); getAppsMethod.setQueryString("q=name:" + appName + "&inline-relations-depth=1"); //$NON-NLS-1$ //$NON-NLS-2$ ServerStatus appsStatus = HttpUtil.executeMethod(getAppsMethod); status.add(appsStatus); if (!status.isOK()) return status; JSONObject jsonData = appsStatus.getJsonData(); if (!jsonData.has("resources") || jsonData.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$ return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found", null); JSONArray apps = jsonData.getJSONArray("resources"); // get app routes String routesUrl = apps.getJSONObject(0).getJSONObject("entity").getString("routes_url"); URI routesURI = targetURI.resolve(routesUrl); GetMethod getRoutesMethod = new GetMethod(routesURI.toString()); HttpUtil.configureHttpMethod(getRoutesMethod, target); ServerStatus routesStatus = HttpUtil.executeMethod(getRoutesMethod); status.add(routesStatus); if (!status.isOK()) return status; jsonData = routesStatus.getJsonData(); if (!jsonData.has("resources") || jsonData.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$ return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, "No routes for the app", null); JSONArray routes = jsonData.getJSONArray("resources"); for (int i = 0; i < routes.length(); ++i) { JSONObject route = routes.getJSONObject(i); // delete route String routeUrl = route.getJSONObject(CFProtocolConstants.V2_KEY_METADATA) .getString(CFProtocolConstants.V2_KEY_URL); URI routeURI = targetURI.resolve(routeUrl); //$NON-NLS-1$ DeleteMethod deleteRouteMethod = new DeleteMethod(routeURI.toString()); HttpUtil.configureHttpMethod(deleteRouteMethod, target); ServerStatus deleteStatus = HttpUtil.executeMethod(deleteRouteMethod); status.add(deleteStatus); if (!status.isOK()) return status; } return status; } catch (Exception e) { String msg = NLS.bind("An error occured when performing operation {0}", commandName); logger.error(msg, e); return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e); } }
From source file:org.callimachusproject.client.HttpClientFactoryTest.java
@Test public void test302RedirectTarget() throws Exception { HttpContext localContext = new BasicHttpContext(); HttpGet get = new HttpGet("http://example.com/302"); get.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); BasicHttpResponse redirect = new BasicHttpResponse(_302); redirect.setHeader("Location", "http://example.com/200"); responses.add(redirect);/* ww w. jav a2 s.com*/ responses.add(new BasicHttpResponse(_200)); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_200.getStatusCode(), response.getStatusLine().getStatusCode()); return null; } }, localContext); HttpHost host = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST); URI root = new URI(host.getSchemeName(), null, host.getHostName(), host.getPort(), "/", null, null); assertEquals("http://example.com/200", root.resolve(req.getURI()).toASCIIString()); }