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:org.mule.endpoint.AbstractEndpoint.java

@Override
public String toString() {
    // Use the interface to retrieve the string and set
    // the endpoint uri to a default value
    String sanitizedEndPointUri = null;
    URI uri = null;
    if (endpointUri != null) {
        sanitizedEndPointUri = endpointUri.toString();
        uri = endpointUri.getUri();// w  w w  .j  a va  2 s . c  o m
    }
    // The following will further sanitize the endpointuri by removing
    // the embedded password. This will only remove the password if the
    // uri contains all the necessary information to successfully rebuild the url
    if (uri != null && (uri.getRawUserInfo() != null) && (uri.getScheme() != null) && (uri.getHost() != null)
            && (uri.getRawPath() != null)) {
        // build a pattern up that matches what we need tp strip out the password
        Pattern sanitizerPattern = Pattern.compile("(.*):.*");
        Matcher sanitizerMatcher = sanitizerPattern.matcher(uri.getRawUserInfo());
        if (sanitizerMatcher.matches()) {
            sanitizedEndPointUri = new StringBuffer(uri.getScheme()).append("://")
                    .append(sanitizerMatcher.group(1)).append(":<password>").append("@").append(uri.getHost())
                    .append(uri.getRawPath()).toString();
        }
        if (uri.getRawQuery() != null) {
            sanitizedEndPointUri = sanitizedEndPointUri + "?" + uri.getRawQuery();
        }

    }

    return ClassUtils.getClassName(getClass()) + "{endpointUri=" + sanitizedEndPointUri + ", connector="
            + connector + ",  name='" + name + "', mep=" + messageExchangePattern + ", properties=" + properties
            + ", transactionConfig=" + transactionConfig + ", deleteUnacceptedMessages="
            + deleteUnacceptedMessages + ", initialState=" + initialState + ", responseTimeout="
            + responseTimeout + ", endpointEncoding=" + endpointEncoding + ", disableTransportTransformer="
            + disableTransportTransformer + "}";
}

From source file:org.apache.manifoldcf.agents.output.filesystem.FileOutputConnector.java

/**
 * @param documentURI//from w w  w. j  a v a2 s .c  o m
 * @return
 * @throws URISyntaxException
 * @throws NullPointerException
 */
final private String documentURItoFilePath(String documentURI) throws URISyntaxException, NullPointerException {
    StringBuffer path = new StringBuffer();
    URI uri = null;

    uri = new URI(documentURI);

    if (uri.getScheme() != null) {
        path.append(uri.getScheme());
        path.append("/");
    }

    if (uri.getHost() != null) {
        path.append(uri.getHost());
        if (uri.getPort() != -1) {
            path.append(":");
            path.append(uri.getPort());
        }
        if (uri.getRawPath() != null) {
            if (uri.getRawPath().length() == 0) {
                path.append("/");
            } else if (uri.getRawPath().equals("/")) {
                path.append(uri.getRawPath());
            } else {
                for (String name : uri.getRawPath().split("/")) {
                    if (name.length() > 0) {
                        path.append("/");
                        path.append(convertString(name));
                    }
                }
            }
        }
        if (uri.getRawQuery() != null) {
            path.append("?");
            path.append(convertString(uri.getRawQuery()));
        }
    } else {
        if (uri.getRawSchemeSpecificPart() != null) {
            for (String name : uri.getRawSchemeSpecificPart().split("/")) {
                if (name.length() > 0) {
                    path.append("/");
                    path.append(convertString(name));
                }
            }
        }
    }

    if (path.toString().endsWith("/")) {
        path.append(".content");
    }
    return path.toString();
}

From source file:org.apache.manifoldcf.agents.output.hdfs.HDFSOutputConnector.java

/**
 * @param documentURI/*  w  w w .java  2s. c om*/
 * @return
 * @throws URISyntaxException
 */
