Example usage for java.net URI getRawQuery

List of usage examples for java.net URI getRawQuery

Introduction

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

Prototype

public String getRawQuery() 

Source Link

Document

Returns the raw query component of this URI.

Usage

From source file:com.netflix.iep.http.RxHttp.java

/**
 * Perform a POST request with form data. The body will be extracted from the query string
 * in the URI./*from  w w  w .  j a  v a  2 s  .c  o m*/
 *
 * @param uri
 *     Location to send the data.
 * @return
 *     Observable with the response of the request.
 */
public Observable<HttpClientResponse<ByteBuf>> postForm(URI uri) {
    Preconditions.checkNotNull(uri.getRawQuery(), "uri.query");
    byte[] entity = getBytes(uri.getRawQuery());
    return post(uri, HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED, entity);
}

From source file:com.mgmtp.perfload.core.client.web.request.HttpRequestHandler.java

/**
 * Creates the request object./*from w w w. ja  v a  2 s.  com*/
 * 
 * @param type
 *            the type of the HTTP request (GET, TRACE, DELETE, OPTIONS, HEAD, POST, PUT)
 * @param uri
 *            the uri
 * @param parameters
 *            the request parameters
 * @param body
 *            the request body
 * @return the request
 */
protected HttpRequestBase createRequest(final String type, final URI uri, final List<NameValuePair> parameters,
        final Body body) throws Exception {
    HttpRequestBase request = HttpMethod.valueOf(type).create(uri);
    if (!(request instanceof HttpEntityEnclosingRequest)) {
        //  GET, TRACE, DELETE, OPTIONS, HEAD
        if (!parameters.isEmpty()) {
            String query = URLEncodedUtils.format(parameters, "UTF-8");
            URI requestURI = new URI(
                    uri.getRawQuery() == null ? uri.toString() + '?' + query : uri.toString() + '&' + query);
            request.setURI(requestURI);
        }
    } else {
        // POST, PUT
        final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        if (body != null) {
            // this only sets the content, header come from the request flow
            entityRequest.setEntity(new ByteArrayEntity(body.getContent()));
        } else {
            checkState(request instanceof HttpPost, "Invalid request: " + request.getMethod()
                    + ". Cannot add post parameters to this kind of request. Please check the request flow.");
            entityRequest.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
        }
    }
    return request;
}

From source file:code.google.restclient.client.HitterClient.java

private ViewRequest prepareViewRequest(ViewRequest req, HttpHandler handler) {
    if (handler != null && req != null) {
        req.setReqLine(handler.getRequestLine());
        // req.setUrl(handler.getUrl());
        req.setHeaders(handler.getRequestHeaders());
        URI uri = handler.getUri();
        if (uri != null) {
            req.setHost(uri.getHost());/*from  w  ww  . j  a  va2s.com*/
            req.setPort(uri.getPort());
            req.setPath(uri.getRawPath());
            req.setScheme(uri.getScheme());
            req.setQueryStrRaw(uri.getRawQuery());
        }
        req.setProtocolVersion(handler.getProtocolVersion());
    }
    return req;
}

From source file:com.discovery.darchrow.net.ParamUtil.java

/**
 * ? <br>//from www. j a  v a  2 s.  c o m
 * ?????a=1&a=2,a,[3,4] a=3&a=4.
 * 
 * @param uri
 *            URI ? (URI),<br>
 *            ? ?,?,??,<br>
 *            ??, ?
 * @param nameAndValueMap
 *            nameAndValueMap  request.getParameterMap
 * @param charsetType
 *            ?
 * @return ? ???
 */
public static String addParameterArrayMap(URI uri, Map<String, String[]> nameAndValueMap, String charsetType) {
    if (null == uri) {
        throw new IllegalArgumentException("uri can not be null!");
    }
    if (Validator.isNullOrEmpty(nameAndValueMap)) {
        throw new IllegalArgumentException("nameAndValueMap can not be null!");
    }
    // ***********************************************************************
    String url = uri.toString();
    String before = URIUtil.getBeforePath(url);
    // ***********************************************************************
    // getQuery()  URI ??
    // getRawQuery()  URI ? URI ???? URI 
    String query = uri.getRawQuery();
    // ***********************************************************************
    Map<String, String[]> map = new LinkedHashMap<String, String[]>();
    // url??
    if (Validator.isNullOrEmpty(query)) {
        // nothing to do
    } else {
        Map<String, String[]> originalMap = URIUtil.parseQueryToArrayMap(query, null);
        map.putAll(originalMap);
    }
    map.putAll(nameAndValueMap);
    // **************************************************************
    return URIUtil.getEncodedUrlByArrayMap(before, map, charsetType);
}

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:org.ldp4j.tutorial.client.CachedRepresentationManager.java

