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 com.foxinmy.weixin4j.http.apache.mime;
29
30 import com.foxinmy.weixin4j.http.ContentType;
31 import com.foxinmy.weixin4j.http.apache.content.AbstractContentBody;
32 import com.foxinmy.weixin4j.http.apache.content.ContentBody;
33
34 /**
35 * FormBodyPart class represents a content body that can be used as a part of multipart encoded
36 * entities. This class automatically populates the header with standard fields based on
37 * the content description of the enclosed body.
38 *
39 * @since 4.0
40 */
41 public class FormBodyPart {
42
43 private final String name;
44 private final Header header;
45 private final ContentBody body;
46
47 FormBodyPart(final String name, final ContentBody body, final Header header) {
48 super();
49 this.name = name;
50 this.body = body;
51 this.header = header != null ? header : new Header();
52 }
53
54 /**
55 * (4.4) use {@link com.foxinmy.weixin4j.http.apache.mime.FormBodyPartBuilder}.
56 */
57 public FormBodyPart(final String name, final ContentBody body) {
58 super();
59 this.name = name;
60 this.body = body;
61 this.header = new Header();
62
63 generateContentDisp(body);
64 generateContentType(body);
65 generateTransferEncoding(body);
66 }
67
68 public String getName() {
69 return this.name;
70 }
71
72 public ContentBody getBody() {
73 return this.body;
74 }
75
76 public Header getHeader() {
77 return this.header;
78 }
79
80 public void addField(final String name, final String value) {
81 this.header.addField(new MinimalField(name, value));
82 }
83
84 /**
85 * (4.4) use {@link com.foxinmy.weixin4j.http.apache.mime.FormBodyPartBuilder}.
86 */
87 protected void generateContentDisp(final ContentBody body) {
88 final StringBuilder buffer = new StringBuilder();
89 buffer.append("form-data; name=\"");
90 buffer.append(getName());
91 buffer.append("\"");
92 if (body.getFilename() != null) {
93 buffer.append("; filename=\"");
94 buffer.append(body.getFilename());
95 buffer.append("\"");
96 }
97 addField(MIME.CONTENT_DISPOSITION, buffer.toString());
98 }
99
100 /**
101 * (4.4) use {@link com.foxinmy.weixin4j.http.apache.mime.FormBodyPartBuilder}.
102 */
103 protected void generateContentType(final ContentBody body) {
104 final ContentType contentType;
105 if (body instanceof AbstractContentBody) {
106 contentType = ((AbstractContentBody) body).getContentType();
107 } else {
108 contentType = null;
109 }
110 if (contentType != null) {
111 addField(MIME.CONTENT_TYPE, contentType.toString());
112 } else {
113 final StringBuilder buffer = new StringBuilder();
114 buffer.append(body.getMimeType()); // MimeType cannot be null
115 if (body.getCharset() != null) { // charset may legitimately be null
116 buffer.append("; charset=");
117 buffer.append(body.getCharset());
118 }
119 addField(MIME.CONTENT_TYPE, buffer.toString());
120 }
121 }
122
123 /**
124 * (4.4) use {@link com.foxinmy.weixin4j.http.apache.mime.FormBodyPartBuilder}.
125 */
126 protected void generateTransferEncoding(final ContentBody body) {
127 addField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()); // TE cannot be null
128 }
129 }