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  
28  package org.apache.hc.client5.http.auth;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  
33  /**
34   *  Immutable class encapsulating Kerberos configuration options.
35   *
36   *  @since 4.6
37   */
38  @Contract(threading = ThreadingBehavior.IMMUTABLE)
39  public class KerberosConfig implements Cloneable {
40  
41      public enum Option {
42  
43          DEFAULT,
44          ENABLE,
45          DISABLE
46  
47      }
48  
49      public static final KerberosConfig DEFAULT = new Builder().build();
50  
51      private final Option stripPort;
52      private final Option useCanonicalHostname;
53      private final Option requestDelegCreds;
54  
55      /**
56       * Intended for CDI compatibility
57      */
58      protected KerberosConfig() {
59          this(Option.DEFAULT, Option.DEFAULT, Option.DEFAULT);
60      }
61  
62      KerberosConfig(
63              final Option stripPort,
64              final Option useCanonicalHostname,
65              final Option requestDelegCreds) {
66          super();
67          this.stripPort = stripPort;
68          this.useCanonicalHostname = useCanonicalHostname;
69          this.requestDelegCreds = requestDelegCreds;
70      }
71  
72      public Option getStripPort() {
73          return stripPort;
74      }
75  
76      public Option getUseCanonicalHostname() {
77          return useCanonicalHostname;
78      }
79  
80      public Option getRequestDelegCreds() {
81          return requestDelegCreds;
82      }
83  
84      @Override
85      protected KerberosConfig clone() throws CloneNotSupportedException {
86          return (KerberosConfig) super.clone();
87      }
88  
89      @Override
90      public String toString() {
91          final StringBuilder builder = new StringBuilder();
92          builder.append("[");
93          builder.append("stripPort=").append(stripPort);
94          builder.append(", useCanonicalHostname=").append(useCanonicalHostname);
95          builder.append(", requestDelegCreds=").append(requestDelegCreds);
96          builder.append("]");
97          return builder.toString();
98      }
99  
100     public static KerberosConfig.Builder custom() {
101         return new Builder();
102     }
103 
104     public static KerberosConfig.Builder copy(final KerberosConfig config) {
105         return new Builder()
106                 .setStripPort(config.getStripPort())
107                 .setUseCanonicalHostname(config.getUseCanonicalHostname())
108                 .setRequestDelegCreds(config.getRequestDelegCreds());
109     }
110 
111     public static class Builder {
112 
113         private Option stripPort;
114         private Option useCanonicalHostname;
115         private Option requestDelegCreds;
116 
117         Builder() {
118             super();
119             this.stripPort = Option.DEFAULT;
120             this.useCanonicalHostname = Option.DEFAULT;
121             this.requestDelegCreds = Option.DEFAULT;
122         }
123 
124         public Builder setStripPort(final Option stripPort) {
125             this.stripPort = stripPort;
126             return this;
127         }
128 
129         public Builder setStripPort(final boolean stripPort) {
130             this.stripPort = stripPort ? Option.ENABLE : Option.DISABLE;
131             return this;
132         }
133 
134         public Builder setUseCanonicalHostname(final Option useCanonicalHostname) {
135             this.useCanonicalHostname = useCanonicalHostname;
136             return this;
137         }
138 
139         public Builder setUseCanonicalHostname(final boolean useCanonicalHostname) {
140             this.useCanonicalHostname = useCanonicalHostname ? Option.ENABLE : Option.DISABLE;
141             return this;
142         }
143 
144         public Builder setRequestDelegCreds(final Option requestDelegCreds) {
145             this.requestDelegCreds = requestDelegCreds;
146             return this;
147         }
148 
149         public KerberosConfig build() {
150             return new KerberosConfig(
151                     stripPort,
152                     useCanonicalHostname,
153                     requestDelegCreds);
154         }
155 
156     }
157 
158 }