Package com.ning.http.client

Examples of com.ning.http.client.Response


        for (int i=0; i < 10000; i++) {
            b.append("======");
        }
        b.append("message");

        Response r = c.preparePost(targetUrl + "/suspend").setContentLength(b.toString().length()).setBody(b.toString()).execute().get();
        assertEquals(r.getStatusCode(), 200);

        l.await(5, TimeUnit.SECONDS);

        assertEquals(response.get().getStatusCode(), 200);
        assertEquals(response.get().getResponseBody().trim(), b.toString());
View Full Code Here


    }

    @Override
    public Response prepareResponse(final HttpResponseStatus status, final HttpResponseHeaders headers, final List<HttpResponseBodyPart> bodyParts) {
        final HttpResponseBodyPart bodyPart = bodyParts.get(0);
        return new Response() {
            @Override
            public int getStatusCode() {
                return status.getStatusCode();
            }
View Full Code Here

        final Request request = httpclient.prepareGet(uriBuilder.build().toString()).build();
        LOG.debug("API Request {} {}", request.getMethod(), request.getUrl());
        final Future<Response> f = httpclient.executeRequest(request);

        final Response r;
        try {
            r = f.get();
        } catch (InterruptedException | ExecutionException e) {
            LOG.error("Unable to fetch inputs from master: ", e);
            return Collections.emptyList();
        }

        if (r.getStatusCode() != 200) {
            throw new RuntimeException("Expected HTTP response [200] for list of persisted input but got [" + r.getStatusCode() + "].");
        }
        final String responseBody = r.getResponseBody();
        PersistedInputsResponse persistedInputsResponse = mapper.readValue(responseBody,
                PersistedInputsResponse.class);
        return persistedInputsResponse.inputs;
    }
View Full Code Here

        Future<Response> f = httpclient.preparePost(uriBuilder.build().toString())
                .setBody(json)
                .execute();

        Response r = f.get();

        RegisterInputResponse response = mapper.readValue(r.getResponseBody(), RegisterInputResponse.class);

        // Set the ID that was generated in the server as persist ID of this input.
        input.setPersistId(response.persistId);

        if (r.getStatusCode() != 201) {
            throw new RuntimeException("Expected HTTP response [201] for input registration but got [" + r.getStatusCode() + "].");
        }

        return response;
    }
View Full Code Here

    public void unregisterInCluster(MessageInput input) throws ExecutionException, InterruptedException, IOException {
        final UriBuilder uriBuilder = UriBuilder.fromUri(serverUrl);
        uriBuilder.path("/system/radios/" + serverStatus.getNodeId().toString() + "/inputs/" + input.getPersistId());

        Future<Response> f = httpclient.prepareDelete(uriBuilder.build().toString()).execute();
        Response r = f.get();
        if (r.getStatusCode() != 204) {
            throw new RuntimeException("Expected HTTP response [204] for input unregistration but got [" + r.getStatusCode() + "].");
        }
    }
View Full Code Here

        Future<Response> f = client.preparePut(uriBuilder.build().toString())
                .setBody(rootNode.toString())
                .execute();

        Response r = f.get();

        if (r.getStatusCode() != 200) {
            throw new RuntimeException("Expected ping HTTP response [200] but got [" + r.getStatusCode() + "].");
        }
    }
View Full Code Here

    public void call(final Stream stream, final AlertCondition.CheckResult result) throws AlarmCallbackException {
        final Map<String, Object> event = Maps.newHashMap();
        event.put("stream", stream);
        event.put("check_result", result);

        final Response r;
        try {
            final String body = objectMapper.writeValueAsString(event);
            final URL url = new URL(configuration.getString(CK_URL));
            r = asyncHttpClient.preparePost(url.toString())
                    .setBody(body)
                    .execute().get();
        } catch (JsonProcessingException e) {
            throw new AlarmCallbackException("Unable to serialize alarm", e);
        } catch (MalformedURLException e) {
            throw new AlarmCallbackException("Malformed URL", e);
        } catch (IOException | InterruptedException | ExecutionException e) {
            throw new AlarmCallbackException(e.getMessage(), e);
        }

        if (r.getStatusCode() != 200) {
            throw new AlarmCallbackException("Expected ping HTTP response [200] but got [" + r.getStatusCode() + "].");
        }
    }
View Full Code Here

            }
            for (F.Tuple<ListenableFuture<Response>, Node> requestAndNode : requests) {
                final ListenableFuture<Response> request = requestAndNode._1;
                final Node node = requestAndNode._2;
                try {
                    final Response response = request.get(timeoutValue, timeoutUnit);
                    node.touch();
                    results.put(node, deserializeJson(response, responseClass));
                } catch (InterruptedException e) {
                    LOG.error("API call Interrupted", e);
                    node.markFailure();
View Full Code Here

                        LOG.info("Checking Elasticsearch HTTP API at http://{}:9200/", hostAndPort.getHostText());

                        try {
                            // Try the HTTP API endpoint
                            final ListenableFuture<Response> future = httpClient.prepareGet("http://" + hostAndPort.getHostText() + ":9200/_nodes").execute();
                            final Response response = future.get();

                            final JsonNode resultTree = new ObjectMapper().readTree(response.getResponseBody());
                            final String clusterName = resultTree.get("cluster_name").textValue();
                            final JsonNode nodesList = resultTree.get("nodes");

                            final Iterator<String> nodes = nodesList.fieldNames();
                            while (nodes.hasNext()) {
View Full Code Here

                expectedResponseCodes.add(Http.Status.OK);
            }

            try {
                // TODO implement streaming responses
                Response response = requestBuilder.execute().get(timeoutValue, timeoutUnit);

                target.touch();

                // TODO this is wrong, shouldn't it accept some callback instead of throwing an exception?
                if (!expectedResponseCodes.contains(response.getStatusCode())) {
                    throw new APIException(request, response);
                }

                // TODO: once we switch to jackson we can take the media type into account automatically
                final MediaType responseContentType;
                if (response.getContentType() == null) {
                    responseContentType = MediaType.JSON_UTF_8;
                } else {
                    responseContentType = MediaType.parse(response.getContentType());
                }

                if (!responseContentType.is(mediaType.withoutParameters())) {
                    LOG.warn("We said we'd accept {} but got {} back, let's see how that's going to work out...", mediaType, responseContentType);
                }
                if (responseClass.equals(String.class)) {
                    return responseClass.cast(response.getResponseBody("UTF-8"));
                }

                if (expectedResponseCodes.contains(response.getStatusCode())
                        || (response.getStatusCode() >= 200 && response.getStatusCode() < 300)) {
                    T result;
                    try {
                        if (response.getResponseBody().isEmpty()) {
                            return null;
                        }

                        if (responseContentType.is(MediaType.JSON_UTF_8.withoutParameters())) {
                            result = deserializeJson(response, responseClass);
                        } else {
                            LOG.error("Don't know how to deserialize objects with content in {}, expected {}, failing.", responseContentType, mediaType);
                            throw new APIException(request, response);
                        }

                        if (result == null) {
                            throw new APIException(request, response);
                        }

                        return result;
                    } catch (Exception e) {
                        LOG.error("Caught Exception while deserializing JSON request: ", e);
                        LOG.debug("Response from backend was: " + response.getResponseBody("UTF-8"));

                        throw new APIException(request, response, e);
                    }
                } else {
                    return null;
View Full Code Here

TOP

Related Classes of com.ning.http.client.Response

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.