private static void addContent(
     final OperationBuilder builder, final ModelNode op, final InputStream content) {
   final ModelNode contentNode = op.get(CONTENT);
   final ModelNode contentItem = contentNode.get(0);
   contentItem.get(ClientConstants.INPUT_STREAM_INDEX).set(0);
   builder.addInputStream(content);
 }
  /**
   * Handle a form POST deployment upload request.
   *
   * @param http The HttpExchange object that allows access to the request and response.
   * @throws IOException if an error occurs while attempting to extract the deployment from the
   *     multipart/form data.
   */
  private void processUploadRequest(final HttpExchange http) throws IOException {
    ModelNode response = null;

    try {
      SeekResult result = seekToDeployment(http);

      final ModelNode dmr = new ModelNode();
      dmr.get("operation").set("upload-deployment-stream");
      dmr.get("address").setEmptyList();
      dmr.get("input-stream-index").set(0);

      OperationBuilder operation = new OperationBuilder(dmr);
      operation.addInputStream(result.stream);
      response = modelController.execute(operation.build());
      drain(http.getRequestBody());
    } catch (Throwable t) {
      // TODO Consider draining input stream
      ROOT_LOGGER.uploadError(t);
      http.sendResponseHeaders(INTERNAL_SERVER_ERROR, -1);

      return;
    }

    // TODO Determine what format the response should be in for a deployment upload request.
    writeResponse(http, false, false, response, OK, false, TEXT_HTML);
  }
  @Test
  public void testDeploymentViaStream() throws Exception {
    ModelNode content = new ModelNode();
    content.get(INPUT_STREAM_INDEX).set(0);
    ModelNode composite =
        createDeploymentOperation(
            content, OTHER_SERVER_GROUP_DEPLOYMENT_ADDRESS, MAIN_SERVER_GROUP_DEPLOYMENT_ADDRESS);
    OperationBuilder builder = new OperationBuilder(composite, true);
    builder.addInputStream(webArchive.as(ZipExporter.class).exportAsInputStream());

    executeOnMaster(builder.build());

    performHttpCall(DomainTestSupport.slaveAddress, 8080);
  }
  @Test
  public void testJsfWorks() throws Exception {
    ModelNode content = new ModelNode();
    content.get(INPUT_STREAM_INDEX).set(0);
    // Just be lazy here and deploy the jsf-test.war with the same name as the other deployments we
    // tried
    ModelNode composite =
        createDeploymentOperation(
            content, OTHER_SERVER_GROUP_DEPLOYMENT_ADDRESS, MAIN_SERVER_GROUP_DEPLOYMENT_ADDRESS);
    OperationBuilder builder = new OperationBuilder(composite, true);
    builder.addInputStream(jsfTestArchive.as(ZipExporter.class).exportAsInputStream());

    executeOnMaster(builder.build());

    performHttpCall(DomainTestSupport.slaveAddress, 8080, "test/home.jsf", "Bean Works");
  }
示例#5
0
  @Override
  protected void doHandle(CommandContext ctx) {

    ModelControllerClient client = ctx.getModelControllerClient();

    if (!ctx.hasArguments()) {
      printList(ctx, Util.getDeployments(client));
      return;
    }

    String filePath = null;
    String name = null;
    String runtimeName = null;

    for (String arg : ctx.getArguments()) {
      if (filePath == null) {
        filePath = arg;
      } else if (name == null) {
        name = arg;
      } else {
        runtimeName = arg;
      }
    }

    if (filePath == null) {
      printList(ctx, Util.getDeployments(client));
      return;
    }

    File f = new File(filePath);
    if (!f.exists()) {
      ctx.printLine("The path doesn't exist: " + f.getAbsolutePath());
      return;
    }

    if (name == null) {
      name = f.getName();
    }

    if (Util.isDeployed(name, client)) {
      if (ctx.hasSwitch("f")) {
        DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();

        ModelNode result;

        // replace
        builder = new DefaultOperationRequestBuilder();
        builder.setOperationName("full-replace-deployment");
        builder.addProperty("name", name);
        if (runtimeName != null) {
          builder.addProperty("runtime-name", runtimeName);
        }

        FileInputStream is = null;
        try {
          is = new FileInputStream(f);
          ModelNode request = builder.buildRequest();
          OperationBuilder op = OperationBuilder.Factory.create(request);
          op.addInputStream(is);
          request.get("input-stream-index").set(0);
          result = client.execute(op.build());
        } catch (Exception e) {
          ctx.printLine("Failed to replace the deployment: " + e.getLocalizedMessage());
          return;
        } finally {
          StreamUtils.safeClose(is);
        }
        if (!Util.isSuccess(result)) {
          ctx.printLine(Util.getFailureDescription(result));
          return;
        }

        ctx.printLine("'" + name + "' re-deployed successfully.");
      } else {
        ctx.printLine("'" + name + "' is already deployed (use -f to force re-deploy).");
      }

      return;
    } else {

      DefaultOperationRequestBuilder builder;

      ModelNode result;

      // add
      builder = new DefaultOperationRequestBuilder();
      builder.setOperationName("add");
      builder.addNode("deployment", name);
      if (runtimeName != null) {
        builder.addProperty("runtime-name", runtimeName);
      }

      FileInputStream is = null;
      try {
        is = new FileInputStream(f);
        ModelNode request = builder.buildRequest();
        OperationBuilder op = OperationBuilder.Factory.create(request);
        op.addInputStream(is);
        request.get("input-stream-index").set(0);
        result = client.execute(op.build());
      } catch (Exception e) {
        ctx.printLine(
            "Failed to add the deployment content to the repository: " + e.getLocalizedMessage());
        return;
      } finally {
        StreamUtils.safeClose(is);
      }
      if (!Util.isSuccess(result)) {
        ctx.printLine(Util.getFailureDescription(result));
        return;
      }

      // deploy
      builder = new DefaultOperationRequestBuilder();
      builder.setOperationName("deploy");
      builder.addNode("deployment", name);
      try {
        ModelNode request = builder.buildRequest();
        result = client.execute(request);
      } catch (Exception e) {
        ctx.printLine("Failed to deploy: " + e.getLocalizedMessage());
        return;
      }
      if (!Util.isSuccess(result)) {
        ctx.printLine(Util.getFailureDescription(result));
        return;
      }
      ctx.printLine("'" + name + "' deployed successfully.");
    }
  }