Java Code Examples for org.apache.http.client.fluent.Response#returnResponse()

The following examples show how to use org.apache.http.client.fluent.Response#returnResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SoapRawClient.java    From tutorial-soap-spring-boot-cxf with MIT License 6 votes vote down vote up
public SoapRawClientResponse callSoapService(InputStream xmlFile) throws InternalBusinessException {
	SoapRawClientResponse rawSoapResponse = new SoapRawClientResponse();
	
	LOGGER.debug("Calling SoapService with POST on Apache HTTP-Client and configured URL: {}", soapServiceUrl);
	
	try {
		Response httpResponseContainer = Request
				.Post(soapServiceUrl)
				.bodyStream(xmlFile, contentTypeTextXmlUtf8())
				.addHeader("SOAPAction", "\"" + soapAction + "\"")
				.execute();
		
		HttpResponse httpResponse = httpResponseContainer.returnResponse();			
		rawSoapResponse.setHttpStatusCode(httpResponse.getStatusLine().getStatusCode());
		rawSoapResponse.setHttpResponseBody(XmlUtils.parseFileStream2Document(httpResponse.getEntity().getContent()));
		
	} catch (Exception exception) {
		throw new InternalBusinessException("Some Error accured while trying to Call SoapService for test: " + exception.getMessage());
	}		
	return rawSoapResponse;
}
 
Example 2
Source File: InvalidHttpMethodGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRespondWithNotImplemented() throws Exception {
    wireMockRule.stubFor(any(urlEqualTo("/team/my_team"))
            .willReturn(aResponse().withStatus(HttpStatus.SC_NOT_IMPLEMENTED)));


    Request request = Request.Get("http://localhost:8082/test/my_team");

    // A little bit of reflection to set an unknown HTTP method since the fluent API does not allow it.
    Field requestField = request.getClass().getDeclaredField("request");
    requestField.setAccessible(true);
    Field methodField = requestField.get(request).getClass().getDeclaredField("method");
    methodField.setAccessible(true);
    methodField.set(requestField.get(request), "unkown-method");

    Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, returnResponse.getStatusLine().getStatusCode());

    wireMockRule.verify(anyRequestedFor(urlPathEqualTo("/team/my_team")));
}
 
Example 3
Source File: WeightedRoundRobinLoadBalancingTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void call_weighted_lb_multiple_endpoints() throws Exception {
    wireMockRule.stubFor(get(urlEqualTo("/api1")).willReturn(ok()));
    wireMockRule.stubFor(get(urlEqualTo("/api2")).willReturn(ok()));

    Request request = Request.Get("http://localhost:8082/api");

    int calls = 10;

    for(int i = 0 ; i < calls ; i++) {
        Response response = request.execute();
        HttpResponse returnResponse = response.returnResponse();

        assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());
    }

    wireMockRule.verify(3, getRequestedFor(urlPathEqualTo("/api1")));
    wireMockRule.verify(7, getRequestedFor(urlPathEqualTo("/api2")));
}
 
Example 4
Source File: PostContentGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void no_content_without_chunked_encoding_transfer() throws Exception {
    stubFor(post(urlEqualTo("/team/my_team")).willReturn(ok()));

    Request request = Request.Post("http://localhost:8082/test/my_team")
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
            .removeHeaders(HttpHeaders.TRANSFER_ENCODING);

    Response response = request.execute();

    HttpResponse returnResponse = response.returnResponse();
    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    // Set chunk mode in request but returns raw because of the size of the content
    assertEquals(null, returnResponse.getFirstHeader("X-Forwarded-Transfer-Encoding"));

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(0, responseContent.length());

    verify(postRequestedFor(urlEqualTo("/team/my_team"))
            .withoutHeader(HttpHeaders.TRANSFER_ENCODING)
            .withHeader(io.gravitee.common.http.HttpHeaders.CONTENT_TYPE, new EqualToPattern(MediaType.APPLICATION_JSON)));
}
 
Example 5
Source File: PostContentGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void get_no_content_with_chunked_encoding_transfer() throws Exception {
    stubFor(get(urlEqualTo("/team/my_team")).willReturn(ok()));

    Request request = Request.Get("http://localhost:8082/test/my_team")
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
            .removeHeaders(HttpHeaders.TRANSFER_ENCODING);

    Response response = request.execute();

    HttpResponse returnResponse = response.returnResponse();
    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    // Set chunk mode in request but returns raw because of the size of the content
    assertEquals(null, returnResponse.getFirstHeader("X-Forwarded-Transfer-Encoding"));

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(0, responseContent.length());

    verify(getRequestedFor(urlEqualTo("/team/my_team"))
            .withoutHeader(HttpHeaders.TRANSFER_ENCODING)
            .withHeader(io.gravitee.common.http.HttpHeaders.CONTENT_TYPE, new EqualToPattern(MediaType.APPLICATION_JSON)));
}
 
Example 6
Source File: PostContentGatewayTest.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
@Test
public void get_no_content_with_chunked_encoding_transfer_and_content_type() throws Exception {
    stubFor(get(urlEqualTo("/team/my_team")).willReturn(ok()));

    Request request = Request.Get("http://localhost:8082/test/my_team")
            .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);

    Response response = request.execute();

    HttpResponse returnResponse = response.returnResponse();
    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    // Set chunk mode in request but returns raw because of the size of the content
    assertEquals(null, returnResponse.getFirstHeader("X-Forwarded-Transfer-Encoding"));

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(0, responseContent.length());

    verify(getRequestedFor(urlEqualTo("/team/my_team"))
            .withHeader(io.gravitee.common.http.HttpHeaders.CONTENT_TYPE, new EqualToPattern(MediaType.APPLICATION_JSON)));
}
 
