How to deploy archive to Jboss as 7 using HTTP managment api?
I tryied to use the following code that I created from those RHQ plugin classes
public class Main2 {
public static final String MANAGEMENT = "/management";
public static void main(String[] args) throws ClientProtocolException, IOException {
String runtimeName= "jboss-as-numberguess33.war";
String filename = "/Users/mac/jboss-as-numberguess.war";
String deploymentName="jboss-as-numberguess.war";
File file=new File(filename);
//STEP 1 UPLOADING
ASUploadConnection uploadConnection=new ASUploadConnection("localhost", 9990,"username", "password");
uploadConnection.getOutputStream(filename);
JsonNode uploadResult = uploadConnection.finishUpload();
if (ASUploadConnection.isErrorReply(uploadResult)) {
System.out.println("Error reply");
}
else{
JsonNode resultNode = uploadResult.get("result");
String hash = resultNode.get("BYTES_VALUE").getTextValue();
System.out.println(uploadResult);
//--------------------------------------
//STEP 2 DEPLOING
System.out.println("Deploying [" + runtimeName + "] to server");
Operation step1 = new Operation("add", "deployment", runtimeName);
step1.addAdditionalProperty("hash", new PROPERTY_VALUE("BYTES_VALUE", hash));
List<Object> content = new ArrayList<Object>(1);
Map<String, Object> contentValues = new HashMap<String, Object>();
contentValues.put("hash", new PROPERTY_VALUE("BYTES_VALUE", hash));
content.add(contentValues);
step1.addAdditionalProperty("content", content);
step1.addAdditionalProperty("name", deploymentName);
step1.addAdditionalProperty("runtime-name", runtimeName);
CompositeOperation cop = new CompositeOperation();
cop.addStep(step1);
Operation step2 = new Operation("deploy", step1.getAddress());
cop.addStep(step2);
execute(cop);
}
}
private static void execute(Operation operation) throws JsonGenerationException, JsonMappingException, IOException {
long requestStartTime = System.currentTimeMillis();
int timeoutSec=10;
HttpURLConnection conn=null;
OutputStream out;
try {
URL url = new URL("http", "localhost", 9990, MANAGEMENT);
conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getURL());
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.addRequestProperty("Content-Type", "application/json");
conn.addRequestProperty("Accept", "application/json");
conn.setInstanceFollowRedirects(false);
int timeoutMillis = timeoutSec * 1000;
conn.setConnectTimeout(timeoutMillis);
conn.setReadTimeout(timeoutMillis);
if (conn.getReadTimeout() != timeoutMillis) {
System.out.println("Read timeout did not get set on HTTP connection - the JRE uses a broken timeout mechanism - nothing we can do.");
}
out = conn.getOutputStream();
//------------------------------------------------------
Authenticator passwordAuthenticator = new AS7Authenticator("username","password");
Authenticator.setDefault(passwordAuthenticator);
// read system property "as7plugin.verbose"
Boolean verbose = true;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String json_to_send = mapper.writeValueAsString(operation);
// Check for spaces in the path, which the AS7 server will reject. Log verbose error and
// generate failure indicator.
if ((operation != null) && (operation.getAddress() != null) && operation.getAddress().getPath() != null) {
if (containsSpaces(operation.getAddress().getPath())) {
Result noResult = new Result();
String outcome = "- Path '" + operation.getAddress().getPath()
+ "' is invalid as it contains spaces -";
if (verbose) {
System.out.println(outcome);
}
noResult.setFailureDescription(outcome);
noResult.setOutcome("failure");
JsonNode invalidPathResult = mapper.valueToTree(noResult);
System.out.println(invalidPathResult);
}
}
if (verbose) {
System.out.println("JSON to send: " + json_to_send);
System.out.println(conn.getInputStream());
}
mapper.writeValue(out, operation);
out.flush();
out.close();
InputStream inputStream = (conn.getInputStream() != null) ? conn.getInputStream() : conn.getErrorStream();
if (inputStream != null) {
// Note: slurp() will close the stream once it's done slurping it.
byte[] responseBodyBytes = StreamUtil.slurp(inputStream);
ByteArrayInputStream arrayInputStream=new ByteArrayInputStream(responseBodyBytes);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(arrayInputStream));
String responseBody="";
while (bufferedReader.ready()) {
responseBody+=bufferedReader.readLine()+"\n";
}
String outcome;
JsonNode operationResult=null;
if (!responseBody.isEmpty()) {
outcome = responseBody;
operationResult = mapper.readTree(outcome);
if (verbose) {
ObjectMapper om2 = new ObjectMapper();
om2.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
String tmp = om2.writeValueAsString(operationResult);
System.out.println(tmp);
}
} else {
System.out.println("Null stream");
}
System.out.println(operationResult);
}
} catch (IllegalArgumentException iae) {}
}
public static boolean containsSpaces(String path) {
boolean includesSpaces = false;
StringTokenizer components = new StringTokenizer(path, " ");
if (components.countTokens() > 1) {
includesSpaces = true;
}
return includesSpaces;
}
}
STEP 1(uloading) I thought it works fine, it returns success reponse, also says
06:18:37,551 INFO [org.jboss.as.repository] (HttpManagementService-threads - 6) JBAS014900: Content added at location /servers/jboss-as-7.1.2.Final/standalone/data/content/da/39a3ee5e6b4b0d3255bfef95601890afd80709/content
But this content file is empty!!!
Also when STEP 2(deploying) runs it throws following error
Server returned HTTP response code: 500 for URL: http://localhost:9990/management
In stack trace I have
07:19:35,870 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit."jboss-as-numberguess33.war".STRUCTURE: org.jboss.msc.service.StartException in service jboss.deployment.unit."jboss-as-numberguess33.war".STRUCTURE: JBAS018733: Failed to process phase STRUCTURE of deployment "jboss-as-numberguess33.war"
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:123) [jboss-as-server-7.1.2.Final-SNAPSHOT.jar:7.1.2.Final-SNAPSHOT]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [classes.jar:1.6.0_24]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [classes.jar:1.6.0_24]
at java.lang.Thread.run(Thread.java:680) [classes.jar:1.6.0_24]
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS018740: Failed to mount deployment content
at org.jboss.as.server.deployment.module.DeploymentRootMountProcessor.deploy(DeploymentRootMountProcessor.java:92) [jboss-as-server-7.1.2.Final-SNAPSHOT.jar:7.1.2.Final-SNAPSHOT]
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116) [jboss-as-server-7.1.2.Final-SNAPSHOT.jar:7.1.2.Final-SNAPSHOT]
... 5 more
Caused by: java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method) [classes.jar:1.6.0_24]
at java.util.zip.ZipFile.<init>(ZipFile.java:127) [classes.jar:1.6.0_24]
at java.util.zip.ZipFile.<init>(ZipFile.java:144) [classes.jar:1.6.0_24]
at org.jboss.vfs.VFSUtils.unzip(VFSUtils.java:845)
at org.jboss.vfs.VFS.mountZipExpanded(VFS.java:536)
at org.jboss.vfs.VFS.mountZipExpanded(VFS.java:567)
at org.jboss.as.server.deployment.DeploymentMountProvider$Factory$ServerDeploymentRepositoryImpl.mountDeploymentContent(DeploymentMountProvider.java:97) [jboss-as-server-7.1.2.Final-SNAPSHOT.jar:7.1.2.Final-SNAPSHOT]
at org.jboss.as.server.deployment.module.DeploymentRootMountProcessor.deploy(DeploymentRootMountProcessor.java:88) [jboss-as-server-7.1.2.Final-SNAPSHOT.jar:7.1.2.Final-SNAPSHOT]
Please reply if you have any ideas about it.
Depending on what you want to do, this is a multi-step task
We are doing this in RHQ as part of the AS7 plugin For the first part, you can have a look at ASUploadConnection. This is triggered from BaseComponent#deployContent, which after a successful upload also runs the next two steps in #runDeploymentMagicOnServer.
Feel free to copy/re-use that code