Search in sources :

Example 6 with HttpClient

use of org.apache.http.client.HttpClient in project pinot by linkedin.

the class SegmentPushControllerAPIs method deleteSegment.

private boolean deleteSegment(String tablename, String segmentName) throws IOException {
    boolean deleteSuccessful = false;
    HttpClient controllerClient = new DefaultHttpClient();
    HttpGet req = new HttpGet(TABLES_ENDPOINT + URLEncoder.encode(tablename, UTF_8) + "/" + SEGMENTS_ENDPOINT + URLEncoder.encode(segmentName, UTF_8) + DROP_PARAMETERS);
    HttpResponse res = controllerClient.execute(controllerHttpHost, req);
    try {
        if (res == null || res.getStatusLine() == null || res.getStatusLine().getStatusCode() != 200 || !isDeleteSuccessful(tablename, segmentName)) {
            LOGGER.info("Exception in deleting segment, trying again {}", res);
        } else {
            deleteSuccessful = true;
        }
    } finally {
        if (res.getEntity() != null) {
            EntityUtils.consume(res.getEntity());
        }
    }
    return deleteSuccessful;
}
Also used : 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) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 7 with HttpClient

use of org.apache.http.client.HttpClient in project pinot by linkedin.

the class SegmentPushControllerAPIs method getAllSegments.

private List<String> getAllSegments(String tablename, String segmentName) throws IOException {
    List<String> allSegments = new ArrayList<>();
    HttpClient controllerClient = new DefaultHttpClient();
    HttpGet req = new HttpGet(SEGMENTS_ENDPOINT + URLEncoder.encode(tablename, UTF_8));
    HttpResponse res = controllerClient.execute(controllerHttpHost, req);
    try {
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new IllegalStateException(res.getStatusLine().toString());
        }
        InputStream content = res.getEntity().getContent();
        String response = IOUtils.toString(content);
        List<String> allSegmentsPaths = getSegmentsFromResponse(response);
        for (String segment : allSegmentsPaths) {
            allSegments.add(segment.substring(segment.lastIndexOf("/") + 1));
        }
        LOGGER.info("All segments : {}", allSegments);
    } finally {
        if (res.getEntity() != null) {
            EntityUtils.consume(res.getEntity());
        }
    }
    return allSegments;
}
Also used : InputStream(java.io.InputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 8 with HttpClient

use of org.apache.http.client.HttpClient in project pinot by linkedin.

the class AbstractResourceHttpUtils method callJobEndpoint.

protected String callJobEndpoint(HttpRequest req) throws IOException {
    HttpClient controllerClient = new DefaultHttpClient();
    HttpResponse res = controllerClient.execute(resourceHttpHost, req);
    String response = null;
    try {
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new IllegalStateException(res.getStatusLine().toString());
        }
        InputStream content = res.getEntity().getContent();
        response = IOUtils.toString(content);
    } finally {
        if (res.getEntity() != null) {
            EntityUtils.consume(res.getEntity());
        }
    }
    return response;
}
Also used : 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)

Example 9 with HttpClient

use of org.apache.http.client.HttpClient in project pinot by linkedin.

the class FetchMetricDataAndExistingAnomaliesTool method fetchMetric.

/**
   * Fetch metric from thirdeye
   * @param host host name (includes http://)
   * @param port port number
   * @param dataset dataset/collection name
   * @param metric metric name
   * @param startTime start time of requested data in DateTime
   * @param endTime end time of requested data in DateTime
   * @param timeGranularity the time granularity
   * @param dimensions the list of dimensions
   * @param filterJson filters, in JSON
   * @return {dimension-> {DateTime: value}}
   * @throws IOException
   */
