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 super(httpClient);
62 Assert.notNull(credentialsSource, "credentialsSource cannot be null.");
63 Assert.notNull(serviceConfiguration, "serviceConfiguration cannot be null.");
64 this.credentialsSource = credentialsSource;
65 this.serviceConfiguration = serviceConfiguration;
66 }
67
68
69
70
71
72
73 protected void setRequestBody(final HttpInvokerClientConfiguration config,
74 final PostMethod postMethod, final ByteArrayOutputStream baos) throws IOException {
75 final UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) this.credentialsSource.getCredentials(this.serviceConfiguration.getEndpointUrl().toExternalForm());
76
77 final String base64 = credentials.getUsername() + ":"
78 + credentials.getPassword();
79 postMethod.addRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(base64.getBytes())));
80
81 if (logger.isDebugEnabled()) {
82 logger
83 .debug("HttpInvocation now presenting via BASIC authentication CredentialsSource-derived: "
84 + credentials.getUsername());
85 }
86
87 super.setRequestBody(config, postMethod, baos);
88 }
89 }