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.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.rice.ksb.service.BasicAuthenticationConnectionCredentials;
28  import org.kuali.rice.ksb.service.BasicAuthenticationCredentials;
29  import org.kuali.rice.ksb.service.BasicAuthenticationService;
30  
31  /**
32   * Implements the BasicAuthenticationService
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   * @see org.kuali.rice.ksb.service.BasicAuthenticationService
36   */
37  
38  public class BasicAuthenticationServiceImpl implements BasicAuthenticationService {
39  
40      private Map<QName, List<BasicAuthenticationCredentials>> serviceCredentialsMap;
41      private Map<QName, BasicAuthenticationConnectionCredentials> connectionCredentialsMap;
42  
43      /**
44       * Constructs BasicAuthenticationServiceImpl with a serviceCredentialsMap and a connectionCredentialsMap
45       */
46      public BasicAuthenticationServiceImpl() {
47          this.serviceCredentialsMap = Collections.synchronizedMap(
48                  new HashMap<QName, List<BasicAuthenticationCredentials>>());
49          this.connectionCredentialsMap = Collections.synchronizedMap(
50                  new HashMap<QName, BasicAuthenticationConnectionCredentials>());
51      }
52  
53      public boolean checkServiceAuthentication(String serviceNameSpaceURI, QName serviceName, String username,
54              String password) {
55          List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
56          if (credentialsList != null) {
57              synchronized (credentialsList) {
58                  for (BasicAuthenticationCredentials credentials : credentialsList) {
59                      if (StringUtils.equals(username, credentials.getUsername()) && StringUtils.equals(
60                              serviceNameSpaceURI, credentials.getServiceNameSpaceURI())) {
61                          return StringUtils.equals(password, credentials.getPassword());
62                      }
63                  }
64              }
65          }
66          return false;
67      }
68  
69      public BasicAuthenticationConnectionCredentials getConnectionCredentials(String serviceNameSpaceURI,
70              String serviceName) {
71          return connectionCredentialsMap.get(new QName(serviceNameSpaceURI, serviceName));
72      }
73  
74      public void registerServiceCredentials(BasicAuthenticationCredentials credentials) {
75          synchronized (serviceCredentialsMap) {
76              QName serviceName = new QName(credentials.getServiceNameSpaceURI(), credentials.getLocalServiceName());
77              List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
78              if (credentialsList == null) {
79                  credentialsList = Collections.synchronizedList(new ArrayList<BasicAuthenticationCredentials>());
80                  serviceCredentialsMap.put(serviceName, credentialsList);
81              }
82              credentialsList.add(credentials);
83          }
84      }
85  
86      public void registerConnectionCredentials(BasicAuthenticationConnectionCredentials credentials) {
87          synchronized (connectionCredentialsMap) {
88              connectionCredentialsMap.put(new QName(credentials.getServiceNameSpaceURI(),
89                      credentials.getLocalServiceName()), credentials);
90          }
91      }
92  
93  }