Java Code Examples for org.apache.http.HeaderIterator#hasNext()

The following examples show how to use org.apache.http.HeaderIterator#hasNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NUHttpOptions.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: HttpClientUtils.java    From onetwo with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 3
Source File: HttpClientTest.java    From onetwo with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 4
Source File: AbstractHttpTransport.java    From consul-api with Apache License 2.0 5 votes vote down vote up
private void logRequest(HttpUriRequest httpRequest) {
	StringBuilder sb = new StringBuilder();

	// method
	sb.append(httpRequest.getMethod());
	sb.append(" ");

	// url
	sb.append(httpRequest.getURI());
	sb.append(" ");

	// headers, if any
	HeaderIterator iterator = httpRequest.headerIterator();
	if (iterator.hasNext()) {
		sb.append("Headers:[");

		Header header = iterator.nextHeader();
		sb.append(header.getName()).append("=").append(header.getValue());

		while (iterator.hasNext()) {
			header = iterator.nextHeader();
			sb.append(header.getName()).append("=").append(header.getValue());
			sb.append(";");
		}

		sb.append("] ");
	}

	//
	log.finest(sb.toString());
}
 
Example 5
Source File: HttpProtocol.java    From storm-crawler with Apache License 2.0 4 votes vote down vote up
@Override
public ProtocolResponse handleResponse(HttpResponse response)
        throws IOException {

    StatusLine statusLine = response.getStatusLine();
    int status = statusLine.getStatusCode();

    StringBuilder verbatim = new StringBuilder();
    if (storeHTTPHeaders) {
        verbatim.append(statusLine.toString()).append("\r\n");
    }

    Metadata metadata = new Metadata();
    HeaderIterator iter = response.headerIterator();
    while (iter.hasNext()) {
        Header header = iter.nextHeader();
        if (storeHTTPHeaders) {
            verbatim.append(header.toString()).append("\r\n");
        }
        metadata.addValue(header.getName().toLowerCase(Locale.ROOT),
                header.getValue());
    }

    MutableBoolean trimmed = new MutableBoolean();

    byte[] bytes = new byte[] {};

    if (!Status.REDIRECTION.equals(Status.fromHTTPCode(status))) {
        bytes = HttpProtocol.toByteArray(response.getEntity(), maxContent,
                trimmed);
        if (trimmed.booleanValue()) {
            metadata.setValue(ProtocolResponse.TRIMMED_RESPONSE_KEY, "true");
            LOG.warn("HTTP content trimmed to {}", bytes.length);
        }
    }

    if (storeHTTPHeaders) {
        verbatim.append("\r\n");
        metadata.setValue(ProtocolResponse.RESPONSE_HEADERS_KEY,
                verbatim.toString());
    }

    return new ProtocolResponse(bytes, status, metadata);
}