public Map<String, Map<Long, String>> fetchMetric(String host, int port, String dataset, String metric, DateTime startTime, DateTime endTime, TimeGranularity timeGranularity, String dimensions, String filterJson, String timezone) throws IOException {
    HttpClient client = HttpClientBuilder.create().build();
    DateTimeZone dateTimeZone = DateTimeZone.forID(timezone);
    startTime = new DateTime(startTime, dateTimeZone);
    endTime = new DateTime(endTime, dateTimeZone);
    // format http GET command
    StringBuilder urlBuilder = new StringBuilder(host + ":" + port + DEFAULT_PATH_TO_TIMESERIES);
    urlBuilder.append(DATASET + EQUALS + dataset + AND);
    urlBuilder.append(METRIC + EQUALS + metric + AND);
    urlBuilder.append(VIEW + EQUALS + DEFAULT_VIEW + AND);
    urlBuilder.append(TIME_START + EQUALS + Long.toString(startTime.getMillis()) + AND);
    urlBuilder.append(TIME_END + EQUALS + Long.toString(endTime.getMillis()) + AND);
    urlBuilder.append(GRANULARITY + EQUALS + timeGranularity.toString() + AND);
    if (dimensions != null || !dimensions.isEmpty()) {
        urlBuilder.append(DIMENSIONS + EQUALS + dimensions + AND);
    }
    if (filterJson != null || !filterJson.isEmpty()) {
        urlBuilder.append(FILTERS + EQUALS + URLEncoder.encode(filterJson, "UTF-8"));
    }
    HttpGet httpGet = new HttpGet(urlBuilder.toString());
    // Execute GET command
    httpGet.addHeader("User-Agent", "User");
    HttpResponse response = client.execute(httpGet);
    LOG.info("Response Code : {}", response.getStatusLine().getStatusCode());
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer content = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        content.append(line);
    }
    Map<String, Map<Long, String>> resultMap = null;
    try {
        JSONObject jsonObject = new JSONObject(content.toString());
        JSONObject timeSeriesData = (JSONObject) jsonObject.get("timeSeriesData");
        JSONArray timeArray = (JSONArray) timeSeriesData.get("time");
        resultMap = new HashMap<>();
        Iterator<String> timeSeriesDataIterator = timeSeriesData.keys();
        while (timeSeriesDataIterator.hasNext()) {
            String key = timeSeriesDataIterator.next();
            if (key.equalsIgnoreCase("time")) {
                continue;
            }
            Map<Long, String> entry = new HashMap<>();
            JSONArray observed = (JSONArray) timeSeriesData.get(key);
            for (int i = 0; i < timeArray.length(); i++) {
                long timestamp = (long) timeArray.get(i);
                String observedValue = observed.get(i).toString();
                entry.put(timestamp, observedValue);
            }
            resultMap.put(key, entry);
        }
    } catch (JSONException e) {
        LOG.error("Unable to resolve JSON string {}", e);
    }
    return resultMap;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) JSONObject(org.json.JSONObject) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map) DimensionMap(com.linkedin.thirdeye.api.DimensionMap)

Example 10 with HttpClient

use of org.apache.http.client.HttpClient in project hadoop by apache.

the class TestWasbRemoteCallHelper method testInvalidContentLengthValue.

/**
   * Test invalid Content-Length value
   * @throws Throwable
   */
// (expected = WasbAuthorizationException.class)
@Test
public void testInvalidContentLengthValue() throws Throwable {
    setupExpectations();
    // set up mocks
    HttpClient mockHttpClient = Mockito.mock(HttpClient.class);
    HttpResponse mockHttpResponse = Mockito.mock(HttpResponse.class);
    Mockito.when(mockHttpClient.execute(Mockito.<HttpGet>any())).thenReturn(mockHttpResponse);
    Mockito.when(mockHttpResponse.getStatusLine()).thenReturn(newStatusLine(200));
    Mockito.when(mockHttpResponse.getFirstHeader("Content-Type")).thenReturn(newHeader("Content-Type", "application/json"));
    Mockito.when(mockHttpResponse.getFirstHeader("Content-Length")).thenReturn(newHeader("Content-Length", "20abc48"));
    // finished setting up mocks
    performop(mockHttpClient);
}
Also used : HttpClient(org.apache.http.client.HttpClient) Test(org.junit.Test)

Aggregations

HttpClient (org.apache.http.client.HttpClient)941 HttpResponse (org.apache.http.HttpResponse)584 HttpGet (org.apache.http.client.methods.HttpGet)429 IOException (java.io.IOException)308 Test (org.junit.Test)275 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)272 HttpPost (org.apache.http.client.methods.HttpPost)212 HttpEntity (org.apache.http.HttpEntity)128 URI (java.net.URI)92 InputStream (java.io.InputStream)81 StringEntity (org.apache.http.entity.StringEntity)74 ArrayList (java.util.ArrayList)69 ClientProtocolException (org.apache.http.client.ClientProtocolException)66 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)61 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)61 InputStreamReader (java.io.InputStreamReader)59 URL (java.net.URL)57 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)53 URISyntaxException (java.net.URISyntaxException)50 MockResponse (com.google.mockwebserver.MockResponse)48