Example usage for java.net URI getRawPath

List of usage examples for java.net URI getRawPath

Introduction

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

Prototype

public String getRawPath() 

Source Link

Document

Returns the raw path component of this URI.

Usage

From source file:cn.isif.util_plus.http.client.util.URIBuilder.java

private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery());
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

From source file:io.gravitee.gateway.http.vertx.VertxHttpClient.java

@Override
public ClientRequest request(io.gravitee.common.http.HttpMethod method, URI uri, HttpHeaders headers,
        Handler<ClientResponse> responseHandler) {
    HttpClient httpClient = httpClients.computeIfAbsent(Vertx.currentContext(), createHttpClient());

    final int port = uri.getPort() != -1 ? uri.getPort() : (HTTPS_SCHEME.equals(uri.getScheme()) ? 443 : 80);

    String relativeUri = (uri.getRawQuery() == null) ? uri.getRawPath()
            : uri.getRawPath() + '?' + uri.getRawQuery();

    HttpClientRequest clientRequest = httpClient.request(convert(method), port, uri.getHost(), relativeUri,
            clientResponse -> handleClientResponse(clientResponse, responseHandler));

    clientRequest.setTimeout(endpoint.getHttpClientOptions().getReadTimeout());

    VertxClientRequest invokerRequest = new VertxClientRequest(clientRequest);

    clientRequest.exceptionHandler(event -> {
        LOGGER.error("Server proxying failed: {}", event.getMessage());

        if (invokerRequest.connectTimeoutHandler() != null && event instanceof ConnectTimeoutException) {
            invokerRequest.connectTimeoutHandler().handle(event);
        } else {/*from w w w . j  a va 2  s. c o  m*/
            VertxClientResponse clientResponse = new VertxClientResponse(
                    ((event instanceof ConnectTimeoutException) || (event instanceof TimeoutException))
                            ? HttpStatusCode.GATEWAY_TIMEOUT_504
                            : HttpStatusCode.BAD_GATEWAY_502);

            clientResponse.headers().set(HttpHeaders.CONNECTION, HttpHeadersValues.CONNECTION_CLOSE);

            Buffer buffer = null;

            if (event.getMessage() != null) {
                // Create body content with error message
                buffer = Buffer.buffer(event.getMessage());
                clientResponse.headers().set(HttpHeaders.CONTENT_LENGTH, Integer.toString(buffer.length()));
            }

            responseHandler.handle(clientResponse);

            if (buffer != null) {
                clientResponse.bodyHandler().handle(buffer);
            }

            clientResponse.endHandler().handle(null);
        }
    });

    // Copy headers to final API
    copyRequestHeaders(headers, clientRequest,
            (port == 80 || port == 443) ? uri.getHost() : uri.getHost() + ':' + port);

    // Check chunk flag on the request if there are some content to push and if transfer_encoding is set
    // with chunk value
    if (hasContent(headers)) {
        String transferEncoding = headers.getFirst(HttpHeaders.TRANSFER_ENCODING);
        if (HttpHeadersValues.TRANSFER_ENCODING_CHUNKED.equalsIgnoreCase(transferEncoding)) {
            clientRequest.setChunked(true);
        }
    }

    // Send HTTP head as soon as possible
    clientRequest.sendHead();

    return invokerRequest;
}

From source file:com.gistlabs.mechanize.util.apache.URIBuilder.java

private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), Consts.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

From source file:com.android.idtt.http.client.util.URIBuilder.java

private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), Charset.forName(HTTP.UTF_8));
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

From source file:oauth.signpost.signature.SignatureBaseString.java

public String normalizeUrl(String url) throws URISyntaxException {
    URI uri = new URI(url);
    String scheme = uri.getScheme().toLowerCase();
    String authority = uri.getAuthority().toLowerCase();
    boolean dropPort = (scheme.equals("http") && uri.getPort() == 80)
            || (scheme.equals("https") && uri.getPort() == 443);
    if (dropPort) {
        // find the last : in the authority
        int index = authority.lastIndexOf(":");
        if (index >= 0) {
            authority = authority.substring(0, index);
        }/*  w w w .j a va 2  s  .c  o  m*/
    }
    String path = uri.getRawPath();
    if (path == null || path.length() <= 0) {
        path = "/"; // conforms to RFC 2616 section 3.2.2
    }
    // we know that there is no query and no fragment here.
    return scheme + "://" + authority + path;
}

