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:org.dasein.cloud.azure.platform.AzureSQLDatabaseSupportRequests.java

private String getEncodedUri(String urlString) throws InternalException {
    try {//from   www. ja va2s  .c  om
        URL url = new URL(urlString);
        return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef()).toString();
    } catch (Exception e) {
        throw new InternalException(e.getMessage());
    }
}

From source file:it.polimi.chansonnier.agent.YoutubeGrabber.java

/**
 * //from www  . ja v a 2s  .c  om
 * @param pageUrl
 * @return url of the FLV file
 * @throws Exception
 */
private String getVideoUrl(String pageUrl) throws Exception {
    URL page = new URL(pageUrl);
    String query = page.getQuery();
    String[] tokens = query.split("&");
    HashMap<String, String> params = new HashMap<String, String>();
    for (int i = 0; i < tokens.length; i++) {
        String[] parts = tokens[i].split("=");
        params.put(parts[0], parts[1]);
    }
    String video_id = params.get("v");
    if (video_id == null)
        video_id = inbtwn(URLDecoder.decode(getRedirUrl(pageUrl), "UTF-8"), "v=", "&");
    String pageSource = URLUtils.retrieve(new URL("http://www.youtube.com/watch?v=" + video_id));

    String title = inbtwn(pageSource, "'VIDEO_TITLE': '", "',");
    if (title == null)
        title = inbtwn(pageSource, "name=\"title\" content=\"", "\"");
    if (title == null) {
        throw new RuntimeException("The page does not contain a video (" + pageUrl + ").");
    }
    title = setHTMLEntity(title);

    String token = inbtwn(pageSource, "\"t\": \"", "\"");
    if (token == null)
        token = inbtwn(pageSource, "&t=", "&");
    if (!token.endsWith("%3D"))
        token = inbtwnmore(pageSource, "&t=", "&", 2);

    String dl_flvlow = null;
    String dl_flvmed = null;
    String dl_flvmed2 = null;
    String dl_flvhigh = null;

    dl_flvmed = getRedirUrl(
            "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=34");
    if (dl_flvmed != null) {
        _log.debug("flvmed for video " + video_id);
        return dl_flvmed;
    }
    dl_flvmed2 = getRedirUrl(
            "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=6");
    if (dl_flvmed2 != null) {
        _log.debug("flvmed2 for video " + video_id);
        return dl_flvmed2;
    }
    dl_flvlow = getRedirUrl("http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=5");
    if (dl_flvlow != null) {
        _log.debug("flvlow for video " + video_id);
        return dl_flvlow;
    }
    dl_flvhigh = getRedirUrl(
            "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=35");
    if (dl_flvhigh != null) {
        _log.debug("flvhigh for video " + video_id);
        return dl_flvhigh;
    }

    // other formats
    String dl_3gplow = null;
    String dl_3gpmed = null;
    String dl_3gphigh = null;
    String dl_mp4high = null;
    String dl_mp4hd = null;
    String dl_mp4hd2 = null;

    if (dl_3gplow == null)
        dl_3gplow = getRedirUrl(
                "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=13");
    if (dl_3gplow != null)
        return dl_3gplow;
    if (dl_3gpmed == null)
        dl_3gpmed = getRedirUrl(
                "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=17");
    if (dl_3gpmed != null)
        return dl_3gpmed;
    if (dl_3gphigh == null)
        dl_3gphigh = getRedirUrl(
                "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=36");
    if (dl_3gphigh != null)
        return dl_3gphigh;
    if (dl_mp4high == null)
        dl_mp4high = getRedirUrl(
                "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=18");
    if (dl_mp4high != null)
        return dl_mp4high;
    if (dl_mp4hd == null)
        dl_mp4hd = getRedirUrl(
                "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=22");
    if (dl_mp4hd != null)
        return dl_mp4hd;
    if (dl_mp4hd2 == null)
        dl_mp4hd2 = getRedirUrl(
                "http://www.youtube.com/get_video?video_id=" + video_id + "&t=" + token + "&fmt=37");
    if (dl_mp4hd2 != null)
        return dl_mp4hd2;
    throw new Exception("No suitable file found.");

}

From source file:org.geotools.data.wfs.protocol.http.AbstractHttpProtocol.java

