/
HttpResponseMessage.java
70 lines (59 loc) · 2.03 KB
/
HttpResponseMessage.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.shinemo.mpush.common.message;
import com.shinemo.mpush.api.connection.Connection;
import com.shinemo.mpush.api.protocol.Packet;
import com.shinemo.mpush.tools.Constants;
import com.shinemo.mpush.tools.MPushUtil;
import io.netty.buffer.ByteBuf;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Created by ohun on 2016/2/15.
*/
public class HttpResponseMessage extends ByteBufMessage {
public int statusCode;
public String reasonPhrase;
public Map<String, String> headers = new HashMap<>();
public byte[] body;
public HttpResponseMessage(Packet message, Connection connection) {
super(message, connection);
}
@Override
public void decode(ByteBuf body) {
statusCode = decodeInt(body);
reasonPhrase = decodeString(body);
headers = MPushUtil.headerFromString(decodeString(body));
this.body = decodeBytes(body);
}
@Override
public void encode(ByteBuf body) {
encodeInt(body, statusCode);
encodeString(body, reasonPhrase);
encodeString(body, MPushUtil.headerToString(headers));
encodeBytes(body, this.body);
}
public static HttpResponseMessage from(HttpRequestMessage src) {
return new HttpResponseMessage(src.createResponse(), src.connection);
}
public HttpResponseMessage setStatusCode(int statusCode) {
this.statusCode = statusCode;
return this;
}
public HttpResponseMessage setReasonPhrase(String reasonPhrase) {
this.reasonPhrase = reasonPhrase;
return this;
}
public HttpResponseMessage addHeader(String name, String value) {
this.headers.put(name, value);
return this;
}
@Override
public String toString() {
return "HttpResponseMessage{" +
"statusCode=" + statusCode +
", reasonPhrase='" + reasonPhrase + '\'' +
", headers=" + headers +
", body=" + (body == null ? "" : new String(body, Constants.UTF_8)) +
'}';
}
}