11package org .rest .sec .client .template .newer ;
22
3- import org .apache .http .HttpHost ;
43import org .apache .http .auth .AuthScope ;
54import org .apache .http .auth .UsernamePasswordCredentials ;
6- import org .apache .http .client .AuthCache ;
7- import org .apache .http .client .protocol .ClientContext ;
8- import org .apache .http .impl .auth .DigestScheme ;
9- import org .apache .http .impl .client .BasicAuthCache ;
105import org .apache .http .impl .client .DefaultHttpClient ;
11- import org .apache .http .protocol .BasicHttpContext ;
126import org .rest .client .AbstractClientRESTTemplate ;
13- import org .rest .client .IClientTemplate ;
147import org .rest .sec .client .ExamplePaths ;
158import org .rest .sec .model .dto .User ;
169import org .rest .sec .util .SecurityConstants ;
17- import org .rest .security .DigestHttpComponentsClientHttpRequestFactory ;
1810import org .springframework .beans .factory .annotation .Autowired ;
1911import org .springframework .beans .factory .annotation .Value ;
2012import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
@@ -30,8 +22,6 @@ public class UserClientRESTTemplate extends AbstractClientRESTTemplate<User> {
3022 private String host ;
3123 @ Value ("${http.port}" )
3224 private int port ;
33- @ Value ("${http.protocol}" )
34- private String protocol ;
3525
3626 @ Value ("${sec.auth.basic}" )
3727 private boolean basicAuth ;
@@ -40,8 +30,6 @@ public UserClientRESTTemplate() {
4030 super (User .class );
4131 }
4232
43- //
44-
4533 // template method
4634
4735 @ Override
@@ -50,35 +38,20 @@ public final String getURI() {
5038 }
5139
5240 @ Override
53- public final IClientTemplate <User > givenAuthenticated () {
54- if (basicAuth ) {
55- final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory ) restTemplate .getRequestFactory ();
56- final DefaultHttpClient httpClient = (DefaultHttpClient ) requestFactory .getHttpClient ();
57- httpClient .getCredentialsProvider ().setCredentials (new AuthScope (host , port , AuthScope .ANY_REALM ), new UsernamePasswordCredentials (SecurityConstants .ADMIN_USERNAME , SecurityConstants .ADMIN_PASSWORD ));
58- } else {
59- final HttpHost targetHost = new HttpHost (host , port , protocol );
60-
61- // Create AuthCache instance
62- final AuthCache authCache = new BasicAuthCache ();
63- // Generate DIGEST scheme object, initialize it and add it to the local auth cache
64- final DigestScheme digestAuth = new DigestScheme ();
65- // Suppose we already know the realm name
66- digestAuth .overrideParamter ("realm" , "Contacts Realm via Digest Authentication" );
67- // Suppose we already know the expected nonce value
68- digestAuth .overrideParamter ("nonce" , "acegi" );
69- authCache .put (targetHost , digestAuth );
70-
71- // Add AuthCache to the execution context
72- final BasicHttpContext localcontext = new BasicHttpContext ();
73- localcontext .setAttribute (ClientContext .AUTH_CACHE , authCache );
41+ protected boolean isBasicAuth () {
42+ return basicAuth ;
43+ }
7444
75- final DigestHttpComponentsClientHttpRequestFactory requestFactory = (DigestHttpComponentsClientHttpRequestFactory ) restTemplate .getRequestFactory ();
76- requestFactory .setHttpContext (localcontext );
77- final DefaultHttpClient httpClient = (DefaultHttpClient ) requestFactory .getHttpClient ();
78- httpClient .getCredentialsProvider ().setCredentials (new AuthScope (host , port , AuthScope .ANY_REALM ), new UsernamePasswordCredentials (SecurityConstants .ADMIN_USERNAME , SecurityConstants .ADMIN_PASSWORD ));
79- }
45+ @ Override
46+ protected void basicAuth () {
47+ final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory ) restTemplate .getRequestFactory ();
48+ final DefaultHttpClient httpClient = (DefaultHttpClient ) requestFactory .getHttpClient ();
49+ httpClient .getCredentialsProvider ().setCredentials (new AuthScope (host , port , AuthScope .ANY_REALM ), new UsernamePasswordCredentials (SecurityConstants .ADMIN_USERNAME , SecurityConstants .ADMIN_PASSWORD ));
50+ }
8051
81- return this ;
52+ @ Override
53+ protected void digestAuth () {
54+ throw new UnsupportedOperationException ();
8255 }
8356
8457}
0 commit comments