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.core5.http.config;
29  
30  import java.nio.charset.Charset;
31  import java.nio.charset.CodingErrorAction;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.util.Args;
37  
38  /**
39   * HTTP/1.1 char coding configuration.
40   *
41   * @since 4.3
42   */
43  @Contract(threading = ThreadingBehavior.IMMUTABLE)
44  public class CharCodingConfig {
45  
46      public static final CharCodingConfig DEFAULT = new Builder().build();
47  
48      private static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
49  
50      private final Charset charset;
51      private final CodingErrorAction malformedInputAction;
52      private final CodingErrorAction unmappableInputAction;
53  
54      CharCodingConfig(
55              final Charset charset,
56              final CodingErrorAction malformedInputAction,
57              final CodingErrorAction unmappableInputAction) {
58          super();
59          this.charset = charset;
60          this.malformedInputAction = malformedInputAction;
61          this.unmappableInputAction = unmappableInputAction;
62      }
63  
64      public Charset getCharset() {
65          return charset;
66      }
67  
68      public CodingErrorAction getMalformedInputAction() {
69          return malformedInputAction;
70      }
71  
72      public CodingErrorAction getUnmappableInputAction() {
73          return unmappableInputAction;
74      }
75  
76      @Override
77      public String toString() {
78          final StringBuilder builder = new StringBuilder();
79          builder.append("[charset=").append(this.charset)
80                  .append(", malformedInputAction=").append(this.malformedInputAction)
81                  .append(", unmappableInputAction=").append(this.unmappableInputAction)
82                  .append("]");
83          return builder.toString();
84      }
85  
86      public static CharCodingConfig.Builder custom() {
87          return new Builder();
88      }
89  
90      public static CharCodingConfig.Builder copy(final CharCodingConfig config) {
91          Args.notNull(config, "Config");
92          return new Builder()
93              .setCharset(config.getCharset())
94              .setMalformedInputAction(config.getMalformedInputAction())
95              .setUnmappableInputAction(config.getUnmappableInputAction());
96      }
97  
98      public static class Builder {
99  
100         private Charset charset;
101         private CodingErrorAction malformedInputAction;
102         private CodingErrorAction unmappableInputAction;
103 
104         Builder() {
105         }
106 
107         public Builder setCharset(final Charset charset) {
108             this.charset = charset;
109             return this;
110         }
111 
112         public Builder setMalformedInputAction(final CodingErrorAction malformedInputAction) {
113             this.malformedInputAction = malformedInputAction;
114             if (malformedInputAction != null && this.charset == null) {
115                 this.charset = DEFAULT_CHARSET;
116             }
117             return this;
118         }
119 
120         public Builder setUnmappableInputAction(final CodingErrorAction unmappableInputAction) {
121             this.unmappableInputAction = unmappableInputAction;
122             if (unmappableInputAction != null && this.charset == null) {
123                 this.charset = DEFAULT_CHARSET;
124             }
125             return this;
126         }
127 
128         public CharCodingConfig build() {
129             Charset charsetCopy = charset;
130             if (charsetCopy == null && (malformedInputAction != null || unmappableInputAction != null)) {
131                 charsetCopy = DEFAULT_CHARSET;
132             }
133             return new CharCodingConfig(charsetCopy, malformedInputAction, unmappableInputAction);
134         }
135 
136     }
137 
138 }