/** * Overridden to expose an {@link InterceptingClientHttpRequestFactory} * if necessary. * @see #getInterceptors() */ @Override public ClientHttpRequestFactory getRequestFactory() { List<ClientHttpRequestInterceptor> interceptors = getInterceptors(); if (!CollectionUtils.isEmpty(interceptors)) { ClientHttpRequestFactory factory = this.interceptingRequestFactory; if (factory == null) { factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors); this.interceptingRequestFactory = factory; } return factory; } else { return super.getRequestFactory(); } }
@Override protected ClientHttpRequestFactory createRequestFactory() { HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setBufferRequestBody(false); return new InterceptingClientHttpRequestFactory(requestFactory, null); }
/** * Overridden to expose an {@link InterceptingClientHttpRequestFactory} * if necessary. * @see #getInterceptors() */ @Override public ClientHttpRequestFactory getRequestFactory() { List<ClientHttpRequestInterceptor> interceptors = getInterceptors(); if (!CollectionUtils.isEmpty(interceptors)) { ClientHttpRequestFactory factory = this.interceptingRequestFactory; if (factory == null) { factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors); this.interceptingRequestFactory = factory; } return factory; } else { return super.getRequestFactory(); } }
@Test public void changeBody() throws Exception { final byte[] changedBody = "Foo".getBytes(); ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { return execution.execute(request, changedBody); } }; requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor)); ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); request.execute(); assertTrue(Arrays.equals(changedBody, requestMock.body.toByteArray())); }
@Test public void changeURI() throws Exception { final URI changedUri = new URI("http://example.com/2"); ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { return execution.execute(new HttpRequestWrapper(request) { @Override public URI getURI() { return changedUri; } }, body); } }; requestFactoryMock = new RequestFactoryMock() { @Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { assertEquals(changedUri, uri); return super.createRequest(uri, httpMethod); } }; requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor)); ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); request.execute(); }
@Test public void basic() throws Exception { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new NoOpInterceptor()); interceptors.add(new NoOpInterceptor()); interceptors.add(new NoOpInterceptor()); requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, interceptors); ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); ClientHttpResponse response = request.execute(); assertTrue(((NoOpInterceptor) interceptors.get(0)).invoked); assertTrue(((NoOpInterceptor) interceptors.get(1)).invoked); assertTrue(((NoOpInterceptor) interceptors.get(2)).invoked); assertTrue(requestMock.executed); assertSame(responseMock, response); }
@Test public void noExecution() throws Exception { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { return responseMock; } }); interceptors.add(new NoOpInterceptor()); requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, interceptors); ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); ClientHttpResponse response = request.execute(); assertFalse(((NoOpInterceptor) interceptors.get(1)).invoked); assertFalse(requestMock.executed); assertSame(responseMock, response); }
@Test public void changeMethod() throws Exception { final HttpMethod changedMethod = HttpMethod.POST; ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { return execution.execute(new HttpRequestWrapper(request) { @Override public HttpMethod getMethod() { return changedMethod; } }, body); } }; requestFactoryMock = new RequestFactoryMock() { @Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { assertEquals(changedMethod, httpMethod); return super.createRequest(uri, httpMethod); } }; requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor)); ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); request.execute(); }
@Test public void changeHeaders() throws Exception { final String headerName = "Foo"; final String headerValue = "Bar"; final String otherValue = "Baz"; ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpRequestWrapper wrapper = new HttpRequestWrapper(request); wrapper.getHeaders().add(headerName, otherValue); return execution.execute(wrapper, body); } }; requestMock = new RequestMock() { @Override public ClientHttpResponse execute() throws IOException { List<String> headerValues = getHeaders().get(headerName); assertEquals(2, headerValues.size()); assertEquals(headerValue, headerValues.get(0)); assertEquals(otherValue, headerValues.get(1)); return super.execute(); } }; requestMock.getHeaders().add(headerName, headerValue); requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor)); ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); request.execute(); }
@Test // SPR-8809 public void interceptor() throws Exception { final String headerName = "MyHeader"; final String headerValue = "MyValue"; ClientHttpRequestInterceptor interceptor = (request, body, execution) -> { request.getHeaders().add(headerName, headerValue); return execution.execute(request, body); }; InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory( createRequestFactory(), Collections.singletonList(interceptor)); ClientHttpResponse response = null; try { ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.GET); response = request.execute(); assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode()); HttpHeaders responseHeaders = response.getHeaders(); assertEquals("Custom header invalid", headerValue, responseHeaders.getFirst(headerName)); } finally { if (response != null) { response.close(); } } }
@Bean public ClientHttpRequestFactory clientHttpRequestFactory() { List<ClientHttpRequestInterceptor> interceptors = Arrays .asList(getSecurityInterceptor()); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy = this.properties.getRemote().getProxy(); if (proxy.getHost() != null && proxy.getPort() != null) { requestFactory.setProxy(new java.net.Proxy(Type.HTTP, new InetSocketAddress(proxy.getHost(), proxy.getPort()))); } return new InterceptingClientHttpRequestFactory(requestFactory, interceptors); }
/** * Initialize the internal restTemplate instance */ @PostConstruct protected void init() { logger.debug("Create the RestTemplate instance that will be wrapped"); makeRestTemplateWithCustomObjectMapper(restTemplate); logger.debug("Set interceptor for authentication"); List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList(formLoginAuthenticationCsrfTokenInterceptor); restTemplate.setRequestFactory(new InterceptingClientHttpRequestFactory(restTemplate.getRequestFactory(), interceptors)); }
@Override public ClientHttpRequestFactory getRequestFactory() { ClientHttpRequestFactory delegate = super.getRequestFactory(); if (!CollectionUtils.isEmpty(getInterceptors())) { return new InterceptingClientHttpRequestFactory(delegate, getInterceptors()); } else { return delegate; } }
private RamlRestTemplate(RamlChecker ramlChecker, boolean notSending, ReportStore reportStore, ClientHttpRequestFactory requestFactory) { this.ramlChecker = ramlChecker; this.notSending = notSending; this.reportStore = reportStore; this.originalRequestFactory = requestFactory; final RamlRequestInterceptor interceptor = new RamlRequestInterceptor(ramlChecker, notSending, reportStore); setRequestFactory(new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(requestFactory), Collections.<ClientHttpRequestInterceptor>singletonList(interceptor))); }
private RamlRestTemplate(RamlChecker ramlChecker, boolean notSending, ReportStore reportStore, ClientHttpRequestFactory requestFactory) { this.ramlChecker = ramlChecker; this.notSending = notSending; this.reportStore = reportStore; this.originalRequestFactory = requestFactory; final RamlRequestInterceptor interceptor = new RamlRequestInterceptor(ramlChecker, notSending, reportStore); setRequestFactory(new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(requestFactory), Collections.<ClientHttpRequestInterceptor>singletonList(interceptor))); }
/** * Overridden to expose an {@link InterceptingClientHttpRequestFactory} * if necessary. * @see #getInterceptors() */ @Override public ClientHttpRequestFactory getRequestFactory() { List<ClientHttpRequestInterceptor> interceptors = getInterceptors(); if (!CollectionUtils.isEmpty(interceptors)) { ClientHttpRequestFactory factory = this.interceptingRequestFactory; if (factory == null) { factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors); this.interceptingRequestFactory = factory; } return factory; } else { return super.getRequestFactory(); } }
/** * Overridden to expose an {@link InterceptingClientHttpRequestFactory} * if necessary. * @see #getInterceptors() */ @Override public ClientHttpRequestFactory getRequestFactory() { List<ClientHttpRequestInterceptor> interceptors = getInterceptors(); if (!CollectionUtils.isEmpty(interceptors)) { ClientHttpRequestFactory factory = this.interceptingRequestFactory; if (factory == null) { factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors); this.interceptingRequestFactory = factory; } return factory; } else { return super.getRequestFactory(); } }
/** * Init */ @PostConstruct protected void init() { restTemplateForAuthenticationFlow = new CookieStoreRestTemplate(); cookieStore = restTemplateForAuthenticationFlow.getCookieStore(); logger.debug("Inject cookie store used in the rest template for authentication flow into the authRestTemplate so that they will match"); authRestTemplate.restTemplate.setCookieStoreAndUpdateRequestFactory(cookieStore); List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList(new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { if (latestCsrfToken != null) { // At the beginning of auth flow, there's no token yet injectCsrfTokenIntoHeader(request, latestCsrfToken); } return execution.execute(request, body); } }); restTemplateForAuthenticationFlow.setRequestFactory(new InterceptingClientHttpRequestFactory(restTemplateForAuthenticationFlow.getRequestFactory(), interceptors)); }
public RestService(String url, String username, String password) { this.url = url; template = new RestTemplate(); List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); if (username != null) { interceptors.add(new BasicAuthenticationInterceptor(username, password)); } interceptors.add(new LoggingInterceptor()); template.setRequestFactory( new InterceptingClientHttpRequestFactory(template.getRequestFactory(), interceptors)); prismContext = ProxyCreator.getProxy(PrismContext.class, () -> { try { PrismContextFactory factory = new MidPointPrismContextFactory() { @Override protected void registerExtensionSchemas(SchemaRegistryImpl schemaRegistry) throws SchemaException, FileNotFoundException { super.registerExtensionSchemas(schemaRegistry); RestService.this.registerExtensionSchemas(schemaRegistry); } }; return factory.createPrismContext(); } catch (SchemaException | FileNotFoundException ex) { throw new NinjaException("Couldn't load prism context", ex); } }); }