Example usage for java.net URL getQuery

List of usage examples for java.net URL getQuery

Introduction

In this page you can find the example usage for java.net URL getQuery.

Prototype

public String getQuery() 

Source Link

Document

Gets the query part of this URL .

Usage

From source file:edu.cornell.mannlib.vitro.webapp.filters.VitroURL.java

public VitroURL(String urlStr, String characterEncoding) {
    this.characterEncoding = characterEncoding;
    if (urlStr.indexOf("&") > -1) {
        wasXMLEscaped = true;/*from  w ww . jav a 2 s .  co  m*/
        urlStr = StringEscapeUtils.unescapeXml(urlStr);
    }
    try {
        URL url = new URL(urlStr);
        this.protocol = url.getProtocol();
        this.host = url.getHost();
        this.port = Integer.toString(url.getPort());
        this.pathParts = splitPath(url.getPath());
        this.pathBeginsWithSlash = beginsWithSlash(url.getPath());
        this.pathEndsInSlash = endsInSlash(url.getPath());
        this.queryParams = parseQueryParams(url.getQuery());
        this.fragment = url.getRef();
    } catch (Exception e) {
        // Under normal circumstances, this is because the urlStr is relative
        // We'll assume that we just have a path and possibly a query string.
        // This is likely to be a bad assumption, but let's roll with it.
        Matcher m = pathPattern.matcher(urlStr);
        String[] urlParts = new String[2];
        if (m.matches()) {
            urlParts[0] = m.group(1);
            if (m.groupCount() == 2)
                urlParts[1] = m.group(2);
        } else {
            //???
        }

        try {
            this.pathParts = splitPath(URLDecoder.decode(getPath(urlStr), characterEncoding));
            this.pathBeginsWithSlash = beginsWithSlash(urlParts[0]);
            this.pathEndsInSlash = endsInSlash(urlParts[0]);
            if (urlParts.length > 1) {
                this.queryParams = parseQueryParams(URLDecoder.decode(urlParts[1], characterEncoding));
            }
        } catch (UnsupportedEncodingException uee) {
            log.error("Unable to use character encoding " + characterEncoding, uee);
        }
    }
}

From source file:bixo.fetcher.simulation.FakeRobotsFetcher.java

@Override
public FetchedDatum get(ScoredUrlDatum scoredUrl) throws BaseFetchException {
    String url = scoredUrl.getUrl();
    LOGGER.trace("Fake fetching " + url);

    URL theUrl;
    try {//from   www .j a v  a2 s . com
        theUrl = new URL(url);
    } catch (MalformedURLException e) {
        throw new UrlFetchException(url, e.getMessage());
    }

    int statusCode = HttpStatus.SC_OK;
    int contentSize = 10000;
    int bytesPerSecond = 100000;

    if (_randomFetching) {
        contentSize = Math.max(0, (int) (_rand.nextGaussian() * 5000.0) + 10000) + 100;
        bytesPerSecond = Math.max(0, (int) (_rand.nextGaussian() * 25000.0) + 50000) + 1000;
    } else {
        String query = theUrl.getQuery();
        if (query != null) {
            String[] params = query.split("&");
            for (String param : params) {
                String[] keyValue = param.split("=");
                if (keyValue[0].equals("status")) {
                    statusCode = Integer.parseInt(keyValue[1]);
                } else if (keyValue[0].equals("size")) {
                    contentSize = Integer.parseInt(keyValue[1]);
                } else if (keyValue[0].equals("speed")) {
                    bytesPerSecond = Integer.parseInt(keyValue[1]);
                } else {
                    LOGGER.warn("Unknown fake URL parameter: " + keyValue[0]);
                }
            }
        }
    }

    if (statusCode != HttpStatus.SC_OK) {
        throw new HttpFetchException(url, "Exception requested from FakeHttpFetcher", statusCode, null);
    }

    // Now we want to delay for as long as it would take to fill in the data.
    float duration = (float) contentSize / (float) bytesPerSecond;
    LOGGER.trace(String.format("Fake fetching %d bytes at %d bps (%fs) from %s", contentSize, bytesPerSecond,
            duration, url));
    try {
        Thread.sleep((long) (duration * 1000.0));
    } catch (InterruptedException e) {
        // Break out of our delay, but preserve interrupt state.
        Thread.currentThread().interrupt();
    }

    HttpHeaders headers = new HttpHeaders();
    headers.add("x-responserate", "" + bytesPerSecond);
    FetchedDatum result = new FetchedDatum(url, url, System.currentTimeMillis(), headers,
            new ContentBytes(new byte[contentSize]), "text/html", bytesPerSecond);

    result.setPayload(scoredUrl.getPayload());
    return result;
}

