protected boolean hasEncoding( final HttpResponse response, final String encoding ) { HttpEntity entity = response.getEntity(); if ( entity == null ) return false; Header ceHeader = entity.getContentEncoding(); if ( ceHeader == null ) return false; HeaderElement[] codecs = ceHeader.getElements(); for ( int i = 0; i < codecs.length; i++ ) if ( encoding.equalsIgnoreCase( codecs[i].getName() ) ) return true; return false; } }
@Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) { return Long.parseLong(value) * 1000; } } return HTTPCLIENT_KEEPALIVEDURATION; } })
@Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) { return Long.parseLong(value) * 1000; } } return HTTPCLIENT_KEEPALIVEDURATION; } })
public HeaderElement[] parseElements(final CharArrayBuffer buffer, final ParserCursor cursor) { if (buffer == null) { throw new IllegalArgumentException("Char array buffer may not be null"); } if (cursor == null) { throw new IllegalArgumentException("Parser cursor may not be null"); } List elements = new ArrayList(); while (!cursor.atEnd()) { HeaderElement element = parseHeaderElement(buffer, cursor); if (!(element.getName().length() == 0 && element.getValue() == null)) { elements.add(element); } } return (HeaderElement[]) elements.toArray(new HeaderElement[elements.size()]); }
public Set<String> getAllowedMethods(final HttpResponse response) { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } HeaderIterator it = response.headerIterator("Allow"); Set<String> methods = new HashSet<String>(); while (it.hasNext()) { Header header = it.nextHeader(); HeaderElement[] elements = header.getElements(); for (HeaderElement element : elements) { methods.add(element.getName()); } } return methods; }
@Override public void process(final HttpResponse response, final HttpContext context) { // Inflate any responses compressed with gzip final HttpEntity entity = response.getEntity(); if (entity != null) { final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(IgnitedHttp.ENCODING_GZIP)) { response.setEntity(new GzipInflatingEntity(response.getEntity())); break; } } } } }
@Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } return keepAliveTimeOut * 1000; }
@Override protected void parseChallenge( final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException { HeaderValueParser parser = BasicHeaderValueParser.DEFAULT; ParserCursor cursor = new ParserCursor(pos, buffer.length()); HeaderElement[] elements = parser.parseElements(buffer, cursor); if (elements.length == 0) { throw new MalformedChallengeException("Authentication challenge is empty"); } this.params = new HashMap<String, String>(elements.length); for (HeaderElement element : elements) { this.params.put(element.getName(), element.getValue()); } }
public long getKeepAliveDuration(HttpResponse response, HttpContext context) { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch(NumberFormatException ignore) { } } } return -1; }
/** * Estimates the length of a formatted header element. * * @param elem the header element to format, or <code>null</code> * * @return a length estimate, in number of characters */ protected int estimateHeaderElementLen(final HeaderElement elem) { if (elem == null) return 0; int result = elem.getName().length(); // name final String value = elem.getValue(); if (value != null) { // assume quotes, but no escaped characters result += 3 + value.length(); // ="value" } final int parcnt = elem.getParameterCount(); if (parcnt > 0) { for (int i=0; i<parcnt; i++) { result += 2 + // ; <param> estimateNameValuePairLen(elem.getParameter(i)); } } return result; }
String name = headerelement.getName(); String value = headerelement.getValue(); if (name == null || name.length() == 0) {
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (HeaderElement headerelement : elems) { String name = headerelement.getName(); String value = headerelement.getValue(); if (name == null || name.length() == 0) { throw new MalformedCookieException("Cookie name may not be empty"); } BasicClientCookie cookie = new BasicClientCookie(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { NameValuePair attrib = attribs[j]; String s = attrib.getName().toLowerCase(Locale.ENGLISH); cookie.setAttribute(s, attrib.getValue()); CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
private void parseNextElement() { // loop while there are headers left to parse while (this.headerIt.hasNext() || this.cursor != null) { if (this.cursor == null || this.cursor.atEnd()) { // get next header value bufferHeaderValue(); } // Anything buffered? if (this.cursor != null) { // loop while there is data in the buffer while (!this.cursor.atEnd()) { HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor); if (!(e.getName().length() == 0 && e.getValue() == null)) { // Found something this.currentElement = e; return; } } // if at the end of the buffer if (this.cursor.atEnd()) { // discard it this.cursor = null; this.buffer = null; } } } }
public CharArrayBuffer formatHeaderElement(CharArrayBuffer buffer, final HeaderElement elem, final boolean quote) { if (elem == null) { throw new IllegalArgumentException ("Header element must not be null."); } int len = estimateHeaderElementLen(elem); if (buffer == null) { buffer = new CharArrayBuffer(len); } else { buffer.ensureCapacity(len); } buffer.append(elem.getName()); final String value = elem.getValue(); if (value != null) { buffer.append('='); doFormatValue(buffer, value, quote); } final int parcnt = elem.getParameterCount(); if (parcnt > 0) { for (int i=0; i<parcnt; i++) { buffer.append("; "); formatNameValuePair(buffer, elem.getParameter(i), quote); } } return buffer; }
protected boolean hasEncoding( final HttpResponse response, final String encoding ) { HttpEntity entity = response.getEntity(); if ( entity == null ) return false; Header ceHeader = entity.getContentEncoding(); if ( ceHeader == null ) return false; HeaderElement[] codecs = ceHeader.getElements(); for ( int i = 0; i < codecs.length; i++ ) if ( encoding.equalsIgnoreCase( codecs[i].getName() ) ) return true; return false; } }
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding"; private static final String ENCODING_GZIP = "gzip"; final DefaultHttpClient client = new DefaultHttpClient(manager, parameters); client.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { // Add header to accept gzip content if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } } }); client.addResponseInterceptor(new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { // Inflate any responses compressed with gzip final HttpEntity entity = response.getEntity(); final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } });
public static String getSessionIdFromResponse( final HttpResponse response ) { final Header cookie = response.getFirstHeader( "Set-Cookie" ); if ( cookie != null ) { for ( final HeaderElement header : cookie.getElements() ) { if ( "JSESSIONID".equals( header.getName() ) ) { return header.getValue(); } } } return null; }
String suffix = null; if (CommonUtils.notEmpty(entity.getContentType().getElements())) { suffix = CONTENT_TYPE_MAP.get(entity.getContentType().getElements()[0].getName());
String suffix = null; if (CommonUtils.notEmpty(entity.getContentType().getElements())) { suffix = CONTENT_TYPE_MAP.get(entity.getContentType().getElements()[0].getName());