in src/main/java/com/aliyun/mns/common/http/ServiceClient.java [160:230]
protected abstract <T> Future<HttpResponse> sendRequestCore(
Request request, ExecutionContext context, HttpCallback<T> callback)
throws Exception;
private Request buildRequest(RequestMessage requestMessage,
ExecutionContext context) throws ClientException {
Request request = new Request();
request.setMethod(requestMessage.getMethod());
request.setHeaders(requestMessage.getHeaders());
// The header must be converted after the request is signed,
// otherwise the signature will be incorrect.
if (request.getHeaders() != null) {
HttpUtil.convertHeaderCharsetToIso88591(request.getHeaders());
}
final String delimiter = "/";
String uri = requestMessage.getEndpoint().toString();
if (!uri.endsWith(delimiter)
&& (requestMessage.getResourcePath() == null || !requestMessage
.getResourcePath().startsWith(delimiter))) {
uri += delimiter;
}
if (requestMessage.getResourcePath() != null) {
uri += requestMessage.getResourcePath();
}
String paramString;
try {
paramString = HttpUtil.paramToQueryString(
requestMessage.getParameters(), context.getCharset());
} catch (UnsupportedEncodingException e) {
// Assertion error because the caller should guarantee the charset.
throw new AssertionError(rm.getFormattedString("EncodingFailed",
e.getMessage()));
}
/*
* For all non-POST requests, and any POST requests that already have a
* payload, we put the encoded params directly in the URI, otherwise,
* we'll put them in the POST request's payload.
*/
boolean requestHasNoPayload = requestMessage.getContent() != null;
boolean requestIsPost = requestMessage.getMethod() == HttpMethod.POST;
boolean putParamsInUri = !requestIsPost || requestHasNoPayload;
if (paramString != null && putParamsInUri) {
uri += "?" + paramString;
}
request.setUrl(uri);
if (requestIsPost && requestMessage.getContent() == null
&& paramString != null) {
// Put the param string to the request body if POSTing and
// no content.
try {
byte[] buf = paramString.getBytes(context.getCharset());
ByteArrayInputStream content = new ByteArrayInputStream(buf);
request.setContent(content);
request.setContentLength(buf.length);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(rm.getFormattedString(
"EncodingFailed", e.getMessage()));
}
} else {
request.setContent(requestMessage.getContent());
request.setContentLength(requestMessage.getContentLength());
}
return request;
}