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 HeaderIterator headerIterator() { return new HeaderIterator() { int index = 0; @Override public boolean hasNext() { return index < headers.length; } @Override public Header nextHeader() { if (index >= headers.length) throw new NoSuchElementException(); return headers[index++]; } @Override public Object next() { return nextHeader(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
@Test public void shouldSupportHeaderIteratorWithArg() throws Exception { HttpResponse resp = new TestHttpResponse(304, "REDIRECTED", new BasicHeader("Location", "http://bar.com"), new BasicHeader("X-Zombo-Com", "http://zombo.com"), new BasicHeader("Location", "http://foo.com")); HeaderIterator it = resp.headerIterator("Location"); assertThat(it.hasNext()).isTrue(); assertThat(it.nextHeader().getValue()).isEqualTo("http://bar.com"); assertThat(it.hasNext()).isTrue(); assertThat(it.nextHeader().getValue()).isEqualTo("http://foo.com"); assertThat(it.hasNext()).isFalse(); }
public HttpRequest receiveRequest() throws HttpException, IOException { HttpRequest request = (HttpRequest) this.requestParser.parse(); if (HEADERLOG.isDebugEnabled()) { HEADERLOG.debug(">> " + request.getRequestLine().toString()); for (HeaderIterator it = request.headerIterator(); it.hasNext(); ) { HEADERLOG.debug(">> " + it.nextHeader().toString()); } } // Prepare input stream this.in = null; if (request instanceof HttpEntityEnclosingRequest) { long len = this.contentLenStrategy.determineLength(request); if (len == ContentLengthStrategy.CHUNKED) { this.in = new ChunkedInputStream(this.inbuffer); } else if (len == ContentLengthStrategy.IDENTITY) { this.in = new IdentityInputStream(this.inbuffer); } else { this.in = new ContentLengthInputStream(inbuffer, len); } } return request; }
/** * Render the headers and the response content as a string. * This could include more things such as the status code. * * @param response * @return A string consisting of the HTTP Response headers nicely formatted. */ public String buildResponseStr(CloseableHttpResponse response) { StringBuilder sb = new StringBuilder(); HeaderIterator hi = response.headerIterator(); while (hi.hasNext()) { Header header = hi.nextHeader(); sb.append(header.getName()).append(": ").append(header.getValue()).append("\r\n"); } sb.append("\r\n"); try { sb.append(EntityUtils.toString(response.getEntity())); } catch (IOException | IllegalStateException ex) { // Don't care } return sb.toString(); }
public void removeHeaders(final String name) { if (name == null) { return; } for (HeaderIterator i = this.headergroup.iterator(); i.hasNext(); ) { Header header = i.nextHeader(); if (name.equalsIgnoreCase(header.getName())) { i.remove(); } } }
/** * Creates a new instance of BasicHeaderElementIterator */ public BasicHeaderElementIterator( final HeaderIterator headerIterator, final HeaderValueParser parser) { if (headerIterator == null) { throw new IllegalArgumentException("Header iterator may not be null"); } if (parser == null) { throw new IllegalArgumentException("Parser may not be null"); } this.headerIt = headerIterator; this.parser = parser; }
/** * Creates a new instance of {@link BasicTokenIterator}. * * @param headerIterator the iterator for the headers to tokenize */ public BasicTokenIterator(final HeaderIterator headerIterator) { if (headerIterator == null) { throw new IllegalArgumentException ("Header iterator must not be null."); } this.headerIt = headerIterator; this.searchPos = findNext(-1); }
public void write(final T message) throws IOException, HttpException { if (message == null) { throw new IllegalArgumentException("HTTP message may not be null"); } writeHeadLine(message); for (HeaderIterator it = message.headerIterator(); it.hasNext(); ) { Header header = it.nextHeader(); this.sessionBuffer.writeLine (lineFormatter.formatHeader(this.lineBuf, header)); } this.lineBuf.clear(); this.sessionBuffer.writeLine(this.lineBuf); }
protected static void writeHeaders(final HeaderGroup headers, final OutputStream output) throws IOException { for (final HeaderIterator it = headers.iterator(); it.hasNext();) { final org.apache.http.Header header = it.nextHeader(); Util.toOutputStream(BasicLineFormatter.formatHeader(header, null), output); output.write(ByteArraySessionOutputBuffer.CRLF); } }
public static Set<String> keys(HeaderGroup hg) { Set<String> ret = new HashSet<>(); for (HeaderIterator it = hg.iterator(); it.hasNext();) { Header header = it.nextHeader(); ret.add(header.getName().toLowerCase()); } return ret; }
public Set<String> getAllowedMethods(final HttpResponse response) { Args.notNull(response, "HTTP response"); final HeaderIterator it = response.headerIterator("Allow"); final Set<String> methods = new HashSet<String>(); while (it.hasNext()) { final Header header = it.nextHeader(); final HeaderElement[] elements = header.getElements(); for (final HeaderElement element : elements) { methods.add(element.getName()); } } return methods; }
public RequestBuilder removeHeaders(final String name) { if (name == null || headergroup == null) { return this; } for (final HeaderIterator i = headergroup.iterator(); i.hasNext(); ) { final Header header = i.nextHeader(); if (name.equalsIgnoreCase(header.getName())) { i.remove(); } } return this; }
public void write(final HttpMessage message) throws IOException, HttpException { Args.notNull(message, "HTTP message"); writeHeadLine(message); for (final HeaderIterator it = message.headerIterator(); it.hasNext(); ) { final Header header = it.nextHeader(); this.sessionBuffer.writeLine (lineFormatter.formatHeader(this.lineBuf, header)); } this.lineBuf.clear(); this.sessionBuffer.writeLine(this.lineBuf); }
/** * Returns all the headers that were on the origin response. */ public Header[] getAllHeaders() { final HeaderGroup filteredHeaders = new HeaderGroup(); for (final HeaderIterator iterator = responseHeaders.iterator(); iterator .hasNext();) { final Header header = (Header) iterator.next(); if (!REQUEST_METHOD_HEADER_NAME.equals(header.getName())) { filteredHeaders.addHeader(header); } } return filteredHeaders.getAllHeaders(); }
@Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { Args.notNull(response, "HTTP request"); Args.notNull(context, "HTTP context"); final HttpClientContext clientContext = HttpClientContext.adapt(context); // Obtain actual CookieSpec instance final CookieSpec cookieSpec = clientContext.getCookieSpec(); if (cookieSpec == null) { this.log.debug("Cookie spec not specified in HTTP context"); return; } // Obtain cookie store final CookieStore cookieStore = clientContext.getCookieStore(); if (cookieStore == null) { this.log.debug("Cookie store not specified in HTTP context"); return; } // Obtain actual CookieOrigin instance final CookieOrigin cookieOrigin = clientContext.getCookieOrigin(); if (cookieOrigin == null) { this.log.debug("Cookie origin not specified in HTTP context"); return; } HeaderIterator it = response.headerIterator(SM.SET_COOKIE); processCookies(it, cookieSpec, cookieOrigin, cookieStore); // see if the cookie spec supports cookie versioning. if (cookieSpec.getVersion() > 0) { // process set-cookie2 headers. // Cookie2 will replace equivalent Cookie instances it = response.headerIterator(SM.SET_COOKIE2); processCookies(it, cookieSpec, cookieOrigin, cookieStore); } }
public static String toString(HttpResponse response) throws ParseException, IOException{ StringBuilder str = new StringBuilder(); str.append("statusCode:") .append(response.getStatusLine().getStatusCode()) .append("\n"); HeaderIterator headerIt = response.headerIterator(); while(headerIt.hasNext()){ Header header = headerIt.nextHeader(); str.append(header).append("\n"); } HttpEntity entity = response.getEntity(); str.append(EntityUtils.toString(entity)); return str.toString(); }
static void printResponse(HttpResponse response) throws ParseException, IOException{ System.out.println("statusCode:"+response.getStatusLine().getStatusCode()); HeaderIterator headerIt = response.headerIterator(); while(headerIt.hasNext()){ Header header = headerIt.nextHeader(); System.out.println(header); } HttpEntity entity = response.getEntity(); System.out.println(EntityUtils.toString(entity)); }
@Override public ProtocolResponse handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); Metadata metadata = new Metadata(); HeaderIterator iter = response.headerIterator(); while (iter.hasNext()) { Header header = iter.nextHeader(); metadata.addValue(header.getName().toLowerCase(Locale.ROOT), header.getValue()); } // TODO find a way of limiting by maxContent byte[] bytes = EntityUtils.toByteArray(response.getEntity()); return new ProtocolResponse(bytes, status, metadata); }
@Override public HeaderIterator headerIterator(final String s) { return new HeaderIterator() { int index = 0; @Override public boolean hasNext() { return nextIndex() != -1; } private int nextIndex() { for (int i = index; i<headers.length; i++) { if (headers[i].getName().equalsIgnoreCase(s)) { return i; } } return -1; } @Override public Header nextHeader() { index = nextIndex(); if (index == -1) throw new NoSuchElementException(); return headers[index++]; } @Override public Object next() { return nextHeader(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
@Test public void shouldSupportHeaderIterator() throws Exception { HttpResponse resp = new TestHttpResponse(304, "REDIRECTED", new BasicHeader("Location", "http://bar.com"), new BasicHeader("Location", "http://zombo.com")); HeaderIterator it = resp.headerIterator(); assertThat(it.hasNext()).isTrue(); assertThat(it.nextHeader().getValue()).isEqualTo("http://bar.com"); assertThat(it.nextHeader().getValue()).isEqualTo("http://zombo.com"); assertThat(it.hasNext()).isFalse(); }
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { if (response == null) { throw new IllegalArgumentException("HTTP request may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } // Obtain actual CookieSpec instance CookieSpec cookieSpec = (CookieSpec) context.getAttribute( ClientContext.COOKIE_SPEC); if (cookieSpec == null) { return; } // Obtain cookie store CookieStore cookieStore = (CookieStore) context.getAttribute( ClientContext.COOKIE_STORE); if (cookieStore == null) { this.log.info("CookieStore not available in HTTP context"); return; } // Obtain actual CookieOrigin instance CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute( ClientContext.COOKIE_ORIGIN); if (cookieOrigin == null) { this.log.info("CookieOrigin not available in HTTP context"); return; } HeaderIterator it = response.headerIterator(SM.SET_COOKIE); processCookies(it, cookieSpec, cookieOrigin, cookieStore); // see if the cookie spec supports cookie versioning. if (cookieSpec.getVersion() > 0) { // process set-cookie2 headers. // Cookie2 will replace equivalent Cookie instances it = response.headerIterator(SM.SET_COOKIE2); processCookies(it, cookieSpec, cookieOrigin, cookieStore); } }
public void sendResponse(final HttpResponse response) throws HttpException, IOException { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } if (HEADERLOG.isDebugEnabled()) { HEADERLOG.debug("<< " + response.getStatusLine().toString()); for (HeaderIterator it = response.headerIterator(); it.hasNext(); ) { HEADERLOG.debug("<< " + it.nextHeader().toString()); } } this.responseWriter.write(response); // Prepare output stream this.out = null; ProtocolVersion ver = response.getStatusLine().getProtocolVersion(); HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); if (entity.isChunked() && ver.greaterEquals(HttpVersion.HTTP_1_1)) { this.out = new ChunkedOutputStream(this.outbuffer); } else if (len >= 0) { this.out = new ContentLengthOutputStream(this.outbuffer, len); } else { this.out = new IdentityOutputStream(this.outbuffer); } } else { this.outbuffer.flush(); } }
@Override public HeaderIterator headerIterator() { return httpResponse.headerIterator(); }
@Override public HeaderIterator headerIterator(String name) { return httpResponse.headerIterator(name); }
public HeaderIterator headerIterator() { return this.original.headerIterator(); }
public HeaderIterator headerIterator(String name) { return this.original.headerIterator(name); }