final private String documentURItoFilePath(String documentURI) throws URISyntaxException {
    StringBuffer path = new StringBuffer();
    URI uri = null;

    uri = new URI(documentURI);

    if (uri.getScheme() != null) {
        path.append(uri.getScheme());
        path.append("/");
    }

    if (uri.getHost() != null) {
        path.append(uri.getHost());
        if (uri.getPort() != -1) {
            path.append(":");
            path.append(Integer.toString(uri.getPort()));
        }
        if (uri.getRawPath() != null) {
            if (uri.getRawPath().length() == 0) {
                path.append("/");
            } else if (uri.getRawPath().equals("/")) {
                path.append(uri.getRawPath());
            } else {
                for (String name : uri.getRawPath().split("/")) {
                    if (name != null && name.length() > 0) {
                        path.append("/");
                        path.append(name);
                    }
                }
            }
        }
        if (uri.getRawQuery() != null) {
            path.append("?");
            path.append(uri.getRawQuery());
        }
    } else {
        if (uri.getRawSchemeSpecificPart() != null) {
            for (String name : uri.getRawSchemeSpecificPart().split("/")) {
                if (name != null && name.length() > 0) {
                    path.append("/");
                    path.append(name);
                }
            }
        }
    }

    if (path.toString().endsWith("/")) {
        path.append(".content");
    }
    return path.toString();
}

From source file:org.lockss.servlet.ServeContent.java

protected LockssUrlConnection openLockssUrlConnection(LockssUrlConnectionPool pool) throws IOException {

    boolean isInCache = isInCache();
    String ifModified = null;//from  ww w.ja va2 s  . c  o  m
    String referer = null;

    LockssUrlConnection conn = UrlUtil.openConnection(url, pool);

    // check connection header
    String connectionHdr = req.getHeader(HttpFields.__Connection);
    if (connectionHdr != null && (connectionHdr.equalsIgnoreCase(HttpFields.__KeepAlive)
            || connectionHdr.equalsIgnoreCase(HttpFields.__Close)))
        connectionHdr = null;

    // copy request headers into new request
    for (Enumeration en = req.getHeaderNames(); en.hasMoreElements();) {
        String hdr = (String) en.nextElement();

        if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0)
            continue;

        if (isInCache) {
            if (HttpFields.__IfModifiedSince.equalsIgnoreCase(hdr)) {
                ifModified = req.getHeader(hdr);
                continue;
            }
        }

        if (HttpFields.__Referer.equalsIgnoreCase(hdr)) {
            referer = req.getHeader(hdr);
            continue;
        }

        // XXX Conceivably should suppress Accept-Encoding: header if it
        // specifies an encoding we don't understand, as that would prevent
        // us from rewriting.

        // copy request headers to connection
        Enumeration vals = req.getHeaders(hdr);
        while (vals.hasMoreElements()) {
            String val = (String) vals.nextElement();
            if (val != null) {
                conn.addRequestProperty(hdr, val);
            }
        }
    }

    // If the user sent an if-modified-since header, use it unless the
    // cache file has a later last-modified
    if (isInCache) {
        CIProperties cuprops = cu.getProperties();
        String cuLast = cuprops.getProperty(CachedUrl.PROPERTY_LAST_MODIFIED);
        if (log.isDebug3()) {
            log.debug3("ifModified: " + ifModified);
            log.debug3("cuLast: " + cuLast);
        }
        try {
            ifModified = HeaderUtil.later(ifModified, cuLast);
        } catch (DateParseException e) {
            // preserve user's header if parse failure
            // ignore error, serve file
            log.warning("Handling ifModifiedSince: " + ifModified + "or cuLastModified: " + cuLast + " throws ",
                    e);
        }
    }

    if (ifModified != null) {
        conn.setRequestProperty(HttpFields.__IfModifiedSince, ifModified);
    }

    // If the Referer: is a ServeContent URL then the real referring page
    // is in the url query arg.
    if (referer != null) {
        try {
            URI refUri = new URI(referer);
            if (refUri.getPath().endsWith(myServletDescr().getPath())) {
                String rawquery = refUri.getRawQuery();
                if (log.isDebug3())
                    log.debug3("rawquery: " + rawquery);
                if (!StringUtil.isNullString(rawquery)) {
                    Matcher m1 = URL_ARG_PAT.matcher(rawquery);
                    if (m1.find()) {
                        referer = UrlUtil.decodeUrl(m1.group(1));
                    }
                }
            }
        } catch (URISyntaxException e) {
            log.siteWarning("Can't perse Referer:, ignoring: " + referer);
        }

        log.debug2("Sending referer: " + referer);
        conn.setRequestProperty(HttpFields.__Referer, referer);
    }
    // send address of original requester
    conn.addRequestProperty(HttpFields.__XForwardedFor, req.getRemoteAddr());
    conn.addRequestProperty(HttpFields.__Via, proxyMgr.makeVia(getMachineName(), reqURL.getPort()));

    String cookiePolicy = proxyMgr.getCookiePolicy();
    if (cookiePolicy != null && !cookiePolicy.equalsIgnoreCase(ProxyManager.COOKIE_POLICY_DEFAULT)) {
        conn.setCookiePolicy(cookiePolicy);
    }

    return conn;
}

