http client configuration work
This commit is contained in:
parent
a830caca39
commit
1e7f8dc2b0
@ -1,5 +1,4 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.baeldung</groupId>
|
<groupId>org.baeldung</groupId>
|
||||||
<artifactId>spring-security-rest-template</artifactId>
|
<artifactId>spring-security-rest-template</artifactId>
|
||||||
@ -94,17 +93,17 @@
|
|||||||
|
|
||||||
<!-- http -->
|
<!-- http -->
|
||||||
|
|
||||||
<!-- <dependency> -->
|
<!-- <dependency> -->
|
||||||
<!-- <groupId>org.apache.httpcomponents</groupId> -->
|
<!-- <groupId>org.apache.httpcomponents</groupId> -->
|
||||||
<!-- <artifactId>fluent-hc</artifactId> -->
|
<!-- <artifactId>fluent-hc</artifactId> -->
|
||||||
<!-- <version>4.2.5</version> -->
|
<!-- <version>4.2.5</version> -->
|
||||||
<!-- <exclusions> -->
|
<!-- <exclusions> -->
|
||||||
<!-- <exclusion> -->
|
<!-- <exclusion> -->
|
||||||
<!-- <artifactId>commons-logging</artifactId> -->
|
<!-- <artifactId>commons-logging</artifactId> -->
|
||||||
<!-- <groupId>commons-logging</groupId> -->
|
<!-- <groupId>commons-logging</groupId> -->
|
||||||
<!-- </exclusion> -->
|
<!-- </exclusion> -->
|
||||||
<!-- </exclusions> -->
|
<!-- </exclusions> -->
|
||||||
<!-- </dependency> -->
|
<!-- </dependency> -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
@ -154,6 +153,31 @@
|
|||||||
<version>${guava.version}</version>
|
<version>${guava.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- logging -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
<!-- <scope>runtime</scope> -->
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
|
||||||
|
</dependency>
|
||||||
|
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>log4j-over-slf4j</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -269,7 +293,7 @@
|
|||||||
|
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<org.slf4j.version>1.7.5</org.slf4j.version>
|
<org.slf4j.version>1.7.5</org.slf4j.version>
|
||||||
<logback.version>1.0.11</logback.version>
|
<logback.version>1.0.11</logback.version> <!-- do not upgrade - see http://jira.qos.ch/browse/LOGBACK-851 -->
|
||||||
|
|
||||||
<!-- various -->
|
<!-- various -->
|
||||||
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
|
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package org.baeldung.web.controller;
|
||||||
|
|
||||||
|
import org.baeldung.web.dto.Bar;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(value = "/bars")
|
||||||
|
public class BarController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationEventPublisher eventPublisher;
|
||||||
|
|
||||||
|
public BarController() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||||
|
@ResponseBody
|
||||||
|
public Bar findOne(@PathVariable("id") final Long id) {
|
||||||
|
return new Bar();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -30,10 +30,4 @@ public class FooController {
|
|||||||
return new Foo();
|
return new Foo();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/{id}/unsecured", method = RequestMethod.GET)
|
|
||||||
@ResponseBody
|
|
||||||
public Foo findOneUnsecured(@PathVariable("id") final Long id) {
|
|
||||||
return new Foo();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,14 @@
|
|||||||
|
package org.baeldung.web.dto;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
public class Bar implements Serializable {
|
||||||
|
|
||||||
|
public Bar() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,7 +7,6 @@
|
|||||||
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
|
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
|
||||||
|
|
||||||
<http use-expressions="true">
|
<http use-expressions="true">
|
||||||
<intercept-url pattern="/**" access="isAuthenticated()" />
|
|
||||||
|
|
||||||
<http-basic />
|
<http-basic />
|
||||||
|
|
||||||
@ -21,4 +20,6 @@
|
|||||||
</authentication-provider>
|
</authentication-provider>
|
||||||
</authentication-manager>
|
</authentication-manager>
|
||||||
|
|
||||||
|
<global-method-security pre-post-annotations="enabled" />
|
||||||
|
|
||||||
</beans:beans>
|
</beans:beans>
|
@ -1,11 +1,15 @@
|
|||||||
package org.baeldung.client;
|
package org.baeldung.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.ClientProtocolException;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.params.ClientPNames;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.params.CoreConnectionPNames;
|
||||||
import org.apache.http.params.HttpConnectionParams;
|
import org.apache.http.params.HttpConnectionParams;
|
||||||
import org.apache.http.params.HttpParams;
|
import org.apache.http.params.HttpParams;
|
||||||
import org.baeldung.client.spring.ClientConfig;
|
import org.baeldung.client.spring.ClientConfig;
|
||||||
@ -25,21 +29,39 @@ public class RawClientLiveTest {
|
|||||||
public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
|
public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
|
||||||
final DefaultHttpClient httpClient = new DefaultHttpClient();
|
final DefaultHttpClient httpClient = new DefaultHttpClient();
|
||||||
|
|
||||||
final int timeout = 5; // seconds
|
final int timeout = 20; // seconds
|
||||||
final HttpParams httpParams = httpClient.getParams();
|
final HttpParams httpParams = httpClient.getParams();
|
||||||
// - note: timeout via raw String parameters
|
configureViaRawApi(timeout, httpParams);
|
||||||
// httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
|
// configureViaHighLevelApi(timeout, httpParams);
|
||||||
// httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
|
|
||||||
// httpParams.setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
|
|
||||||
// httpParams.setParameter("http.protocol.head-body-timeout", timeout * 1000);
|
|
||||||
|
|
||||||
// - note: timeout via the API
|
final HttpGet getMethod = new HttpGet("http://localhost:8080/spring-security-rest-template/api/bars/1");
|
||||||
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
|
||||||
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
|
||||||
|
|
||||||
final HttpResponse response = httpClient.execute(new HttpGet("http://localhost:8080/spring-security-rest-template/api/foos/1/unsecured"));
|
final int hardTimeout = 5; // seconds
|
||||||
final int statusCode = response.getStatusLine().getStatusCode();
|
final TimerTask task = new TimerTask() {
|
||||||
System.out.println(statusCode);
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (getMethod != null) {
|
||||||
|
getMethod.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new Timer(true).schedule(task, hardTimeout * 1000);
|
||||||
|
|
||||||
|
final HttpResponse response = httpClient.execute(getMethod);
|
||||||
|
System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// util
|
||||||
|
|
||||||
|
final void configureViaHighLevelApi(final int timeout, final HttpParams httpParams) {
|
||||||
|
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
||||||
|
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
||||||
|
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
final void configureViaRawApi(final int timeout, final HttpParams httpParams) {
|
||||||
|
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
|
||||||
|
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
|
||||||
|
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user