private File createFile(String resource) {
    URI uri = URI.create(resource);
    StringBuilder builder = new StringBuilder();
    builder.append(uri.getScheme()).append("_");
    String userInfo = uri.getUserInfo();
    if (userInfo != null) {
        builder.append(userInfo).append("@");
    }/* w ww  .jav a2 s .co  m*/
    builder.append(uri.getHost());
    if (uri.getPort() >= 0) {
        builder.append("_").append(uri.getPort());
    }
    if (uri.getPath() != null) {
        builder.append(uri.getRawPath().replace("/", "_"));
    }
    if (uri.getQuery() != null) {
        builder.append("?").append(uri.getRawQuery());
    }
    if (uri.getFragment() != null) {
        builder.append("#").append(uri.getRawFragment());
    }
    builder.append(".dat");
    File file = new File(this.cacheDirectory, builder.toString());
    return file;
}

From source file:org.wikipedia.vlsergey.secretary.jwpf.HttpBot.java

protected final void performAction(final ContentProcessable contentProcessable)
        throws ActionException, ProcessException {
    List<HttpRequestBase> msgs = contentProcessable.getMessages();
    Iterator<HttpRequestBase> it = msgs.iterator();
    while (it.hasNext()) {
        HttpRequestBase httpMethod = it.next();
        if (getSite() != null) {

            URI uri = httpMethod.getURI();
            if (!uri.getPath().startsWith("/wiki/")) {
                try {
                    String str = getSite().getScheme() + "://" + getSite().getHost()
                            + (getSite().getPort() == -1 ? "" : ":" + getSite().getPort()) + getSite().getPath()
                            + uri.getPath() + (uri.getRawQuery() != null ? ("?" + uri.getRawQuery()) : "");
                    uri = new URI(str);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }/*from  w w  w  . j av a 2s. com*/
                httpMethod.setURI(uri);
            } else {
                try {
                    String str = getSite().getScheme() + "://" + getSite().getHost()
                            + (getSite().getPort() == -1 ? "" : ":" + getSite().getPort()) + uri.getPath()
                            + (uri.getRawQuery() != null ? ("?" + uri.getRawQuery()) : "");
                    uri = new URI(str);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                httpMethod.setURI(uri);
            }

            // logger.debug("path is: " + httpMethod.getURI());
        }

        try {
            while (true) {
                try {
                    if (httpMethod instanceof HttpGet) {
                        get((HttpGet) httpMethod, contentProcessable);
                    } else {
                        post((HttpPost) httpMethod, contentProcessable);
                    }
                    break;
                } catch (NoHttpResponseException exc) {
                    log.info("NoHttpResponseException, wait 6 seconds");
                    try {
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                    }
                } catch (SocketException exc) {
                    log.info("SocketException, wait 5 seconds");
                    try {
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                    }
                } catch (ServerErrorException exc) {
                    log.info("ServerErrorException (" + exc.getStatusLine() + "), wait 5 seconds");
                    try {
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                    }
                } catch (DatabaseLagException exc) {
                    log.info("Database lag occured: " + exc.databaseLag);
                    int retryAfter = 6;
                    try {
                        retryAfter = Integer.parseInt(exc.retryAfter.getValue());
                    } catch (Exception exc2) {
                        // ignore
                    }
                    if (retryAfter != 0) {
                        log.info("Waiting for " + retryAfter + " seconds");
                        try {
                            Thread.sleep(retryAfter * 1000);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }
        } catch (IOException e1) {
            throw new ActionException(e1);
        }
    }
}

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: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  . ja  v a2 s  .c o  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: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();
}