use of org.apache.http.entity.BufferedHttpEntity in project SeaStar by 13120241790.
the class SyncHttpClient method sendRequest.
/**
* Puts a new request in queue as a new thread in pool to be executed
*
* @param client
* HttpClient to be used for request, can differ in single requests
* @param httpContext
* HttpContext in which the request will be executed
* @param uriRequest
* instance of HttpUriRequest, which means it must be of
* HttpDelete, HttpPost, HttpGet, HttpPut, etc.
* @param contentType
* MIME body type, for POST and PUT requests, may be null
* @param context
* Context of Android application
* @return
* @throws HttpException
*/
protected String sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, Context context) throws HttpException {
String responseBody = "";
if (contentType != null) {
uriRequest.addHeader("Content-Type", contentType);
}
// set cookie
List<Cookie> list = cookieStore.getCookies();
if (list != null && list.size() > 0) {
for (Cookie cookie : list) {
uriRequest.setHeader("Cookie", cookie.getValue());
}
}
try {
// get the response from assets
URI uri = uriRequest.getURI();
NLog.e(tag, "url : " + uri.toString());
String scheme = uri.getScheme();
if (!TextUtils.isEmpty(scheme) && ASSETS_PATH.equals(scheme)) {
String fileName = uri.getAuthority();
InputStream intput = context.getAssets().open(fileName);
responseBody = inputSteamToString(intput);
NLog.e(tag, "responseBody : " + responseBody);
return responseBody;
}
// get the response from network
HttpEntity bufferEntity = null;
HttpResponse response = client.execute(uriRequest, httpContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
bufferEntity = new BufferedHttpEntity(entity);
responseBody = EntityUtils.toString(bufferEntity, ENCODE_UTF8);
// if(NLog.isDebug()){
// String key = uriRequest.getURI().toString().replace("/", "");
// CacheManager.saveTestData(responseBody, key);
// }
NLog.e(tag, "responseBody : " + responseBody);
}
// get cookie to save local
Header[] headers = response.getHeaders("Set-Cookie");
if (headers != null && headers.length > 0) {
for (int i = 0; i < headers.length; i++) {
String cookie = headers[i].getValue();
BasicClientCookie newCookie = new BasicClientCookie("cookie" + i, cookie);
cookieStore.addCookie(newCookie);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new HttpException(e);
}
return responseBody;
}
use of org.apache.http.entity.BufferedHttpEntity in project ThinkAndroid by white-cat.
the class BinaryHttpResponseHandler method sendResponseMessage.
// Interface to AsyncHttpRequest
@Override
protected void sendResponseMessage(HttpResponse response) {
StatusLine status = response.getStatusLine();
Header[] contentTypeHeaders = response.getHeaders("Content-Type");
byte[] responseBody = null;
if (contentTypeHeaders.length != 1) {
// malformed/ambiguous HTTP Header, ABORT!
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "None, or more than one, Content-Type Header found!"), responseBody);
return;
}
Header contentTypeHeader = contentTypeHeaders[0];
boolean foundAllowedContentType = false;
for (String anAllowedContentType : mAllowedContentTypes) {
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
foundAllowedContentType = true;
}
}
if (!foundAllowedContentType) {
// Content-Type not in allowed list, ABORT!
sendFailureMessage(new HttpResponseException(status.getStatusCode(), "Content-Type not allowed!"), responseBody);
return;
}
try {
HttpEntity entity = null;
HttpEntity temp = response.getEntity();
if (temp != null) {
entity = new BufferedHttpEntity(temp);
}
responseBody = EntityUtils.toByteArray(entity);
} catch (IOException e) {
sendFailureMessage(e, (byte[]) null);
}
if (status.getStatusCode() >= 300) {
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
} else {
sendSuccessMessage(status.getStatusCode(), responseBody);
}
}
use of org.apache.http.entity.BufferedHttpEntity in project gerrit by GerritCodeReview.
the class RestSession method putRaw.
public RestResponse putRaw(String endPoint, RawInput stream) throws IOException {
requireNonNull(stream);
Request put = Request.Put(getUrl(endPoint));
put.addHeader(new BasicHeader(CONTENT_TYPE, stream.getContentType()));
put.body(new BufferedHttpEntity(new InputStreamEntity(stream.getInputStream(), stream.getContentLength())));
return execute(put);
}
use of org.apache.http.entity.BufferedHttpEntity in project robolectric by robolectric.
the class DefaultRequestDirector method createTunnelToTarget.
// establishConnection
/**
* Creates a tunnel to the target server.
* The connection must be established to the (last) proxy.
* A CONNECT request for tunnelling through the proxy will
* be created and sent, the response received and checked.
* This method does <i>not</i> update the connection with
* information about the tunnel, that is left to the caller.
*
* @param route the route to establish
* @param context the context for request execution
*
* @return {@code true} if the tunnelled route is secure,
* {@code false} otherwise.
* The implementation here always returns {@code false},
* but derived classes may override.
*
* @throws HttpException in case of a problem
* @throws IOException in case of an IO problem
*/
protected boolean createTunnelToTarget(HttpRoute route, HttpContext context) throws HttpException, IOException {
HttpHost proxy = route.getProxyHost();
HttpHost target = route.getTargetHost();
HttpResponse response = null;
boolean done = false;
while (!done) {
done = true;
if (!this.managedConn.isOpen()) {
this.managedConn.open(route, context, this.params);
}
HttpRequest connect = createConnectRequest(route, context);
connect.setParams(this.params);
// Populate the execution context
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, managedConn);
context.setAttribute(ClientContext.TARGET_AUTH_STATE, targetAuthState);
context.setAttribute(ClientContext.PROXY_AUTH_STATE, proxyAuthState);
context.setAttribute(ExecutionContext.HTTP_REQUEST, connect);
this.requestExec.preProcess(connect, this.httpProcessor, context);
response = this.requestExec.execute(connect, this.managedConn, context);
response.setParams(this.params);
this.requestExec.postProcess(response, this.httpProcessor, context);
int status = response.getStatusLine().getStatusCode();
if (status < 200) {
throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
}
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {
this.log.debug("Proxy requested authentication");
Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(response, context);
try {
processChallenges(challenges, this.proxyAuthState, this.proxyAuthHandler, response, context);
} catch (AuthenticationException ex) {
if (this.log.isWarnEnabled()) {
this.log.warn("Authentication error: " + ex.getMessage());
break;
}
}
updateAuthState(this.proxyAuthState, proxy, credsProvider);
if (this.proxyAuthState.getCredentials() != null) {
done = false;
// Retry request
if (this.reuseStrategy.keepAlive(response, context)) {
this.log.debug("Connection kept alive");
// Consume response content
HttpEntity entity = response.getEntity();
if (entity != null) {
entity.consumeContent();
}
} else {
this.managedConn.close();
}
}
} else {
// Reset proxy auth scope
this.proxyAuthState.setAuthScope(null);
}
}
}
// can't be null
int status = response.getStatusLine().getStatusCode();
if (status > 299) {
// Buffer response content
HttpEntity entity = response.getEntity();
if (entity != null) {
response.setEntity(new BufferedHttpEntity(entity));
}
this.managedConn.close();
throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response);
}
this.managedConn.markReusable();
// Leave it to derived classes, consider insecure by default here.
return false;
}
use of org.apache.http.entity.BufferedHttpEntity in project openkit-android by OpenKit.
the class AsyncHttpResponseHandler method sendResponseMessage.
// Interface to AsyncHttpRequest
void sendResponseMessage(HttpResponse response) {
StatusLine status = response.getStatusLine();
String responseBody = null;
try {
HttpEntity entity = null;
HttpEntity temp = response.getEntity();
if (temp != null) {
entity = new BufferedHttpEntity(temp);
responseBody = EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
sendFailureMessage(e, (String) null);
}
if (status.getStatusCode() >= 300) {
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
} else {
sendSuccessMessage(status.getStatusCode(), responseBody);
}
}
Aggregations