Example 7
Source File: AbstractStreamingAnalyticsConnection.java    From streamsx.topology with Apache License 2.0 6 votes vote down vote up
protected boolean delete(String deleteJobUrl) throws IOException {
    boolean rc = false;
    String sReturn = "";

    Request request = Request
            .Delete(deleteJobUrl)
            .addHeader(AUTH.WWW_AUTH_RESP, getAuthorization())
            .useExpectContinue();

    Response response = executor.execute(request);
    HttpResponse hResponse = response.returnResponse();
    int rcResponse = hResponse.getStatusLine().getStatusCode();

    if (HttpStatus.SC_OK == rcResponse) {
        sReturn = EntityUtils.toString(hResponse.getEntity());
        rc = true;
    } else {
        rc = false;
    }
    traceLog.finest("Request: [" + deleteJobUrl + "]");
    traceLog.finest(rcResponse + ": " + sReturn);
    return rc;
}
 
Example 8
Source File: UnfollowRedirectTest.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFollowRedirect() throws Exception {
    wireMockRule.stubFor(get("/redirect").willReturn(permanentRedirect("http://localhost:" + wireMockRule.port() + "/final")));

    HttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();

    Request request = Request.Get("http://localhost:8082/api/redirect");
    Response response = Executor.newInstance(client).execute(request);
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_MOVED_PERMANENTLY, returnResponse.getStatusLine().getStatusCode());

    wireMockRule.verify(1, getRequestedFor(urlPathEqualTo("/redirect")));
    wireMockRule.verify(0, getRequestedFor(urlPathEqualTo("/final")));
}
 
Example 9
Source File: StreamsRestUtils.java    From streamsx.topology with Apache License 2.0 5 votes vote down vote up
private static InputStream rawStreamingGet(Executor executor,
        String auth, String url) throws IOException {
    TRACE.fine("HTTP GET: " + url);
    String accepted =
            ContentType.APPLICATION_OCTET_STREAM.getMimeType()
            + ","
            + "application/x-compressed";
    Request request = Request
            .Get(url)
            .addHeader("accept",accepted)
            .useExpectContinue();
    
    if (null != auth) {
        request = request.addHeader(AUTH.WWW_AUTH_RESP, auth);
    }
    
    Response response = executor.execute(request);
    HttpResponse hResponse = response.returnResponse();
    int rcResponse = hResponse.getStatusLine().getStatusCode();
    
    if (HttpStatus.SC_OK == rcResponse) {
        return hResponse.getEntity().getContent();
    } else {
        // all other errors...
        String httpError = "HttpStatus is " + rcResponse + " for url " + url;
        throw new RESTException(rcResponse, httpError);
    }
}
 
Example 10
Source File: StreamsRestActions.java    From streamsx.topology with Apache License 2.0 5 votes vote down vote up
static Toolkit uploadToolkit(StreamsBuildService connection, File path) throws IOException {
  // Make sure it is a directory
  if (! path.isDirectory()) {
    throw new IllegalArgumentException("The specified toolkit path '" + path.toString() + "' is not a directory.");
  }

  // Make sure it contains toolkit.xml
  File toolkit = new File(path, "toolkit.xml");
  if (! toolkit.isFile()) {
    throw new IllegalArgumentException("The specified toolkit path '" + path.toString() + "' is not a toolkit.");
  }

  String toolkitsURL = connection.getToolkitsURL();
  Request post = Request.Post(toolkitsURL);
  post.addHeader(AUTH.WWW_AUTH_RESP, connection.getAuthorization());
  post.bodyStream(DirectoryZipInputStream.fromPath(path.toPath()), ContentType.create("application/zip"));

  Response response = connection.getExecutor().execute(post);
  HttpResponse httpResponse = response.returnResponse();
  int statusCode = httpResponse.getStatusLine().getStatusCode();
  // TODO The API is supposed to return CREATED, but there is a bug and it
  // returns OK.  When the bug is fixed, change this to accept only OK
  if (statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_OK) {
    String message = EntityUtils.toString(httpResponse.getEntity());
    throw RESTException.create(statusCode, message);
  }
  
  HttpEntity entity = httpResponse.getEntity();
  try (Reader r = new InputStreamReader(entity.getContent())) {
    JsonObject jresponse = new Gson().fromJson(r, JsonObject.class);
    EntityUtils.consume(entity);
    List<Toolkit> toolkitList = Toolkit.createToolkitList(connection, jresponse);

    // We expect a list of zero or one element.
    if (toolkitList.size() == 0) {
      return null;
    }
    return toolkitList.get(0);
  }
}
 
Example 11
Source File: OrionConnector.java    From kurento-tutorial-java with Apache License 2.0 5 votes vote down vote up
private <T> HttpResponse checkResponse(Response response) {
  HttpResponse httpResponse;
  try {
    httpResponse = response.returnResponse();
  } catch (IOException e) {
    throw new OrionConnectorException("Could not obtain HTTP response", e);
  }

  if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    throw new OrionConnectorException(
        "Failed with HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
  }
  return httpResponse;
}