Example usage for java.net URI resolve

List of usage examples for java.net URI resolve

Introduction

In this page you can find the example usage for java.net URI resolve.

Prototype

public URI resolve(String str) 

Source Link

Document

Constructs a new URI by parsing the given string and then resolving it against this URI.

Usage

From source file:uk.org.taverna.commons.update.impl.UpdateManagerImpl.java

@Override
public boolean update() throws UpdateException {
    if (updateAvailable) {
        ApplicationProfile applicationProfile = applicationConfiguration.getApplicationProfile();
        Updates updates = applicationProfile.getUpdates();
        URL profileURL;/*from w ww. j a  v a2 s  .  c  om*/
        try {
            URI updateSiteURI = new URI(updates.getUpdateSite());
            profileURL = updateSiteURI.resolve(latestVersion.getFile()).toURL();
        } catch (MalformedURLException e) {
            throw new UpdateException(
                    String.format("Application profile URL (%s) is not a valid URL", latestVersion.getFile()),
                    e);
        } catch (URISyntaxException e) {
            throw new UpdateException(
                    String.format("Update site URL (%s) is not a valid URL", updates.getUpdateSite()), e);
        }

        File updateDirectory = new File(applicationConfiguration.getApplicationHomeDir(), "updates");
        updateDirectory.mkdirs();
        File latestProfileFile = new File(updateDirectory,
                "ApplicationProfile-" + latestVersion.getVersion() + ".xml");
        try {
            downloadManager.download(profileURL, latestProfileFile, DIGEST_ALGORITHM);
        } catch (DownloadException e) {
            throw new UpdateException(String.format("Error downloading %1$s", profileURL), e);
        }

        ApplicationProfile latestProfile;
        try {
            latestProfile = (ApplicationProfile) unmarshaller.unmarshal(latestProfileFile);
        } catch (JAXBException e) {
            throw new UpdateException(String.format("Error reading %s", latestProfileFile.getName()), e);
        }

        Set<BundleInfo> requiredBundles = getRequiredBundles(applicationConfiguration.getApplicationProfile(),
                latestProfile);
        downloadBundles(latestProfile, requiredBundles,
                new File(applicationConfiguration.getStartupDir(), "lib"));
        File applicationProfileFile = new File(applicationConfiguration.getStartupDir(),
                "ApplicationProfile.xml");
        try {
            FileUtils.copyFile(latestProfileFile, applicationProfileFile);
        } catch (IOException e) {
            throw new UpdateException(String.format("Error copying %1$s to %2$s", latestProfileFile.getName(),
                    applicationProfileFile.getName()), e);
        }
        //         eventAdmin.postEvent(new Event("UpdateManagerEvent", new HashMap()));
        updateAvailable = false;
        return true;
    }
    return false;
}

From source file:org.eclipse.orion.server.cf.commands.GetDomainsCommand.java

@Override
protected ServerStatus _doIt() {
    try {/*w w w. j  a va 2  s.  com*/
        /* get available domains */
        URI targetURI = URIUtil.toURI(target.getUrl());
        String domainsURL = target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_ENTITY)
                .getString(CFProtocolConstants.V2_KEY_DOMAINS_URL);
        URI domainsURI = targetURI.resolve(domainsURL);

        GetMethod getDomainsMethod = new GetMethod(domainsURI.toString());
        HttpUtil.configureHttpMethod(getDomainsMethod, target);
        getDomainsMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$
        ServerStatus getDomainStatus = HttpUtil.executeMethod(getDomainsMethod);

        /* extract available domains */
        JSONObject domainsJSON = getDomainStatus.getJsonData();

        if (domainsJSON.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) {
            return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, null, null);
        }

        JSONObject result = new JSONObject();
        domains = new ArrayList<Domain>();
        JSONArray resources = domainsJSON.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES);
        for (int k = 0; k < resources.length(); ++k) {
            JSONObject domainJSON = resources.getJSONObject(k);
            Domain domain = new Domain();
            domain.setCFJSON(domainJSON);
            if (domainName == null || domainName.equals(domain.getDomainName())) {
                domains.add(domain);
                result.append("Domains", domain.toJSON());
            }
        }

        if (domains.isEmpty())
            return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Domain can not be found",
                    null);

        return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
    } catch (Exception e) {
        String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
        logger.error(msg, e);
        return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
    }
}

From source file:org.eclipse.orion.server.cf.commands.GetRoutesCommand.java

