List of usage examples for org.apache.http.client.utils URLEncodedUtils parse
public static List<NameValuePair> parse(final String s, final Charset charset)
From source file:org.opennms.netmgt.provision.detector.web.client.WebClient.java
@Override public void connect(InetAddress address, int port, int timeout) throws IOException, Exception { final URIBuilder ub = new URIBuilder(); ub.setScheme(m_schema);//from w ww. ja v a 2 s . c om ub.setHost(InetAddressUtils.str(address)); ub.setPort(port); ub.setPath(m_path); if (m_queryString != null && m_queryString.trim().length() > 0) { final List<NameValuePair> params = URLEncodedUtils.parse(m_queryString, Charset.forName("UTF-8")); if (!params.isEmpty()) { ub.setParameters(params); } } m_httpMethod = new HttpGet(ub.build()); m_httpMethod.setProtocolVersion(m_version); m_httpClientWrapper = HttpClientWrapper.create(); if (m_overrideSSL) { try { m_httpClientWrapper.trustSelfSigned("https"); } catch (final Exception e) { LOG.warn("Failed to create relaxed SSL client.", e); } } if (m_userAgent != null && !m_userAgent.trim().isEmpty()) { m_httpClientWrapper.setUserAgent(m_userAgent); } if (timeout > 0) { m_httpClientWrapper.setConnectionTimeout(timeout); m_httpClientWrapper.setSocketTimeout(timeout); } if (m_virtualHost != null && !m_virtualHost.trim().isEmpty()) { m_httpClientWrapper.setVirtualHost(m_virtualHost); } if (m_userName != null && !m_userName.trim().isEmpty()) { m_httpClientWrapper.addBasicCredentials(m_userName, m_password); } if (m_authPreemptive) { m_httpClientWrapper.usePreemptiveAuth(); } }
From source file:org.opennms.protocols.http.HttpUrlConnection.java
@Override public InputStream getInputStream() throws IOException { try {/*w w w. j a v a 2s . c o m*/ if (m_clientWrapper == null) { connect(); } // Build URL int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort(); URIBuilder ub = new URIBuilder(); ub.setPort(port); ub.setScheme(m_url.getProtocol()); ub.setHost(m_url.getHost()); ub.setPath(m_url.getPath()); if (m_url.getQuery() != null && !m_url.getQuery().trim().isEmpty()) { final List<NameValuePair> params = URLEncodedUtils.parse(m_url.getQuery(), Charset.forName("UTF-8")); if (!params.isEmpty()) { ub.addParameters(params); } } // Build Request HttpRequestBase request = null; if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) { final Content cnt = m_request.getContent(); HttpPost post = new HttpPost(ub.build()); ContentType contentType = ContentType.create(cnt.getType()); LOG.info("Processing POST request for {}", contentType); if (contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) { FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData()); post.setEntity(fields.getEntity()); } else { StringEntity entity = new StringEntity(cnt.getData(), contentType); post.setEntity(entity); } request = post; } else { request = new HttpGet(ub.build()); } if (m_request != null) { // Add Custom Headers for (final Header header : m_request.getHeaders()) { request.addHeader(header.getName(), header.getValue()); } } // Get Response CloseableHttpResponse response = m_clientWrapper.execute(request); return response.getEntity().getContent(); } catch (Exception e) { throw new IOException( "Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e); } }
From source file:org.pac4j.saml.client.RedirectSaml2ClientIT.java
private String getInflatedAuthnRequest(String location) throws Exception { List<NameValuePair> pairs = URLEncodedUtils.parse(URI.create(location), "UTF-8"); Inflater inflater = new Inflater(true); byte[] decodedRequest = Base64.decodeBase64(pairs.get(0).getValue()); ByteArrayInputStream is = new ByteArrayInputStream(decodedRequest); InflaterInputStream inputStream = new InflaterInputStream(is, inflater); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); return reader.readLine(); }
From source file:org.vroyer.hive.solr.SolrTableCursor.java
private void fetchNextDocumentChunk() throws IOException { SolrQuery query = new SolrQuery(); query.setStart(start);/*from ww w. j a v a 2 s. c o m*/ if (table.facetType == null) { query.setRows(Math.min(count, this.solrSplitSize)); for (String s : table.fields) { // Don't push solr_query, this is an internal column when a cassandra table is indexed by SOLR. if (!s.equalsIgnoreCase("solr_query")) { query.addField(s); } } } else { query.setRows(0); } List<NameValuePair> params = URLEncodedUtils.parse(table.qs, Charset.forName("UTF-8")); for (NameValuePair nvp : params) { query.set(nvp.getName(), nvp.getValue()); } if (table.fq.length() > 0) { query.set("fq", table.fq.toString()); } if (table.q.length() > 0) { query.set("q", table.q.toString()); } if (query.get("q") == null) { query.set("q", "*:*"); } if (log.isInfoEnabled()) { StringBuffer sb = new StringBuffer(""); Map<String, String[]> map = query.toMultiMap(query.toNamedList()); for (String s : map.keySet()) { sb.append(s).append('=').append(Arrays.toString(map.get(s))).append(' '); } log.info("SOLR request: " + table.url + " " + sb.toString()); } QueryResponse response; try { response = server.query(query); } catch (SolrServerException e) { throw new IOException(e); } if (table.facetType != null) { if (table.facetType.equalsIgnoreCase("ranges")) { List<RangeFacet.Count> counts = response.getFacetRanges().get(0).getCounts(); for (RangeFacet.Count rfc : counts) { facets.add(new FacetEntry(rfc.getValue(), rfc.getCount())); } } else if (table.facetType.equalsIgnoreCase("fields")) { List<FacetField.Count> counts = response.getFacetFields().get(0).getValues(); for (FacetField.Count rfc : counts) { facets.add(new FacetEntry(rfc.getName(), rfc.getCount())); } } else if (table.facetType.equalsIgnoreCase("queries")) { Map<String, Integer> queries = response.getFacetQuery(); for (String k : queries.keySet()) { facets.add(new FacetEntry(k, queries.get(k))); } } } else { buffer = response.getResults(); numFound = buffer.getNumFound(); log.info("SOLR response numFound=" + buffer.getNumFound()); start += buffer.size(); count -= buffer.size(); } pos = 0; }
From source file:org.wso2.identity.scenarios.commons.util.DataExtractUtil.java
/** * Extract query parameters in a URL/* ww w. ja v a 2 s .c om*/ * * @param Url url for extracting data. * @return HashMap of extracted query parameters * @throws Exception */ public static Map<String, String> getQueryParams(String Url) throws Exception { Map<String, String> queryParams = new HashMap<>(); List<NameValuePair> params = URLEncodedUtils.parse(new URI(Url), StandardCharsets.UTF_8.name()); for (NameValuePair param : params) { queryParams.put(param.getName(), param.getValue()); } return queryParams; }
From source file:org.xwiki.skinx.internal.AbstractSxExportURLFactoryActionHandler.java
private URL processSx(List<String> spaceNames, String name, String queryString, XWikiContext context, FilesystemExportContext exportContext) throws Exception { SxSource sxSource = null;/* w ww. j a v a 2 s. c om*/ // Check if we have the JAR_RESOURCE_REQUEST_PARAMETER parameter in the query string List<NameValuePair> params = URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8); for (NameValuePair param : params) { if (param.getName().equals(JAR_RESOURCE_REQUEST_PARAMETER)) { sxSource = new SxResourceSource(param.getValue()); break; } } if (sxSource == null) { sxSource = new SxDocumentSource(context, getExtensionType()); } String content = getContent(sxSource, exportContext); // Write the content to file // We need a unique name for that SSX content String targetPath = String.format("%s/%s/%s", getSxPrefix(), StringUtils.join(spaceNames, '/'), name); File targetDirectory = new File(exportContext.getExportDir(), targetPath); if (!targetDirectory.exists()) { targetDirectory.mkdirs(); } File targetLocation = File.createTempFile(getSxPrefix(), "." + getFileSuffix(), targetDirectory); FileUtils.writeStringToFile(targetLocation, content); // Rewrite the URL StringBuilder path = new StringBuilder("file://"); // Adjust based on current document's location. We need to account for the fact that the current document // is stored in subdirectories inside the top level "pages" directory. Since the SX files are also in top // subdirectories, we need to compute the path to them. path.append(StringUtils.repeat("../", exportContext.getDocParentLevel())); path.append(getSxPrefix()); path.append(URL_PATH_SEPARATOR); for (String spaceName : spaceNames) { path.append(encodeURLPart(spaceName)); path.append(URL_PATH_SEPARATOR); } path.append(encodeURLPart(name)); path.append(URL_PATH_SEPARATOR); path.append(encodeURLPart(targetLocation.getName())); return new URL(path.toString()); }