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:com.eucalyptus.ws.MappingHttpRequest.java

public MappingHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri) {
    super(httpVersion);
    this.method = method;
    this.uri = uri;
    try {/*  ww  w  .  j  a va 2  s  .  c om*/
        URL url = new URL("http://eucalyptus" + uri);
        this.servicePath = url.getPath();
        this.parameters = new HashMap<String, String>();
        this.query = this.query == url.getQuery() ? this.query : url.getQuery();// new URLCodec().decode(url.toURI( ).getQuery( ) ).replaceAll( " ", "+" );
        this.formFields = new HashMap<String, String>();
        this.populateParameters();
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jaspersoft.jrx.query.JRXPathQueryExecuter.java

private Document getDocumentFromUrl(Map<String, ? extends JRValueParameter> parametersMap) throws Exception {
    // Get the url...
    String urlString = (String) getParameterValue(JRXPathQueryExecuterFactory.XML_URL);

    // add GET parameters to the urlString...
    Iterator<String> i = parametersMap.keySet().iterator();

    String div = "?";
    URL url = new URL(urlString);
    if (url.getQuery() != null)
        div = "&";

    while (i.hasNext()) {
        String keyName = "" + i.next();
        if (keyName.startsWith("XML_GET_PARAM_")) {
            String paramName = keyName.substring("XML_GET_PARAM_".length());
            String value = (String) getParameterValue(keyName);

            urlString += div + URLEncoder.encode(paramName, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
            div = "&";
        }/*  w  w w  .j a v a2s. c om*/
    }

    url = new URL(urlString);

    if (url.getProtocol().toLowerCase().equals("file")) {
        // do nothing
        return JRXmlUtils.parse(url.openStream());
    } else if (url.getProtocol().toLowerCase().equals("http")
            || url.getProtocol().toLowerCase().equals("https")) {
        String username = (String) getParameterValue(JRXPathQueryExecuterFactory.XML_USERNAME);
        String password = (String) getParameterValue(JRXPathQueryExecuterFactory.XML_PASSWORD);

        if (url.getProtocol().toLowerCase().equals("https")) {
            JRPropertiesUtil dPROP = PropertiesHelper.DPROP;
            String socketFactory = dPROP
                    .getProperty("net.sf.jasperreports.query.executer.factory.xPath.DefaultSSLSocketFactory");
            if (socketFactory == null) {
                socketFactory = dPROP.getProperty(
                        "net.sf.jasperreports.query.executer.factory.XPath.DefaultSSLSocketFactory");
            }

            if (socketFactory != null) {
                // setSSLSocketFactory
                HttpsURLConnection.setDefaultSSLSocketFactory(
                        (SSLSocketFactory) Class.forName(socketFactory).newInstance());
            } else {
                log.debug("No SSLSocketFactory defined, using default");
            }

            String hostnameVerifyer = dPROP
                    .getProperty("net.sf.jasperreports.query.executer.factory.xPath.DefaultHostnameVerifier");
            if (hostnameVerifyer == null) {
                hostnameVerifyer = dPROP.getProperty(
                        "net.sf.jasperreports.query.executer.factory.XPath.DefaultHostnameVerifier");
            }

            if (hostnameVerifyer != null) {
                // setSSLSocketFactory
                HttpsURLConnection.setDefaultHostnameVerifier(
                        (HostnameVerifier) Class.forName(hostnameVerifyer).newInstance());
            } else {
                log.debug("No HostnameVerifier defined, using default");
            }
        }

        URLConnection conn = url.openConnection();

        if (username != null && username.length() > 0 && password != null) {
            ByteArrayInputStream bytesIn = new ByteArrayInputStream((username + ":" + password).getBytes());
            ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
            Base64Encoder enc = new Base64Encoder(bytesIn, dataOut);
            enc.process();
            String encoding = dataOut.toString();
            conn.setRequestProperty("Authorization", "Basic " + encoding);
        }

        // add POST parameters to the urlString...
        i = parametersMap.keySet().iterator();

        String data = "";
        div = "";
        while (i.hasNext()) {
            String keyName = "" + i.next();
            if (keyName.startsWith("XML_POST_PARAM_")) {
                String paramName = keyName.substring("XML_POST_PARAM_".length());
                String value = (String) getParameterValue(keyName);
                data += div + URLEncoder.encode(paramName, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
                div = "&";
            }
        }

        conn.setDoOutput(true);

        if (data.length() > 0) {
            conn.setDoInput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();
        }

        try {
            return XMLUtils.parseNoValidation(conn.getInputStream());
        } catch (SAXException e) {
            throw new JRException("Failed to parse the xml document", e);
        } catch (IOException e) {
            throw new JRException("Failed to parse the xml document", e);
        } catch (ParserConfigurationException e) {
            throw new JRException("Failed to create a document builder factory", e);
        }

        // return JRXmlUtils.parse(conn.getInputStream());
    } else {
        throw new JRException("URL protocol not supported");
    }
}

From source file:org.wso2.identity.integration.test.auth.RiskBasedLoginTestCase.java

@Test(groups = "wso2.is", description = "Check conditional authentication flow.")
public void testAuthenticationForNoRisk() throws Exception {

    response = loginWithOIDC(PRIMARY_IS_APPLICATION_NAME, consumerKey, client);

    EntityUtils.consume(response.getEntity());

    Header locationHeader = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION);
    Assert.assertNotNull(locationHeader, "Login response header is null");
    locationHeader = handleConsent(locationHeader);

    URL clientUrl = new URL(locationHeader.getValue());
    Assert.assertTrue(clientUrl.getQuery().contains("code="),
            "Authentication flow was un-successful with " + "identifier first login");

}

From source file:org.talend.datatools.xml.utils.SchemaPopulationUtil.java

public static XSModel getXSModel(String fileName) throws URISyntaxException, MalformedURLException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//w  w  w  .j  a  v a2  s.  co m
    URI uri = null;
    File f = new File(fileName);
    if (f.exists()) {
        uri = f.toURI();
    } else {
        URL url = new URL(fileName);
        uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
    }

    // Then try to parse the input string as a url in web.
    if (uri == null) {
        uri = new URI(fileName);
    }

    // fixed a bug when parse one file contians Franch ,maybe need modification
    XMLSchemaLoader xsLoader = new XMLSchemaLoader();
    XSModel xsModel = xsLoader.loadURI(uri.toString());
    if (xsModel == null) {
        try {
            Grammar loadGrammar = xsLoader.loadGrammar(
                    new XMLInputSource(null, uri.toString(), null, new FileInputStream(f), "ISO-8859-1"));
            xsModel = ((XSGrammar) loadGrammar).toXSModel();
        } catch (XNIException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return xsModel;
}

From source file:org.deegree.protocol.ows.http.OwsHttpClientImpl.java

protected URL normalizeGetUrl(URL url) throws MalformedURLException {
    // TODO: this method does not work. url.getQuery is the query part not the base url
    String s = url.toString();//from   ww w. ja v a  2  s.c o  m
    if (url.getQuery() != null) {
        if (!s.endsWith("&") && (!s.endsWith("?") && s.length() == 1)) {
            s += "&";
        }
    } else {
        if (!s.endsWith("?")) {
            s += "?";
        }
    }
    return new URL(s);
}

From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java

/**
 * <p>Encodes illegal characters in the specified URL's path, query string and anchor according to the URL
 * encoding rules observed in real browsers.</p>
 *
 * <p>For example, this method changes <tt>"http://first/?a=b c"</tt> to <tt>"http://first/?a=b%20c"</tt>.</p>
 *
 * @param url the URL to encode//from w  w w  . j a v  a  2s.  c o  m
 * @param minimalQueryEncoding whether or not to perform minimal query encoding, like IE does
 * @param charset the charset
 * @return the encoded URL
 */
public static URL encodeUrl(final URL url, final boolean minimalQueryEncoding, final String charset) {
    if (!isNormalUrlProtocol(URL_CREATOR.getProtocol(url))) {
        return url; // javascript:, about:, data: and anything not supported like foo:
    }

    try {
        String path = url.getPath();
        if (path != null) {
            path = encode(path, PATH_ALLOWED_CHARS, "UTF-8");
        }
        String query = url.getQuery();
        if (query != null) {
            if (minimalQueryEncoding) {
                query = org.apache.commons.lang3.StringUtils.replace(query, " ", "%20");
            } else {
                query = encode(query, QUERY_ALLOWED_CHARS, charset);
            }
        }
        String anchor = url.getRef();
        if (anchor != null) {
            anchor = encode(anchor, ANCHOR_ALLOWED_CHARS, "UTF-8");
        }
        return createNewUrl(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), path, anchor,
                query);
    } catch (final MalformedURLException e) {
        // Impossible... I think.
        throw new RuntimeException(e);
    }
}

From source file:esg.node.filters.UrlResolvingDAO.java

/**
   Resolves the given URL object described by the DRS Taxonomy to "local" resource location
   @param inputUrl URL object to (virtual) resource
   @return The string value referencing the resource the DRS syntax resolves to.
 *///from  ww  w  .  j  a va 2 s .  c  o  m
public String resolveDRSUrl(URL inputUrl) {
    String urlQuery = null;
    String result = null;
    if (null == (urlQuery = inputUrl.getQuery())) {
        result = resolveDRSPath(inputUrl.getPath());
    } else {
        result = resolveDRSQuery(urlQuery);
    }
    return result;
}

From source file:com.k42b3.aletheia.protocol.http.Request.java

private void parseUrl(URL url) {
    this.host = url.getHost();
    this.path = url.getPath().isEmpty() ? "/" : url.getPath();

    if (url.getQuery() != null) {
        this.path += "?" + url.getQuery();
    }//  ww w . java2s. com

    if (url.getRef() != null) {
        this.path += "#" + url.getRef();
    }
}

From source file:org.hypertopic.RESTDatabase.java

public URL getURL(String path) throws Exception {
    URL url = new URL(this.baseUrl + path);
    return new URL(new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
            url.getQuery(), url.getRef()).toASCIIString());
}