View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Extension to {@link KSBHttpInvokerRequestExecutor} that retrieves
34   * credentials from the CredentialsSource and places them in a BASIC HTTP
35   * Authorization header.
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   * @since 0.9
39   */
40  public final class AuthenticationCommonsHttpInvokerRequestExecutor extends
41      KSBHttpInvokerRequestExecutor {
42  
43      /**
44       * Source of the credentials to pass via BASIC AUTH.
45       */
46      private final CredentialsSource credentialsSource;
47  
48      /**
49       * Details about the service that the CredentialsSource may need.
50       */
51      private final ServiceConfiguration serviceConfiguration;
52  
53      /**
54       * Constructor that accepts the CredentialsSource and Service Info.
55       * 
56       * @param credentialsSource the source of credentials.
57       * @param serviceInfo information about the service.
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       * Overridden to obtain the Credentials from the CredentialsSource and pass
70       * them via HTTP BASIC Authorization.
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  }