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"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>spring-security-rest-template</artifactId>
|
||||
@ -94,17 +93,17 @@
|
||||
|
||||
<!-- http -->
|
||||
|
||||
<!-- <dependency> -->
|
||||
<!-- <groupId>org.apache.httpcomponents</groupId> -->
|
||||
<!-- <artifactId>fluent-hc</artifactId> -->
|
||||
<!-- <version>4.2.5</version> -->
|
||||
<!-- <exclusions> -->
|
||||
<!-- <exclusion> -->
|
||||
<!-- <artifactId>commons-logging</artifactId> -->
|
||||
<!-- <groupId>commons-logging</groupId> -->
|
||||
<!-- </exclusion> -->
|
||||
<!-- </exclusions> -->
|
||||
<!-- </dependency> -->
|
||||
<!-- <dependency> -->
|
||||
<!-- <groupId>org.apache.httpcomponents</groupId> -->
|
||||
<!-- <artifactId>fluent-hc</artifactId> -->
|
||||
<!-- <version>4.2.5</version> -->
|
||||
<!-- <exclusions> -->
|
||||
<!-- <exclusion> -->
|
||||
<!-- <artifactId>commons-logging</artifactId> -->
|
||||
<!-- <groupId>commons-logging</groupId> -->
|
||||
<!-- </exclusion> -->
|
||||
<!-- </exclusions> -->
|
||||
<!-- </dependency> -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
@ -154,6 +153,31 @@
|
||||
<version>${guava.version}</version>
|
||||
</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 -->
|
||||
|
||||
<dependency>
|
||||
@ -269,7 +293,7 @@
|
||||
|
||||
<!-- logging -->
|
||||
<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 -->
|
||||
<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();
|
||||
}
|
||||
|
||||
@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 use-expressions="true">
|
||||
<intercept-url pattern="/**" access="isAuthenticated()" />
|
||||
|
||||
<http-basic />
|
||||
|
||||
@ -21,4 +20,6 @@
|
||||
</authentication-provider>
|
||||
</authentication-manager>
|
||||
|
||||
<global-method-security pre-post-annotations="enabled" />
|
||||
|
||||
</beans:beans>
|
@ -1,11 +1,15 @@
|
||||
package org.baeldung.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
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.params.CoreConnectionPNames;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.baeldung.client.spring.ClientConfig;
|
||||
@ -25,21 +29,39 @@ public class RawClientLiveTest {
|
||||
public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
|
||||
final DefaultHttpClient httpClient = new DefaultHttpClient();
|
||||
|
||||
final int timeout = 5; // seconds
|
||||
final int timeout = 20; // seconds
|
||||
final HttpParams httpParams = httpClient.getParams();
|
||||
// - note: timeout via raw String parameters
|
||||
// httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
|
||||
// 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);
|
||||
configureViaRawApi(timeout, httpParams);
|
||||
// configureViaHighLevelApi(timeout, httpParams);
|
||||
|
||||
// - note: timeout via the API
|
||||
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
||||
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
||||
final HttpGet getMethod = new HttpGet("http://localhost:8080/spring-security-rest-template/api/bars/1");
|
||||
|
||||
final HttpResponse response = httpClient.execute(new HttpGet("http://localhost:8080/spring-security-rest-template/api/foos/1/unsecured"));
|
||||
final int statusCode = response.getStatusLine().getStatusCode();
|
||||
System.out.println(statusCode);
|
||||
final int hardTimeout = 5; // seconds
|
||||
final TimerTask task = new TimerTask() {
|
||||
@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