This project has retired. For details please refer to its Attic page.
BasicAuthCallContextHandler 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.server.shared;
20  
21  import java.io.Serializable;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.chemistry.opencmis.commons.server.CallContext;
28  import org.apache.commons.codec.binary.Base64;
29  
30  /**
31   * Call Context handler that handles basic authentication.
32   */
33  public class BasicAuthCallContextHandler implements CallContextHandler, Serializable {
34  
35      private static final long serialVersionUID = 1L;
36  
37      /**
38       * Constructor.
39       */
40      public BasicAuthCallContextHandler() {
41      }
42  
43      public Map<String, String> getCallContextMap(HttpServletRequest request) {
44          Map<String, String> result = null;
45  
46          String authHeader = request.getHeader("Authorization");
47          if ((authHeader != null) && (authHeader.trim().toLowerCase().startsWith("basic "))) {
48              int x = authHeader.lastIndexOf(' ');
49              if (x == -1) {
50                  return result;
51              }
52  
53              String credentials = null;
54              try {
55                  credentials = new String(Base64.decodeBase64(authHeader.substring(x + 1).getBytes("ISO-8859-1")),
56                          "ISO-8859-1");
57              } catch (Exception e) {
58                  return result;
59              }
60  
61              x = credentials.indexOf(':');
62              if (x == -1) {
63                  return result;
64              }
65  
66              // extract user and password and add them to map
67              result = new HashMap<String, String>();
68              result.put(CallContext.USERNAME, credentials.substring(0, x));
69              result.put(CallContext.PASSWORD, credentials.substring(x + 1));
70          }
71  
72          return result;
73      }
74  }