1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package com.foxinmy.weixin4j.http.apache.mime;
29
30 import java.util.List;
31
32 import com.foxinmy.weixin4j.http.ContentType;
33 import com.foxinmy.weixin4j.http.apache.content.AbstractContentBody;
34 import com.foxinmy.weixin4j.http.apache.content.ContentBody;
35
36
37
38
39
40
41 public class FormBodyPartBuilder {
42
43 private String name;
44 private ContentBody body;
45 private final Header header;
46
47 public static FormBodyPartBuilder create(final String name, final ContentBody body) {
48 return new FormBodyPartBuilder(name, body);
49 }
50
51 public static FormBodyPartBuilder create() {
52 return new FormBodyPartBuilder();
53 }
54
55 FormBodyPartBuilder(final String name, final ContentBody body) {
56 this();
57 this.name = name;
58 this.body = body;
59 }
60
61 FormBodyPartBuilder() {
62 this.header = new Header();
63 }
64
65 public FormBodyPartBuilder setName(final String name) {
66 this.name = name;
67 return this;
68 }
69
70 public FormBodyPartBuilder setBody(final ContentBody body) {
71 this.body = body;
72 return this;
73 }
74
75 public FormBodyPartBuilder addField(final String name, final String value) {
76 this.header.addField(new MinimalField(name, value));
77 return this;
78 }
79
80 public FormBodyPartBuilder setField(final String name, final String value) {
81 this.header.setField(new MinimalField(name, value));
82 return this;
83 }
84
85 public FormBodyPartBuilder removeFields(final String name) {
86 this.header.removeFields(name);
87 return this;
88 }
89
90 public FormBodyPart build() {
91 final Header headerCopy = new Header();
92 final List<MinimalField> fields = this.header.getFields();
93 for (final MinimalField field: fields) {
94 headerCopy.addField(field);
95 }
96 if (headerCopy.getField(MIME.CONTENT_DISPOSITION) == null) {
97 final StringBuilder buffer = new StringBuilder();
98 buffer.append("form-data; name=\"");
99 buffer.append(encodeForHeader(this.name));
100 buffer.append("\"");
101 if (this.body.getFilename() != null) {
102 buffer.append("; filename=\"");
103 buffer.append(encodeForHeader(this.body.getFilename()));
104 buffer.append("\"");
105 }
106 headerCopy.addField(new MinimalField(MIME.CONTENT_DISPOSITION, buffer.toString()));
107 }
108 if (headerCopy.getField(MIME.CONTENT_TYPE) == null) {
109 final ContentType contentType;
110 if (body instanceof AbstractContentBody) {
111 contentType = ((AbstractContentBody) body).getContentType();
112 } else {
113 contentType = null;
114 }
115 if (contentType != null) {
116 headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, contentType.toString()));
117 } else {
118 final StringBuilder buffer = new StringBuilder();
119 buffer.append(this.body.getMimeType());
120 if (this.body.getCharset() != null) {
121 buffer.append("; charset=");
122 buffer.append(this.body.getCharset());
123 }
124 headerCopy.addField(new MinimalField(MIME.CONTENT_TYPE, buffer.toString()));
125 }
126 }
127 if (headerCopy.getField(MIME.CONTENT_TRANSFER_ENC) == null) {
128
129 headerCopy.addField(new MinimalField(MIME.CONTENT_TRANSFER_ENC, body.getTransferEncoding()));
130 }
131 return new FormBodyPart(this.name, this.body, headerCopy);
132 }
133
134 private static String encodeForHeader(final String headerName) {
135 if (headerName == null) {
136 return null;
137 }
138 final StringBuilder sb = new StringBuilder();
139 for (int i = 0; i < headerName.length(); i++) {
140 final char x = headerName.charAt(i);
141 if (x == '"' || x == '\\' || x == '\r') {
142 sb.append("\\");
143 }
144 sb.append(x);
145 }
146 return sb.toString();
147 }
148
149 }