http work
This commit is contained in:
parent
a71bba8654
commit
7d17c4dd9d
@ -1,6 +1,9 @@
|
|||||||
package org.baeldung.client;
|
package org.baeldung.client;
|
||||||
|
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.params.HttpConnectionParams;
|
||||||
|
import org.apache.http.params.HttpParams;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -34,7 +37,22 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
|
|||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
final HttpHost host = new HttpHost("localhost", 8080, "http");
|
final HttpHost host = new HttpHost("localhost", 8080, "http");
|
||||||
restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactoryBasicAuth(host));
|
final HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
|
||||||
|
restTemplate = new RestTemplate(requestFactory);
|
||||||
|
|
||||||
|
final int timeout = 5;
|
||||||
|
final HttpClient httpClient = requestFactory.getHttpClient();
|
||||||
|
// - note: timeout via raw String parameters
|
||||||
|
// httpClient.getParams().setParameter("http.connection.timeout", timeout * 1000);
|
||||||
|
// httpClient.getParams().setParameter("http.socket.timeout", timeout * 1000);
|
||||||
|
|
||||||
|
// httpClient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
|
||||||
|
// httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
|
||||||
|
|
||||||
|
// - note: timeout via the API
|
||||||
|
final HttpParams httpParams = httpClient.getParams();
|
||||||
|
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
||||||
|
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package org.baeldung.client;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.ClientProtocolException;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.baeldung.client.spring.ClientConfig;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class RawClientLiveTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
// tests
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
|
||||||
|
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
|
||||||
|
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
|
||||||
|
httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user", "userPass"));
|
||||||
|
|
||||||
|
final HttpResponse response = httpClient.execute(new HttpGet("http://localhost:8080/spring-security-rest-template/api/foos/1"));
|
||||||
|
final int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
System.out.println(statusCode);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user