@Override
  public RequestBuilder transform(HttpServletRequest request)
      throws NoSuchRequestHandlingMethodException, URISyntaxException, IOException {
    RequestBuilder requestBuilder = predecessor.transform(request);

    String requestContent = request.getReader().lines().collect(Collectors.joining(""));
    if (!requestContent.isEmpty()) {
      StringEntity entity = new StringEntity(requestContent, ContentType.APPLICATION_JSON);
      requestBuilder.setEntity(entity);
    }

    return requestBuilder;
  }
Пример #2
0
  private <B, R> R executeRequest(
      RequestSpec requestSpec,
      PathParams pathParams,
      B requestBody,
      Class<R> responseType,
      int expectedStatus) {
    String url =
        String.format(
            ADMIN_URL_PREFIX + requestSpec.path(pathParams), scheme, host, port, urlPathPrefix);
    RequestBuilder requestBuilder =
        RequestBuilder.create(requestSpec.method().getName()).setUri(url);

    if (requestBody != null) {
      requestBuilder.setEntity(jsonStringEntity(Json.write(requestBody)));
    }

    String responseBodyString = safelyExecuteRequest(url, expectedStatus, requestBuilder.build());

    return responseType == Void.class ? null : Json.read(responseBodyString, responseType);
  }
  /**
   * 设置请求参数<br>
   * 如果想提交一段字符串<br>
   * 那么需要将header中的content-type设置成非application/x-www-form-urlencoded;<br>
   * 将字符串放到postdata中参数名postdata
   *
   * @param method
   * @param postdata
   * @param headers
   * @return
   */
  protected RequestBuilder selectRequestMethod(
      String method, Map<String, String> postdata, Map<String, String> headers) {
    if (method == null || method.equalsIgnoreCase("get")) {
      return RequestBuilder.get();
    } else if (method.equalsIgnoreCase("post")) {
      RequestBuilder requestBuilder = RequestBuilder.post();
      if (postdata != null) {

        String contenttype = "application/x-www-form-urlencoded; charset=UTF-8";
        if (headers != null && headers.size() > 0) {
          for (String ksy : headers.keySet()) {
            if ("Content-Type".equalsIgnoreCase(ksy)) {
              contenttype = headers.get(ksy).toLowerCase().trim();
              break;
            }
          }
        }
        String charset = ""; // 提交数据的传输编码
        if (contenttype.contains("charset")) {
          // 如果在请求的编码中,存在网页编码,那么提取改编码
          String ncharset =
              contenttype.substring(contenttype.lastIndexOf("=") + 1, contenttype.length());
          // 如果提取到的编码合法那么使用该编码
          if (checkCharset(ncharset)) {
            charset = ncharset;
          }
        }

        // 如果编码为空或不存在,在提交数据中提取编码
        if (null == charset || "".equals(charset)) {
          charset = postdata.get("charset"); // 提交数据的传输编码
        }
        if (null == charset || "".equals(charset)) {
          charset = "UTF-8"; // 提交数据的传输编码
        }

        if ("".equals(contenttype) || contenttype.toLowerCase().contains("x-www-form-urlencoded")) {
          List<NameValuePair> formParams = new ArrayList<NameValuePair>();
          for (String str : postdata.keySet()) {
            NameValuePair n = new BasicNameValuePair(str, postdata.get(str));
            formParams.add(n);
          }

          HttpEntity entity = null;
          try {

            entity = new UrlEncodedFormEntity(formParams, charset);
          } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
          }
          requestBuilder.setEntity(entity);
        } else {

          log.info("post Content-Type : [ " + contenttype + " ] , pay attention to it .");
          String pstdata = postdata.get("postdata"); // 提交的数据
          if ("".equals(pstdata) || pstdata == null) {
            pstdata = postdata.get(""); // 提交的数据
          }
          if (pstdata == null) {
            pstdata = "";
          }
          StringEntity entity = new StringEntity(pstdata, charset); // 解决中文乱码问题
          entity.setContentEncoding(charset);
          entity.setContentType(contenttype);
          entity.setChunked(true);
          requestBuilder.setEntity(entity);
        }
      } else {
        log.warn("The Method Is Post,But No Post Data .");
      }
      return requestBuilder;
    } else if (method.equalsIgnoreCase("head")) {
      return RequestBuilder.head();
    } else if (method.equalsIgnoreCase("put")) {
      return RequestBuilder.put();
    } else if (method.equalsIgnoreCase("delete")) {
      return RequestBuilder.delete();
    } else if (method.equalsIgnoreCase("trace")) {
      return RequestBuilder.trace();
    }
    throw new IllegalArgumentException("Illegal HTTP Method " + method);
  }