This project has retired. For details please refer to its
Attic page.
BasicAuthCallContextHandler xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.server.shared;
2021import java.io.Serializable;
22import java.util.HashMap;
23import java.util.Map;
2425import javax.servlet.http.HttpServletRequest;
2627import org.apache.chemistry.opencmis.commons.server.CallContext;
28import org.apache.commons.codec.binary.Base64;
2930/**31 * Call Context handler that handles basic authentication.32 */33publicclassBasicAuthCallContextHandler implements CallContextHandler, Serializable {
3435privatestaticfinallong serialVersionUID = 1L;
3637/**38 * Constructor.39 */40publicBasicAuthCallContextHandler() {
41 }
4243public Map<String, String> getCallContextMap(HttpServletRequest request) {
44 Map<String, String> result = null;
4546 String authHeader = request.getHeader("Authorization");
47if ((authHeader != null) && (authHeader.trim().toLowerCase().startsWith("basic "))) {
48int x = authHeader.lastIndexOf(' ');
49if (x == -1) {
50return result;
51 }
5253 String credentials = null;
54try {
55 credentials = new String(Base64.decodeBase64(authHeader.substring(x + 1).getBytes("ISO-8859-1")),
56"ISO-8859-1");
57 } catch (Exception e) {
58return result;
59 }
6061 x = credentials.indexOf(':');
62if (x == -1) {
63return result;
64 }
6566// extract user and password and add them to map67 result = new HashMap<String, String>();
68 result.put(CallContext.USERNAME, credentials.substring(0, x));
69 result.put(CallContext.PASSWORD, credentials.substring(x + 1));
70 }
7172return result;
73 }
74 }