private void setRestTemplateParams(Map<String, Object> clientParams) {
    int maxConnPerRoute = DEFAULT_MAX_CONN_PER_ROUTE;
    int maxConnTotal = DEFAULT_MAX_CONN_TOTAL;
    int connectTimeoutInMs = DEFAULT_CONNECT_TIMEOUT_IN_MS;

    if (clientParams.containsKey(DEFAULT_URI_VARIABLES.value)) {
      restTemplate.setDefaultUriVariables(
          (Map<String, ?>) clientParams.get(DEFAULT_URI_VARIABLES.value));
    }
    if (clientParams.containsKey(ERROR_HANDLER.value)) {
      restTemplate.setErrorHandler((ResponseErrorHandler) clientParams.get(ERROR_HANDLER.value));
    }
    if (clientParams.containsKey(MESSAGE_CONVERTERS.value)) {
      restTemplate.setMessageConverters(
          (List<HttpMessageConverter<?>>) clientParams.get(MESSAGE_CONVERTERS.value));
    }
    if (clientParams.containsKey(URI_TEMPLATE_HANDLER.value)) {
      restTemplate.setUriTemplateHandler(
          (UriTemplateHandler) clientParams.get(URI_TEMPLATE_HANDLER.value));
    }
    if (clientParams.containsKey(INTERCEPTORS.value)) {
      restTemplate.setInterceptors(
          (List<ClientHttpRequestInterceptor>) clientParams.get(INTERCEPTORS.value));
    }
    if (clientParams.containsKey(MAX_CONN_PER_ROUTE.value)) {
      Object value = clientParams.get(MAX_CONN_PER_ROUTE.value);
      if (value instanceof String) maxConnPerRoute = Integer.parseInt((String) value);
      else maxConnPerRoute = (int) value;
    }
    if (clientParams.containsKey(MAX_CONN_TOTAL.value)) {
      Object value = clientParams.get(MAX_CONN_TOTAL.value);
      if (value instanceof String) maxConnTotal = Integer.parseInt((String) value);
      else maxConnTotal = (int) value;
    }
    if (clientParams.containsKey(CONNECT_TIMEOUT_IN_MS.value)) {
      Object value = clientParams.get(CONNECT_TIMEOUT_IN_MS.value);
      if (value instanceof String) connectTimeoutInMs = Integer.parseInt((String) value);
      else connectTimeoutInMs = (int) value;
    }

    if (clientParams.containsKey(REQUEST_FACTORY.value)) {
      restTemplate.setRequestFactory(
          (ClientHttpRequestFactory) clientParams.get(REQUEST_FACTORY.value));
    }
    if (!clientParams.containsKey(REQUEST_FACTORY.value)
        && containsAnyRequestFactoryParam(clientParams)) {
      restTemplate.setRequestFactory(
          getRequestFactory(maxConnPerRoute, maxConnTotal, connectTimeoutInMs));
    } else if (clientParams.containsKey(REQUEST_FACTORY.value)
        && containsAnyRequestFactoryParam(clientParams)) {
      throw new IllegalArgumentException(
          "Parameters max_conn_total, max_conn_per_route and connect_timeout cannot be set if "
              + "request_factory parameter presents. You must configure these parameters in your request_factory entity.");
    }
  }
    @Override
    protected ItemGridModel[] doInBackground(Integer... args) {

      try {

        RestTemplate restTemplate = new RestTemplate();

        HttpComponentsClientHttpRequestFactory httpRequestFactory =
            new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout(10 * 1000);
        httpRequestFactory.setReadTimeout(10 * 1000);
        restTemplate.setRequestFactory(httpRequestFactory);

        String url = Constant.URL_SERVICE;

        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        return restTemplate.getForObject(url, ItemGridModel[].class);
      } catch (ResourceAccessException e) {
        Log.e(Constant.TAG, "Error update grid", e);
      } catch (Exception e) {
        Log.e(Constant.TAG, "Error update grid", e);
      }
      return null;
    }
  private HttpHeaders setUpMocksToCaptureSentHeaders(RestTemplate restTemplate) throws IOException {

    HttpHeaders headers = new HttpHeaders();

    ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
    ClientHttpRequest clientRequest = mock(ClientHttpRequest.class);
    when(clientRequest.getHeaders()).thenReturn(headers);

    when(requestFactory.createRequest(any(URI.class), any(HttpMethod.class)))
        .thenReturn(clientRequest);

    ClientHttpResponse response = mock(ClientHttpResponse.class);
    when(response.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
    when(response.getStatusText()).thenReturn("Not Found");
    when(response.getBody()).thenReturn(new ByteArrayInputStream(new byte[0]));

    HttpHeaders responseHeaders = new HttpHeaders();
    when(response.getHeaders()).thenReturn(responseHeaders);

    when(clientRequest.execute()).thenReturn(response);

    restTemplate.setRequestFactory(requestFactory);

    return headers;
  }
 @Bean
 public RestTemplate restTemplate() {
   RestTemplate restTemplate = new RestTemplate();
   restTemplate.setRequestFactory(httpRequestFactory());
   restTemplate.setMessageConverters(messageConverters());
   return restTemplate;
 }
 /**
  * Upload an app using the legacy API used for older vcap versions like Micro Cloud Foundry 1.1
  * and older As of Micro Cloud Foundry 1.2 and for any recent CloudFoundry.com deployment the
  * current method of setting the content type as JSON works fine.
  *
  * @param path app path
  * @param entity HttpEntity for the payload
  * @param appName name of app
  * @throws HttpServerErrorException
  */
 private void uploadAppUsingLegacyApi(String path, HttpEntity<?> entity, String appName)
     throws HttpServerErrorException {
   RestTemplate legacyRestTemplate = new RestTemplate();
   legacyRestTemplate.setRequestFactory(this.getRestTemplate().getRequestFactory());
   legacyRestTemplate.setErrorHandler(new ErrorHandler());
   legacyRestTemplate.setMessageConverters(getLegacyMessageConverters());
   legacyRestTemplate.put(path, entity, appName);
 }
 public void customize(RestTemplate restTemplate) {
   restTemplate.setRequestFactory(
       new HttpComponentsClientHttpRequestFactory() {
         @Override
         protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
           HttpClientContext context = HttpClientContext.create();
           Builder builder =
               RequestConfig.custom()
                   .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
                   .setAuthenticationEnabled(false)
                   .setRedirectsEnabled(false);
           context.setRequestConfig(builder.build());
           return context;
         }
       });
 }
 @Before
 public void setUp() throws Exception {
   ((RestTemplate) restOperations)
       .setRequestFactory(new IntegrationTestUtils.StatelessRequestFactory());
   ClientCredentialsResourceDetails clientCredentials =
       getClientCredentialsResource(
           new String[] {"scim.write"},
           testAccounts.getAdminClientId(),
           testAccounts.getAdminClientSecret());
   client = IntegrationTestUtils.getClientCredentialsTempate(clientCredentials);
   user =
       createUser(
           new RandomValueStringGenerator().generate(),
           "openiduser",
           "openidlast",
           "test@openid,com",
           true);
 }
