List of usage examples for org.apache.http.util CharArrayBuffer append
public void append(Object obj)
From source file:Main.java
/** * Returns a list of {@link org.apache.http.NameValuePair NameValuePairs} as parsed. * * @param s text to parse./*from ww w . j a va2 s . c o m*/ * @since 4.2 */ public static List<NameValuePair> parse(final String s) { if (s == null) { return Collections.emptyList(); } BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT; CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); ParserCursor cursor = new ParserCursor(0, buffer.length()); List<NameValuePair> list = new ArrayList<NameValuePair>(); while (!cursor.atEnd()) { NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM); if (nvp.getName().length() > 0) { list.add(new BasicNameValuePair(nvp.getName(), nvp.getValue())); } } return list; }
From source file:tv.arte.resteventapi.core.clients.RestEventApiRestClient.java
/** * Executes the REST request described by the {@link RestEvent} * //from www. j a v a 2 s . c o m * @param restEvent The {@link RestEvent} to process * @return A result of the execution * @throws RestEventApiRuntimeException In case of non managed errors */ public static RestClientExecutionResult execute(final RestEvent restEvent) throws RestEventApiRuntimeException { RestClientExecutionResult result = new RestClientExecutionResult(); String url = restEvent.getUrl(); CloseableHttpClient client = null; HttpUriRequest request = null; Integer responseCode = null; try { //Request custom configs RequestConfig.Builder requestBuilder = RequestConfig.custom(); requestBuilder = requestBuilder.setConnectTimeout(restEvent.getTimeout()); requestBuilder = requestBuilder.setConnectionRequestTimeout(restEvent.getTimeout()); client = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build()).build(); //Determine the method to execute switch (restEvent.getMethod()) { case GET: request = new HttpGet(url); break; case POST: request = new HttpPost(url); break; case PUT: request = new HttpPut(url); break; case DELETE: request = new HttpDelete(url); break; case PATCH: request = new HttpPatch(url); break; case HEAD: request = new HttpHead(url); break; default: throw new RestEventApiRuntimeException("RestEventAPI unsupported HTTP method"); } //Set the body for eligible methods if (restEvent.getBody() != null && request instanceof HttpEntityEnclosingRequestBase) { ((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(restEvent.getBody())); } //Set headers if (CollectionUtils.isNotEmpty(restEvent.getHeaders())) { for (String strHeader : restEvent.getHeaders()) { CharArrayBuffer headerBuffer = new CharArrayBuffer(strHeader.length() + 1); headerBuffer.append(strHeader); request.addHeader(new BufferedHeader(headerBuffer)); } } HttpResponse response = client.execute(request); responseCode = response.getStatusLine().getStatusCode(); result.setState(RestClientCallState.OK); } catch (ConnectTimeoutException e) { result.setState(RestClientCallState.TIMEOUT); } catch (Exception e) { throw new RestEventApiRuntimeException("Un error occured while processing rest event", e); } finally { result.setResponseCode(responseCode); try { client.close(); } catch (Exception e2) { logger.warn("Unable to close HTTP client", e2); } } return result; }
From source file:cn.aage.robot.http.entity.ContentType.java
/** * Parses textual representation of <code>Content-Type</code> value. * * @param s text/*from w w w .j a v a2 s .c o m*/ * @return content type * @throws ParseException if the given text does not represent a valid * <code>Content-Type</code> value. * @throws UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ public static ContentType parse(final String s) throws ParseException, UnsupportedCharsetException { Args.notNull(s, "Content type"); final CharArrayBuffer buf = new CharArrayBuffer(s.length()); buf.append(s); final ParserCursor cursor = new ParserCursor(0, s.length()); final HeaderElement[] elements = BasicHeaderValueParser.DEFAULT.parseElements(buf, cursor); if (elements.length > 0) { return create(elements[0]); } else { throw new ParseException("Invalid content type: " + s); } }
From source file:com.mcxiaoke.next.http.entity.ContentType.java
/** * Parses textual representation of <code>Content-Type</code> value. * * @param s text//from www. j a v a 2 s . co m * @return content type * @throws org.apache.http.ParseException if the given text does not represent a valid * <code>Content-Type</code> value. * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ public static ContentType parse(final String s) throws ParseException, UnsupportedCharsetException { AssertUtils.notNull(s, "Content type"); final CharArrayBuffer buf = new CharArrayBuffer(s.length()); buf.append(s); final ParserCursor cursor = new ParserCursor(0, s.length()); final HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(buf, cursor); if (elements.length > 0) { return create(elements[0]); } else { throw new ParseException("Invalid content type: " + s); } }
From source file:com.sina.cloudstorage.util.URLEncodedUtils.java
/** * Returns a list of {@link NameValuePair NameValuePairs} as parsed from the given string * using the given character encoding./*from www.j a v a 2 s . c o m*/ * * @param s * text to parse. * @param charset * Encoding to use when decoding the parameters. * * @since 4.2 */ public static List<NameValuePair> parse(final String s, final Charset charset) { if (s == null) { return Collections.emptyList(); } BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT; CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); ParserCursor cursor = new ParserCursor(0, buffer.length()); List<NameValuePair> list = new ArrayList<NameValuePair>(); while (!cursor.atEnd()) { NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM); if (nvp.getName().length() > 0) { list.add(new BasicNameValuePair(decode(nvp.getName(), charset), decode(nvp.getValue(), charset))); } } return list; }
From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java
/** * Parses elements with the given parser. * * @param value the header value to parse * @param parser the parser to use, or <code>null</code> for default * @return array holding the header elements, never <code>null</code> */// www . j a v a 2 s . c o m public static HeaderElement[] parseElements(final String value, final HeaderValueParser parser) throws ParseException { AssertUtils.notNull(value, "Value"); final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); final ParserCursor cursor = new ParserCursor(0, value.length()); return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseElements(buffer, cursor); }
From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java
/** * Parses an element with the given parser. * * @param value the header element to parse * @param parser the parser to use, or <code>null</code> for default * @return the parsed header element/*from w w w. j ava2 s . c o m*/ */ public static HeaderElement parseHeaderElement(final String value, final HeaderValueParser parser) throws ParseException { AssertUtils.notNull(value, "Value"); final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); final ParserCursor cursor = new ParserCursor(0, value.length()); return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseHeaderElement(buffer, cursor); }
From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java
/** * Parses parameters with the given parser. * * @param value the parameter list to parse * @param parser the parser to use, or <code>null</code> for default * @return array holding the parameters, never <code>null</code> *//* w w w. j a v a 2s .c om*/ public static NameValuePair[] parseParameters(final String value, final HeaderValueParser parser) throws ParseException { AssertUtils.notNull(value, "Value"); final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); final ParserCursor cursor = new ParserCursor(0, value.length()); return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseParameters(buffer, cursor); }
From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java
/** * Parses a name-value-pair with the given parser. * * @param value the NVP to parse//from w ww . j a v a2s .co m * @param parser the parser to use, or <code>null</code> for default * @return the parsed name-value pair */ public static NameValuePair parseNameValuePair(final String value, final HeaderValueParser parser) throws ParseException { AssertUtils.notNull(value, "Value"); final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); final ParserCursor cursor = new ParserCursor(0, value.length()); return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseNameValuePair(buffer, cursor); }
From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java
/** * This method takes in a ContentType and returns an equivalent ContentType, only overriding the * charset. This is needed because ContentType.withCharset(charset) does not correctly handle * headers with multiple parameters. Parsing a ContentType from a String works, calling * toString() to get the correct header value works, but there's no way from ContentType itself * to update a specific parameter in-place. *//*from w ww. ja v a 2s . c om*/ public static ContentType setCharset(ContentType contentType, Charset charset) throws ParseException, UnsupportedCharsetException { // Get the correct header value String contentTypeString = contentType.toString(); // Parse the header manually the same way ContentType does it CharArrayBuffer buffer = new CharArrayBuffer(contentTypeString.length()); buffer.append(contentTypeString); ParserCursor cursor = new ParserCursor(0, contentTypeString.length()); HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(buffer, cursor); if (ArrayUtils.isNotEmpty(elements)) { String mimeType = elements[0].getName(); NameValuePair[] params = elements[0].getParameters(); List<NameValuePair> paramsList = new ArrayList<NameValuePair>(); boolean charsetFound = false; // Iterate through each parameter and override the charset if present if (ArrayUtils.isNotEmpty(params)) { for (NameValuePair nvp : params) { if (nvp.getName().equalsIgnoreCase("charset")) { charsetFound = true; nvp = new BasicNameValuePair(nvp.getName(), charset.name()); } paramsList.add(nvp); } } // Add the charset at the end if it wasn't found before if (!charsetFound) { paramsList.add(new BasicNameValuePair("charset", charset.name())); } // Format the header the same way ContentType does it CharArrayBuffer newBuffer = new CharArrayBuffer(64); newBuffer.append(mimeType); newBuffer.append("; "); BasicHeaderValueFormatter.INSTANCE.formatParameters(newBuffer, paramsList.toArray(new NameValuePair[paramsList.size()]), false); // Once we have the correct string, let ContentType do the rest return ContentType.parse(newBuffer.toString()); } else { throw new ParseException("Invalid content type: " + contentTypeString); } }