Search in sources :

Example 1 with Request

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");
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Request(org.apache.http.client.fluent.Request) WechatRequest(org.usc.wechat.mp.sdk.vo.WechatRequest) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 2 with Request

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();
    }
}
Also used : Request(org.apache.http.client.fluent.Request) Result(com.google.gerrit.acceptance.PushOneCommit.Result) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 3 with Request

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();
}
Also used : Request(org.apache.http.client.fluent.Request) Result(com.google.gerrit.acceptance.PushOneCommit.Result) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 4 with Request

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();
}
Also used : Request(org.apache.http.client.fluent.Request) Result(com.google.gerrit.acceptance.PushOneCommit.Result) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with Request

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);
}
Also used : BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) Request(org.apache.http.client.fluent.Request) BasicHeader(org.apache.http.message.BasicHeader) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

Request (org.apache.http.client.fluent.Request)11 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 Result (com.google.gerrit.acceptance.PushOneCommit.Result)4 URI (java.net.URI)4 Test (org.junit.Test)4 BasicHeader (org.apache.http.message.BasicHeader)3 HttpResponse (org.apache.http.HttpResponse)2 HttpResponseException (org.apache.http.client.HttpResponseException)2 StringEntity (org.apache.http.entity.StringEntity)2 RestResponse (com.google.gerrit.acceptance.RestResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 Executor (org.apache.http.client.fluent.Executor)1 Response (org.apache.http.client.fluent.Response)1 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1