Search in sources :

Example 16 with HttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse in project hive by apache.

the class PTestClient method downloadTestResults.

private void downloadTestResults(String testHandle, String testOutputDir) throws Exception {
    HttpGet request = new HttpGet(mLogsEndpoint + testHandle + "/test-results.tar.gz");
    FileOutputStream output = null;
    try {
        HttpResponse httpResponse = mHttpClient.execute(request);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        output = new FileOutputStream(new File(testOutputDir, "test-results.tar.gz"));
        IOUtils.copyLarge(httpResponse.getEntity().getContent(), output);
        output.flush();
    } finally {
        request.abort();
        if (output != null) {
            output.close();
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpGet(org.apache.http.client.methods.HttpGet) FileOutputStream(java.io.FileOutputStream) HttpResponse(org.apache.http.HttpResponse) File(java.io.File)

Example 17 with HttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse in project hive by apache.

the class PTestClient method post.

private <S extends GenericResponse> S post(Object payload, boolean agressiveRetry) throws Exception {
    EndPointResponsePair endPointResponse = Preconditions.checkNotNull(REQUEST_TO_ENDPOINT.get(payload.getClass()), payload.getClass().getName());
    HttpPost request = new HttpPost(mApiEndPoint + endPointResponse.getEndpoint());
    try {
        String payloadString = mMapper.writeValueAsString(payload);
        StringEntity params = new StringEntity(payloadString);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        if (agressiveRetry) {
            mHttpClient.setHttpRequestRetryHandler(new PTestHttpRequestRetryHandler());
        }
        HttpResponse httpResponse = mHttpClient.execute(request);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new IllegalStateException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        @SuppressWarnings("unchecked") S result = (S) endPointResponse.getResponseClass().cast(mMapper.readValue(response, endPointResponse.getResponseClass()));
        Status.assertOK(result.getStatus());
        if (System.getProperty("DEBUG_PTEST_CLIENT") != null) {
            System.err.println("payload " + payloadString);
            if (result instanceof TestLogResponse) {
                System.err.println("response " + ((TestLogResponse) result).getOffset() + " " + ((TestLogResponse) result).getStatus());
            } else {
                System.err.println("response " + response);
            }
        }
        Thread.sleep(1000);
        return result;
    } finally {
        request.abort();
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) TestLogResponse(org.apache.hive.ptest.api.response.TestLogResponse) HttpResponse(org.apache.http.HttpResponse)

Example 18 with HttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse in project PlayerHater by chrisrhoden.

the class PlaylistParser method parsePls.

private static Uri[] parsePls(Uri uri) {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(uri.toString()));
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String header = reader.readLine();
        if (header.trim().equalsIgnoreCase("[playlist]")) {
            String line;
            ArrayList<Uri> uriList = new ArrayList<Uri>();
            do {
                line = reader.readLine();
                if (line != null) {
                    if (line.startsWith("File")) {
                        String fileName = line.substring(line.indexOf("=") + 1).trim();
                        uriList.add(Uri.parse(fileName));
                    }
                }
            } while (line != null);
            if (uriList.size() > 0) {
                Uri[] res = new Uri[uriList.size()];
                return uriList.toArray(res);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new Uri[] { uri };
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) Uri(android.net.Uri) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader)

Example 19 with HttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse in project Android by hmkcode.

the class MainActivity method POST.

public static String POST(String url, Person person) {
    InputStream inputStream = null;
    String result = "";
    try {
        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);
        String json = "";
        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("name", person.getName());
        jsonObject.accumulate("country", person.getCountry());
        jsonObject.accumulate("twitter", person.getTwitter());
        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();
        // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person); 
        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);
        // 6. set httpPost Entity
        httpPost.setEntity(se);
        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);
        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
        // 10. convert inputstream to string
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }
    // 11. return result
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException)

Example 20 with HttpResponse

use of org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse 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)

Aggregations

HttpResponse (org.apache.http.HttpResponse)4366 Test (org.junit.Test)2158 HttpGet (org.apache.http.client.methods.HttpGet)1833 IOException (java.io.IOException)1110 URI (java.net.URI)834 HttpPost (org.apache.http.client.methods.HttpPost)759 HttpClient (org.apache.http.client.HttpClient)600 HttpEntity (org.apache.http.HttpEntity)541 TestHttpClient (io.undertow.testutils.TestHttpClient)403 InputStream (java.io.InputStream)398 Header (org.apache.http.Header)385 StringEntity (org.apache.http.entity.StringEntity)363 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)344 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)338 HttpPut (org.apache.http.client.methods.HttpPut)320 ArrayList (java.util.ArrayList)316 Identity (org.olat.core.id.Identity)262 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)253 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)209 File (java.io.File)196