List of usage examples for org.apache.http.message BasicHeaderValueParser INSTANCE
BasicHeaderValueParser INSTANCE
To view the source code for org.apache.http.message BasicHeaderValueParser INSTANCE.
Click Source Link
From source file:io.cloudslang.content.httpclient.consume.HttpResponseConsumer.java
public void consume(Map<String, String> result) throws IOException { if (httpResponse.getEntity() != null) { if (responseCharacterSet == null || responseCharacterSet.isEmpty()) { Header contentType = httpResponse.getEntity().getContentType(); if (contentType != null) { String value = contentType.getValue(); NameValuePair[] nameValuePairs = BasicHeaderValueParser.parseParameters(value, BasicHeaderValueParser.INSTANCE); for (NameValuePair nameValuePair : nameValuePairs) { if (nameValuePair.getName().equalsIgnoreCase("charset")) { responseCharacterSet = nameValuePair.getValue(); break; }//from w ww . ja va2 s . c o m } } if (responseCharacterSet == null || responseCharacterSet.isEmpty()) { responseCharacterSet = Consts.ISO_8859_1.name(); } } consumeResponseContent(result); } }
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 ww w .j av a2s. c o m 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); } }