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.hc.client5.http.auth;
28  
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Collections;
32  import java.util.List;
33  
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.http.NameValuePair;
37  import org.apache.hc.core5.util.Args;
38  
39  /**
40   * This class represents an authentication challenge consisting of a auth scheme
41   * and either a single parameter or a list of name / value pairs.
42   *
43   * @since 5.0
44   */
45  @Contract(threading = ThreadingBehavior.IMMUTABLE)
46  public final class AuthChallenge {
47  
48      private final ChallengeType challengeType;
49      private final String schemeName;
50      private final String value;
51      private final List<NameValuePair> params;
52  
53      public AuthChallenge(final ChallengeType challengeType, final String schemeName, final String value, final List<? extends NameValuePair> params) {
54          super();
55          this.challengeType = Args.notNull(challengeType, "Challenge type");
56          this.schemeName = Args.notNull(schemeName, "schemeName");
57          this.value = value;
58          this.params = params != null ? Collections.unmodifiableList(new ArrayList<>(params)) : null;
59      }
60  
61      public AuthChallenge(final ChallengeType challengeType, final String schemeName, final NameValuePair... params) {
62          this(challengeType, schemeName, null, Arrays.asList(params));
63      }
64  
65      public ChallengeType getChallengeType() {
66          return challengeType;
67      }
68  
69      public String getSchemeName() {
70          return schemeName;
71      }
72  
73      public String getValue() {
74          return value;
75      }
76  
77      public List<NameValuePair> getParams() {
78          return params;
79      }
80  
81      @Override
82      public String toString() {
83          final StringBuilder buffer = new StringBuilder();
84          buffer.append(schemeName).append(" ");
85          if (value != null) {
86              buffer.append(value);
87          } else if (params != null) {
88              buffer.append(params);
89          }
90          return buffer.toString();
91      }
92  
93  }
94