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.io.IOException;
30  import java.io.ObjectInputStream;
31  import java.io.ObjectOutputStream;
32  import java.io.ObjectStreamException;
33  import java.io.Serializable;
34  import java.nio.charset.Charset;
35  import java.util.HashMap;
36  import java.util.Locale;
37  import java.util.Map;
38  
39  import org.apache.http.Consts;
40  import org.apache.http.HeaderElement;
41  import org.apache.http.HttpRequest;
42  import org.apache.http.auth.ChallengeState;
43  import org.apache.http.auth.MalformedChallengeException;
44  import org.apache.http.auth.params.AuthPNames;
45  import org.apache.http.message.BasicHeaderValueParser;
46  import org.apache.http.message.HeaderValueParser;
47  import org.apache.http.message.ParserCursor;
48  import org.apache.http.util.CharArrayBuffer;
49  import org.apache.http.util.CharsetUtils;
50  
51  /**
52   * Abstract authentication scheme class that lays foundation for all
53   * RFC 2617 compliant authentication schemes and provides capabilities common
54   * to all authentication schemes defined in RFC 2617.
55   *
56   * @since 4.0
57   */
58  @SuppressWarnings("deprecation")
59  public abstract class RFC2617Scheme extends AuthSchemeBase implements Serializable {
60  
61      private static final long serialVersionUID = -2845454858205884623L;
62  
63      private final Map<String, String> params;
64      private transient Charset credentialsCharset;
65  
66      /**
67       * Creates an instance of {@code RFC2617Scheme} with the given challenge
68       * state.
69       *
70       * @since 4.2
71       *
72       * @deprecated (4.3) do not use.
73       */
74      @Deprecated
75      public RFC2617Scheme(final ChallengeState challengeState) {
76          super(challengeState);
77          this.params = new HashMap<String, String>();
78          this.credentialsCharset = Consts.ASCII;
79      }
80  
81      /**
82       * @since 4.3
83       */
84      public RFC2617Scheme(final Charset credentialsCharset) {
85          super();
86          this.params = new HashMap<String, String>();
87          this.credentialsCharset = credentialsCharset != null ? credentialsCharset : Consts.ASCII;
88      }
89  
90      public RFC2617Scheme() {
91          this(Consts.ASCII);
92      }
93  
94  
95      /**
96       * @since 4.3
97       */
98      public Charset getCredentialsCharset() {
99          return credentialsCharset != null ? credentialsCharset : Consts.ASCII;
100     }
101 
102     String getCredentialsCharset(final HttpRequest request) {
103         String charset = (String) request.getParams().getParameter(AuthPNames.CREDENTIAL_CHARSET);
104         if (charset == null) {
105             charset = getCredentialsCharset().name();
106         }
107         return charset;
108     }
109 
110     @Override
111     protected void parseChallenge(
112             final CharArrayBuffer buffer, final int pos, final int len) throws MalformedChallengeException {
113         final HeaderValueParser parser = BasicHeaderValueParser.INSTANCE;
114         final ParserCursor cursor = new ParserCursor(pos, buffer.length());
115         final HeaderElement[] elements = parser.parseElements(buffer, cursor);
116         this.params.clear();
117         for (final HeaderElement element : elements) {
118             this.params.put(element.getName().toLowerCase(Locale.ROOT), element.getValue());
119         }
120     }
121 
122     /**
123      * Returns authentication parameters map. Keys in the map are lower-cased.
124      *
125      * @return the map of authentication parameters
126      */
127     protected Map<String, String> getParameters() {
128         return this.params;
129     }
130 
131     /**
132      * Returns authentication parameter with the given name, if available.
133      *
134      * @param name The name of the parameter to be returned
135      *
136      * @return the parameter with the given name
137      */
138     @Override
139     public String getParameter(final String name) {
140         if (name == null) {
141             return null;
142         }
143         return this.params.get(name.toLowerCase(Locale.ROOT));
144     }
145 
146     /**
147      * Returns authentication realm. The realm may not be null.
148      *
149      * @return the authentication realm
150      */
151     @Override
152     public String getRealm() {
153         return getParameter("realm");
154     }
155 
156     private void writeObject(final ObjectOutputStream out) throws IOException {
157         out.defaultWriteObject();
158         out.writeUTF(this.credentialsCharset.name());
159         out.writeObject(this.challengeState);
160     }
161 
162     @SuppressWarnings("unchecked")
163     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
164         in.defaultReadObject();
165         this.credentialsCharset = CharsetUtils.get(in.readUTF());
166         if (this.credentialsCharset == null) {
167             this.credentialsCharset = Consts.ASCII;
168         }
169         this.challengeState = (ChallengeState) in.readObject();
170     }
171 
172     private void readObjectNoData() throws ObjectStreamException {
173     }
174 
175 }