chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.alipay.easysdk.kms.aliyun.credentials.http;
 
import com.alipay.easysdk.kms.aliyun.credentials.exceptions.CredentialException;
import com.alipay.easysdk.kms.aliyun.credentials.utils.Base64Utils;
 
import javax.net.ssl.KeyManager;
import javax.net.ssl.X509TrustManager;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
 
public class HttpMessage {
    protected static final String CONTENT_TYPE = "Content-Type";
    protected static final String CONTENT_MD5 = "Content-MD5";
    protected static final String CONTENT_LENGTH = "Content-Length";
    protected FormatType httpContentType = null;
    protected byte[] httpContent = null;
    protected String encoding = null;
    protected Map<String, String> headers = new HashMap<String, String>();
    protected Integer connectTimeout = null;
    protected Integer readTimeout = null;
    private String url = null;
    private MethodType method = null;
    protected boolean ignoreSSLCerts = false;
    private KeyManager[] keyManagers = null;
    private X509TrustManager[] x509TrustManagers = null;
 
    public HttpMessage(String strUrl) {
        this.url = strUrl;
    }
 
    public HttpMessage() {
    }
 
    public FormatType getHttpContentType() {
        return httpContentType;
    }
 
    public void setHttpContentType(FormatType httpContentType) {
        this.httpContentType = httpContentType;
    }
 
    public byte[] getHttpContent() {
        return httpContent;
    }
 
    public String getHttpContentString() throws CredentialException {
        String stringContent = "";
        if (this.httpContent != null) {
            try {
                if (this.encoding == null) {
                    stringContent = new String(this.httpContent);
                } else {
                    stringContent = new String(this.httpContent, this.encoding);
                }
            } catch (UnsupportedEncodingException e) {
                throw new CredentialException("UnsupportedEncoding: Can not parse response due to unsupported encoding.");
            }
        }
        return stringContent;
    }
 
    public void setHttpContent(byte[] httpContent, String encoding, FormatType format) throws NoSuchAlgorithmException {
        this.httpContent = httpContent;
        if (null == httpContent) {
            this.headers.remove(CONTENT_MD5);
            this.headers.put(CONTENT_LENGTH, "0");
            this.headers.remove(CONTENT_TYPE);
            this.httpContentType = null;
            this.httpContent = null;
            this.encoding = null;
            return;
        }
 
        // for GET HEADER DELETE OPTION method, sdk should ignore the content
        if (getMethod() != null && !getMethod().hasContent()) {
            httpContent = new byte[0];
        }
 
        this.httpContent = httpContent;
        this.encoding = encoding;
        String contentLen = String.valueOf(httpContent.length);
        String strMd5 = md5Sum(httpContent);
        this.headers.put(CONTENT_MD5, strMd5);
        this.headers.put(CONTENT_LENGTH, contentLen);
        if (null != format) {
            this.headers.put(CONTENT_TYPE, FormatType.mapFormatToAccept(format));
        }
    }
 
    public static String md5Sum(byte[] buff) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(buff);
        return Base64Utils.encode(messageDigest);
 
    }
 
    public String getEncoding() {
        return encoding;
    }
 
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
 
    public Map<String, String> getHeaders() {
        return Collections.unmodifiableMap(headers);
    }
 
    public String getHeaderValue(String name) {
        return this.headers.get(name);
    }
 
    public void putHeaderParameter(String name, String value) {
        if (null != name && null != value) {
            this.headers.put(name, value);
        }
    }
 
    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }
 
    public Integer getConnectTimeout() {
        return connectTimeout;
    }
 
    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }
 
    public Integer getReadTimeout() {
        return readTimeout;
    }
 
    public void setReadTimeout(Integer readTimeout) {
        this.readTimeout = readTimeout;
    }
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public MethodType getMethod() {
        return method;
    }
 
    public void setMethod(MethodType method) {
        this.method = method;
    }
 
    public boolean isIgnoreSSLCerts() {
        return ignoreSSLCerts;
    }
 
    public void setIgnoreSSLCerts(boolean ignoreSSLCerts) {
        this.ignoreSSLCerts = ignoreSSLCerts;
    }
 
    public KeyManager[] getKeyManagers() {
        return keyManagers;
    }
 
    public void setKeyManagers(KeyManager[] keyManagers) {
        this.keyManagers = keyManagers;
    }
 
    public X509TrustManager[] getX509TrustManagers() {
        return x509TrustManagers;
    }
 
    public void setX509TrustManagers(X509TrustManager[] x509TrustManagers) {
        this.x509TrustManagers = x509TrustManagers;
    }
}