@Override
protected ServerStatus _doIt() {
    try {//w  w w  .j a va 2 s.  c  o m
        /* get available routes */
        URI targetURI = URIUtil.toURI(target.getUrl());
        String routesURL = target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_ENTITY)
                .getString(CFProtocolConstants.V2_KEY_ROUTES_URL);
        URI routesURI = targetURI.resolve(routesURL);

        GetMethod getRoutesMethod = new GetMethod(routesURI.toString());
        HttpUtil.configureHttpMethod(getRoutesMethod, target);
        getRoutesMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$

        ServerStatus getRouteStatus = HttpUtil.executeMethod(getRoutesMethod);
        if (!getRouteStatus.isOK())
            return getRouteStatus;

        /* extract available routes */
        JSONObject routesJSON = getRouteStatus.getJsonData();

        if (routesJSON.getInt(CFProtocolConstants.V2_KEY_TOTAL_RESULTS) < 1) {
            return new ServerStatus(IStatus.OK, HttpServletResponse.SC_OK, null, null);
        }

        JSONObject result = new JSONObject();
        routes = new ArrayList<Route>();
        JSONArray resources = routesJSON.getJSONArray(CFProtocolConstants.V2_KEY_RESOURCES);
        for (int k = 0; k < resources.length(); ++k) {
            JSONObject routeJSON = resources.getJSONObject(k);
            Route route = new Route();
            route.setCFJSON(routeJSON);

            if (domainName == null || (domainName.equals(route.getDomainName())
                    && (hostName == null || hostName.equals(route.getHost())))) {
                if (!orphaned || route.getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_ENTITY)
                        .getJSONArray(CFProtocolConstants.V2_KEY_APPS).length() == 0) {
                    routes.add(route);
                    result.append("Routes", route.toJSON());
                }
            }
        }

        return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
    } catch (Exception e) {
        String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
        logger.error(msg, e);
        return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
    }
}

From source file:org.mule.tools.rhinodo.rhino.NodeRequire.java

public Object callSuperWrapped(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    try {//from  www  .  ja  v  a 2 s. c  om
        ScriptableObject.putProperty(thisObj, "_compile", compile);
        return super.call(cx, scope, thisObj, args);
    } catch (JavaScriptException e) {
        if (thisObj instanceof ModuleScope) {
            String id = (String) Context.jsToJava(args[0], String.class);
            ModuleScope moduleScope = (ModuleScope) thisObj;
            URI base = moduleScope.getBase();
            URI current = moduleScope.getUri();
            URI uri = current.resolve(id + ".js/");

            if (!id.startsWith("./") && !id.startsWith("../") && base != null
                    && new File(uri.getPath()).exists()) {
                // try to convert to a relative URI rooted on base
                return super.call(cx, scope, thisObj, new Object[] { uri.getPath() });
            }
        }
        throw e;
    }
}

From source file:org.apache.taverna.download.impl.DownloadManagerImpl.java

@Override
public void download(URI source, Path destination, String digestAlgorithm) throws DownloadException {
    URI digestSource = null;/*w w w.j a v a  2 s . co m*/
    if (digestAlgorithm != null) {
        // Note: Will break with ?download=file.xml kind of URLs
        digestSource = source.resolve(source.getPath() + mapAlgorithmToFileExtension(digestAlgorithm));
    }
    download(source, destination, digestAlgorithm, digestSource);
}

From source file:org.portletbridge.portlet.BridgeFunctions.java

private URI resolveUrl(URI base, String trim) {
    URI url = null;/*from   w  w w  .j  a va2s.  c  om*/
    if (trim.startsWith("?")) {
        return URI.create(base.getScheme() + "://" + base.getAuthority() + base.getPath() + trim);
    } else {
        url = base.resolve(trim);
    }
    return url;
}

From source file:org.craftercms.engine.freemarker.RenderComponentDirective.java

protected SiteItem getComponent(String componentPath, Environment env) throws TemplateException {
    SiteItem currentPage = unwrap(KEY_CONTENT_MODEL, env.getVariable(CrafterPageView.KEY_CONTENT_MODEL),
            SiteItem.class, env);
    if (currentPage != null) {
        String basePath = FilenameUtils.getFullPath(currentPage.getStoreUrl());
        URI baseUri = URI.create(basePath);

        try {//ww  w  . j  a  v  a  2  s .  co  m
            componentPath = baseUri.resolve(componentPath).toString();
        } catch (IllegalArgumentException e) {
            throw new TemplateException("Invalid relative component URL " + componentPath, e, env);
        }
    }

    SiteItem component;
    try {
        component = siteItemService.getSiteItem(componentPath);
    } catch (Exception e) {
        throw new TemplateException(
                "Unable to load component " + componentPath + " from the underlying repository", e, env);
    }

    if (component == null) {
        throw new TemplateException("No component found at path '" + componentPath + "'", env);
    }

    return component;
}

From source file:org.mobicents.servlet.sip.restcomm.interpreter.tagstrategy.RcmlTagStrategy.java

