Search in sources :

Example 76 with HttpEntity

use of org.apache.http.HttpEntity in project rainbow by juankysoriano.

the class RainbowIO method createInputRaw.

/**
     * Call createInput() without automatic gzip decompression.
     */
public static InputStream createInputRaw(Context context, final String filename) {
    InputStream stream = null;
    if (filename == null) {
        return null;
    }
    if (filename.length() == 0) {
        return null;
    }
    if (filename.indexOf(":") != -1) {
        // at least smells like URL
        try {
            HttpGet httpRequest = null;
            httpRequest = new HttpGet(URI.create(filename));
            final HttpClient httpclient = new DefaultHttpClient();
            final HttpResponse response = httpclient.execute(httpRequest);
            final HttpEntity entity = response.getEntity();
            return entity.getContent();
        } catch (final MalformedURLException mfue) {
        } catch (final FileNotFoundException fnfe) {
        } catch (final IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    // Try the assets folder
    final AssetManager assets = context.getAssets();
    try {
        stream = assets.open(filename);
        if (stream != null) {
            return stream;
        }
    } catch (final IOException e) {
    }
    // Maybe this is an absolute path, didja ever think of that?
    final File absFile = new File(filename);
    if (absFile.exists()) {
        try {
            stream = new FileInputStream(absFile);
            if (stream != null) {
                return stream;
            }
        } catch (final FileNotFoundException fnfe) {
        // fnfe.printStackTrace();
        }
    }
    // Maybe this is a file that was written by the sketch later.
    final File sketchFile = new File(sketchPath(context, filename));
    if (sketchFile.exists()) {
        try {
            stream = new FileInputStream(sketchFile);
            if (stream != null) {
                return stream;
            }
        } catch (final FileNotFoundException fnfe) {
        }
    }
    // Attempt to load the file more directly. Doesn't like paths.
    try {
        stream = context.openFileInput(filename);
        if (stream != null) {
            return stream;
        }
    } catch (final FileNotFoundException e) {
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) AssetManager(android.content.res.AssetManager) HttpEntity(org.apache.http.HttpEntity) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) FileNotFoundException(java.io.FileNotFoundException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) File(java.io.File) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) FileInputStream(java.io.FileInputStream)

Example 77 with HttpEntity

use of org.apache.http.HttpEntity in project android-volley by mcxiaoke.

the class HttpClientStack method setEntityIfNonEmptyBody.

private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 78 with HttpEntity

use of org.apache.http.HttpEntity in project pinpoint by naver.

the class DefaultClientExchangeHandlerImplStartMethodInterceptor method recordEntity.

protected void recordEntity(HttpMessage httpMessage, SpanEventRecorder recorder) {
    if (httpMessage instanceof HttpEntityEnclosingRequest) {
        final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) httpMessage;
        try {
            final HttpEntity entity = entityRequest.getEntity();
            if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) {
                if (entitySampler.isSampling()) {
                    final String entityString = entityUtilsToString(entity, "UTF8", 1024);
                    recorder.recordAttribute(AnnotationKey.HTTP_PARAM_ENTITY, entityString);
                }
            }
        } catch (Exception e) {
            logger.debug("HttpEntityEnclosingRequest entity record fail. Caused:{}", e.getMessage(), e);
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) ParseException(org.apache.http.ParseException) IOException(java.io.IOException)

Example 79 with HttpEntity

use of org.apache.http.HttpEntity in project neo4j by neo4j.

the class RetrieveNodeIT method shouldParameteriseUrisInNodeRepresentationWithHostHeaderValue.

@Test
public void shouldParameteriseUrisInNodeRepresentationWithHostHeaderValue() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(nodeUri);
        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("Host", "dummy.neo4j.org");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String entityBody = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
        assertThat(entityBody, containsString("http://dummy.neo4j.org/db/data/node/"));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Example 80 with HttpEntity

use of org.apache.http.HttpEntity in project neo4j by neo4j.

the class RetrieveNodeIT method shouldParameteriseUrisInNodeRepresentationWithoutHostHeaderUsingRequestUri.

@Test
public void shouldParameteriseUrisInNodeRepresentationWithoutHostHeaderUsingRequestUri() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(nodeUri);
        httpget.setHeader("Accept", "application/json");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String entityBody = IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8);
        assertThat(entityBody, containsString(nodeUri.toString()));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Aggregations

HttpEntity (org.apache.http.HttpEntity)518 HttpResponse (org.apache.http.HttpResponse)185 HttpGet (org.apache.http.client.methods.HttpGet)152 IOException (java.io.IOException)144 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)105 Test (org.junit.Test)93 HttpPost (org.apache.http.client.methods.HttpPost)84 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)83 InputStream (java.io.InputStream)71 ArrayList (java.util.ArrayList)69 Header (org.apache.http.Header)64 StatusLine (org.apache.http.StatusLine)61 URI (java.net.URI)58 NameValuePair (org.apache.http.NameValuePair)58 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)58 HttpClient (org.apache.http.client.HttpClient)55 StringEntity (org.apache.http.entity.StringEntity)51 InputStreamReader (java.io.InputStreamReader)44 BufferedReader (java.io.BufferedReader)41