timeout work
This commit is contained in:
		
							parent
							
								
									7d17c4dd9d
								
							
						
					
					
						commit
						a830caca39
					
				@ -1,4 +1,5 @@
 | 
				
			|||||||
<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>
 | 
						<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>
 | 
				
			||||||
@ -28,6 +29,12 @@
 | 
				
			|||||||
			<groupId>org.springframework</groupId>
 | 
								<groupId>org.springframework</groupId>
 | 
				
			||||||
			<artifactId>spring-core</artifactId>
 | 
								<artifactId>spring-core</artifactId>
 | 
				
			||||||
			<version>${org.springframework.version}</version>
 | 
								<version>${org.springframework.version}</version>
 | 
				
			||||||
 | 
								<exclusions>
 | 
				
			||||||
 | 
									<exclusion>
 | 
				
			||||||
 | 
										<artifactId>commons-logging</artifactId>
 | 
				
			||||||
 | 
										<groupId>commons-logging</groupId>
 | 
				
			||||||
 | 
									</exclusion>
 | 
				
			||||||
 | 
								</exclusions>
 | 
				
			||||||
		</dependency>
 | 
							</dependency>
 | 
				
			||||||
		<dependency>
 | 
							<dependency>
 | 
				
			||||||
			<groupId>org.springframework</groupId>
 | 
								<groupId>org.springframework</groupId>
 | 
				
			||||||
@ -87,6 +94,18 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		<!-- http -->
 | 
							<!-- 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>
 | 
							<dependency>
 | 
				
			||||||
			<groupId>org.apache.httpcomponents</groupId>
 | 
								<groupId>org.apache.httpcomponents</groupId>
 | 
				
			||||||
			<artifactId>httpcore</artifactId>
 | 
								<artifactId>httpcore</artifactId>
 | 
				
			||||||
 | 
				
			|||||||
@ -30,4 +30,10 @@ 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();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -7,6 +7,8 @@
 | 
				
			|||||||
		</encoder>
 | 
							</encoder>
 | 
				
			||||||
	</appender>
 | 
						</appender>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						<logger name="org.apache.http" level="TRACE" />
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	<logger name="org.springframework" level="WARN" />
 | 
						<logger name="org.springframework" level="WARN" />
 | 
				
			||||||
	<logger name="org.springframework.transaction" level="WARN" />
 | 
						<logger name="org.springframework.transaction" level="WARN" />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -3,38 +3,43 @@ package org.baeldung.client;
 | 
				
			|||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.apache.http.HttpResponse;
 | 
					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.ClientProtocolException;
 | 
				
			||||||
import org.apache.http.client.methods.HttpGet;
 | 
					import org.apache.http.client.methods.HttpGet;
 | 
				
			||||||
import org.apache.http.impl.client.DefaultHttpClient;
 | 
					import org.apache.http.impl.client.DefaultHttpClient;
 | 
				
			||||||
 | 
					import org.apache.http.params.HttpConnectionParams;
 | 
				
			||||||
 | 
					import org.apache.http.params.HttpParams;
 | 
				
			||||||
import org.baeldung.client.spring.ClientConfig;
 | 
					import org.baeldung.client.spring.ClientConfig;
 | 
				
			||||||
import org.junit.Test;
 | 
					import org.junit.Test;
 | 
				
			||||||
import org.junit.runner.RunWith;
 | 
					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.ContextConfiguration;
 | 
				
			||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 | 
					import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 | 
				
			||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
 | 
					import org.springframework.test.context.support.AnnotationConfigContextLoader;
 | 
				
			||||||
import org.springframework.web.client.RestTemplate;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RunWith(SpringJUnit4ClassRunner.class)
 | 
					@RunWith(SpringJUnit4ClassRunner.class)
 | 
				
			||||||
@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
 | 
					@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
 | 
				
			||||||
public class RawClientLiveTest {
 | 
					public class RawClientLiveTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Autowired
 | 
					 | 
				
			||||||
    private RestTemplate restTemplate;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // tests
 | 
					    // tests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
 | 
					    public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
 | 
				
			||||||
        final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
 | 
					        final DefaultHttpClient httpClient = new DefaultHttpClient();
 | 
				
			||||||
        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 timeout = 5; // 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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // - note: timeout via the API
 | 
				
			||||||
 | 
					        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 statusCode = response.getStatusLine().getStatusCode();
 | 
					        final int statusCode = response.getStatusLine().getStatusCode();
 | 
				
			||||||
        System.out.println(statusCode);
 | 
					        System.out.println(statusCode);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user