List of usage examples for org.apache.http.impl.conn DefaultHttpResponseParser DefaultHttpResponseParser
public DefaultHttpResponseParser(final SessionInputBuffer buffer)
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); }