Search in sources :

Example 46 with HttpUriRequest

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();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Header(org.apache.http.Header) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) RequestWrapper(org.apache.http.impl.client.RequestWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI)

Example 47 with HttpUriRequest

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);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) URI(java.net.URI)

Example 48 with HttpUriRequest

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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ClientResponse(org.glassfish.jersey.client.ClientResponse) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) StatusLine(org.apache.http.StatusLine) ClientResponse(org.glassfish.jersey.client.ClientResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Response(javax.ws.rs.core.Response) Header(org.apache.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ProcessingException(javax.ws.rs.ProcessingException)

Example 49 with HttpUriRequest

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;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse)

Example 50 with HttpUriRequest

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));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)185 Test (org.junit.Test)62 TestRequest (com.android.volley.mock.TestRequest)52 HttpGet (org.apache.http.client.methods.HttpGet)45 URI (java.net.URI)43 HttpResponse (org.apache.http.HttpResponse)41 HttpEntity (org.apache.http.HttpEntity)38 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)36 HttpPost (org.apache.http.client.methods.HttpPost)22 IOException (java.io.IOException)19 Header (org.apache.http.Header)18 JSONObject (org.json.JSONObject)18 BufferedReader (java.io.BufferedReader)17 InputStreamReader (java.io.InputStreamReader)17 PrintWriter (java.io.PrintWriter)17 StringWriter (java.io.StringWriter)17 HttpHost (org.apache.http.HttpHost)13 HttpPut (org.apache.http.client.methods.HttpPut)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 StringEntity (org.apache.http.entity.StringEntity)10