From source file:org.apache.jmeter.protocol.amf.proxy.AmfRequestHdr.java

private String getUrlWithoutQuery(URL _url) {
    String fullUrl = _url.toString();
    String urlWithoutQuery = fullUrl;
    String query = _url.getQuery();
    if (query != null) {
        // Get rid of the query and the ?
        urlWithoutQuery = urlWithoutQuery.substring(0, urlWithoutQuery.length() - query.length() - 1);
    }/*from w w  w.  java2  s.c o m*/
    return urlWithoutQuery;
}

From source file:bixo.fetcher.simulation.FakeHttpFetcher.java

private FetchedDatum doGet(String url, Payload payload, boolean returnContent) throws BaseFetchException {
    LOGGER.trace("Fake fetching " + url);

    URL theUrl;
    try {//from  ww  w .  j a v a2s . co  m
        theUrl = new URL(url);
    } catch (MalformedURLException e) {
        throw new UrlFetchException(url, e.getMessage());
    }

    int statusCode = HttpStatus.SC_OK;
    int contentSize = 10000;
    int bytesPerSecond = 100000;

    if (_randomFetching) {
        contentSize = Math.max(0, (int) (_rand.nextGaussian() * 5000.0) + 10000) + 100;
        bytesPerSecond = Math.max(0, (int) (_rand.nextGaussian() * 25000.0) + 50000) + 1000;
    } else {
        String query = theUrl.getQuery();
        if (query != null) {
            String[] params = query.split("&");
            for (String param : params) {
                String[] keyValue = param.split("=");
                if (keyValue[0].equals("status")) {
                    statusCode = Integer.parseInt(keyValue[1]);
                } else if (keyValue[0].equals("size")) {
                    contentSize = Integer.parseInt(keyValue[1]);
                } else if (keyValue[0].equals("speed")) {
                    bytesPerSecond = Integer.parseInt(keyValue[1]);
                } else {
                    LOGGER.warn("Unknown fake URL parameter: " + keyValue[0]);
                }
            }
        }
    }

    if (statusCode != HttpStatus.SC_OK) {
        throw new HttpFetchException(url, "Exception requested from FakeHttpFetcher", statusCode, null);
    }

    if (!returnContent) {
        contentSize = 0;
    }

    // Now we want to delay for as long as it would take to fill in the data.
    float duration = (float) contentSize / (float) bytesPerSecond;
    LOGGER.trace(String.format("Fake fetching %d bytes at %d bps (%fs) from %s", contentSize, bytesPerSecond,
            duration, url));
    try {
        Thread.sleep((long) (duration * 1000.0));
    } catch (InterruptedException e) {
        // Break out of our delay, but preserve interrupt state.
        Thread.currentThread().interrupt();
    }

    HttpHeaders headers = new HttpHeaders();
    headers.add("x-responserate", "" + bytesPerSecond);
    FetchedDatum result = new FetchedDatum(url, url, System.currentTimeMillis(), headers,
            new ContentBytes(new byte[contentSize]), "text/html", bytesPerSecond);
    result.setPayload(payload);
    return result;
}

From source file:de.innovationgate.utils.URLBuilder.java

/**
  * Creates a URLBuilder that parses an existing URL
 * @param url The URL to parse//from   w w w.  j a v a2s.  c  o  m
 */
public URLBuilder(URL url) {
    this(url.getProtocol(), url.getPort(), url.getHost(), url.getPath(), url.getQuery(), url.getRef(), "UTF-8");
}

From source file:com.microsoft.aad.adal.Discovery.java

@Override
public boolean isValidAuthority(URL authorizationEndpoint) {
    // For comparison purposes, convert to lowercase Locale.US
    // getProtocol returns scheme and it is available if it is absolute url
    // Authority is in the form of https://Instance/tenant/somepath
    if (authorizationEndpoint != null && !StringExtensions.IsNullOrBlank(authorizationEndpoint.getHost())
            && authorizationEndpoint.getProtocol().equals("https")
            && StringExtensions.IsNullOrBlank(authorizationEndpoint.getQuery())
            && StringExtensions.IsNullOrBlank(authorizationEndpoint.getRef())
            && !StringExtensions.IsNullOrBlank(authorizationEndpoint.getPath())) {

        if (UrlExtensions.isADFSAuthority(authorizationEndpoint)) {
            Logger.e(TAG, "Instance validation returned error", "",
                    ADALError.DEVELOPER_AUTHORITY_CAN_NOT_BE_VALIDED,
                    new AuthenticationException(ADALError.DISCOVERY_NOT_SUPPORTED));
            return false;
        } else if (sValidHosts.contains(authorizationEndpoint.getHost().toLowerCase(Locale.US))) {
            // host can be the instance or inside the validated list.
            // Valid hosts will help to skip validation if validated before
            // call Callback and skip the look up
            return true;
        } else {/*from   w w w. j  av a 2s  . co m*/
            // Only query from Prod instance for now, not all of the
            // instances in the list
            return queryInstance(authorizationEndpoint);
        }
    }

    return false;
}

