001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.impl.auth;
028
029import java.io.IOException;
030import java.io.ObjectInputStream;
031import java.io.ObjectOutputStream;
032import java.io.ObjectStreamException;
033import java.io.Serializable;
034import java.nio.charset.Charset;
035import java.util.HashMap;
036import java.util.Locale;
037import java.util.Map;
038
039import org.apache.http.Consts;
040import org.apache.http.HeaderElement;
041import org.apache.http.HttpRequest;
042import org.apache.http.auth.ChallengeState;
043import org.apache.http.auth.MalformedChallengeException;
044import org.apache.http.auth.params.AuthPNames;
045import org.apache.http.message.BasicHeaderValueParser;
046import org.apache.http.message.HeaderValueParser;
047import org.apache.http.message.ParserCursor;
048import org.apache.http.util.CharArrayBuffer;
049import org.apache.http.util.CharsetUtils;
050
051/**
052 * Abstract authentication scheme class that lays foundation for all
053 * RFC 2617 compliant authentication schemes and provides capabilities common
054 * to all authentication schemes defined in RFC 2617.
055 *
056 * @since 4.0
057 */
058@SuppressWarnings("deprecation")
059public abstract class RFC2617Scheme extends AuthSchemeBase implements Serializable {
060
061    private static final long serialVersionUID = -2845454858205884623L;
062
063    private final Map<String, String> params;
064    private transient Charset credentialsCharset;
065
066    /**
067     * Creates an instance of {@code RFC2617Scheme} with the given challenge
068     * state.
069     *
070     * @since 4.2
071     *
072     * @deprecated (4.3) do not use.
073     */
074    @Deprecated
075    public RFC2617Scheme(final ChallengeState challengeState) {
076        super(challengeState);
077        this.params = new HashMap<String, String>();
078        this.credentialsCharset = Consts.ASCII;
079    }
080
081    /**
082     * @since 4.3
083     */
084    public RFC2617Scheme(final Charset credentialsCharset) {
085        super();
086        this.params = new HashMap<String, String>();
087        this.credentialsCharset = credentialsCharset != null ? credentialsCharset : Consts.ASCII;
088    }
089
090    public RFC2617Scheme() {
091        this(Consts.ASCII);
092    }
093
094
095    /**
096     * @since 4.3
097     */
098    public Charset getCredentialsCharset() {
099        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}