From source file:com.chigix.bio.proxy.buffer.FixedBufferTest.java

License:asdf

@Test
public void testURI() {
    try {/*  w ww . j  a  v a 2  s . co m*/
        URI uri = new URI(
                "http://www.baidu.com/awefawgwage/awefwfa/wefafwef/awdfa?safwefawf=awefaf&afwef=fafwef");
        System.out.println(uri.getScheme());
        System.out.println(uri.getHost());
        System.out.println(uri.getPort());
        System.out.println(uri.getQuery());
        System.out.println(uri.getPath());
    } catch (URISyntaxException ex) {
        Logger.getLogger(FixedBufferTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    String illegalQuery = "http://sclick.baidu.com/w.gif?q=a&fm=se&T=1423492890&y=55DFFF7F&rsv_cache=0&rsv_pre=0&rsv_reh=109_130_149_109_85_195_85_85_85_85|540&rsv_scr=1899_1720_0_0_1080_1920&rsv_sid=10383_1469_12498_10902_11101_11399_11277_11241_11401_12550_11243_11403_12470&cid=0&qid=fd67eec000006821&t=1423492880700&rsv_iorr=1&rsv_tn=baidu&path=http%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8%26f%3D8%26rsv_bp%3D1%26rsv_idx%3D1%26ch%3D%26tn%3Dbaidu%26bar%3D%26wd%3Da%26rn%3D%26rsv_pq%3Dda7dc5fb00004904%26rsv_t%3D55188AMIFp8JX4Jb3hJkfCZHYxQdZOBK%252FhV0kLFfAPijGGrceXBoFpnHzmI%26rsv_enter%3D1%26inputT%3D111";
    URI uri;
    while (true) {
        try {
            uri = new URI(illegalQuery);
        } catch (URISyntaxException ex) {
            System.out.println(illegalQuery);
            System.out.println(illegalQuery.charAt(ex.getIndex()));
            System.out.println(illegalQuery.substring(0, ex.getIndex()));
            System.out.println(illegalQuery.substring(ex.getIndex() + 1));
            try {
                illegalQuery = illegalQuery.substring(0, ex.getIndex())
                        + URLEncoder.encode(String.valueOf(illegalQuery.charAt(ex.getIndex())), "utf-8")
                        + illegalQuery.substring(ex.getIndex() + 1);
            } catch (UnsupportedEncodingException ex1) {
            }
            System.out.println(illegalQuery);
            continue;
        }
        break;
    }
    System.out.println("SCHEME: " + uri.getScheme());
    System.out.println("HOST: " + uri.getHost());
    System.out.println("path: " + uri.getRawPath());
    System.out.println("query: " + uri.getRawQuery());
    System.out.println("PORT: " + uri.getPort());
}

From source file:org.dasein.cloud.atmos.AtmosMethod.java

private @Nonnull String toSignatureString(@Nonnull HttpRequestBase method, @Nonnull String contentType,
        @Nonnull String range, @Nonnull String date, @Nonnull URI resource, @Nonnull List<Header> emcHeaders) {
    StringBuilder emcHeaderString = new StringBuilder();

    TreeSet<String> sorted = new TreeSet<String>();

    for (Header header : emcHeaders) {
        sorted.add(header.getName().toLowerCase());
    }/*from   w  w  w. j ava 2  s.c  o m*/
    boolean first = true;
    for (String headerName : sorted) {
        for (Header header : emcHeaders) {
            if (header.getName().toLowerCase().equals(headerName)) {
                if (!first) {
                    emcHeaderString.append("\n");
                } else {
                    first = false;
                }
                String val = header.getValue();

                if (val == null) {
                    val = "";
                }
                emcHeaderString.append(headerName);
                emcHeaderString.append(":");
                StringBuilder tmp = new StringBuilder();
                for (char c : val.toCharArray()) {
                    if (Character.isWhitespace(c)) {
                        tmp.append(" ");
                    } else {
                        tmp.append(c);
                    }
                }
                val = tmp.toString();
                while (val.contains("  ")) {
                    val = val.replaceAll("  ", " ");
                }
                emcHeaderString.append(val);
            }
        }
    }
    String path = resource.getRawPath().toLowerCase();

    if (resource.getRawQuery() != null) {
        path = path + "?" + resource.getRawQuery().toLowerCase();
    }

    return (method.getMethod() + "\n" + contentType + "\n" + range + "\n" + date + "\n" + path + "\n"
            + emcHeaderString.toString());
}

From source file:com.groupon.odo.bmp.http.BrowserMobHttpClient.java

private URI makeUri(String url) throws URISyntaxException {
    // MOB-120: check for | character and change to correctly escaped %7C
    url = url.replace(" ", "%20");
    url = url.replace(">", "%3C");
    url = url.replace("<", "%3E");
    url = url.replace("#", "%23");
    url = url.replace("{", "%7B");
    url = url.replace("}", "%7D");
    url = url.replace("|", "%7C");
    url = url.replace("\\", "%5C");
    url = url.replace("^", "%5E");
    url = url.replace("~", "%7E");
    url = url.replace("[", "%5B");
    url = url.replace("]", "%5D");
    url = url.replace("`", "%60");
    url = url.replace("\"", "%22");

    URI uri = new URI(url);

    // are we using the default ports for http/https? if so, let's rewrite the URI to make sure the :80 or :443
    // is NOT included in the string form the URI. The reason we do this is that in HttpClient 4.0 the Host header
    // would include a value such as "yahoo.com:80" rather than "yahoo.com". Not sure why this happens but we don't
    // want it to, and rewriting the URI solves it
    if ((uri.getPort() == 80 && "http".equals(uri.getScheme()))
            || (uri.getPort() == 443 && "https".equals(uri.getScheme()))) {
        // we rewrite the URL with a StringBuilder (vs passing in the components of the URI) because if we were
        // to pass in these components using the URI's 7-arg constructor query parameters get double escaped (bad!)
        StringBuilder sb = new StringBuilder(uri.getScheme()).append("://");
        if (uri.getRawUserInfo() != null) {
            sb.append(uri.getRawUserInfo()).append("@");
        }/*from   w ww  .  j av a2  s  . com*/
        sb.append(uri.getHost());
        if (uri.getRawPath() != null) {
            sb.append(uri.getRawPath());
        }
        if (uri.getRawQuery() != null) {
            sb.append("?").append(uri.getRawQuery());
        }
        if (uri.getRawFragment() != null) {
            sb.append("#").append(uri.getRawFragment());
        }

        uri = new URI(sb.toString());
    }
    return uri;
}

From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java

/**
 * Authorizes an HTTP/S request by signing it with an HMAC signature compatible with
 * the S3 service and Google Storage (legacy) authorization techniques.
 *
 * The signature is added to the request as an Authorization header.
 *
 * @param httpMethod//from  ww w.  j a  v a 2s .  c  o  m
 * the request object
 * @throws ServiceException
 */
public void authorizeHttpRequest(HttpUriRequest httpMethod, HttpContext context) throws ServiceException {
    if (getProviderCredentials() != null) {
        if (log.isDebugEnabled()) {
            log.debug("Adding authorization for Access Key '" + getProviderCredentials().getAccessKey() + "'.");
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Service has no Credential and is un-authenticated, skipping authorization");
        }
        return;
    }

    URI uri = httpMethod.getURI();
    String hostname = uri.getHost();

    /*
     * Determine the complete URL for the S3 resource, including any S3-specific parameters.
     */
    // Use raw-path, otherwise escaped characters are unescaped and a wrong
    // signature is produced
    String xfullUrl = uri.getPath();
    String fullUrl = uri.getRawPath();

    // If we are using an alternative hostname, include the hostname/bucketname in the resource path.
    String s3Endpoint = this.getEndpoint();
    if (hostname != null && !s3Endpoint.equals(hostname)) {
        int subdomainOffset = hostname.lastIndexOf("." + s3Endpoint);
        if (subdomainOffset > 0) {
            // Hostname represents an S3 sub-domain, so the bucket's name is the CNAME portion
            fullUrl = "/" + hostname.substring(0, subdomainOffset) + fullUrl;
        } else {
            // Hostname represents a virtual host, so the bucket's name is identical to hostname
            fullUrl = "/" + hostname + fullUrl;
        }
    }

    String queryString = uri.getRawQuery();
    if (queryString != null && queryString.length() > 0) {
        fullUrl += "?" + queryString;
    }

    // Set/update the date timestamp to the current time
    // Note that this will be over-ridden if an "x-amz-date" or
    // "x-goog-date" header is present.
    httpMethod.setHeader("Date", ServiceUtils.formatRfc822Date(getCurrentTimeWithOffset()));

    if (log.isDebugEnabled()) {
        log.debug("For creating canonical string, using uri: " + fullUrl);
    }

    // Generate a canonical string representing the operation.
    String canonicalString = null;
    try {
        canonicalString = RestUtils.makeServiceCanonicalString(httpMethod.getMethod(), fullUrl,
                convertHeadersToMap(httpMethod.getAllHeaders()), null, getRestHeaderPrefix(),
                getResourceParameterNames());
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Canonical string ('|' is a newline): " + canonicalString.replace('\n', '|'));
    }

    // Sign the canonical string.
    String signedCanonical = ServiceUtils.signWithHmacSha1(getProviderCredentials().getSecretKey(),
            canonicalString);

    // Add encoded authorization to connection as HTTP Authorization header.
    String authorizationString = getSignatureIdentifier() + " " + getProviderCredentials().getAccessKey() + ":"
            + signedCanonical;
    httpMethod.setHeader("Authorization", authorizationString);
}

From source file:org.apache.manifoldcf.crawler.connectors.webcrawler.WebcrawlerConnector.java

/** Convert a document identifier to filename.
 * @param documentIdentifier/*from   w  w  w . j  a  va 2  s.  com*/
 * @return
 * @throws URISyntaxException
 */
protected String documentIdentifiertoFileName(String documentIdentifier) throws URISyntaxException {
    StringBuffer path = new StringBuffer();
    URI uri = null;

    uri = new URI(documentIdentifier);

    if (uri.getRawPath() != null) {
        if (uri.getRawPath().equals("")) {
            path.append("");
        } else if (uri.getRawPath().equals("/")) {
            path.append("index.html");
        } else if (uri.getRawPath().length() != 0) {
            if (uri.getRawPath().endsWith("/")) {
                path.append("index.html");
            } else {
                String[] names = uri.getRawPath().split("/");
                path.append(names[names.length - 1]);
            }
        }
    }

    if (path.length() > 0) {
        if (uri.getRawQuery() != null) {
            path.append("?");
            path.append(uri.getRawQuery());
        }
    }

    return path.toString();
}