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 org.apache.commons.codec.binary.Base64;
19 import org.apache.http.client.HttpClient;
20 import org.apache.http.client.methods.HttpPost;
21 import org.kuali.rice.core.api.security.credentials.CredentialsSource;
22 import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
23 import org.kuali.rice.ksb.messaging.KSBHttpInvokerRequestExecutor;
24 import org.kuali.rice.ksb.security.credentials.UsernamePasswordCredentials;
25 import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
26 import org.springframework.util.Assert;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
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
60 public AuthenticationCommonsHttpInvokerRequestExecutor(final HttpClient httpClient,
61 final CredentialsSource credentialsSource, final ServiceConfiguration serviceConfiguration) {
62 super(httpClient);
63 Assert.notNull(credentialsSource, "credentialsSource cannot be null.");
64 Assert.notNull(serviceConfiguration, "serviceConfiguration cannot be null.");
65 this.credentialsSource = credentialsSource;
66 this.serviceConfiguration = serviceConfiguration;
67 }
68
69
70
71
72
73
74 protected void setRequestBody(final HttpInvokerClientConfiguration config,
75 final HttpPost httpPost, final ByteArrayOutputStream baos) throws IOException {
76 final UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) this.credentialsSource.getCredentials(this.serviceConfiguration.getEndpointUrl().toExternalForm());
77
78 final String base64 = credentials.getUsername() + ":"
79 + credentials.getPassword();
80 httpPost.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(base64.getBytes())));
81
82 if (logger.isDebugEnabled()) {
83 logger
84 .debug("HttpInvocation now presenting via BASIC authentication CredentialsSource-derived: "
85 + credentials.getUsername());
86 }
87
88 super.setRequestBody(config, httpPost, baos);
89 }
90 }