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:net.sourceforge.processdash.ev.ui.EVReport.java

private String fixChartHelpContent(String helpContent, String helpBaseUri, Map<String, String> chartHelp) {

    // discard headers and footers from the help content
    int cutStart = helpContent.indexOf("</h1>");
    if (cutStart != -1)
        helpContent = helpContent.substring(cutStart + 5);
    int cutEnd = helpContent.lastIndexOf("</body");
    if (cutEnd != -1)
        helpContent = helpContent.substring(0, cutEnd);

    // create a map of the chart help topics
    Map<String, String> chartUrls = new HashMap<String, String>();
    for (Map.Entry<String, String> e : chartHelp.entrySet()) {
        String chartId = e.getKey();
        String chartUrl = getChartDrillDownUrl(chartId);
        String helpUri = e.getValue();
        String helpName = hrefFileName(helpUri);
        chartUrls.put(helpName, chartUrl);
    }//from www  .ja v  a 2 s  . c  o m

    // find and fix all the hrefs in this help topic:
    //   * If any hrefs point to the help topic for a different chart,
    //     rewrite the href so it actually loads the "drill-down page"
    //     for that chart instead.
    //   * For links that point to some non-chart help topic, rewrite the
    //     href to be absolute (so the help-relative URI won't break)

    StringBuilder html = new StringBuilder(helpContent);
    int pos = 0;
    while (true) {
        // find the next href in the document.
        pos = html.indexOf("href=", pos);
        if (pos == -1)
            break; // no more hrefs to fix

        pos += 6;
        int beg = pos; // the first character of the href value itself
        char delim = html.charAt(beg - 1);
        int end = html.indexOf(String.valueOf(delim), beg);
        if (end == -1)
            continue; // invalid href syntax.  Skip to the next one.

        // extract the href value
        String oneHref = html.substring(beg, end);
        // extract the final portion of the path name
        String oneName = hrefFileName(oneHref);
        // see if that name refers to one of the charts we can display
        String chartUrl = chartUrls.get(oneName);
        if (chartUrl != null) {
            // replace the href with a chart drill-down URL
            html.replace(beg, end, chartUrl);
            pos = beg + chartUrl.length();
        } else {
            try {
                // make the URL absolute, and set a "target" attribute
                // so it will open in another window.
                URI base = new URI(helpBaseUri);
                URI target = base.resolve(oneHref);
                String newUri = target.toString();
                html.replace(beg, end, newUri);
                html.insert(beg - 6, "target='evHelp' ");
                pos = beg + newUri.length() + 16;
            } catch (Exception e) {
                // problems resolving the URI?  Turn the link into an
                // anchor so it can't be clicked on anymore.
                html.replace(beg - 6, beg - 2, "name");
            }
        }
    }

    return html.toString();
}

From source file:org.sakaiproject.lessonbuildertool.service.LessonBuilderEntityProducer.java

/**
 * Takes a URL and then decides if it should be replaced.
 * /*from  w ww  .  j  ava2 s .  co m*/
 * @param value
 * @return
 */
private String processUrl(ContentCopyContext context, String value, String contentUrl,
        Map<Long, Long> itemMap) {
    // Need to deal with backticks.
    // - /access/group/{siteId}/
    // - /web/{siteId}/
    // - /dav/{siteId}/
    // http(s)://weblearn.ox.ac.uk/ - needs trimming
    try {
        URI uri = new URI(value);
        uri = uri.normalize();
        if (value.startsWith(ITEMDUMMY)) {
            String num = value.substring(ITEMDUMMYLEN);
            int i = num.indexOf("/");
            if (i >= 0)
                num = num.substring(0, i);
            else
                return value;
            long oldItem = 0;
            try {
                oldItem = Long.parseLong(num);
            } catch (Exception e) {
                return value;
            }
            Long newItem = itemMap.get(oldItem);
            if (newItem == null)
                return value;
            return ITEMDUMMY + newItem + "/";
        } else if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
            if (uri.getHost() != null) {
                // oldserver is the server that this archive is coming from
                // oldserver null means it's a local copy, e.g. duplicate site
                // for null we match URL against all of our server names
                String oldServer = context.getOldServer();
                if (oldServer == null && servers.contains(uri.getHost()) || uri.getHost().equals(oldServer)) {
                    // Drop the protocol and the host.
                    uri = new URI(null, null, null, -1, uri.getPath(), uri.getQuery(), uri.getFragment());
                }
            }
        }
        // Only do replacement on our URLs.
        if (uri.getHost() == null && uri.getPath() != null) {
            // Need to attempt todo path replacement now.
            String path = uri.getPath();
            Matcher matcher = pathPattern.matcher(path);

            if (matcher.matches() && context.getOldSiteId().equals(matcher.group(1))) {
                // Need to push the old URL onto the list of resources to
                // process. Except that we can't do that inside Lesson Builder
                //          addPath(context, path);
                String replacementPath = path.substring(0, matcher.start(1)) + context.getNewSiteId()
                        + path.substring(matcher.end(1));
                // Create a new URI with the new path
                uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), replacementPath,
                        uri.getQuery(), uri.getFragment());
            } else if (!path.startsWith("/") && contentUrl != null) {
                // Relative URL.
                try {
                    URI base = new URI(contentUrl);
                    URI link = base.resolve(uri);
                    // sorry, no can do
                    //addPath(context, link.getPath());
                } catch (URISyntaxException e) {
                    System.err.println("Supplied contentUrl isn't valid: " + contentUrl);
                }
            }
        }
        return uri.toString();
    } catch (URISyntaxException e) {
        // Log this so we may get an idea of the things that are breaking
        // the parser.
        System.err.println("Failed to parse URL: " + value + " " + e.getMessage());
    }
    return value;
}

