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.http.config;
29  
30  import java.nio.charset.Charset;
31  import java.nio.charset.CodingErrorAction;
32  
33  import org.apache.http.Consts;
34  import org.apache.http.annotation.ThreadingBehavior;
35  import org.apache.http.annotation.Contract;
36  import org.apache.http.util.Args;
37  
38  /**
39   * HTTP connection configuration.
40   *
41   * @since 4.3
42   */
43  @Contract(threading = ThreadingBehavior.IMMUTABLE)
44  public class ConnectionConfig implements Cloneable {
45  
46      public static final ConnectionConfig DEFAULT = new Builder().build();
47  
48      private final int bufferSize;
49      private final int fragmentSizeHint;
50      private final Charset charset;
51      private final CodingErrorAction malformedInputAction;
52      private final CodingErrorAction unmappableInputAction;
53      private final MessageConstraints messageConstraints;
54  
55      ConnectionConfig(
56              final int bufferSize,
57              final int fragmentSizeHint,
58              final Charset charset,
59              final CodingErrorAction malformedInputAction,
60              final CodingErrorAction unmappableInputAction,
61              final MessageConstraints messageConstraints) {
62          super();
63          this.bufferSize = bufferSize;
64          this.fragmentSizeHint = fragmentSizeHint;
65          this.charset = charset;
66          this.malformedInputAction = malformedInputAction;
67          this.unmappableInputAction = unmappableInputAction;
68          this.messageConstraints = messageConstraints;
69      }
70  
71      public int getBufferSize() {
72          return bufferSize;
73      }
74  
75      public int getFragmentSizeHint() {
76          return fragmentSizeHint;
77      }
78  
79      public Charset getCharset() {
80          return charset;
81      }
82  
83      public CodingErrorAction getMalformedInputAction() {
84          return malformedInputAction;
85      }
86  
87      public CodingErrorAction getUnmappableInputAction() {
88          return unmappableInputAction;
89      }
90  
91      public MessageConstraints getMessageConstraints() {
92          return messageConstraints;
93      }
94  
95      @Override
96      protected ConnectionConfig clone() throws CloneNotSupportedException {
97          return (ConnectionConfig) super.clone();
98      }
99  
100     @Override
101     public String toString() {
102         final StringBuilder builder = new StringBuilder();
103         builder.append("[bufferSize=").append(this.bufferSize)
104                 .append(", fragmentSizeHint=").append(this.fragmentSizeHint)
105                 .append(", charset=").append(this.charset)
106                 .append(", malformedInputAction=").append(this.malformedInputAction)
107                 .append(", unmappableInputAction=").append(this.unmappableInputAction)
108                 .append(", messageConstraints=").append(this.messageConstraints)
109                 .append("]");
110         return builder.toString();
111     }
112 
113     public static ConnectionConfig.Builder custom() {
114         return new Builder();
115     }
116 
117     public static ConnectionConfig.Builder copy(final ConnectionConfig config) {
118         Args.notNull(config, "Connection config");
119         return new Builder()
120             .setCharset(config.getCharset())
121             .setMalformedInputAction(config.getMalformedInputAction())
122             .setUnmappableInputAction(config.getUnmappableInputAction())
123             .setMessageConstraints(config.getMessageConstraints());
124     }
125 
126     public static class Builder {
127 
128         private int bufferSize;
129         private int fragmentSizeHint;
130         private Charset charset;
131         private CodingErrorAction malformedInputAction;
132         private CodingErrorAction unmappableInputAction;
133         private MessageConstraints messageConstraints;
134 
135         Builder() {
136             this.fragmentSizeHint = -1;
137         }
138 
139         public Builder setBufferSize(final int bufferSize) {
140             this.bufferSize = bufferSize;
141             return this;
142         }
143 
144         public Builder setFragmentSizeHint(final int fragmentSizeHint) {
145             this.fragmentSizeHint = fragmentSizeHint;
146             return this;
147         }
148 
149         public Builder setCharset(final Charset charset) {
150             this.charset = charset;
151             return this;
152         }
153 
154         public Builder setMalformedInputAction(final CodingErrorAction malformedInputAction) {
155             this.malformedInputAction = malformedInputAction;
156             if (malformedInputAction != null && this.charset == null) {
157                 this.charset = Consts.ASCII;
158             }
159             return this;
160         }
161 
162         public Builder setUnmappableInputAction(final CodingErrorAction unmappableInputAction) {
163             this.unmappableInputAction = unmappableInputAction;
164             if (unmappableInputAction != null && this.charset == null) {
165                 this.charset = Consts.ASCII;
166             }
167             return this;
168         }
169 
170         public Builder setMessageConstraints(final MessageConstraints messageConstraints) {
171             this.messageConstraints = messageConstraints;
172             return this;
173         }
174 
175         public ConnectionConfig build() {
176             Charset cs = charset;
177             if (cs == null && (malformedInputAction != null || unmappableInputAction != null)) {
178                 cs = Consts.ASCII;
179             }
180             final int bufSize = this.bufferSize > 0 ? this.bufferSize : 8 * 1024;
181             final int fragmentHintSize  = this.fragmentSizeHint >= 0 ? this.fragmentSizeHint : bufSize;
182             return new ConnectionConfig(
183                     bufSize,
184                     fragmentHintSize,
185                     cs,
186                     malformedInputAction,
187                     unmappableInputAction,
188                     messageConstraints);
189         }
190 
191     }
192 
193 }