http client configuration work
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+31
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
-6
@@ -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>
|
||||
+34
-12
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user