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.Queue;
30  
31  import org.apache.hc.core5.util.Args;
32  
33  /**
34   * This class represents the actual state of authentication handshake including the current {@link AuthScheme}
35   * used for request authorization as well as a collection of backup authentication options if available.
36   *
37   * @since 4.5
38   */
39  public class AuthExchange {
40  
41      public enum State {
42  
43          UNCHALLENGED, CHALLENGED, HANDSHAKE, FAILURE, SUCCESS
44  
45      }
46  
47      private State state;
48      private AuthScheme authScheme;
49      private Queue<AuthScheme> authOptions;
50      private String pathPrefix;
51  
52      public AuthExchange() {
53          super();
54          this.state = State.UNCHALLENGED;
55      }
56  
57      public void reset() {
58          this.state = State.UNCHALLENGED;
59          this.authOptions = null;
60          this.authScheme = null;
61          this.pathPrefix = null;
62      }
63  
64      public State getState() {
65          return this.state;
66      }
67  
68      public void setState(final State state) {
69          this.state = state != null ? state : State.UNCHALLENGED;
70      }
71  
72      /**
73       * Returns actual {@link AuthScheme}. May be null.
74       */
75      public AuthScheme getAuthScheme() {
76          return this.authScheme;
77      }
78  
79      /**
80       * Returns {@code true} if the actual authentication scheme is connection based.
81       */
82      public boolean isConnectionBased() {
83          return this.authScheme != null && this.authScheme.isConnectionBased();
84      }
85  
86      /**
87       * @since 5.2
88       */
89      public String getPathPrefix() {
90          return pathPrefix;
91      }
92  
93      /**
94       * @since 5.2
95       */
96      public void setPathPrefix(final String pathPrefix) {
97          this.pathPrefix = pathPrefix;
98      }
99  
100     /**
101      * Resets the auth state with {@link AuthScheme} and clears auth options.
102      *
103      * @param authScheme auth scheme. May not be null.
104      */
105     public void select(final AuthScheme authScheme) {
106         Args.notNull(authScheme, "Auth scheme");
107         this.authScheme = authScheme;
108         this.authOptions = null;
109     }
110 
111     /**
112      * Returns available auth options. May be null.
113      */
114     public Queue<AuthScheme> getAuthOptions() {
115         return this.authOptions;
116     }
117 
118     /**
119      * Updates the auth state with a queue of auth options.
120      *
121      * @param authOptions a queue of auth options. May not be null or empty.
122      */
123     public void setOptions(final Queue<AuthScheme> authOptions) {
124         Args.notEmpty(authOptions, "Queue of auth options");
125         this.authOptions = authOptions;
126     }
127 
128     @Override
129     public String toString() {
130         final StringBuilder buffer = new StringBuilder();
131         buffer.append("[").append(this.state);
132         if (this.authScheme != null) {
133             buffer.append(" ").append(this.authScheme);
134         }
135         buffer.append("]");
136         return buffer.toString();
137     }
138 
139 }