From source file:org.tallison.cc.CCGetter.java

private void fetch(CCIndexRecord r, Path rootDir, BufferedWriter writer) throws IOException {
    Path targFile = rootDir.resolve(r.getDigest().substring(0, 2) + "/" + r.getDigest());

    if (Files.isRegularFile(targFile)) {
        writeStatus(r, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        logger.info("already retrieved:" + targFile.toAbsolutePath());
        return;// w  ww .j  a  va  2s .co  m
    }

    String url = AWS_BASE + r.getFilename();
    URI uri = null;
    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        logger.warn("Bad url: " + url);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpHost target = new HttpHost(uri.getHost());
    String urlPath = uri.getRawPath();
    if (uri.getRawQuery() != null) {
        urlPath += "?" + uri.getRawQuery();
    }
    HttpGet httpGet = null;
    try {
        httpGet = new HttpGet(urlPath);
    } catch (Exception e) {
        logger.warn("bad path " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    if (proxyHost != null && proxyPort > -1) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        httpGet.setConfig(requestConfig);
    }
    httpGet.addHeader("Range", r.getOffsetHeader());
    HttpCoreContext coreContext = new HttpCoreContext();
    CloseableHttpResponse httpResponse = null;
    URI lastURI = null;
    try {
        httpResponse = httpClient.execute(target, httpGet, coreContext);
        RedirectLocations redirectLocations = (RedirectLocations) coreContext
                .getAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        if (redirectLocations != null) {
            for (URI redirectURI : redirectLocations.getAll()) {
                lastURI = redirectURI;
            }
        } else {
            lastURI = httpGet.getURI();
        }
    } catch (IOException e) {
        logger.warn("IOException for " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.FETCHED_IO_EXCEPTION, writer);
        return;
    }
    lastURI = uri.resolve(lastURI);

    if (httpResponse.getStatusLine().getStatusCode() != 200
            && httpResponse.getStatusLine().getStatusCode() != 206) {
        logger.warn("Bad status for " + uri.toString() + " : " + httpResponse.getStatusLine().getStatusCode());
        writeStatus(r, FETCH_STATUS.FETCHED_NOT_200, writer);
        return;
    }
    Path tmp = null;
    Header[] headers = null;
    boolean isTruncated = false;
    try {
        //this among other parts is plagiarized from centic9's CommonCrawlDocumentDownload
        //probably saved me hours.  Thank you, Dominik!
        tmp = Files.createTempFile("cc-getter", "");
        try (InputStream is = new GZIPInputStream(httpResponse.getEntity().getContent())) {
            WARCRecord warcRecord = new WARCRecord(new FastBufferedInputStream(is), "", 0);
            ArchiveRecordHeader archiveRecordHeader = warcRecord.getHeader();
            if (archiveRecordHeader.getHeaderFields().containsKey(WARCConstants.HEADER_KEY_TRUNCATED)) {
                isTruncated = true;
            }
            headers = LaxHttpParser.parseHeaders(warcRecord, "UTF-8");

            Files.copy(warcRecord, tmp, StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException e) {
        writeStatus(r, null, headers, 0L, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_READING_ENTITY,
                writer);
        deleteTmp(tmp);
        return;
    }

    String digest = null;
    long tmpLength = 0l;
    try (InputStream is = Files.newInputStream(tmp)) {
        digest = base32.encodeAsString(DigestUtils.sha1(is));
        tmpLength = Files.size(tmp);
    } catch (IOException e) {
        writeStatus(r, null, headers, tmpLength, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_SHA1, writer);
        logger.warn("IOException during digesting: " + tmp.toAbsolutePath());
        deleteTmp(tmp);
        return;
    }

    if (Files.exists(targFile)) {
        writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        deleteTmp(tmp);
        return;
    }
    try {
        Files.createDirectories(targFile.getParent());
        Files.copy(tmp, targFile);
    } catch (IOException e) {
        writeStatus(r, digest, headers, tmpLength, isTruncated,
                FETCH_STATUS.FETCHED_EXCEPTION_COPYING_TO_REPOSITORY, writer);
        deleteTmp(tmp);

    }
    writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ADDED_TO_REPOSITORY, writer);
    deleteTmp(tmp);
}

From source file:org.kitodo.production.services.data.ProcessService.java

private void copyProcessFiles(URI source, URI destination, FilenameFilter filter) throws IOException {
    List<URI> files = fileService.getSubUris(filter, source);

    for (URI file : files) {
        if (fileService.isFile(file)) {
            URI target = destination.resolve(File.separator + fileService.getFileNameWithExtension(file));
            fileService.copyFile(file, target);
        }//from w  ww.  j  a v a2s.  co  m
    }
}

From source file:org.kitodo.production.services.data.ProcessService.java

private void downloadSources(Process process, URI userHome, String atsPpnBand) throws IOException {
    URI source = fileService.getSourceDirectory(process);
    if (fileService.fileExist(source) && !fileService.getSubUris(source).isEmpty()) {
        URI destination = userHome.resolve(File.separator + atsPpnBand + "_src");
        if (!fileService.fileExist(destination)) {
            fileService.createDirectory(userHome, atsPpnBand + "_src");
        }/*  w  w  w.  j  av  a  2  s. c  om*/
        copyProcessFiles(source, destination, null);
    }
}

From source file:org.kitodo.production.services.data.ProcessService.java

/**
 * DMS-Export to a desired location.//from   w ww . j  a v a 2 s  .com
 *
 * @param process
 *            object
 * @param exportWithImages
 *            true or false
 * @param exportFullText
 *            true or false
 * @return true or false
 */

public boolean startDmsExport(Process process, boolean exportWithImages, boolean exportFullText)
        throws IOException {
    LegacyPrefsHelper preferences = ServiceManager.getRulesetService().getPreferences(process.getRuleset());
    String atsPpnBand = Helper.getNormalizedTitle(process.getTitle());

    // read document
    LegacyMetsModsDigitalDocumentHelper gdzfile = readDocument(preferences, process);
    if (Objects.isNull(gdzfile)) {
        return false;
    }

    trimAllMetadata(gdzfile.getDigitalDocument().getLogicalDocStruct());

    // validate metadata
    if (ConfigCore.getBooleanParameterOrDefaultValue(ParameterCore.USE_META_DATA_VALIDATION)
            && !ServiceManager.getMetadataValidationService().validate(gdzfile, preferences, process)) {
        return false;
    }

    Project project = process.getProject();

    // prepare place for save and download
    URI targetDirectory = new File(project.getDmsImportImagesPath()).toURI();
    URI userHome = targetDirectory;

    // if necessary, create an operation folder
    if (project.isDmsImportCreateProcessFolder()) {
        targetDirectory = userHome.resolve(File.separator + Helper.getNormalizedTitle(process.getTitle()));
        boolean created = createOperationDirectory(userHome, process);
        if (!created) {
            return false;
        }
    }

    try {
        if (exportWithImages) {
            downloadImages(process, userHome, atsPpnBand, DIRECTORY_SUFFIX);
            downloadFullText(process, userHome, atsPpnBand);
        } else if (exportFullText) {
            downloadFullText(process, userHome, atsPpnBand);
        }
        directoryDownload(process, targetDirectory);
    } catch (RuntimeException e) {
        Helper.setErrorMessage(ERROR_EXPORT, new Object[] { process.getTitle() }, logger, e);
        return false;
    }

    /*
     * zum Schluss Datei an gewnschten Ort exportieren entweder direkt in den
     * Import-Ordner oder ins Benutzerhome anschliessend den Import-Thread starten
     */
    if (project.isUseDmsImport()) {
        if (MetadataFormat
                .findFileFormatsHelperByName(project.getFileFormatDmsExport()) == MetadataFormat.METS) {
            // if METS, then write by writeMetsFile...
            writeMetsFile(process, userHome + File.separator + atsPpnBand + ".xml", gdzfile, false);
        } else {
            // ...if not, just write a Fileformat
            gdzfile.write(userHome + File.separator + atsPpnBand + ".xml");
        }

        // if necessary, METS and RDF should be written in the export
        if (MetadataFormat
                .findFileFormatsHelperByName(project.getFileFormatDmsExport()) == MetadataFormat.METS_AND_RDF) {
            writeMetsFile(process, userHome + File.separator + atsPpnBand + ".mets.xml", gdzfile, false);
        }

        Helper.setMessage(process.getTitle() + ": ", "DMS-Export started");

        if (!ConfigCore.getBooleanParameterOrDefaultValue(ParameterCore.EXPORT_WITHOUT_TIME_LIMIT)
                && project.isDmsImportCreateProcessFolder()) {
            // again remove success folder
            File successFile = new File(project.getDmsImportSuccessPath() + File.separator
                    + Helper.getNormalizedTitle(process.getTitle()));
            fileService.delete(successFile.toURI());
        }
    }
    return true;
}

From source file:net.sf.taverna.t2.security.credentialmanager.impl.CredentialManagerImpl.java

public void addFragmentedURI(LinkedHashSet<URI> possibles, URI uri, String rawFragment) {
    if (rawFragment != null && rawFragment.length() > 0)
        uri = uri.resolve("#" + rawFragment);
    possibles.add(uri);//  www.j  a va 2  s  .c  o m
}

From source file:net.www_eee.portal.channels.ProxyChannel.java

/**
 * Construct the final {@linkplain URI#isAbsolute() absolute} {@link URL} for the
 * {@linkplain #createProxyRequest(Page.Request, Channel.Mode, CloseableHttpClient) proxied} file by resolving the
 * relative {@linkplain #getProxiedFileLocalURI(Page.Request, Channel.Mode, boolean) proxied file local URI} against
 * the {@linkplain #getProxiedBaseURI(Page.Request) base URI}, and if the result isn't absolute, against the
 * {@linkplain ConfigManager#getContextResourceLocalHostURI(UriInfo, String, Map, String, boolean) local host context}
 * .//from  w  w w  . j  a  v a  2  s  . c o  m
 * 
 * @param pageRequest The {@link net.www_eee.portal.Page.Request Request} currently being processed.
 * @param mode The {@link net.www_eee.portal.Channel.Mode Mode} of the request.
 * @param validate Should any {@linkplain #isParentFoldersRestrictionDisabled(Page.Request) parent folder} or
 * {@linkplain #isDefaultPathRestrictionEnabled(Page.Request) default path} restrictions be evaluated?
 * @return The proxied file {@link URL}.
 * @throws WWWEEEPortal.Exception If a problem occurred while determining the result.
 * @throws WebApplicationException If a problem occurred while determining the result.
 * @see #createProxyRequest(Page.Request, Channel.Mode, CloseableHttpClient)
 * @see #PROXIED_FILE_URL_HOOK
 */
protected URL getProxiedFileURL(final Page.Request pageRequest, final Mode mode, final boolean validate)
        throws WWWEEEPortal.Exception, WebApplicationException {
    final Object[] context = new Object[] { mode, Boolean.valueOf(validate) };
    URL proxiedFileURL = PROXIED_FILE_URL_HOOK.value(plugins, context, pageRequest);
    if (proxiedFileURL == null) {

        try {
            final URI proxiedFileLocalURI = getProxiedFileLocalURI(pageRequest, mode, validate);
            final URI baseURI = getProxiedBaseURI(pageRequest);
            if (proxiedFileLocalURI != null) {

                final URI proxiedFileURI = baseURI.resolve(proxiedFileLocalURI);
                if (proxiedFileURI.isAbsolute()) {
                    proxiedFileURL = proxiedFileURI.toURL();
                } else {
                    proxiedFileURL = ConfigManager
                            .getContextResourceLocalHostURI(pageRequest.getUriInfo(), proxiedFileURI.getPath(),
                                    NetUtil.getQueryParams(proxiedFileURI), proxiedFileURI.getFragment(), true)
                            .toURL();
                }

            } else {

                if (baseURI.isAbsolute()) {
                    proxiedFileURL = baseURI.toURL();
                } else {
                    proxiedFileURL = ConfigManager.getContextResourceLocalHostURI(pageRequest.getUriInfo(),
                            baseURI.getPath(), NetUtil.getQueryParams(baseURI), baseURI.getFragment(), true)
                            .toURL();
                }

            }
        } catch (MalformedURLException mue) {
            throw new WWWEEEPortal.SoftwareException(mue);
        }

    }
    proxiedFileURL = PROXIED_FILE_URL_HOOK
            .requireFilteredResult(PROXIED_FILE_URL_HOOK.filter(plugins, context, pageRequest, proxiedFileURL));
    return proxiedFileURL;
}

From source file:net.sf.taverna.t2.security.credentialmanager.impl.CredentialManagerImpl.java

/**
 * Normalize an URI for insertion as the basis for path-recursive lookups,
 * ie. strip query and filename. For example:
 * /*from   w w  w.j av a 2 s.  c o m*/
 * <pre>
 * URI uri = URI.create("http://foo.org/dir1/dirX/../dir2/filename.html?q=x")
 * System.out.println(CredentialManager.normalizeServiceURI(uri));
 * >>> http://foo.org/dir1/dir2/
 * uri = URI.create("http://foo.org/dir1/dir2/");
 * System.out.println(CredentialManager.normalizeServiceURI(uri));
 * >>> http://foo.org/dir1/dir2/
 * </pre>
 * <p>
 * Note that #fragments are preserved, as these are used to indicate HTTP
 * Basic Auth realms
 * 
 * @param serviceURI
 *            URI for a service that is to be normalized
 * @return A normalized URI without query, userinfo or filename, ie. where
 *         uri.resolve(".").equals(uri).
 */
public URI normalizeServiceURI(URI serviceURI) {
    try {
        // Strip userinfo, keep fragment
        URI normalized = dnParser.setUserInfoForURI(serviceURI, null).normalize();
        return dnParser.setFragmentForURI(normalized.resolve("."), serviceURI.getFragment());
    } catch (URISyntaxException ex) {
        return serviceURI;
    }
}

From source file:org.kitodo.services.data.ProcessService.java

/**
 * Download full text./*from   w  w  w. ja  v  a2  s .  co  m*/
 *
 * @param userHome
 *            safe file
 * @param atsPpnBand
 *            String
 */
private void fulltextDownload(URI userHome, String atsPpnBand, FolderInformation fi) throws IOException {

    // download sources
    URI sources = fi.getSourceDirectory();
    if (fileService.fileExist(sources) && fileService.getSubUris(sources).size() > 0) {
        URI destination = userHome.resolve(File.separator + atsPpnBand + "_src");
        if (!fileService.fileExist(destination)) {
            fileService.createDirectory(userHome, atsPpnBand + "_src");
        }
        ArrayList<URI> dateien = fileService.getSubUris(sources);
        for (URI aDateien : dateien) {
            if (fileService.isFile(aDateien)) {
                URI meinZiel = destination
                        .resolve(File.separator + fileService.getFileNameWithExtension(aDateien));
                fileService.copyFile(aDateien, meinZiel);
            }
        }
    }

    URI ocr = fi.getOcrDirectory();
    if (fileService.fileExist(ocr)) {
        ArrayList<URI> folder = fileService.getSubUris(ocr);
        for (URI dir : folder) {
            if (fileService.isDirectory(dir) && fileService.getSubUris(dir).size() > 0
                    && fileService.getFileName(dir).contains("_")) {
                String suffix = fileService.getFileNameWithExtension(dir)
                        .substring(fileService.getFileNameWithExtension(dir).lastIndexOf("_"));
                URI destination = userHome.resolve(File.separator + atsPpnBand + suffix);
                if (!fileService.fileExist(destination)) {
                    fileService.createDirectory(userHome, atsPpnBand + suffix);
                }
                ArrayList<URI> files = fileService.getSubUris(dir);
                for (URI file : files) {
                    if (fileService.isFile(file)) {
                        URI target = destination
                                .resolve(File.separator + fileService.getFileNameWithExtension(file));
                        fileService.copyFile(file, target);
                    }
                }
            }
        }
    }
}