001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.security.httpinvoker;
017
018import java.io.ByteArrayOutputStream;
019import java.io.IOException;
020
021import org.apache.commons.codec.binary.Base64;
022import org.apache.commons.httpclient.HttpClient;
023import org.apache.commons.httpclient.methods.PostMethod;
024import org.kuali.rice.core.api.security.credentials.CredentialsSource;
025import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
026import org.kuali.rice.ksb.messaging.KSBHttpInvokerRequestExecutor;
027import org.kuali.rice.ksb.security.credentials.UsernamePasswordCredentials;
028import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
029import org.springframework.util.Assert;
030
031
032/**
033 * Extension to {@link KSBHttpInvokerRequestExecutor} that retrieves
034 * credentials from the CredentialsSource and places them in a BASIC HTTP
035 * Authorization header.
036 * 
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 * @since 0.9
039 */
040public final class AuthenticationCommonsHttpInvokerRequestExecutor extends
041    KSBHttpInvokerRequestExecutor {
042
043    /**
044     * Source of the credentials to pass via BASIC AUTH.
045     */
046    private final CredentialsSource credentialsSource;
047
048    /**
049     * Details about the service that the CredentialsSource may need.
050     */
051    private final ServiceConfiguration serviceConfiguration;
052
053    /**
054     * Constructor that accepts the CredentialsSource and Service Info.
055     * 
056     * @param credentialsSource the source of credentials.
057     * @param serviceInfo information about the service.
058     */
059    public AuthenticationCommonsHttpInvokerRequestExecutor(final HttpClient httpClient, 
060        final CredentialsSource credentialsSource, final ServiceConfiguration serviceConfiguration) {
061        super(httpClient);
062        Assert.notNull(credentialsSource, "credentialsSource cannot be null.");
063        Assert.notNull(serviceConfiguration, "serviceConfiguration cannot be null.");
064        this.credentialsSource = credentialsSource;
065        this.serviceConfiguration = serviceConfiguration;
066    }
067
068    /**
069     * Overridden to obtain the Credentials from the CredentialsSource and pass
070     * them via HTTP BASIC Authorization.
071     */
072
073    protected void setRequestBody(final HttpInvokerClientConfiguration config,
074        final PostMethod postMethod, final ByteArrayOutputStream baos) throws IOException {
075        final UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) this.credentialsSource.getCredentials(this.serviceConfiguration.getEndpointUrl().toExternalForm());
076
077        final String base64 = credentials.getUsername() + ":"
078        + credentials.getPassword();
079        postMethod.addRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(base64.getBytes())));
080
081        if (logger.isDebugEnabled()) {
082            logger
083                .debug("HttpInvocation now presenting via BASIC authentication CredentialsSource-derived: "
084                    + credentials.getUsername());
085        }
086        
087        super.setRequestBody(config, postMethod, baos);
088    }
089}