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 final Charset charset;
49      private final CodingErrorAction malformedInputAction;
50      private final CodingErrorAction unmappableInputAction;
51  
52      CharCodingConfig(
53              final Charset charset,
54              final CodingErrorAction malformedInputAction,
55              final CodingErrorAction unmappableInputAction) {
56          super();
57          this.charset = charset;
58          this.malformedInputAction = malformedInputAction;
59          this.unmappableInputAction = unmappableInputAction;
60      }
61  
62      public Charset getCharset() {
63          return charset;
64      }
65  
66      public CodingErrorAction getMalformedInputAction() {
67          return malformedInputAction;
68      }
69  
70      public CodingErrorAction getUnmappableInputAction() {
71          return unmappableInputAction;
72      }
73  
74      @Override
75      public String toString() {
76          final StringBuilder builder = new StringBuilder();
77          builder.append("[charset=").append(this.charset)
78                  .append(", malformedInputAction=").append(this.malformedInputAction)
79                  .append(", unmappableInputAction=").append(this.unmappableInputAction)
80                  .append("]");
81          return builder.toString();
82      }
83  
84      public static CharCodingConfig.Builder custom() {
85          return new Builder();
86      }
87  
88      public static CharCodingConfig.Builder copy(final CharCodingConfig config) {
89          Args.notNull(config, "Config");
90          return new Builder()
91              .setCharset(config.getCharset())
92              .setMalformedInputAction(config.getMalformedInputAction())
93              .setUnmappableInputAction(config.getUnmappableInputAction());
94      }
95  
96      public static class Builder {
97  
98          private Charset charset;
99          private CodingErrorAction malformedInputAction;
100         private CodingErrorAction unmappableInputAction;
101 
102         Builder() {
103         }
104 
105         public Builder setCharset(final Charset charset) {
106             this.charset = charset;
107             return this;
108         }
109 
110         public Builder setMalformedInputAction(final CodingErrorAction malformedInputAction) {
111             this.malformedInputAction = malformedInputAction;
112             if (malformedInputAction != null && this.charset == null) {
113                 this.charset = StandardCharsets.US_ASCII;
114             }
115             return this;
116         }
117 
118         public Builder setUnmappableInputAction(final CodingErrorAction unmappableInputAction) {
119             this.unmappableInputAction = unmappableInputAction;
120             if (unmappableInputAction != null && this.charset == null) {
121                 this.charset = StandardCharsets.US_ASCII;
122             }
123             return this;
124         }
125 
126         public CharCodingConfig build() {
127             Charset charsetCopy = charset;
128             if (charsetCopy == null && (malformedInputAction != null || unmappableInputAction != null)) {
129                 charsetCopy = StandardCharsets.US_ASCII;
130             }
131             return new CharCodingConfig(charsetCopy, malformedInputAction, unmappableInputAction);
132         }
133 
134     }
135 
136 }