This project has retired. For details please refer to its Attic page.
RepositoryMap xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.fileshare;
20  
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
26  import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
27  import org.apache.chemistry.opencmis.commons.server.CallContext;
28  
29  /**
30   * Repository map.
31   */
32  public class RepositoryMap {
33  
34      private final Map<String, FileShareRepository> map;
35      private final Map<String, String> logins;
36  
37      public RepositoryMap() {
38          map = new HashMap<String, FileShareRepository>();
39          logins = new HashMap<String, String>();
40      }
41  
42      /**
43       * Adds a repository object.
44       */
45      public void addRepository(FileShareRepository fsr) {
46          if ((fsr == null) || (fsr.getRepositoryId() == null)) {
47              return;
48          }
49  
50          map.put(fsr.getRepositoryId(), fsr);
51      }
52  
53      /**
54       * Gets a repository object by id.
55       */
56      public FileShareRepository getRepository(String repositoryId) {
57          // get repository object
58          FileShareRepository result = map.get(repositoryId);
59          if (result == null) {
60              throw new CmisObjectNotFoundException("Unknown repository '" + repositoryId + "'!");
61          }
62  
63          return result;
64      }
65  
66      /**
67       * Takes user and password from the CallContext and checks them.
68       */
69      public void authenticate(CallContext context) {
70          // check user and password first
71          if (!authenticate(context.getUsername(), context.getPassword())) {
72              throw new CmisPermissionDeniedException();
73          }
74      }
75  
76      /**
77       * Returns all repository objects.
78       */
79      public Collection<FileShareRepository> getRepositories() {
80          return map.values();
81      }
82  
83      /**
84       * Adds a login.
85       */
86      public void addLogin(String username, String password) {
87          if ((username == null) || (password == null)) {
88              return;
89          }
90  
91          logins.put(username.trim(), password);
92      }
93  
94      /**
95       * Authenticates a user against the configured logins.
96       */
97      private boolean authenticate(String username, String password) {
98          String pwd = logins.get(username);
99          if (pwd == null) {
100             return false;
101         }
102 
103         return pwd.equals(password);
104     }
105 }