Exemplo n.º 1
0
 /**
  * 获取聊天消息
  *
  * @param queryStrNode
  */
 public static ObjectNode getChatMessages(ObjectNode queryStrNode) {
   ObjectNode objectNode = factory.objectNode();
   // check appKey format
   if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
     LOGGER.error("Bad format of Appkey: " + APPKEY);
     objectNode.put("message", "Bad format of Appkey");
     return objectNode;
   }
   try {
     JerseyWebTarget webTarget =
         EndPoints.CHATMESSAGES_TARGET
             .resolveTemplate("org_name", APPKEY.split("#")[0])
             .resolveTemplate("app_name", APPKEY.split("#")[1]);
     if (null != queryStrNode
         && null != queryStrNode.get("ql")
         && !StringUtils.isEmpty(queryStrNode.get("ql").asText())) {
       webTarget = webTarget.queryParam("ql", queryStrNode.get("ql").asText());
     }
     if (null != queryStrNode
         && null != queryStrNode.get("limit")
         && !StringUtils.isEmpty(queryStrNode.get("limit").asText())) {
       webTarget = webTarget.queryParam("limit", queryStrNode.get("limit").asText());
     }
     if (null != queryStrNode
         && null != queryStrNode.get("cursor")
         && !StringUtils.isEmpty(queryStrNode.get("cursor").asText())) {
       webTarget = webTarget.queryParam("cursor", queryStrNode.get("cursor").asText());
     }
     objectNode =
         JerseyUtils.sendRequest(webTarget, null, credential, HTTPMethod.METHOD_GET, null);
   } catch (Exception e) {
     e.printStackTrace();
   }
   return objectNode;
 }
  public static <T> T post(Object requestObject, Class<T> responseClass, String path) {
    try {

      jerseyWebTarget.path(path);

      Response response =
          jerseyWebTarget
              .request(MediaType.APPLICATION_JSON_TYPE)
              .post(Entity.entity(requestObject, MediaType.APPLICATION_JSON_TYPE));

      if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
      }

      System.out.println("Output from Service for path " + path + " .... \n");
      T output = response.readEntity(responseClass);
      System.out.println(ToStringBuilder.reflectionToString(output));

      return output;
    } catch (Exception e) {

      e.printStackTrace();
      return null;
    }
  }
  /**
   * We can inject either proxy or a WebTarget for low level manipulations and assertions.
   *
   * @param webTarget configured resource ready for use, injected by Arquillian
   */
  @Test
  public void createCustomerBareJAXRSResource(
      @ArquillianResteasyResource("rest/customer") JerseyWebTarget webTarget) {
    //        Given
    final Invocation.Builder invocationBuilder = webTarget.request();
    final Invocation invocation =
        invocationBuilder.buildPost(Entity.entity(new Customer(), MediaType.APPLICATION_JSON_TYPE));

    //        When
    final Response response = invocation.invoke();

    //        Then
    assertEquals(deploymentURL + "rest/customer", webTarget.getUri().toASCIIString());
    assertEquals(MediaType.APPLICATION_JSON, response.getMediaType().toString());
    assertEquals(200, response.getStatus());
  }