use of org.apache.http.client.fluent.Request in project wechat-mp-sdk by usc.
the class HttpUtil method postBodyRequest.
public static <T extends JsonRtn> T postBodyRequest(WechatRequest request, License license, Map<String, String> paramMap, Object requestBody, Class<T> jsonRtnClazz) {
if (request == null || license == null || jsonRtnClazz == null) {
return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "missing post request params");
}
String requestUrl = request.getUrl();
String requestName = request.getName();
List<NameValuePair> form = buildNameValuePairs(paramMap);
String body = requestBody != null ? JSONObject.toJSONString(requestBody) : StringUtils.EMPTY;
URI uri = buildURI(requestUrl, buildNameValuePairs(license));
if (uri == null) {
return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "build request URI failed");
}
try {
Request post = Request.Post(uri).connectTimeout(CONNECT_TIMEOUT).socketTimeout(SOCKET_TIMEOUT);
if (paramMap != null) {
post.bodyForm(form);
}
if (StringUtils.isNotEmpty(body)) {
post.bodyString(body, ContentType.create("text/html", Consts.UTF_8));
}
String rtnJson = post.execute().handleResponse(HttpUtil.UTF8_CONTENT_HANDLER);
T jsonRtn = JsonRtnUtil.parseJsonRtn(rtnJson, jsonRtnClazz);
log.info(requestName + " result:\n url={},\n form={},\n body={},\n rtn={},\n {}", uri, form, body, rtnJson, jsonRtn);
return jsonRtn;
} catch (Exception e) {
String msg = requestName + " failed:\n url=" + uri + ",\n form=" + form + ",\n body=" + body;
log.error(msg, e);
return JsonRtnUtil.buildFailureJsonRtn(jsonRtnClazz, "post request server failed");
}
}
use of org.apache.http.client.fluent.Request in project gerrit by GerritCodeReview.
the class CorsIT method preflightBadMethod.
@Test
public void preflightBadMethod() throws Exception {
Result change = createChange();
for (String method : new String[] { "POST", "PUT", "DELETE", "PATCH" }) {
Request req = Request.Options(adminRestSession.url() + "/a/changes/" + change.getChangeId() + "/detail");
req.addHeader(ORIGIN, "http://example.com");
req.addHeader(ACCESS_CONTROL_REQUEST_METHOD, method);
adminRestSession.execute(req).assertBadRequest();
}
}
use of org.apache.http.client.fluent.Request in project gerrit by GerritCodeReview.
the class CorsIT method preflightBadOrigin.
@Test
public void preflightBadOrigin() throws Exception {
Result change = createChange();
Request req = Request.Options(adminRestSession.url() + "/a/changes/" + change.getChangeId() + "/detail");
req.addHeader(ORIGIN, "http://evil.attacker");
req.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET");
adminRestSession.execute(req).assertBadRequest();
}
use of org.apache.http.client.fluent.Request in project gerrit by GerritCodeReview.
the class CorsIT method preflightBadHeader.
@Test
public void preflightBadHeader() throws Exception {
Result change = createChange();
Request req = Request.Options(adminRestSession.url() + "/a/changes/" + change.getChangeId() + "/detail");
req.addHeader(ORIGIN, "http://example.com");
req.addHeader(ACCESS_CONTROL_REQUEST_METHOD, "GET");
req.addHeader(ACCESS_CONTROL_REQUEST_HEADERS, "X-Gerrit-Auth");
adminRestSession.execute(req).assertBadRequest();
}
use of org.apache.http.client.fluent.Request in project gerrit by GerritCodeReview.
the class RestSession method putRaw.
public RestResponse putRaw(String endPoint, RawInput stream) throws IOException {
Preconditions.checkNotNull(stream);
Request put = Request.Put(getUrl(endPoint));
put.addHeader(new BasicHeader("Content-Type", stream.getContentType()));
put.body(new BufferedHttpEntity(new InputStreamEntity(stream.getInputStream(), stream.getContentLength())));
return execute(put);
}
Aggregations