Example usage for org.apache.http.impl.conn DefaultHttpResponseParser DefaultHttpResponseParser

List of usage examples for org.apache.http.impl.conn DefaultHttpResponseParser DefaultHttpResponseParser

Introduction

In this page you can find the example usage for org.apache.http.impl.conn DefaultHttpResponseParser DefaultHttpResponseParser.

Prototype

public DefaultHttpResponseParser(final SessionInputBuffer buffer) 

Source Link

Document

Creates new instance of DefaultHttpResponseParser.

Usage

From source file:com.meplato.store2.MockResponse.java

/**
 * Parse a response from String contents.
 *
 * @param contents/*  w  w w .  j  av  a2s.  co m*/
 * @return String contents
 * @throws IOException
 * @throws HttpException
 * @throws ServiceException
 */
public static Response fromContents(String contents) throws IOException, HttpException, ServiceException {
    // If this code works, it was written by Georg Wall.
    SessionInputBufferImpl sib = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 65535);
    sib.bind(new ByteArrayInputStream(contents.getBytes(Consts.UTF_8)));
    DefaultHttpResponseParser parser = new DefaultHttpResponseParser(sib);
    HttpResponse httpResponse = parser.parse();
    int endOfHeader = contents.indexOf("\r\n\r\n");
    if (endOfHeader >= 0) {
        endOfHeader += 4; // for \r\n\r\n
        byte[] bytes = contents.getBytes(Consts.UTF_8);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes, endOfHeader, bytes.length - endOfHeader);
        InputStreamEntity entity = new InputStreamEntity(bais);
        entity.setContentType(httpResponse.getFirstHeader("Content-Type"));
        entity.setContentEncoding(httpResponse.getFirstHeader("Content-Encoding"));
        httpResponse.setEntity(entity);
    }
    return new ApacheHttpResponse(httpResponse);
}