View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.auth;
28  
29  import java.nio.charset.Charset;
30  import java.util.HashMap;
31  import java.util.Locale;
32  import java.util.Map;
33  
34  import org.apache.http.Consts;
35  import org.apache.http.HeaderElement;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.annotation.NotThreadSafe;
38  import org.apache.http.auth.ChallengeState;
39  import org.apache.http.auth.MalformedChallengeException;
40  import org.apache.http.auth.params.AuthPNames;
41  import org.apache.http.message.BasicHeaderValueParser;
42  import org.apache.http.message.HeaderValueParser;
43  import org.apache.http.message.ParserCursor;
44  import org.apache.http.util.CharArrayBuffer;
45  
46  /**
47   * Abstract authentication scheme class that lays foundation for all
48   * RFC 2617 compliant authentication schemes and provides capabilities common
49   * to all authentication schemes defined in RFC 2617.
50   *
51   * @since 4.0
52   */
53  @SuppressWarnings("deprecation")
54  @NotThreadSafe // AuthSchemeBase, params
55  public abstract class RFC2617Scheme extends AuthSchemeBase {
56  
57      private final Map<String, String> params;
58      private final Charset credentialsCharset;
59  
60      /**
61       * Creates an instance of <tt>RFC2617Scheme</tt> with the given challenge
62       * state.
63       *
64       * @since 4.2
65       *
66       * @deprecated (4.3) do not use.
67       */
68      @Deprecated
69      public RFC2617Scheme(final ChallengeState challengeState) {
70          super(challengeState);
71          this.params = new HashMap<String, String>();
72          this.credentialsCharset = Consts.ASCII;
73      }
74  
75      /**
76       * @since 4.3
77       */
78      public RFC2617Scheme(final Charset credentialsCharset) {
79          super();
80          this.params = new HashMap<String, String>();
81          this.credentialsCharset = credentialsCharset != null ? credentialsCharset : Consts.ASCII;
82      }
83  
84      public RFC2617Scheme() {
85          this(Consts.ASCII);
86      }
87  
88  
89      /**
90       * @since 4.3
91       */
92      public Charset getCredentialsCharset() {
93          return credentialsCharset;
94      }
95  
96      String getCredentialsCharset(final HttpRequest request) {
97          String charset = (String) request.getParams().getParameter(AuthPNames.CREDENTIAL_CHARSET);
98          if (charset == null) {
99              charset = getCredentialsCharset().name();
100         }
101         return charset;
102     }
103 
104     @Override
105     protected void parseChallenge(
106             final CharArrayBuffer buffer, final int pos, final int len) throws MalformedChallengeException {
107         final HeaderValueParser parser = BasicHeaderValueParser.INSTANCE;
108         final ParserCursor cursor = new ParserCursor(pos, buffer.length());
109         final HeaderElement[] elements = parser.parseElements(buffer, cursor);
110         if (elements.length == 0) {
111             throw new MalformedChallengeException("Authentication challenge is empty");
112         }
113         this.params.clear();
114         for (final HeaderElement element : elements) {
115             this.params.put(element.getName(), element.getValue());
116         }
117     }
118 
119     /**
120      * Returns authentication parameters map. Keys in the map are lower-cased.
121      *
122      * @return the map of authentication parameters
123      */
124     protected Map<String, String> getParameters() {
125         return this.params;
126     }
127 
128     /**
129      * Returns authentication parameter with the given name, if available.
130      *
131      * @param name The name of the parameter to be returned
132      *
133      * @return the parameter with the given name
134      */
135     public String getParameter(final String name) {
136         if (name == null) {
137             return null;
138         }
139         return this.params.get(name.toLowerCase(Locale.ENGLISH));
140     }
141 
142     /**
143      * Returns authentication realm. The realm may not be null.
144      *
145      * @return the authentication realm
146      */
147     public String getRealm() {
148         return getParameter("realm");
149     }
150 
151 }