use of org.apache.http.client.methods.HttpUriRequest in project android_frameworks_base by ParanoidAndroid.
the class AndroidHttpClient method toCurl.
/**
* Generates a cURL command equivalent to the given request.
*/
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append("curl ");
// add in the method
builder.append("-X ");
builder.append(request.getMethod());
builder.append(" ");
for (Header header : request.getAllHeaders()) {
if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) {
continue;
}
builder.append("--header \"");
builder.append(header.toString().trim());
builder.append("\" ");
}
URI uri = request.getURI();
// relative URI. We want an absolute URI.
if (request instanceof RequestWrapper) {
HttpRequest original = ((RequestWrapper) request).getOriginal();
if (original instanceof HttpUriRequest) {
uri = ((HttpUriRequest) original).getURI();
}
}
builder.append("\"");
builder.append(uri);
builder.append("\"");
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
HttpEntity entity = entityRequest.getEntity();
if (entity != null && entity.isRepeatable()) {
if (entity.getContentLength() < 1024) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
entity.writeTo(stream);
if (isBinaryContent(request)) {
String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
builder.append(" --data-binary @/tmp/$$.bin");
} else {
String entityString = stream.toString();
builder.append(" --data-ascii \"").append(entityString).append("\"");
}
} else {
builder.append(" [TOO MUCH DATA TO INCLUDE]");
}
}
}
return builder.toString();
}
use of org.apache.http.client.methods.HttpUriRequest in project pinpoint by naver.
the class HttpClientExecuteMethodWithHttpUriRequestInterceptor method getHost.
@Override
protected NameIntValuePair<String> getHost(Object[] args) {
final HttpUriRequest httpUriRequest = getHttpUriRequest(args);
if (httpUriRequest == null) {
return null;
}
final URI uri = httpUriRequest.getURI();
return extractHost(uri);
}
use of org.apache.http.client.methods.HttpUriRequest 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.client.methods.HttpUriRequest in project spark by perwendel.
the class SparkTestUtil method doMethod.
public UrlResponse doMethod(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) throws IOException {
HttpUriRequest httpRequest = getHttpRequest(requestMethod, path, body, secureConnection, acceptType, reqHeaders);
HttpResponse httpResponse = httpClient.execute(httpRequest);
UrlResponse urlResponse = new UrlResponse();
urlResponse.status = httpResponse.getStatusLine().getStatusCode();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
urlResponse.body = EntityUtils.toString(entity);
} else {
urlResponse.body = "";
}
Map<String, String> headers = new HashMap<>();
Header[] allHeaders = httpResponse.getAllHeaders();
for (Header header : allHeaders) {
headers.put(header.getName(), header.getValue());
}
urlResponse.headers = headers;
return urlResponse;
}
use of org.apache.http.client.methods.HttpUriRequest in project mobile-android by photo.
the class ApiBase method execute.
/**
* Execute a request to the API.
*
* @param request request to perform
* @param baseUrl the base server url
* @param consumer the oauth consumer key to sign request
* @param listener Progress Listener with callback on progress
* @param connectionTimeout the connection and socket timeout
* @return the response from the API
* @throws ClientProtocolException
* @throws IOException
*/
public ApiResponse execute(ApiRequest request, String baseUrl, OAuthConsumer consumer, ProgressListener listener, int connectionTimeout) throws ClientProtocolException, IOException {
// PoolingClientConnectionManager();
HttpParams params = new BasicHttpParams();
// set params for connection...
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
HttpConnectionParams.setSoTimeout(params, connectionTimeout);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
DefaultHttpClient httpClient = new DefaultHttpClient(params);
HttpUriRequest httpRequest = createHttpRequest(request, baseUrl, listener);
httpRequest.getParams().setBooleanParameter("http.protocol.expect-continue", false);
httpRequest.setHeader("User-Agent", "android");
httpRequest.setHeader("source", "android");
if (consumer != null) {
try {
consumer.sign(httpRequest);
} catch (Exception e) {
GuiUtils.noAlertError(TAG, "Error signing request", e);
}
} else {
TrackerUtils.trackBackgroundEvent("not_signed_request", baseUrl + request.getPath());
}
return new ApiResponse(baseUrl + request.getPath(), httpClient.execute(httpRequest));
}
Aggregations