use of org.apache.http.StatusLine in project android_frameworks_base by ParanoidAndroid.
the class AndroidHttpClientConnection method parseResponseHeader.
/**
* Parses the response headers and adds them to the
* given {@code headers} object, and returns the response StatusLine
* @param headers store parsed header to headers.
* @throws IOException
* @return StatusLine
* @see HttpClientConnection#receiveResponseHeader()
*/
public StatusLine parseResponseHeader(Headers headers) throws IOException, ParseException {
assertOpen();
CharArrayBuffer current = new CharArrayBuffer(64);
if (inbuffer.readLine(current) == -1) {
throw new NoHttpResponseException("The target server failed to respond");
}
// Create the status line from the status string
StatusLine statusline = BasicLineParser.DEFAULT.parseStatusLine(current, new ParserCursor(0, current.length()));
if (HttpLog.LOGV)
HttpLog.v("read: " + statusline);
int statusCode = statusline.getStatusCode();
// Parse header body
CharArrayBuffer previous = null;
int headerNumber = 0;
while (true) {
if (current == null) {
current = new CharArrayBuffer(64);
} else {
// This must be he buffer used to parse the status
current.clear();
}
int l = inbuffer.readLine(current);
if (l == -1 || current.length() < 1) {
break;
}
// Parse the header name and value
// Check for folded headers first
// Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
// discussion on folded headers
char first = current.charAt(0);
if ((first == ' ' || first == '\t') && previous != null) {
// we have continuation folded header
// so append value
int start = 0;
int length = current.length();
while (start < length) {
char ch = current.charAt(start);
if (ch != ' ' && ch != '\t') {
break;
}
start++;
}
if (maxLineLength > 0 && previous.length() + 1 + current.length() - start > maxLineLength) {
throw new IOException("Maximum line length limit exceeded");
}
previous.append(' ');
previous.append(current, start, current.length() - start);
} else {
if (previous != null) {
headers.parseHeader(previous);
}
headerNumber++;
previous = current;
current = null;
}
if (maxHeaderCount > 0 && headerNumber >= maxHeaderCount) {
throw new IOException("Maximum header count exceeded");
}
}
if (previous != null) {
headers.parseHeader(previous);
}
if (statusCode >= 200) {
this.metrics.incrementResponseCount();
}
return statusline;
}
use of org.apache.http.StatusLine in project cw-android by commonsguy.
the class ByteArrayResponseHandler method handleResponse.
public byte[] handleResponse(final HttpResponse response) throws IOException, HttpResponseException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
HttpEntity entity = response.getEntity();
if (entity == null) {
return (null);
}
return (EntityUtils.toByteArray(entity));
}
use of org.apache.http.StatusLine in project dropwizard by dropwizard.
the class DropwizardApacheConnector method apply.
/**
* {@inheritDoc}
*/
@Override
public ClientResponse apply(ClientRequest jerseyRequest) {
try {
final HttpUriRequest apacheRequest = buildApacheRequest(jerseyRequest);
final CloseableHttpResponse apacheResponse = client.execute(apacheRequest);
final StatusLine statusLine = apacheResponse.getStatusLine();
final Response.StatusType status = Statuses.from(statusLine.getStatusCode(), firstNonNull(statusLine.getReasonPhrase(), ""));
final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest);
for (Header header : apacheResponse.getAllHeaders()) {
final List<String> headerValues = jerseyResponse.getHeaders().get(header.getName());
if (headerValues == null) {
jerseyResponse.getHeaders().put(header.getName(), Lists.newArrayList(header.getValue()));
} else {
headerValues.add(header.getValue());
}
}
final HttpEntity httpEntity = apacheResponse.getEntity();
jerseyResponse.setEntityStream(httpEntity != null ? httpEntity.getContent() : new ByteArrayInputStream(new byte[0]));
return jerseyResponse;
} catch (Exception e) {
throw new ProcessingException(e);
}
}
use of org.apache.http.StatusLine in project SimplifyReader by chentao0707.
the class HurlStack method performRequest.
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
use of org.apache.http.StatusLine in project intellij-community by JetBrains.
the class EduStepicClient method getFromStepic.
static <T> T getFromStepic(String link, final Class<T> container, @NotNull final CloseableHttpClient client) throws IOException {
if (!link.startsWith("/"))
link = "/" + link;
final HttpGet request = new HttpGet(EduStepicNames.STEPIC_API_URL + link);
final CloseableHttpResponse response = client.execute(request);
final StatusLine statusLine = response.getStatusLine();
final HttpEntity responseEntity = response.getEntity();
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
EntityUtils.consume(responseEntity);
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
throw new IOException("Stepic returned non 200 status code " + responseString);
}
return deserializeStepicResponse(container, responseString);
}
Aggregations