Пример #1
0
  /**
   * Further addresses the use of EJBs as JAX-RS components.
   *
   * @throws Exception
   */
  @Test
  public void testAsJaxRSResource() throws Exception {
    log.info("entering testAsJaxRSResource()");

    // Create book.
    ClientRequest request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/create/");
    Book book1 = new Book("RESTEasy: the Sequel");
    request.body(Constants.MEDIA_TYPE_TEST_XML, book1);
    ClientResponse<?> response = request.post();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    int id1 = response.getEntity(int.class);
    log.info("id: " + id1);
    Assert.assertEquals(Counter.INITIAL_VALUE, id1);

    // Create another book.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/create/");
    Book book2 = new Book("RESTEasy: It's Alive");
    request.body(Constants.MEDIA_TYPE_TEST_XML, book2);
    response = request.post();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    int id2 = response.getEntity(int.class);
    log.info("id: " + id2);
    Assert.assertEquals(Counter.INITIAL_VALUE + 1, id2);

    // Retrieve first book.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/book/" + id1);
    request.accept(Constants.MEDIA_TYPE_TEST_XML);
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    Book result = response.getEntity(Book.class);
    log.info("book: " + book1);
    Assert.assertEquals(book1, result);

    // Retrieve second book.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/book/" + id2);
    request.accept(Constants.MEDIA_TYPE_TEST_XML);
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    result = response.getEntity(Book.class);
    log.info("book: " + book2);
    Assert.assertEquals(book2, result);

    // Verify that EJBBookReader and EJBBookWriter have been used, twice on each side.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/uses/4");
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(200, response.getStatus());
    response.releaseConnection();

    // Reset counter.
    request = new ClientRequest("http://localhost:8080/resteasy-ejb-test/reset");
    response = request.get();
    log.info("Status: " + response.getStatus());
    assertEquals(204, response.getStatus());
    response.releaseConnection();
  }
  private JaxbCommandResponse<?> executeTaskCommand(String deploymentId, Command<?> command)
      throws Exception {
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(command);

    String urlString =
        new URL(
                deploymentUrl,
                deploymentUrl.getPath() + "rest/runtime/" + DEPLOYMENT_ID + "/execute")
            .toExternalForm();
    logger.info("Client request to: " + urlString);
    ClientRequest restRequest = new ClientRequest(urlString);

    JaxbCommandsRequest commandMessage = new JaxbCommandsRequest(commands);
    assertNotNull("Commands are null!", commandMessage.getCommands());
    assertTrue("Commands are empty!", commandMessage.getCommands().size() > 0);

    String body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, body);

    ClientResponse<JaxbCommandsResponse> responseObj = restRequest.post(JaxbCommandsResponse.class);
    checkResponse(responseObj);

    JaxbCommandsResponse cmdsResp = responseObj.getEntity();
    return cmdsResp.getResponses().get(0);
  }
 public <T> T execute(Command<T> command) {
   ClientRequest restRequest = new ClientRequest(url);
   try {
     restRequest.body(MediaType.APPLICATION_XML, new JaxbCommandsRequest(deploymentId, command));
     ClientResponse<Object> response = restRequest.post(Object.class);
     if (response.getResponseStatus() == Status.OK) {
       JaxbCommandsResponse commandResponse = response.getEntity(JaxbCommandsResponse.class);
       List<JaxbCommandResponse<?>> responses = commandResponse.getResponses();
       if (responses.size() == 0) {
         return null;
       } else if (responses.size() == 1) {
         JaxbCommandResponse<?> responseObject = responses.get(0);
         if (responseObject instanceof JaxbExceptionResponse) {
           JaxbExceptionResponse exceptionResponse = (JaxbExceptionResponse) responseObject;
           String causeMessage = exceptionResponse.getCauseMessage();
           throw new RuntimeException(
               exceptionResponse.getMessage()
                   + (causeMessage == null ? "" : " Caused by: " + causeMessage));
         } else {
           return (T) responseObject.getResult();
         }
       } else {
         throw new RuntimeException("Unexpected number of results: " + responses.size());
       }
     } else {
       // TODO error handling
       throw new RuntimeException("REST request error code " + response.getResponseStatus());
     }
   } catch (Exception e) {
     throw new RuntimeException("Unable to execute REST request: " + e.getMessage(), e);
   }
 }
