Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

java - Download/Save attachment using Jboss DMR

I have a Jboss CLI command which I'm trying to implement in Java using the DMR library. The step involves downloading an attachment (display/save), however there's no documentation available anywhere. Any help is appreciated.

CLI command I'm trying to convert to DMR:

attachment display --operation=/deployment=app.war:read-content(path=META-INF/MANIFEST.MF)

Below is my operation object, however there's no way to provide command to display/save attachment

ModelNode request = new ModelNode();
request.get(ClientConstants.OP_ADDR).add(ClientConstants.DEPLOYMENT, "app.war");
request.get(ClientConstants.OP).set(ClientConstants.READ_CONTENT_OPERATION);
request.get(ClientConstants.PATH).set("META-INF/MANIFEST.MF");

All that this DMR operation produces is

{
    "outcome" => "success",
    "result" => {"uuid" => "1f45b2bf-8402-4b46-a721-3e3f23db5d80"},
    "response-headers" => {"attached-streams" => [{
        "uuid" => "1f45b2bf-8402-4b46-a721-3e3f23db5d80",
        "mime-type" => "text/plain"
    }]}
}

Is there a way to either edit the operation or to use the uuid returned in the operation to download the attachment from the stream?

question from:https://stackoverflow.com/questions/65852963/download-save-attachment-using-jboss-dmr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't see any clear documentation on this either. However what you need to use is the OperationResponse to get the stream. Here's an example.

public class ReadContent {

    public static void main(final String[] args) throws Exception {

        try (ModelControllerClient client = ModelControllerClient.Factory.create("localhost", 9990)) {
            try (
                    StreamEntry entry = readContent(client, "kitchensink.war", "META-INF/MANIFEST.MF");
                    InputStream in = entry.getStream();
            ) {
                int len;
                final byte[] buffer = new byte[256];
                while ((len = in.read(buffer)) != -1) {
                    System.out.write(buffer, 0, len);
                }
            }
        }
    }

    private static StreamEntry readContent(final ModelControllerClient client, final String deploymentName, final String path) throws IOException {
        final ModelNode address = Operations.createAddress("deployment", deploymentName);
        final ModelNode op = Operations.createOperation(ClientConstants.READ_CONTENT_OPERATION, address);
        op.get(ClientConstants.PATH).set(path);

        // Execute the operation
        final OperationResponse response = client.executeOperation(Operation.Factory.create(op), OperationMessageHandler.logging);

        // Get the response node to evaluate the outcome
        final ModelNode outcome = response.getResponseNode();
        if (Operations.isSuccessfulOutcome(outcome)) {
            // Read the result and get the UUID the stream is attached to
            final String uuid = Operations.readResult(outcome).get(ClientConstants.UUID).asString();
            return response.getInputStream(uuid);
        }

        // The operation has failed, raise and exception with the failure description
        throw new RuntimeException("Failed to read the content: " + Operations.getFailureDescription(outcome).asString());
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...