protected String createUri(final URL baseUrl, final Map<String, String> queryStringKvp) {
    final String query = baseUrl.getQuery();
    Map<String, String> finalKvpMap = new HashMap<String, String>(queryStringKvp);
    if (query != null && query.length() > 0) {
        Map<String, String> userParams = new CaseInsensitiveMap(queryStringKvp);
        String[] rawUrlKvpSet = query.split("&");
        for (String rawUrlKvp : rawUrlKvpSet) {
            int eqIdx = rawUrlKvp.indexOf('=');
            String key, value;/*w ww  .  j  a  v a  2s . c  om*/
            if (eqIdx > 0) {
                key = rawUrlKvp.substring(0, eqIdx);
                value = rawUrlKvp.substring(eqIdx + 1);
            } else {
                key = rawUrlKvp;
                value = null;
            }
            try {
                value = URLDecoder.decode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            if (userParams.containsKey(key)) {
                LOGGER.fine("user supplied value for query string argument " + key
                        + " overrides the one in the base url");
            } else {
                finalKvpMap.put(key, value);
            }
        }
    }

    String protocol = baseUrl.getProtocol();
    String host = baseUrl.getHost();
    int port = baseUrl.getPort();
    String path = baseUrl.getPath();

    StringBuilder sb = new StringBuilder();
    sb.append(protocol).append("://").append(host);
    if (port != -1 && port != baseUrl.getDefaultPort()) {
        sb.append(':');
        sb.append(port);
    }
    if (!"".equals(path) && !path.startsWith("/")) {
        sb.append('/');
    }
    sb.append(path).append('?');

    String key, value;
    try {
        Entry<String, String> kvp;
        for (Iterator<Map.Entry<String, String>> it = finalKvpMap.entrySet().iterator(); it.hasNext();) {
            kvp = it.next();
            key = kvp.getKey();
            value = kvp.getValue();
            if (value == null) {
                value = "";
            } else {
                value = URLEncoder.encode(value, "UTF-8");
            }
            sb.append(key);
            sb.append('=');
            sb.append(value);
            if (it.hasNext()) {
                sb.append('&');
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    final String finalUrlString = sb.toString();
    return finalUrlString;
}

From source file:com.hp.autonomy.searchcomponents.hod.view.HodViewServerService.java

@Override
public void viewDocument(final String reference, final ResourceIdentifier index,
        final OutputStream outputStream) throws IOException, HodErrorException {
    final GetContentRequestBuilder getContentParams = new GetContentRequestBuilder().setPrint(Print.all);
    final Documents<Document> documents = getContentService.getContent(Collections.singletonList(reference),
            index, getContentParams);/*  w  w w  .  j a  va  2s  . c  om*/

    // This document will always exist because the GetContentService.getContent throws a HodErrorException if the
    // reference doesn't exist in the index
    final Document document = documents.getDocuments().get(0);

    final Map<String, Serializable> fields = document.getFields();
    final Object urlField = fields.get(URL_FIELD);

    final String documentUrl = urlField instanceof List ? ((List<?>) urlField).get(0).toString()
            : document.getReference();

    final UrlValidator urlValidator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
    InputStream inputStream = null;

    try {
        try {
            final URL url = new URL(documentUrl);
            final URI uri = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), null);
            final String encodedUrl = uri.toASCIIString();

            if (urlValidator.isValid(encodedUrl)) {
                inputStream = viewDocumentService.viewUrl(encodedUrl, new ViewDocumentRequestBuilder());
            } else {
                throw new URISyntaxException(encodedUrl, "Invalid URL");
            }
        } catch (URISyntaxException | MalformedURLException e) {
            // URL was not valid, fall back to using the document content
            inputStream = formatRawContent(document);
        } catch (final HodErrorException e) {
            if (e.getErrorCode() == HodErrorCode.BACKEND_REQUEST_FAILED) {
                // HOD failed to read the url, fall back to using the document content
                inputStream = formatRawContent(document);
            } else {
                throw e;
            }
        }

        IOUtils.copy(inputStream, outputStream);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.openteach.diamond.metadata.ServiceURL.java

/**
 * /*from   ww w .  j  a va2s  . c o m*/
 * @throws MalformedURLException
 */
private void parse() throws MalformedURLException {
    int index = strURL.indexOf("://");
    URL url = new URL(String.format("http%s", strURL.substring(index)));
    protocol = strURL.substring(0, index);
    host = url.getHost();
    port = url.getPort();
    serviceName = url.getFile();
    query = url.getQuery();
}

From source file:org.nuxeo.ecm.platform.ui.web.auth.TestLoginScreenConfig.java

@Test
public void iframe_url_embeds_the_distribution_package_type_and_version() throws Exception {

    LoginScreenConfig config = new LoginScreenConfig();
    String strUrl = config.getNewsIframeUrl();
    if (!strUrl.startsWith("http")) {
        strUrl = "http:" + strUrl;
    }/*from   ww w. ja va  2s .  co m*/
    URL url = new URL(strUrl);

    MultivaluedMap<String, String> query = UriComponent.decodeQuery(url.getQuery(), true);

    assertThat(query.keySet()).contains(PRODUCT_VERSION, DISTRIBUTION_VERSION, DISTRIBUTION_PACKAGE);
    assertThat(query.get(PRODUCT_VERSION)).contains("LTS-2015");
    assertThat(query.get(DISTRIBUTION_VERSION)).contains("7.10");
    assertThat(query.get(DISTRIBUTION_PACKAGE)).contains("zip");

}

From source file:org.openmrs.module.uiframework.FragmentActionController.java

/**
 * Exposes any query parameter from url//from   ww  w. j  a  v  a2s .c o  m
 * 
 * @param urlString
 * @param model
 * @throws MalformedURLException
 */
private String redirectHelper(String urlString, Model model) throws MalformedURLException {
    URL url = new URL(urlString);
    if (url.getQuery() != null) {
        for (StringTokenizer st = new StringTokenizer(url.getQuery(), "&"); st.hasMoreTokens();) {
            String item = st.nextToken();
            int ind = item.indexOf('=');
            String name = item.substring(0, ind);
            String value = URLDecoder.decode(item.substring(ind + 1));
            model.addAttribute(name, value);
        }
    }
    return "redirect:" + removeContextPath(url.getPath());
}

From source file:com.ikanow.infinit.e.harvest.extraction.document.file.InfiniteFile.java

public URI getURI() throws MalformedURLException, URISyntaxException { // (note this doesn't work nicely with spaces)
    if (null != _smbFile) {
        URL url = _smbFile.getURL();
        return new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
        // (this odd construct is needed to handle spaces in paths)
    } else {//from   www .  j a v a2 s .c om
        return _localFile.toURI(); // (confirmed spaces in paths works here)
    }
}

From source file:org.eclipse.fx.core.internal.TplURLDynamicDataStreamHandler.java

@Override
public @Nullable InputStream createDataStream(URL url) {
    try {/*from ww  w.  ja v a 2s .c  o m*/
        URL realURL = new URL(url.getPath());
        String data;
        try (InputStream stream = realURL.openStream()) {
            data = StrSubstitutor.replace(IOUtils.readToString(stream, Charset.forName("UTF-8")), //$NON-NLS-1$
                    map(url.getQuery()), "_(", ")"); //$NON-NLS-1$//$NON-NLS-2$
        }
        return new ByteArrayInputStream(data.getBytes());
    } catch (IOException e) {
        LoggerCreator.createLogger(TplURLDynamicDataStreamHandler.class).error("Failed to load real data", e); //$NON-NLS-1$
    }
    return null;
}

From source file:org.talend.commons.ui.html.DynamicURLParser.java

/**
 * Retruns the Query part of the URL as an instance of a Properties class.
 * /*  w  ww  .  ja v  a  2s .  co m*/
 * @param url
 * @return
 */
public Properties getQueryParameters(URL url) {
    // parser all query parameters.
    Properties properties = new Properties();
    String query = url.getQuery();
    if (query == null) {
        // we do not have any parameters in this URL, return an empty
        // Properties instance.
        return properties;
    }
    // now extract the key/value pairs from the query.
    String[] params;
    if (query.indexOf("&amp;") != -1) {
        query = query.replaceAll("&amp;", "&");
    }
    params = StringUtils.split(query, "&"); //$NON-NLS-1$
    for (String param : params) {
        // for every parameter, ie: key=value pair, create a property
        // entry. we know we have the key as the first string in the array,
        // and the value as the second array.
        String[] keyValuePair = StringUtils.split(param, "="); //$NON-NLS-1$
        if (keyValuePair.length != 2) {
            Log.warning("Ignoring the following Intro URL parameter: " //$NON-NLS-1$
                    + param);
            continue;
        }

        String key = urlDecode(keyValuePair[0]);
        if (key == null) {
            Log.warning("Failed to URL decode key: " + keyValuePair[0]); //$NON-NLS-1$
            continue;
        }

        String value = urlDecode(keyValuePair[1]);
        if (value == null) {
            Log.warning("Failed to URL decode value: " + keyValuePair[1]); //$NON-NLS-1$
            continue;
        }

        properties.setProperty(key, value);
    }
    return properties;
}