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 /**
48 * Constructs a new instance.
49 *
50 * @param name the field name.
51 * @param value the field value.
52 */
53 public MimeField(final String name, final String value) {
54 super();
55 this.name = name;
56 this.value = value;
57 this.parameters = Collections.emptyList();
58 }
59
60 /**
61 * Constructs a new instance.
62 *
63 * @param name the field name.
64 * @param value the field value.
65 * @param parameters the field parameters.
66 * @since 4.6
67 */
68 public MimeField(final String name, final String value, final List<NameValuePair> parameters) {
69 this.name = name;
70 this.value = value;
71 this.parameters = parameters != null ?
72 Collections.unmodifiableList(new ArrayList<>(parameters)) : Collections.emptyList();
73 }
74
75 /**
76 * Constructs a new instance by copying another's properties.
77 *
78 * @param from the source field.
79 */
80 public MimeField(final MimeField from) {
81 this(from.name, from.value, from.parameters);
82 }
83
84 /**
85 * Gets the field name.
86 *
87 * @return the field name.
88 */
89 public String getName() {
90 return this.name;
91 }
92
93 /**
94 * Gets the field value.
95 *
96 * @return the field value.
97 * @since 4.6
98 */
99 public String getValue() {
100 return this.value;
101 }
102
103 /**
104 * Converts this instance to a body String.
105 *
106 * @return this instance as a body String.
107 */
108 public String getBody() {
109 final StringBuilder sb = new StringBuilder();
110 sb.append(this.value);
111 parameters.forEach(parameter -> {
112 sb.append("; ");
113 sb.append(parameter.getName());
114 sb.append("=\"");
115 final String v = parameter.getValue();
116 for (int n = 0; n < v.length(); n++) {
117 final char ch = v.charAt(n);
118 if (ch == '"' || ch == '\\' ) {
119 sb.append("\\");
120 }
121 sb.append(ch);
122 }
123 sb.append("\"");
124 });
125 return sb.toString();
126 }
127
128 /**
129 * Gets the field parameters.
130 *
131 * @return the field parameters.
132 */
133 public List<NameValuePair> getParameters() {
134 return this.parameters;
135 }
136
137 @Override
138 public String toString() {
139 final StringBuilder buffer = new StringBuilder();
140 buffer.append(this.name);
141 buffer.append(": ");
142 buffer.append(this.getBody());
143 return buffer.toString();
144 }
145
146 }