Skip to content

Commit 9415f91

Browse files
committed
bug fix mediaType
1 parent f804ddb commit 9415f91

File tree

19 files changed

+172
-162
lines changed

19 files changed

+172
-162
lines changed

bus-core/src/main/java/org/aoju/bus/core/lang/MediaType.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,24 @@ public class MediaType {
304304
public final static MediaType MULTIPART_RELATED_APPLICATION_DICOM_XML_TYPE =
305305
new MediaType("multipart", "related", Collections.singletonMap("type", APPLICATION_DICOM_XML));
306306

307+
/**
308+
* "text/event-stream;type=\"application/event-stream\""
309+
*/
310+
public static final String SERVER_SENT_EVENTS = "text/event-stream";
311+
public static final MediaType SERVER_SENT_EVENTS_TYPE = new MediaType("text", "event-stream");
312+
313+
/**
314+
* "application/json-patch+json;type=\"application/json-patch+json\""
315+
*/
316+
public static final String APPLICATION_JSON_PATCH_JSON = "application/json-patch+json";
317+
public static final MediaType APPLICATION_JSON_PATCH_JSON_TYPE = new MediaType("application", "json-patch+json");
318+
319+
/**
320+
* "application/soap+xml;type=\"application/soap+xml\""
321+
*/
322+
public static final String APPLICATION_SOAP_XML = "application/soap+xml";
323+
public static final MediaType APPLICATION_SOAP_XML_TYPE = new MediaType("application", "soap+xml");
324+
307325

308326
public static final String TOKEN = "([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)";
309327
public static final String QUOTED = "\"([^\"]*)\"";
@@ -316,7 +334,6 @@ public class MediaType {
316334
public final String mediaType;
317335
public Map<String, String> parameters;
318336

319-
320337
public MediaType() {
321338
this(null, MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, null, null);
322339
}
@@ -325,7 +342,6 @@ public MediaType(String mediaType) {
325342
this(mediaType, MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, null, null);
326343
}
327344

328-
329345
public MediaType(String type, String subtype) {
330346
this(null, type, subtype, null, null);
331347
}
@@ -347,10 +363,10 @@ public MediaType(String type, String subtype, String charset, Map<String, String
347363
}
348364

349365
public MediaType(String mediaType, String type, String subtype, String charset, Map<String, String> params) {
350-
this.mediaType = null == mediaType ? APPLICATION_FORM_URLENCODED : mediaType;
351366
this.type = null == type ? MEDIA_TYPE_WILDCARD : type;
352367
this.subtype = null == subtype ? MEDIA_TYPE_WILDCARD : subtype;
353368
this.charset = null == charset ? Charset.DEFAULT_UTF_8 : charset;
369+
this.mediaType = null == mediaType ? this.type + Symbol.C_SLASH + this.subtype + ";charset=" + this.charset : mediaType;
354370
if (MapKit.isNotEmpty(params)) {
355371
params = new TreeMap((Comparator<String>) (o1, o2) -> o1.compareToIgnoreCase(o2));
356372
}

bus-http/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ body 的内容类型,关于 MediaType 的更多信息可以查看 RFC 2045,R
115115
RequestBody requestBody = new RequestBody() {
116116

117117
@Override
118-
public MediaType contentType() {
118+
public MediaType mediaType() {
119119
return MediaType.valueOf("text/x-markdown; charsets=utf-8");
120120
}
121121

bus-http/src/main/java/org/aoju/bus/http/Httpv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ private static void addCopyInterceptor(Httpd.Builder builder) {
442442
// 若是下载文件,则必须指定在 IO 线程操作
443443
return response;
444444
}
445-
ResponseBody newBody = ResponseBody.create(body.contentType(), body.bytes());
445+
ResponseBody newBody = ResponseBody.create(body.mediaType(), body.bytes());
446446
return response.newBuilder().body(newBody).build();
447447
});
448448
}

bus-http/src/main/java/org/aoju/bus/http/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public ResponseBody peekBody(long byteCount) throws IOException {
176176
Buffer buffer = new Buffer();
177177
peeked.request(byteCount);
178178
buffer.write(peeked, Math.min(byteCount, peeked.getBuffer().size()));
179-
return ResponseBody.create(body.contentType(), buffer.size(), buffer);
179+
return ResponseBody.create(body.mediaType(), buffer.size(), buffer);
180180
}
181181

182182
/**

bus-http/src/main/java/org/aoju/bus/http/accord/Exchange.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public void writeRequestHeaders(Request request) throws IOException {
9595

9696
public Sink createRequestBody(Request request, boolean duplex) throws IOException {
9797
this.duplex = duplex;
98-
long contentLength = request.body().contentLength();
98+
long length = request.body().length();
9999
eventListener.requestBodyStart(call);
100-
Sink rawRequestBody = httpCodec.createRequestBody(request, contentLength);
101-
return new RequestBodySink(rawRequestBody, contentLength);
100+
Sink rawRequestBody = httpCodec.createRequestBody(request, length);
101+
return new RequestBodySink(rawRequestBody, length);
102102
}
103103

104104
public void flushRequest() throws IOException {
@@ -146,11 +146,11 @@ public void responseHeadersEnd(Response response) {
146146
public ResponseBody openResponseBody(Response response) throws IOException {
147147
try {
148148
eventListener.responseBodyStart(call);
149-
String contentType = response.header("Content-Type");
150-
long contentLength = httpCodec.reportedContentLength(response);
149+
String mediaType = response.header("Content-Type");
150+
long length = httpCodec.reportedContentLength(response);
151151
Source rawSource = httpCodec.openResponseBodySource(response);
152-
ResponseBodySource source = new ResponseBodySource(rawSource, contentLength);
153-
return new RealResponseBody(contentType, contentLength, IoKit.buffer(source));
152+
ResponseBodySource source = new ResponseBodySource(rawSource, length);
153+
return new RealResponseBody(mediaType, length, IoKit.buffer(source));
154154
} catch (IOException e) {
155155
eventListener.responseFailed(call, e);
156156
trackFailure(e);

bus-http/src/main/java/org/aoju/bus/http/bodys/FormBody.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public String value(int index) {
7272
}
7373

7474
@Override
75-
public MediaType contentType() {
75+
public MediaType mediaType() {
7676
return MediaType.APPLICATION_FORM_URLENCODED_TYPE;
7777
}
7878

7979
@Override
80-
public long contentLength() {
80+
public long length() {
8181
return writeOrCountBytes(null, true);
8282
}
8383

bus-http/src/main/java/org/aoju/bus/http/bodys/MultipartBody.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public class MultipartBody extends RequestBody {
5353

5454
private final ByteString boundary;
5555
private final MediaType originalType;
56-
private final MediaType contentType;
56+
private final MediaType mediaType;
5757
private final List<Part> parts;
5858
private long contentLength = -1L;
5959

60-
MultipartBody(ByteString boundary, MediaType type, List<Part> parts) {
60+
MultipartBody(ByteString boundary, MediaType mediaType, List<Part> parts) {
6161
this.boundary = boundary;
62-
this.originalType = type;
63-
this.contentType = MediaType.valueOf(type + "; boundary=" + boundary.utf8());
62+
this.originalType = mediaType;
63+
this.mediaType = MediaType.valueOf(mediaType.toString() + "; boundary=" + boundary.utf8());
6464
this.parts = org.aoju.bus.http.Builder.immutableList(parts);
6565
}
6666

@@ -122,12 +122,12 @@ public Part part(int index) {
122122
* A combination of {@link #type()} and {@link #boundary()}.
123123
*/
124124
@Override
125-
public MediaType contentType() {
126-
return contentType;
125+
public MediaType mediaType() {
126+
return mediaType;
127127
}
128128

129129
@Override
130-
public long contentLength() throws IOException {
130+
public long length() throws IOException {
131131
long result = contentLength;
132132
if (result != -1L) return result;
133133
return contentLength = writeOrCountBytes(null, true);
@@ -174,14 +174,14 @@ private long writeOrCountBytes(BufferSink sink, boolean countBytes) throws IOExc
174174
}
175175
}
176176

177-
MediaType contentType = body.contentType();
178-
if (null != contentType) {
177+
MediaType mediaType = body.mediaType();
178+
if (null != mediaType) {
179179
sink.writeUtf8(Header.CONTENT_TYPE + ": ")
180-
.writeUtf8(contentType.toString())
180+
.writeUtf8(mediaType.toString())
181181
.write(CRLF);
182182
}
183183

184-
long contentLength = body.contentLength();
184+
long contentLength = body.length();
185185
if (contentLength != -1) {
186186
sink.writeUtf8("Content-Length: ")
187187
.writeDecimalLong(contentLength)

bus-http/src/main/java/org/aoju/bus/http/bodys/RealResponseBody.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ public class RealResponseBody extends ResponseBody {
4040
* 使用字符串避免在需要时才解析内容类型
4141
* 这也避免了由格式不正确的内容类型引起的问题
4242
*/
43-
private final String contentTypeString;
44-
private final long contentLength;
43+
private final String mediaType;
44+
private final long length;
4545
private final BufferSource source;
4646

4747
public RealResponseBody(
48-
String contentTypeString, long contentLength, BufferSource source) {
49-
this.contentTypeString = contentTypeString;
50-
this.contentLength = contentLength;
48+
String mediaType, long length, BufferSource source) {
49+
this.mediaType = mediaType;
50+
this.length = length;
5151
this.source = source;
5252
}
5353

5454
@Override
55-
public MediaType contentType() {
56-
return null != contentTypeString ? MediaType.valueOf(contentTypeString) : null;
55+
public MediaType mediaType() {
56+
return null != mediaType ? MediaType.valueOf(mediaType) : null;
5757
}
5858

5959
@Override
60-
public long contentLength() {
61-
return contentLength;
60+
public long length() {
61+
return length;
6262
}
6363

6464
@Override

bus-http/src/main/java/org/aoju/bus/http/bodys/RequestBody.java

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,40 @@ public abstract class RequestBody {
4646

4747
/**
4848
* 返回传输{@code content}的新请求体。
49-
* 如果{@code contentType}是非空且缺少字符集,则使用UTF-8
49+
* 如果{@code mediaType}是非空且缺少字符集,则使用UTF-8
5050
*
51-
* @param contentType 请求类型
52-
* @param content 内容
51+
* @param mediaType 请求类型
52+
* @param content 内容
5353
* @return 传输请求体
5454
*/
55-
public static RequestBody create(MediaType contentType, String content) {
55+
public static RequestBody create(MediaType mediaType, String content) {
5656
java.nio.charset.Charset charset = Charset.UTF_8;
57-
if (null != contentType) {
58-
charset = contentType.charset();
59-
if (null == charset) {
60-
charset = Charset.UTF_8;
61-
contentType = MediaType.valueOf(contentType + "; charset=utf-8");
62-
}
57+
if (null != mediaType) {
58+
charset = null != mediaType.charset() ? mediaType.charset() : Charset.UTF_8;
6359
}
60+
6461
byte[] bytes = content.getBytes(charset);
65-
return create(contentType, bytes);
62+
return create(mediaType, bytes);
6663
}
6764

6865
/**
6966
* 返回发送{@code content}的新请求体
7067
*
71-
* @param contentType 请求类型
72-
* @param content 内容
68+
* @param mediaType 请求类型
69+
* @param content 内容
7370
* @return 传输请求体
7471
*/
7572
public static RequestBody create(
76-
final MediaType contentType,
73+
final MediaType mediaType,
7774
final ByteString content) {
7875
return new RequestBody() {
7976
@Override
80-
public MediaType contentType() {
81-
return contentType;
77+
public MediaType mediaType() {
78+
return mediaType;
8279
}
8380

8481
@Override
85-
public long contentLength() {
82+
public long length() {
8683
return content.size();
8784
}
8885

@@ -96,37 +93,37 @@ public void writeTo(BufferSink sink) throws IOException {
9693
/**
9794
* 发送{@code content}的新请求体
9895
*
99-
* @param contentType 请求类型
100-
* @param content 内容
96+
* @param mediaType 媒体类型
97+
* @param content 内容
10198
* @return 传输请求体
10299
*/
103-
public static RequestBody create(final MediaType contentType, final byte[] content) {
104-
return create(contentType, content, 0, content.length);
100+
public static RequestBody create(final MediaType mediaType, final byte[] content) {
101+
return create(mediaType, content, 0, content.length);
105102
}
106103

107104
/**
108105
* 发送{@code content}的新请求体
109106
*
110-
* @param contentType 请求类型
111-
* @param content 内容
112-
* @param offset 偏移量
113-
* @param byteCount 当前大小
107+
* @param mediaType 媒体类型
108+
* @param content 内容
109+
* @param offset 偏移量
110+
* @param byteCount 当前大小
114111
* @return 传输请求体
115112
*/
116-
public static RequestBody create(final MediaType contentType, final byte[] content,
113+
public static RequestBody create(final MediaType mediaType, final byte[] content,
117114
final int offset, final int byteCount) {
118115
if (null == content) {
119116
throw new NullPointerException("content == null");
120117
}
121118
Builder.checkOffsetAndCount(content.length, offset, byteCount);
122119
return new RequestBody() {
123120
@Override
124-
public MediaType contentType() {
125-
return contentType;
121+
public MediaType mediaType() {
122+
return mediaType;
126123
}
127124

128125
@Override
129-
public long contentLength() {
126+
public long length() {
130127
return byteCount;
131128
}
132129

@@ -140,23 +137,23 @@ public void writeTo(BufferSink sink) throws IOException {
140137
/**
141138
* 新的请求体,该请求体传输{@code file}的内容
142139
*
143-
* @param contentType 请求类型
144-
* @param file 文件
140+
* @param mediaType 请求类型
141+
* @param file 文件
145142
* @return 传输请求体
146143
*/
147-
public static RequestBody create(final MediaType contentType, final File file) {
144+
public static RequestBody create(final MediaType mediaType, final File file) {
148145
if (null == file) {
149146
throw new NullPointerException("file == null");
150147
}
151148

152149
return new RequestBody() {
153150
@Override
154-
public MediaType contentType() {
155-
return contentType;
151+
public MediaType mediaType() {
152+
return mediaType;
156153
}
157154

158155
@Override
159-
public long contentLength() {
156+
public long length() {
160157
return file.length();
161158
}
162159

@@ -172,15 +169,15 @@ public void writeTo(BufferSink sink) throws IOException {
172169
/**
173170
* @return 返回此主体的媒体类型
174171
*/
175-
public abstract MediaType contentType();
172+
public abstract MediaType mediaType();
176173

177174
/**
178175
* 返回调用{@link #writeTo}时写入{@code sink}的字节数,如果该计数未知,则返回-1
179176
*
180177
* @return 计数信息
181178
* @throws IOException 异常
182179
*/
183-
public long contentLength() throws IOException {
180+
public long length() throws IOException {
184181
return -1;
185182
}
186183

@@ -242,4 +239,5 @@ public boolean isDuplex() {
242239
public boolean isOneShot() {
243240
return false;
244241
}
245-
}
242+
243+
}

0 commit comments

Comments
 (0)