From source file:pl.psnc.synat.wrdz.zmd.input.InputFileUpdateBuilder.java

/**
 * Parses metadata files and returns map with names and URI of them.
 * /*from  w w w .ja v a  2s.  c  o  m*/
 * @param metadataFiles
 *            map of metadata files
 * @return parsed map of metadata files
 * @throws IncompleteDataException
 *             when some data are missing
 * @throws InvalidDataException
 *             when some data are invalid
 */
private Map<String, URI> parseMetadataFiles(Map<String, String> metadataFiles)
        throws IncompleteDataException, InvalidDataException {
    Map<String, URI> uriMetadataFiles = null;
    if (metadataFiles != null) {
        uriMetadataFiles = new HashMap<String, URI>();
        for (String metadata : metadataFiles.keySet()) {
            URI uriMetadata = null;
            try {
                uriMetadata = new URI(metadata);
            } catch (URISyntaxException e) {
                logger.debug("Object metadata URI " + metadata + " is invalid.", e);
                throw new InvalidDataException("Object metadata URI " + metadata + " is invalid.");
            }
            String name = metadataFiles.get(metadata);
            if (name == null) {
                name = uriMetadata.getRawPath();
                if (name != null) {
                    name = FilenameUtils.getName(name);
                }
            }
            if (name == null || name.isEmpty()) {
                logger.debug("Name for the metadata " + metadata + " is missing.");
                throw new IncompleteDataException("Name for the metadata " + metadata + " is missing.");
            }
            uriMetadataFiles.put(name, uriMetadata);
        }
    }
    return uriMetadataFiles;
}

From source file:com.box.restclientv2.httpclientsupport.HttpClientURIBuilder.java

private void digestURI(final URI uri) throws URISyntaxException {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), HttpClientConsts.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

From source file:org.mcxiaoke.commons.http.util.URIBuilderEx.java

private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.encodedSchemeSpecificPart = uri.getRawSchemeSpecificPart();
    this.encodedAuthority = uri.getRawAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.encodedUserInfo = uri.getRawUserInfo();
    this.userInfo = uri.getUserInfo();
    this.encodedPath = uri.getRawPath();
    this.path = uri.getPath();
    this.encodedQuery = uri.getRawQuery();
    this.queryParams = parseQuery(uri.getRawQuery(), URIUtilsEx.UTF_8);
    this.encodedFragment = uri.getRawFragment();
    this.fragment = uri.getFragment();
}

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  w  w .  j a  v  a 2s  . com
    }

    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.cbioportal.security.spring.PortalSavedRequestAwareAuthenticationSuccessHandler.java

private String getRelativeURI(HttpServletRequest request, String targetURI) {
    String relativeURI = null;/*  www  .j  a v a  2s  .co m*/
    try {
        URI originalURI = new URI(targetURI);
        logger.debug("getRelativeURI(): request.getServletContext() = '" + request.getServletContext() + "'");
        logger.debug("getRelativeURI(): testing '" + new URI(request.getContextPath()) + "'");
        // URI(String scheme, String authority, String path, String query, String fragment)
        // use relativize so we do not include context path e.g. /cbioportal/
        // use resolve to make sure we have a "/" at the front
        relativeURI = new URI("/")
                .resolve(new URI(request.getContextPath()).relativize(new URI(null, null,
                        originalURI.getRawPath(), originalURI.getRawQuery(), originalURI.getRawFragment())))
                .toString();
        logger.debug("getRelativeURI(): changing '" + targetURI + "' to '" + relativeURI + "'");
    } catch (URISyntaxException e) {
        return null;
    }
    return relativeURI;
}