Example usage for org.springframework.web.client RestTemplate setInterceptors

List of usage examples for org.springframework.web.client RestTemplate setInterceptors

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate setInterceptors.

Prototype

public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) 

Source Link

Document

Set the request interceptors that this accessor should use.

Usage

From source file:fi.helsinki.opintoni.config.CoursePageConfiguration.java

@Bean
public RestTemplate coursePageRestTemplate() {
    RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
    restTemplate.setMessageConverters(getConverters());
    restTemplate.setInterceptors(Lists.newArrayList(new LoggingInterceptor()));
    return restTemplate;
}

From source file:com.kixeye.chassis.transport.MetricsTest.java

@Test
public void testMetricsPing() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("admin.enabled", "true");
    properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("admin.hostname", "localhost");

    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.hostname", "localhost");

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
    context.setEnvironment(environment);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(TransportConfiguration.class);
    context.register(MetricsConfiguration.class);

    RestTemplate httpClient = new RestTemplate();

    try {//from   w  w w.ja  va 2  s . c o m
        context.refresh();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe serDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(serDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        ResponseEntity<String> response = httpClient.getForEntity(
                new URI("http://localhost:" + properties.get("admin.port") + "/metrics/ping"), String.class);

        logger.info("Got response: [{}]", response);

        Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
        Assert.assertEquals("pong".trim(), response.getBody().trim());
    } finally {
        context.close();
    }
}

From source file:com.bose.aem.spring.config.ConfigServicePropertySourceLocator.java

private RestTemplate getSecureRestTemplate(ConfigClientProperties client) {
    RestTemplate template = new RestTemplate();
    String password = client.getPassword();
    if (password != null) {
        template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
                new BasicAuthorizationInterceptor(client.getUsername(), password)));
    }//from  w ww.java2s  .c om
    return template;
}

From source file:org.trustedanalytics.user.manageusers.UsersConfig.java

private RestTemplate setAccessToken(RestTemplate restTemplate) {
    ClientHttpRequestInterceptor interceptor = new HeaderAddingHttpInterceptor("Authorization",
            "bearer " + getAccessToken());
    restTemplate.setInterceptors(singletonList(interceptor));

    return restTemplate;
}

From source file:io.fabric8.che.starter.client.WorkspaceClient.java

/**
 * Create workspace on the Che server with given URL.
 * /*from   w w w .  j  a va2  s.c o m*/
 * @param cheServerUrl
 * @param keycloakToken
 * @param name
 * @param stackId
 * @param repo
 * @param branch
 * @return
 * @throws StackNotFoundException
 * @throws IOException
 */
public Workspace createWorkspace(String cheServerUrl, String keycloakToken, String name, String stackId,
        String repo, String branch) throws StackNotFoundException, IOException {
    // The first step is to create the workspace
    String url = CheRestEndpoints.CREATE_WORKSPACE.generateUrl(cheServerUrl);

    name = StringUtils.isBlank(name) ? workspaceHelper.generateName() : name;

    WorkspaceConfig wsConfig = stackClient.getStack(cheServerUrl, stackId, null).getWorkspaceConfig();
    wsConfig.setName(name);
    wsConfig.setDescription(repo + "#" + branch);

    RestTemplate template = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    if (keycloakToken != null) {
        template.setInterceptors(Collections.singletonList(new KeycloakInterceptor(keycloakToken)));
    }

    HttpEntity<WorkspaceConfig> entity = new HttpEntity<WorkspaceConfig>(wsConfig, headers);

    ResponseEntity<Workspace> workspaceResponse = template.exchange(url, HttpMethod.POST, entity,
            Workspace.class);
    Workspace workspace = workspaceResponse.getBody();

    LOG.info("Workspace has been created: {}", workspace);

    return workspace;
}

From source file:org.trustedanalytics.metricsprovider.config.Config.java

protected RestOperations restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    OAuth2Authentication authentication = getAuthentication();
    String token = tokenRetriever.getAuthToken(authentication);
    ClientHttpRequestInterceptor interceptor = new HeaderAddingHttpInterceptor("Authorization",
            "bearer " + token);
    restTemplate.setInterceptors(singletonList(interceptor));
    return restTemplate;
}

From source file:com.pepaproch.gtswsdlclient.TestBase.java

protected RestTemplate getRestAuthenticatedTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    AuthHeaderInterceptor securityTokenInterceptor = new AuthHeaderInterceptor(getAuthTokenProvider());
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    if (interceptors == null) {
        interceptors = new ArrayList<>();
        restTemplate.setInterceptors(interceptors);
    }//  w w w.  j a va2s .  com
    interceptors.add(securityTokenInterceptor);
    List<HttpMessageConverter<?>> list = new ArrayList<>();
    list.add(new MappingJackson2HttpMessageConverter());
    restTemplate.setMessageConverters(list);
    return restTemplate;

}

From source file:com.kixeye.chassis.transport.shared.SharedTest.java

@Test
public void testClassPath() throws Exception {

    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("admin.enabled", "true");
    properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("admin.hostname", "localhost");

    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.hostname", "localhost");

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
    context.setEnvironment(environment);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(TransportConfiguration.class);
    context.register(MetricsConfiguration.class);
    RestTemplate httpClient = new RestTemplate();

    try {/*from  w  w  w  .ja  v a2s.  c o  m*/
        context.refresh();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        ResponseEntity<String> response = httpClient.getForEntity(
                new URI("http://localhost:" + properties.get("admin.port") + "/admin/classpath"), String.class);

        logger.info("Got response: [{}]", response);

        Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
        Assert.assertTrue(response.getBody().length() > 0);
    } finally {
        context.close();
    }
}

From source file:com.kixeye.chassis.transport.shared.SharedTest.java

@Test
public void testHealthcheck() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("admin.enabled", "true");
    properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("admin.hostname", "localhost");

    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.hostname", "localhost");

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
    context.setEnvironment(environment);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(TransportConfiguration.class);
    context.register(MetricsConfiguration.class);

    RestTemplate httpClient = new RestTemplate();

    try {/*  w  w w .ja v a  2 s .c  om*/
        context.refresh();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        ResponseEntity<String> response = httpClient.getForEntity(
                new URI("http://localhost:" + properties.get("admin.port") + "/healthcheck"), String.class);

        logger.info("Got response: [{}]", response);

        Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
        Assert.assertEquals("OK", response.getBody());
    } finally {
        context.close();
    }
}

From source file:com.kixeye.chassis.transport.shared.SharedTest.java

@Test
public void testProperties() throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("admin.enabled", "true");
    properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("admin.hostname", "localhost");

    properties.put("websocket.enabled", "true");
    properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("websocket.hostname", "localhost");

    properties.put("http.enabled", "true");
    properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
    properties.put("http.hostname", "localhost");

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
    context.setEnvironment(environment);
    context.register(PropertySourcesPlaceholderConfigurer.class);
    context.register(TransportConfiguration.class);
    context.register(MetricsConfiguration.class);

    RestTemplate httpClient = new RestTemplate();

    try {//ww w.j av a2  s  . c o  m
        context.refresh();

        httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
            messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
        }
        messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
        httpClient.setMessageConverters(messageConverters);

        ResponseEntity<String> response = httpClient.getForEntity(
                new URI("http://localhost:" + properties.get("admin.port") + "/admin/properties"),
                String.class);

        logger.info("Got response: [{}]", response);

        Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
        Assert.assertTrue(response.getBody().contains("user.dir=" + System.getProperty("user.dir")));
    } finally {
        context.close();
    }
}