1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.security.httpinvoker; |
17 | |
|
18 | |
import java.io.ByteArrayOutputStream; |
19 | |
import java.io.IOException; |
20 | |
|
21 | |
import org.apache.commons.codec.binary.Base64; |
22 | |
import org.apache.commons.httpclient.HttpClient; |
23 | |
import org.apache.commons.httpclient.methods.PostMethod; |
24 | |
import org.kuali.rice.core.api.security.credentials.CredentialsSource; |
25 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
26 | |
import org.kuali.rice.ksb.messaging.KSBHttpInvokerRequestExecutor; |
27 | |
import org.kuali.rice.ksb.security.credentials.UsernamePasswordCredentials; |
28 | |
import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration; |
29 | |
import org.springframework.util.Assert; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public final class AuthenticationCommonsHttpInvokerRequestExecutor extends |
41 | |
KSBHttpInvokerRequestExecutor { |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
private final CredentialsSource credentialsSource; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private final ServiceConfiguration serviceConfiguration; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public AuthenticationCommonsHttpInvokerRequestExecutor(final HttpClient httpClient, |
60 | |
final CredentialsSource credentialsSource, final ServiceConfiguration serviceConfiguration) { |
61 | 0 | super(httpClient); |
62 | 0 | Assert.notNull(credentialsSource, "credentialsSource cannot be null."); |
63 | 0 | Assert.notNull(serviceConfiguration, "serviceConfiguration cannot be null."); |
64 | 0 | this.credentialsSource = credentialsSource; |
65 | 0 | this.serviceConfiguration = serviceConfiguration; |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
protected void setRequestBody(final HttpInvokerClientConfiguration config, |
74 | |
final PostMethod postMethod, final ByteArrayOutputStream baos) throws IOException { |
75 | 0 | final UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) this.credentialsSource.getCredentials(this.serviceConfiguration.getEndpointUrl().toExternalForm()); |
76 | |
|
77 | 0 | final String base64 = credentials.getUsername() + ":" |
78 | |
+ credentials.getPassword(); |
79 | 0 | postMethod.addRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(base64.getBytes()))); |
80 | |
|
81 | 0 | if (logger.isDebugEnabled()) { |
82 | 0 | logger |
83 | |
.debug("HttpInvocation now presenting via BASIC authentication CredentialsSource-derived: " |
84 | |
+ credentials.getUsername()); |
85 | |
} |
86 | |
|
87 | 0 | super.setRequestBody(config, postMethod, baos); |
88 | 0 | } |
89 | |
} |