From source file:jeeves.utils.XmlRequest.java

public void setUrl(URL url) {
    host = url.getHost();//from   w w w . ja v  a  2  s .c om
    port = (url.getPort() == -1) ? url.getDefaultPort() : url.getPort();
    protocol = url.getProtocol();
    address = url.getPath();
    query = url.getQuery();
}

From source file:org.apache.nutch.net.urlnormalizer.slash.SlashURLNormalizer.java

public String normalize(String url, CrawlDatum crawlDatum, String scope) throws MalformedURLException {
    // Get URL repr.
    URL u = new URL(url);

    // Get the host
    String host = u.getHost();//from  w w w.  j av a  2s.co  m

    // Do we have a rule for this host?
    if (slashesMap.containsKey(host)) {
        // Yes, separate the path and optional querystring
        String protocol = u.getProtocol();
        String path = u.getPath();

        // Don't do anything to root URL's
        // / is always set by basic normalizer
        if (path.length() > 1) {
            String queryString = u.getQuery();

            // Get the rule
            boolean rule = slashesMap.get(host);

            // Does it have a trailing slash
            int lastIndexOfSlash = path.lastIndexOf(SLASH);
            boolean trailingSlash = (lastIndexOfSlash == path.length() - 1);

            // Do we need to add a trailing slash?
            if (!trailingSlash && rule) {
                // Only add a trailing slash if this path doesn't appear to have an extension/suffix such as .html
                int lastIndexOfDot = path.lastIndexOf(DOT);
                if (path.length() < 6 || lastIndexOfDot == -1 || lastIndexOfDot < path.length() - 6) {
                    StringBuilder buffer = new StringBuilder(protocol);
                    buffer.append(PROTOCOL_DELIMITER);
                    buffer.append(host);
                    buffer.append(path);
                    buffer.append(SLASH);
                    if (queryString != null) {
                        buffer.append(QUESTION_MARK);
                        buffer.append(queryString);
                    }
                    url = buffer.toString();
                }
            }

            // Do we need to remove a trailing slash?
            else if (trailingSlash && !rule) {
                StringBuilder buffer = new StringBuilder(protocol);
                buffer.append(PROTOCOL_DELIMITER);
                buffer.append(host);
                buffer.append(path.substring(0, lastIndexOfSlash));
                if (queryString != null) {
                    buffer.append(QUESTION_MARK);
                    buffer.append(queryString);
                }
                url = buffer.toString();
            }
        }
    }

    return url;
}

From source file:de.innovationgate.utils.URLBuilder.java

/**
 * Creates a URLBuilder that parses an existing URL
 * @param url The URL to parse//w w  w  .j a  v  a  2s  .  com
 * @param encoding The encoding of URL parameters. The URLBuilder will decode them.
 */
public URLBuilder(URL url, String encoding) {
    this(url.getProtocol(), url.getPort(), url.getHost(), url.getPath(), url.getQuery(), url.getRef(),
            encoding);
}

From source file:org.forgerock.openam.authentication.modules.oauth2.OAuth.java

public InputStream getContentStreamByPOST(String serviceUrl) throws LoginException {

    InputStream is = null;//from  www  .  j av  a  2  s.c om

    try {
        OAuthUtil.debugMessage("OAuth.getContentStreamByPOST: URL = " + serviceUrl);

        URL url = new URL(serviceUrl);

        String query = url.getQuery();
        OAuthUtil.debugMessage("OAuth.getContentStreamByPOST: Query: " + query);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(query);
        writer.close();

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            OAuthUtil.debugMessage("OAuth.getContentStreamByPOST: HTTP Conn OK");
            is = connection.getInputStream();
        } else { // Error Code
            String data2[] = { String.valueOf(connection.getResponseCode()) };
            OAuthUtil.debugError("OAuth.getContentStreamByPOST: HTTP Conn Error:\n" + " Response code: "
                    + connection.getResponseCode() + "\n" + " Response message: "
                    + connection.getResponseMessage() + "\n" + " Error stream: " + getErrorStream(connection)
                    + "\n");
            throw new AuthLoginException(BUNDLE_NAME, "httpErrorCode", data2);
        }
    } catch (MalformedURLException e) {
        throw new AuthLoginException(BUNDLE_NAME, "malformedURL", null, e);
    } catch (IOException e) {
        throw new AuthLoginException(BUNDLE_NAME, "ioe", null, e);
    }

    return is;

}