Пример #4
0
  // @Test
  public void testInheritance() throws Exception {
    ResteasyDeployment dep = deployment;
    ClientRequest request = new ClientRequest(generateURL("/datacenters/1"));
    ClientResponse res = request.get();
    Assert.assertEquals(200, res.getStatus());
    DataCenter dc = (DataCenter) res.getEntity(DataCenter.class);
    Assert.assertEquals(dc.getName(), "Bill");
    request = new ClientRequest(generateURL("/datacenters/1"));

    res = request.body("application/xml", dc).put();
    Assert.assertEquals(200, res.getStatus());
    dc = (DataCenter) res.getEntity(DataCenter.class);
    Assert.assertEquals(dc.getName(), "Bill");
  }
  @Test
  public void testContentType() throws Exception {
    String message =
        "--boo\r\n"
            + "Content-Disposition: form-data; name=\"foo\"\r\n"
            + "Content-Transfer-Encoding: 8bit\r\n\r\n"
            + "bar\r\n"
            + "--boo--\r\n";

    ClientRequest request = new ClientRequest(TEST_URI + "/mime");
    request.body("multipart/form-data; boundary=boo", message.getBytes());
    ClientResponse<String> response = request.post(String.class);
    Assert.assertEquals("Status code is wrong.", 20, response.getStatus() / 10);
    Assert.assertEquals(
        "Response text is wrong",
        MediaType.valueOf(TEXT_PLAIN_WITH_CHARSET_UTF_8),
        MediaType.valueOf(response.getEntity()));
  }
  public <T> OpenStackResponse request(OpenStackRequest<T> request) {
    ClientRequest client =
        new ClientRequest(
            UriBuilder.fromUri(request.endpoint() + "/" + request.path()),
            ClientRequest.getDefaultExecutor(),
            providerFactory);

    for (Map.Entry<String, List<Object>> entry : request.queryParams().entrySet()) {
      for (Object o : entry.getValue()) {
        client = client.queryParameter(entry.getKey(), String.valueOf(o));
      }
    }

    for (Entry<String, List<Object>> h : request.headers().entrySet()) {
      StringBuilder sb = new StringBuilder();
      for (Object v : h.getValue()) {
        sb.append(String.valueOf(v));
      }
      client.header(h.getKey(), sb);
    }

    if (request.entity() != null) {
      client.body(request.entity().getContentType(), request.entity().getEntity());
    }

    ClientResponse<T> response;

    try {
      response = client.httpMethod(request.method().name(), request.returnType());
    } catch (Exception e) {
      throw new RuntimeException("Unexpected client exception", e);
    }

    if (response.getStatus() == HttpStatus.SC_OK
        || response.getStatus() == HttpStatus.SC_CREATED
        || response.getStatus() == HttpStatus.SC_NO_CONTENT) {
      return new RESTEasyResponse(response);
    }

    response.releaseConnection();

    throw new OpenStackResponseException(
        response.getResponseStatus().getReasonPhrase(), response.getStatus());
  }
Пример #7
0
 private void addBody(ClientRequest request, String type) {
   Object mapped;
   if ((mapped = mapParams()) != null) {
     request.body(type, mapped);
   }
 }
  @Test
  public void testRestExecuteStartProcess() throws Exception {
    // Start process
    String urlString =
        new URL(deploymentUrl, deploymentUrl.getPath() + "rest/runtime/test/execute")
            .toExternalForm();

    ClientRequest restRequest = new ClientRequest(urlString);
    JaxbCommandsRequest commandMessage =
        new JaxbCommandsRequest(DEPLOYMENT_ID, new StartProcessCommand("org.jbpm.humantask"));
    String body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, body);

    logger.debug(">> [startProcess] " + urlString);
    ClientResponse responseObj = restRequest.post();

    assertEquals(200, responseObj.getStatus());
    JaxbCommandsResponse cmdsResp =
        (JaxbCommandsResponse) responseObj.getEntity(JaxbCommandsResponse.class);
    long procInstId = ((ProcessInstance) cmdsResp.getResponses().get(0)).getId();

    // query tasks
    restRequest = new ClientRequest(urlString);
    commandMessage =
        new JaxbCommandsRequest(DEPLOYMENT_ID, new GetTasksByProcessInstanceIdCommand(procInstId));
    body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, body);

    logger.debug(">> [getTasksByProcessInstanceId] " + urlString);
    responseObj = restRequest.post();
    assertEquals(200, responseObj.getStatus());
    JaxbCommandsResponse cmdResponse =
        (JaxbCommandsResponse) responseObj.getEntity(JaxbCommandsResponse.class);
    List list = (List) cmdResponse.getResponses().get(0).getResult();
    long taskId = (Long) list.get(0);

    // start task

    logger.debug(">> [startTask] " + urlString);
    restRequest = new ClientRequest(urlString);
    commandMessage = new JaxbCommandsRequest(new StartTaskCommand(taskId, USER_ID));
    body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, commandMessage);

    // Get response
    responseObj = restRequest.post();

    // Check response
    checkResponse(responseObj);

    urlString =
        new URL(deploymentUrl, deploymentUrl.getPath() + "rest/task/execute").toExternalForm();

    restRequest = new ClientRequest(urlString);
    commandMessage = new JaxbCommandsRequest(new CompleteTaskCommand(taskId, USER_ID, null));
    body = JaxbSerializationProvider.convertJaxbObjectToString(commandMessage);
    restRequest.body(MediaType.APPLICATION_XML, commandMessage);

    // Get response
    logger.debug(">> [completeTask] " + urlString);
    responseObj = restRequest.post();
    checkResponse(responseObj);
  }