protected URI resolveIfNotAbsolute(final URI base, final URI uri) {
    if (base.equals(uri)) {
        return uri;
    } else {//  w  w w  . j  ava2s  . c o  m
        if (!uri.isAbsolute()) {
            return base.resolve(uri);
        } else {
            return uri;
        }
    }
}

From source file:de.l3s.boilerpipe.sax.HtmlArticleExtractor.java

private String removeNotAllowedTags(String htmlFragment, URI docUri) {
    Source source = new Source(htmlFragment);
    OutputDocument outputDocument = new OutputDocument(source);
    List<Element> elements = source.getAllElements();

    for (Element element : elements) {
        Attributes attrs = element.getAttributes();
        Map<String, String> attrsUpdate = outputDocument.replace(attrs, true);
        if (!element.getName().contains("a")) {
            attrsUpdate.clear();//  w w  w  .  ja va2s  .c  o m
        } else {
            if (attrsUpdate.get("href") != null) {
                String link = attrsUpdate.get("href");
                if (!link.contains("http")) {
                    URI documentUri = docUri;

                    URI anchorUri;
                    try {
                        anchorUri = new URI(link);
                        URI result = documentUri.resolve(anchorUri);

                        attrsUpdate.put("href", result.toString());
                    } catch (URISyntaxException e) {
                        outputDocument.remove(element);
                    }
                }
            }
        }

        if (NOT_ALLOWED_HTML_TAGS.contains(element.getName())) {
            Segment content = element.getContent();
            if (element.getName() == "script" || element.getName() == "style" || element.getName() == "form") {
                outputDocument.remove(content);
            }
            outputDocument.remove(element.getStartTag());

            if (!element.getStartTag().isSyntacticalEmptyElementTag()) {
                outputDocument.remove(element.getEndTag());
            }
        }
    }

    String out = outputDocument.toString();
    out = out.replaceAll("\\n", "");
    out = out.replaceAll("\\t", "");

    return out;
}

From source file:org.eclipse.orion.server.cf.commands.DeleteApplicationCommand.java

@Override
protected ServerStatus _doIt() {
    try {/* w  ww .  j av a  2 s  .  c  o  m*/

        /* read deploy parameters */
        JSONObject appMetadata = null;
        JSONObject appEntity = null;

        URI targetURI = URIUtil.toURI(target.getUrl());

        /* get application details */
        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);
        if (!appsStatus.isOK())
            return appsStatus;

        JSONObject apps = appsStatus.getJsonData();
        if (!apps.has("resources") || apps.getJSONArray("resources").length() == 0) //$NON-NLS-1$//$NON-NLS-2$
            return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, "Application not found",
                    null);

        appMetadata = apps.getJSONArray("resources").getJSONObject(0).getJSONObject("metadata"); //$NON-NLS-1$ //$NON-NLS-2$
        appEntity = apps.getJSONArray("resources").getJSONObject(0).getJSONObject("entity"); //$NON-NLS-1$ //$NON-NLS-2$

        if (application.getGuid() == null) {

            String summaryAppUrl = appMetadata.getString("url") + "/summary"; //$NON-NLS-1$ //$NON-NLS-2$
            URI summaryAppURI = targetURI.resolve(summaryAppUrl);

            GetMethod getSummaryMethod = new GetMethod(summaryAppURI.toString());
            HttpUtil.configureHttpMethod(getSummaryMethod, target);

            ServerStatus getStatus = HttpUtil.executeMethod(getSummaryMethod);
            if (!getStatus.isOK())
                return getStatus;

            JSONObject summaryJSON = getStatus.getJsonData();

            /* set known application GUID */
            application.setGuid(summaryJSON.getString(CFProtocolConstants.V2_KEY_GUID));
        }

        /* gather application service bindings */
        ArrayList<String> serviceInstances = new ArrayList<String>();
        JSONArray appServiceBindings = appEntity.getJSONArray("service_bindings"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        for (int i = 0; i < appServiceBindings.length(); ++i) {
            JSONObject binding = appServiceBindings.getJSONObject(i).getJSONObject("entity"); //$NON-NLS-1$
            serviceInstances.add(binding.getString("service_instance_url")); //$NON-NLS-1$
        }

        /* delete the application */
        URI appURI = targetURI.resolve("/v2/apps/" + application.getGuid()); //$NON-NLS-1$

        DeleteMethod deleteAppMethod = new DeleteMethod(appURI.toString());
        HttpUtil.configureHttpMethod(deleteAppMethod, target);
        deleteAppMethod.setQueryString("recursive=true"); //$NON-NLS-1$

        ServerStatus status = HttpUtil.executeMethod(deleteAppMethod);
        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);
    }
}