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.entity.mime;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.List;
33  
34  import org.apache.hc.core5.http.NameValuePair;
35  
36  /**
37   * Minimal MIME field.
38   *
39   * @since 4.0
40   */
41  public class MimeField {
42  
43      private final String name;
44      private final String value;
45      private final List<NameValuePair> parameters;
46  
47      public MimeField(final String name, final String value) {
48          super();
49          this.name = name;
50          this.value = value;
51          this.parameters = Collections.emptyList();
52      }
53  
54      /**
55       * @since 4.6
56       */
57      public MimeField(final String name, final String value, final List<NameValuePair> parameters) {
58          this.name = name;
59          this.value = value;
60          this.parameters = parameters != null ?
61                  Collections.unmodifiableList(new ArrayList<>(parameters)) : Collections.emptyList();
62      }
63  
64      public MimeField(final MimeField from) {
65          this(from.name, from.value, from.parameters);
66      }
67  
68      public String getName() {
69          return this.name;
70      }
71  
72      /**
73       * @since 4.6
74       */
75      public String getValue() {
76          return this.value;
77      }
78  
79      public String getBody() {
80          final StringBuilder sb = new StringBuilder();
81          sb.append(this.value);
82          for (int i = 0; i < this.parameters.size(); i++) {
83              final NameValuePair parameter = this.parameters.get(i);
84              sb.append("; ");
85              sb.append(parameter.getName());
86              sb.append("=\"");
87              sb.append(parameter.getValue());
88              sb.append("\"");
89          }
90          return sb.toString();
91      }
92  
93      public List<NameValuePair> getParameters() {
94          return this.parameters;
95      }
96  
97      @Override
98      public String toString() {
99          final StringBuilder buffer = new StringBuilder();
100         buffer.append(this.name);
101         buffer.append(": ");
102         buffer.append(this.getBody());
103         return buffer.toString();
104     }
105 
106 }