List of usage examples for org.apache.commons.httpclient.util EncodingUtil formUrlEncode
public static String formUrlEncode(NameValuePair[] pairs, String charset)
From source file:com.thoughtworks.twist.mingle.core.MingleUtils.java
public static String getQueryUrlAfterEncodingURL(String fullUrl) throws UnsupportedEncodingException { String queryUrl = fullUrl.substring(fullUrl.indexOf('?') + 1); String[] split = queryUrl.split("&"); ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); for (String nameValuePair : split) { if (nameValuePair.contains("=")) { int indexOfEquals = nameValuePair.indexOf('='); String name = nameValuePair.substring(0, indexOfEquals); String value = nameValuePair.substring(indexOfEquals + 1); name = URLDecoder.decode(name, "utf-8"); value = URLDecoder.decode(value, "utf-8"); pairs.add(new NameValuePair(name, value)); }/*from w w w .j a v a2s . c o m*/ } String formUrlEncode = EncodingUtil.formUrlEncode(pairs.toArray(new NameValuePair[0]), "utf-8"); return formUrlEncode; }
From source file:com.ltasks.GZipPostMethod.java
@Override protected RequestEntity generateRequestEntity() { if (mIsGzip) { try {// w w w . j a v a 2 s.c o m String contentStr = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet()); ByteArrayOutputStream originalContent = new ByteArrayOutputStream(); originalContent.write(EncodingUtil.getAsciiBytes(contentStr)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos); GZIPOutputStream gzipOut = new GZIPOutputStream(baos); originalContent.writeTo(gzipOut); gzipOut.finish(); byte[] content = baos.toByteArray(); ByteArrayRequestEntity entity = new ByteArrayRequestEntity(content, FORM_URL_ENCODED_CONTENT_TYPE); return entity; } catch (Exception e) { e.printStackTrace(); } return null; } else { return super.generateRequestEntity(); } }
From source file:org.apache.jackrabbit.spi2davex.PostMethod.java
@Override protected RequestEntity generateRequestEntity() { if (!this.params.isEmpty()) { // Use a ByteArrayRequestEntity instead of a StringRequestEntity. // This is to avoid potential encoding issues. Form url encoded strings // are ASCII by definition but the content type may not be. Treating the content // as bytes allows us to keep the current charset without worrying about how // this charset will effect the encoding of the form url encoded string. NameValuePair[] mvps = params.toArray(new NameValuePair[params.size()]); String content = EncodingUtil.formUrlEncode(mvps, getRequestCharSet()); ByteArrayRequestEntity entity = new ByteArrayRequestEntity(EncodingUtil.getAsciiBytes(content), FORM_URL_ENCODED_CONTENT_TYPE); return entity; } else {//from w w w . ja va 2s . co m return super.generateRequestEntity(); } }
From source file:org.apache.ode.axis2.util.URLEncodedTransformer.java
/** * @param values - a map<String, Element>, the key is a part name (without curly braces), the value the replacement value for the part name. If the value is not a simple type, it will be skipped. * @return the encoded params/*from w ww . j av a 2s .co m*/ */ public String transform(Map<String, Element> values) { if (values.isEmpty()) return null; List<NameValuePair> l = new ArrayList<NameValuePair>(values.size()); for (Map.Entry<String, Element> e : values.entrySet()) { String partName = e.getKey(); Element value = e.getValue(); String textValue; if (DOMUtils.isEmptyElement(value)) { textValue = ""; } else { /* The expected part value could be a simple type or an element of a simple type. So if a element is there, take its text content else take the text content of the part element itself */ Element childElement = DOMUtils.getFirstChildElement(value); if (childElement != null) { textValue = DOMUtils.getTextContent(childElement); } else { textValue = DOMUtils.getTextContent(value); } } // if it is not a simple type, skip it if (textValue != null) { l.add(new NameValuePair(e.getKey(), textValue)); } } return EncodingUtil.formUrlEncode(l.toArray(new NameValuePair[0]), "UTF-8"); }
From source file:org.dspace.content.authority.LCNameAuthority.java
/** * Guts of the implementation, returns a complete Choices result, or * null for a failure.//from w w w . j a v a 2s . c om */ private Choices queryPerson(String text, int start, int limit) { // punt if there is no query text if (text == null || text.trim().length() == 0) { return new Choices(true); } // 1. build CQL query DCPersonName pn = new DCPersonName(text); StringBuilder query = new StringBuilder(); query.append("local.FirstName = \"").append(pn.getFirstNames()).append("\" and local.FamilyName = \"") .append(pn.getLastName()).append("\""); // XXX arbitrary default limit - should be configurable? if (limit == 0) { limit = 50; } NameValuePair args[] = new NameValuePair[6]; args[0] = new NameValuePair("operation", "searchRetrieve"); args[1] = new NameValuePair("version", "1.1"); args[2] = new NameValuePair("recordSchema", "info:srw/schema/1/marcxml-v1.1"); args[3] = new NameValuePair("query", query.toString()); args[4] = new NameValuePair("maximumRecords", String.valueOf(limit)); args[5] = new NameValuePair("startRecord", String.valueOf(start + 1)); HttpClient hc = new HttpClient(); String srUrl = url + "?" + EncodingUtil.formUrlEncode(args, "UTF8"); GetMethod get = new GetMethod(srUrl); log.debug("Trying SRU query, URL=" + srUrl); // 2. web request try { int status = hc.executeMethod(get); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRUHandler handler = new SRUHandler(); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(get.getResponseBodyAsStream())); // this probably just means more results available.. if (handler.hits != handler.result.size()) { log.warn("Discrepency in results, result.length=" + handler.result.size() + ", yet expected results=" + handler.hits); } boolean more = handler.hits > (start + handler.result.size()); // XXX add non-auth option; perhaps the UI should do this? // XXX it's really a policy matter if they allow unauth result. // XXX good, stop it. // handler.result.add(new Choice("", text, "Non-Authority: \""+text+"\"")); int confidence; if (handler.hits == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.hits == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result.toArray(new Choice[handler.result.size()]), start, handler.hits, confidence, more); } } catch (HttpException e) { log.error("SRU query failed: ", e); return new Choices(true); } catch (IOException e) { log.error("SRU query failed: ", e); return new Choices(true); } catch (ParserConfigurationException e) { log.warn("Failed parsing SRU result: ", e); return new Choices(true); } catch (SAXException e) { log.warn("Failed parsing SRU result: ", e); return new Choices(true); } finally { get.releaseConnection(); } return new Choices(true); }
From source file:org.dspace.content.authority.SHERPARoMEOProtocol.java
protected Choices query(String result, String label, String authority, NameValuePair[] args, int start, int limit) { HttpClient hc = new HttpClient(); String srUrl = url + "?" + EncodingUtil.formUrlEncode(args, "UTF8"); GetMethod get = new GetMethod(srUrl); log.debug("Trying SHERPA/RoMEO Query, URL=" + srUrl); try {// w w w.j a v a 2s. co m int status = hc.executeMethod(get); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRHandler handler = new SRHandler(result, label, authority); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(get.getResponseBodyAsStream())); int confidence; if (handler.total == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.total == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result, start, handler.total, confidence, false); } } catch (HttpException e) { log.error("SHERPA/RoMEO query failed: ", e); return null; } catch (IOException e) { log.error("SHERPA/RoMEO query failed: ", e); return null; } catch (ParserConfigurationException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } catch (SAXException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } finally { get.releaseConnection(); } return null; }
From source file:org.eclipse.mylyn.tasks.tests.web.WebRepositoryConnectorTest.java
public void testEncodingParameters() throws Exception { TaskRepository repository = new TaskRepository(WebRepositoryConnector.REPOSITORY_TYPE, "http://foo.net"); repository.setAuthenticationCredentials("USER", "PASSWORD"); repository.setProperty(WebRepositoryConnector.PROPERTY_LOGIN_REQUEST_METHOD, WebRepositoryConnector.REQUEST_POST); repository.setProperty(WebRepositoryConnector.PROPERTY_LOGIN_REQUEST_URL, // "${serverUrl}/Login.php?xajax=xCheckUserLogin&xajaxargs[]=<xjxquery><q>${xjxquery}</q></xjxquery>"); repository.setProperty("param_xjxquery", "TestUserName=${userId}&TestUserPWD=${password}&HttpRefer="); Map<String, String> params = new HashMap<String, String>(); PostMethod method = (PostMethod) WebRepositoryConnector.getLoginMethod(params, repository); String form = EncodingUtil.formUrlEncode(method.getParameters(), method.getRequestCharSet()); assertEquals("xajax=xCheckUserLogin&" + // "xajaxargs%5B%5D=%3Cxjxquery%3E%3Cq%3E" + // "TestUserName%3DUSER%26" + // "TestUserPWD%3DPASSWORD%26" + // "HttpRefer%3D" + // "%3C%2Fq%3E%3C%2Fxjxquery%3E", form); }
From source file:org.parosproxy.paros.network.GenericMethod.java
/** * Generates a request entity from the post parameters, if present. Calls * {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set. * /* w w w. ja va 2 s . co m*/ * @since 3.0 */ @Override protected RequestEntity generateRequestEntity() { if (!this.params.isEmpty()) { // Use a ByteArrayRequestEntity instead of a StringRequestEntity. // This is to avoid potential encoding issues. Form url encoded strings // are ASCII by definition but the content type may not be. Treating the content // as bytes allows us to keep the current charset without worrying about how // this charset will effect the encoding of the form url encoded string. String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet()); ByteArrayRequestEntity entity = new ByteArrayRequestEntity(EncodingUtil.getAsciiBytes(content), FORM_URL_ENCODED_CONTENT_TYPE); return entity; } return super.generateRequestEntity(); }