Skip to content

Commit c576a3c

Browse files
author
Eugen
committed
Merge pull request eugenp#293 from Doha2012/master
upgrade httpClient
2 parents ccd1824 + b470c21 commit c576a3c

8 files changed

Lines changed: 61 additions & 29 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<faceted-project>
33
<installed facet="wst.jsdt.web" version="1.0"/>
4-
<installed facet="jst.web" version="3.0"/>
54
<installed facet="java" version="1.8"/>
5+
<installed facet="jst.web" version="3.1"/>
66
</faceted-project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.caching.config;
2+
3+
import java.util.Arrays;
4+
5+
import org.baeldung.caching.example.CustomerDataService;
6+
import org.springframework.cache.CacheManager;
7+
import org.springframework.cache.annotation.EnableCaching;
8+
import org.springframework.cache.concurrent.ConcurrentMapCache;
9+
import org.springframework.cache.support.SimpleCacheManager;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
@Configuration
14+
@EnableCaching
15+
public class CachingConfig {
16+
17+
@Bean
18+
public CustomerDataService customerDataService() {
19+
return new CustomerDataService();
20+
}
21+
22+
@Bean
23+
public CacheManager cacheManager() {
24+
final SimpleCacheManager cacheManager = new SimpleCacheManager();
25+
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("directory"), new ConcurrentMapCache("addresses")));
26+
return cacheManager;
27+
}
28+
29+
}

‎spring-all/src/main/java/org/baeldung/caching/config/MyAppConfig.java‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎spring-all/src/main/java/org/baeldung/caching/example/CustomerDataService.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
import org.springframework.stereotype.Component;
1111

1212
@Component
13-
@CacheConfig(cacheNames = { "addressDemo" })
13+
@CacheConfig(cacheNames = { "addresses" })
1414
public class CustomerDataService {
1515

1616
@Autowired
1717
CacheManager cacheManager;
1818

19+
// this method configuration is equivalent to xml configuration
20+
@Cacheable(value = "addresses", key = "#customer.name")
21+
public String getAddress(final Customer customer) {
22+
return customer.getAddress();
23+
}
24+
1925
/**
2026
* The method returns the customer's address,
2127
only it doesn't find it the cache- addresses and directory.

‎spring-all/src/main/resources/config.xml‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
<cache:annotation-driven/>
1212

13-
<context:annotation-config/>
14-
<bean class="org.baeldung.caching.config.MyAppConfig"/>
13+
<!-- <context:annotation-config/> -->
1514

1615
<!-- the service that you wish to make cacheable. -->
1716
<bean id="customerDataService" class="org.baeldung.caching.example.CustomerDataService"/>
@@ -21,7 +20,6 @@
2120
<set>
2221
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="directory"/>
2322
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addresses"/>
24-
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="addressDemo"/>
2523
</set>
2624
</property>
2725
</bean>
@@ -34,7 +32,7 @@
3432
</cache:advice>
3533

3634

37-
<!-- apply the behavior to all the implementations of CustomerDataService interface -->
35+
<!-- apply the behavior to all the implementations of CustomerDataService -->
3836
<aop:config>
3937
<aop:advisor advice-ref="cachingBehavior" pointcut="execution(* org.baeldung.caching.example.CustomerDataService.*(..))"/>
4038
</aop:config>

‎spring-all/src/test/java/org/baeldung/caching/test/SpringCachingBehaviorTest.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package org.baeldung.caching.test;
22

3+
import org.baeldung.caching.config.CachingConfig;
34
import org.baeldung.caching.example.Customer;
45
import org.baeldung.caching.example.CustomerDataService;
56
import org.junit.Test;
6-
import org.springframework.context.ApplicationContext;
7-
import org.springframework.context.support.ClassPathXmlApplicationContext;
87
import org.springframework.stereotype.Component;
8+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
99

1010
@Component
1111
public class SpringCachingBehaviorTest {
1212

1313
@Test
1414
public void testCaching() {
1515
@SuppressWarnings("resource")
16-
final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
16+
final
17+
// final ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
18+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
19+
context.register(CachingConfig.class);
20+
context.refresh();
1721
final CustomerDataService service = context.getBean(CustomerDataService.class);
1822

1923
final Customer cust = new Customer("Tom", "67-2, Downing Street, NY");
20-
service.getAddress1(cust);
21-
service.getAddress1(cust);
24+
service.getAddress(cust);
25+
service.getAddress(cust);
2226

2327
// fail("Unable to instantiate the CustomerDataService");
2428
}

‎spring-security-rest-digest-auth/src/main/java/org/baeldung/client/ClientPreemptiveDigestAuthentication.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import org.apache.http.auth.AuthScope;
77
import org.apache.http.auth.UsernamePasswordCredentials;
88
import org.apache.http.client.AuthCache;
9+
import org.apache.http.client.CredentialsProvider;
910
import org.apache.http.client.methods.HttpGet;
10-
import org.apache.http.client.protocol.ClientContext;
11+
import org.apache.http.client.protocol.HttpClientContext;
1112
import org.apache.http.impl.auth.DigestScheme;
1213
import org.apache.http.impl.client.BasicAuthCache;
13-
import org.apache.http.impl.client.DefaultHttpClient;
14+
import org.apache.http.impl.client.BasicCredentialsProvider;
15+
import org.apache.http.impl.client.CloseableHttpClient;
16+
import org.apache.http.impl.client.HttpClients;
1417
import org.apache.http.protocol.BasicHttpContext;
1518
import org.apache.http.util.EntityUtils;
1619

@@ -27,9 +30,11 @@ public class ClientPreemptiveDigestAuthentication {
2730
public static void main(final String[] args) throws Exception {
2831
final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
2932

30-
final DefaultHttpClient httpclient = new DefaultHttpClient();
33+
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
34+
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user1", "user1Pass"));
35+
36+
final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
3137
try {
32-
httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user1", "user1Pass"));
3338

3439
// Create AuthCache instance
3540
final AuthCache authCache = new BasicAuthCache();
@@ -43,7 +48,7 @@ public static void main(final String[] args) throws Exception {
4348

4449
// Add AuthCache to the execution context
4550
final BasicHttpContext localcontext = new BasicHttpContext();
46-
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
51+
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
4752

4853
final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1");
4954

@@ -63,7 +68,7 @@ public static void main(final String[] args) throws Exception {
6368
}
6469
} finally {
6570
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
66-
httpclient.getConnectionManager().shutdown();
71+
httpclient.close();
6772
}
6873
}
6974

‎spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.apache.http.HttpHost;
66
import org.apache.http.client.AuthCache;
77
import org.apache.http.client.HttpClient;
8-
import org.apache.http.client.protocol.ClientContext;
8+
import org.apache.http.client.protocol.HttpClientContext;
99
import org.apache.http.impl.auth.DigestScheme;
1010
import org.apache.http.impl.client.BasicAuthCache;
1111
import org.apache.http.protocol.BasicHttpContext;
@@ -41,7 +41,7 @@ private HttpContext createHttpContext() {
4141

4242
// Add AuthCache to the execution context
4343
final BasicHttpContext localcontext = new BasicHttpContext();
44-
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
44+
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
4545
return localcontext;
4646
}
4747

0 commit comments

Comments
 (0)