001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.ksb.service;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.springframework.beans.factory.InitializingBean;
020    
021    /**
022     * Initializes authenticationService with user credentials.
023     *
024     * @author Kuali Rice Team (rice.collab@kuali.org)
025     */
026    public class BasicAuthenticationCredentials implements InitializingBean {
027    
028        private BasicAuthenticationService authenticationService;
029        private String serviceNameSpaceURI;
030        private String localServiceName;
031        private String username;
032        private String password;
033    
034        /**
035         * Register the service credentials for the service
036         *
037         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
038         */
039        public void afterPropertiesSet() throws Exception {
040            if (this.validate()) {
041                this.getAuthenticationService().registerServiceCredentials(this);
042            }
043        }
044    
045        /**
046         * Gets the BasicAuthenticationService to use
047         *
048         * @return BasicAuthenticationService the BasicAuthenticationService
049         */
050        public BasicAuthenticationService getAuthenticationService() {
051            if (this.authenticationService == null) {
052                this.authenticationService = KSBServiceLocator.getBasicAuthenticationService();
053            }
054            return authenticationService;
055        }
056    
057        /**
058         * Sets the authenticationService
059         *
060         * @param authenticationService the BasicAthenticationService to set
061         */
062        public void setAuthenticationService(BasicAuthenticationService authenticationService) {
063            this.authenticationService = authenticationService;
064        }
065    
066        /**
067         * Gets the serviceNameSpaceURI
068         *
069         * @return String serviceNameSpaceURI of this BasicAuthenticationCredentials
070         */
071        public String getServiceNameSpaceURI() {
072            return serviceNameSpaceURI;
073        }
074    
075        /**
076         * Sets the serviceNameSpaceURI
077         *
078         * @param serviceNameSpaceURI the serviceNameSpaceURI to set
079         */
080        public void setServiceNameSpaceURI(String serviceNameSpaceURI) {
081            this.serviceNameSpaceURI = serviceNameSpaceURI;
082        }
083    
084        /**
085         * Gets the localServiceName
086         *
087         * @return String localServiceName of this BasicAuthenticationCredentials
088         */
089        public String getLocalServiceName() {
090            return localServiceName;
091        }
092    
093        /**
094         * Sets the localServiceName
095         *
096         * @param localServiceName the localServiceName to set
097         */
098        public void setLocalServiceName(String localServiceName) {
099            this.localServiceName = localServiceName;
100        }
101    
102        /**
103         * Gets the username
104         *
105         * @return username the username of this BasicAuthenticationCredentials
106         */
107        public String getUsername() {
108            return username;
109        }
110    
111        /**
112         * Sets the username
113         *
114         * @param username the username to set
115         */
116        public void setUsername(String username) {
117            this.username = username;
118        }
119    
120        /**
121         * Gets the password
122         *
123         * @return password the password of this BasicAuthenticationCredentials
124         */
125        public String getPassword() {
126            return password;
127        }
128    
129        /**
130         * Sets the password
131         *
132         * @param password the password to set
133         */
134        public void setPassword(String password) {
135            this.password = password;
136        }
137    
138        /**
139         * Validates authenticationService, serviceNameSpaceURI, localServiceName, username, and password
140         *
141         * @return true if the authentictionSerivce is not null and the serviceNameSpaceURI, localServiceName,
142         *         username, and password are not blank
143         */
144        protected boolean validate() {
145            return this.getAuthenticationService() != null &&
146                    StringUtils.isNotBlank(this.getServiceNameSpaceURI()) &&
147                    StringUtils.isNotBlank(this.getLocalServiceName()) &&
148                    StringUtils.isNotBlank(this.getUsername()) &&
149                    StringUtils.isNotBlank(this.getPassword());
150        }
151    }