Exemple #8
0
  public RestOperations createRestTemplate() {
    RestTemplate client = new RestTemplate();
    client.setRequestFactory(
        new SimpleClientHttpRequestFactory() {
          @Override
          protected void prepareConnection(HttpURLConnection connection, String httpMethod)
              throws IOException {
            super.prepareConnection(connection, httpMethod);
            connection.setInstanceFollowRedirects(false);
          }
        });
    client.setErrorHandler(
        new ResponseErrorHandler() {
          // Pass errors through in response entity for status code analysis
          public boolean hasError(ClientHttpResponse response) throws IOException {
            return false;
          }

          public void handleError(ClientHttpResponse response) throws IOException {}
        });
    return client;
  }
  public RestTemplate getRestTemplate() {
    RestTemplate client = new RestTemplate();
    CommonsClientHttpRequestFactory requestFactory =
        new CommonsClientHttpRequestFactory() {
          @Override
          protected void postProcessCommonsHttpMethod(HttpMethodBase httpMethod) {
            httpMethod.setFollowRedirects(false);
            // We don't want stateful conversations for this test
            httpMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
          }
        };
    client.setRequestFactory(requestFactory);
    client.setErrorHandler(
        new ResponseErrorHandler() {
          // Pass errors through in response entity for status code analysis
          public boolean hasError(ClientHttpResponse response) throws IOException {
            return false;
          }

          public void handleError(ClientHttpResponse response) throws IOException {}
        });
    return client;
  }
 /**
  * Constructor using custom client request factory.
  *
  * @param requestFactory the custom request factory.
  */
 public HttpMessageSender(ClientHttpRequestFactory requestFactory) {
   restTemplate = new RestTemplate();
   restTemplate.setRequestFactory(requestFactory);
 }
 /** Default constructor. */
 public HttpMessageSender() {
   restTemplate = new RestTemplate();
   restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
 }
 private void configureCloudFoundryRequestFactory(RestTemplate restTemplate) {
   ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory();
   restTemplate.setRequestFactory(new CloudFoundryClientHttpRequestFactory(requestFactory));
 }
 public void updateHttpProxyConfiguration(HttpProxyConfiguration httpProxyConfiguration) {
   ClientHttpRequestFactory requestFactory = restUtil.createRequestFactory(httpProxyConfiguration);
   restTemplate.setRequestFactory(requestFactory);
   configureCloudFoundryRequestFactory(restTemplate);
 }
 public SpringBasedHttpClient() {
   clientParams = new HashMap<>();
   restTemplate = new RestTemplate();
   restTemplate.setRequestFactory(getRequestFactory());
   restTemplate.setErrorHandler(new AllowAllCodesResponseErrorHandler());
 }