use of org.guvnor.ala.wildfly.access.exceptions.WildflyClientException in project kie-wb-common by kiegroup.
the class WildflyClient method testConnection.
public String testConnection() throws WildflyClientException {
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, managementPort), new UsernamePasswordCredentials(user, password));
HttpPost post = new HttpPost("http://" + host + ":" + managementPort + "/management");
post.addHeader("X-Management-Client-Name", "GUVNOR-ALA");
ModelNode op = new ModelNode();
op.get("operation").set("read-resource");
post.setEntity(new StringEntity(op.toJSONString(true), ContentType.APPLICATION_JSON));
try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
CloseableHttpResponse httpResponse = httpclient.execute(post)) {
if (HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()) {
throw new Exception("Authentication failed. ");
} else {
String json = EntityUtils.toString(httpResponse.getEntity());
ModelNode returnVal = ModelNode.fromJSONString(json);
String productName = returnVal.get("result").get("product-name").asString();
String productVersion = returnVal.get("result").get("product-version").asString();
String releaseVersion = returnVal.get("result").get("release-version").asString();
String releaseCodeName = returnVal.get("result").get("release-codename").asString();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(productName + ", " + productVersion);
stringBuilder.append(" (" + releaseCodeName + ", " + releaseVersion + ")");
return stringBuilder.toString();
}
} catch (Exception e) {
throw new WildflyClientException(e.getMessage(), e);
}
}
use of org.guvnor.ala.wildfly.access.exceptions.WildflyClientException in project kie-wb-common by kiegroup.
the class WildflyClient method undeploy.
/*
* Undeploys a new WAR file to the Wildfly Instance
* @param String deploymentName
* @return the 200 on successful undeployment
* @throw a WildflyClientException with the status code on failure
* @throw a WildflyClientException with the throwable in case of an internal exception
*/
public int undeploy(String deploymentName) throws WildflyClientException {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, managementPort), new UsernamePasswordCredentials(user, password));
CloseableHttpClient httpclient = custom().setDefaultCredentialsProvider(credsProvider).build();
final HttpPost post = new HttpPost("http://" + host + ":" + managementPort + "/management");
post.addHeader("X-Management-Client-Name", "GUVNOR-ALA");
// the DMR operation
ModelNode operation = new ModelNode();
operation.get("operation").set("remove");
operation.get("address").add("deployment", deploymentName);
post.setEntity(new StringEntity(operation.toJSONString(true), APPLICATION_JSON));
try {
HttpResponse response = httpclient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new WildflyClientException("Error Undeploying App Status Code: " + statusCode);
}
return statusCode;
} catch (IOException ex) {
LOG.error("Error Undeploying App : " + ex.getMessage(), ex);
throw new WildflyClientException("Error Undeploying App : " + ex.getMessage(), ex);
}
}
use of org.guvnor.ala.wildfly.access.exceptions.WildflyClientException in project kie-wb-common by kiegroup.
the class WildflyClient method deploy.
/*
* Deploys a new WAR file to the Wildfly Instance
* @param File to be deployed, it must be a deployable file
* @return the 200 on successful deployment
* @throw a WildflyClientException with the status code on failure
* @throw a WildflyClientException with the throwable in case of an internal exception
*/
public int deploy(File file) throws WildflyClientException {
// the digest auth backend
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, managementPort), new UsernamePasswordCredentials(user, password));
CloseableHttpClient httpclient = custom().setDefaultCredentialsProvider(credsProvider).build();
HttpPost post = new HttpPost("http://" + host + ":" + managementPort + "/management-upload");
post.addHeader("X-Management-Client-Name", "HAL");
// the file to be uploaded
FileBody fileBody = new FileBody(file);
// the DMR operation
ModelNode operation = new ModelNode();
operation.get("address").add("deployment", file.getName());
operation.get("operation").set("add");
operation.get("runtime-name").set(file.getName());
operation.get("enabled").set(true);
// point to the multipart index used
operation.get("content").add().get("input-stream-index").set(0);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try {
operation.writeBase64(bout);
} catch (IOException ex) {
getLogger(WildflyClient.class.getName()).log(SEVERE, null, ex);
}
// the multipart
MultipartEntityBuilder builder = create();
builder.setMode(BROWSER_COMPATIBLE);
builder.addPart("uploadFormElement", fileBody);
builder.addPart("operation", new ByteArrayBody(bout.toByteArray(), create("application/dmr-encoded"), "blob"));
HttpEntity entity = builder.build();
post.setEntity(entity);
try {
HttpResponse response = httpclient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
throw new WildflyClientException("Error Deploying App Status Code: " + statusCode);
}
return statusCode;
} catch (IOException ex) {
LOG.error("Error Deploying App : " + ex.getMessage(), ex);
throw new WildflyClientException("Error Deploying App : " + ex.getMessage(), ex);
}
}
use of org.guvnor.ala.wildfly.access.exceptions.WildflyClientException in project kie-wb-common by kiegroup.
the class WildflyClient method getAppState.
/*
* Returns the state of the application
* @param String deploymentName
* @return WildflyAppState for the given deployment name
* @throw a WildflyClientException with the status code on failure
* @throw a WildflyClientException with the throwable in case of an internal exception
*/
public WildflyAppState getAppState(String deploymentName) throws WildflyClientException {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, managementPort), new UsernamePasswordCredentials(user, password));
CloseableHttpClient httpclient = custom().setDefaultCredentialsProvider(credsProvider).build();
final HttpPost post = new HttpPost("http://" + host + ":" + managementPort + "/management");
post.addHeader("X-Management-Client-Name", "GUVNOR-ALA");
// the DMR operation
ModelNode operation = new ModelNode();
operation.get("operation").set("read-resource");
operation.get("address").add("deployment", deploymentName);
operation.get("resolve-expressions").set("true");
post.setEntity(new StringEntity(operation.toJSONString(true), APPLICATION_JSON));
try {
HttpResponse response = httpclient.execute(post);
String json = EntityUtils.toString(response.getEntity());
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(json);
// contains an array of dataset objects
if (element.isJsonObject()) {
JsonObject outcome = element.getAsJsonObject();
JsonElement resultElement = outcome.get("result");
String enabled = null;
if (resultElement != null) {
JsonObject result = resultElement.getAsJsonObject();
enabled = result.get("enabled").getAsString();
}
String state;
if (Boolean.TRUE.toString().equals(enabled)) {
state = RUNNING;
} else if (Boolean.FALSE.toString().equals(enabled)) {
state = STOPPED;
} else {
state = UNKNOWN;
}
return new WildflyAppState(state, new Date());
}
} catch (IOException ex) {
LOG.error("Error Getting App State : " + ex.getMessage(), ex);
throw new WildflyClientException("Error Getting App State : " + ex.getMessage(), ex);
}
return new WildflyAppState(UNKNOWN, new Date());
}
use of org.guvnor.ala.wildfly.access.exceptions.WildflyClientException in project kie-wb-common by kiegroup.
the class WildflyRuntimeManager method refresh.
@Override
public void refresh(RuntimeId runtimeId) throws RuntimeOperationException {
WildflyRuntime runtime = (WildflyRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId());
try {
WildflyAppState appState = wildfly.getWildflyClient(runtime.getProviderId()).getAppState(runtime.getId());
WildflyRuntime newRuntime = new WildflyRuntime(runtime.getId(), runtime.getName(), runtime.getConfig(), runtime.getProviderId(), runtime.getEndpoint(), runtime.getInfo(), new WildflyRuntimeState(appState.getState(), runtime.getState().getStartedAt()));
runtimeRegistry.registerRuntime(newRuntime);
} catch (WildflyClientException ex) {
throw new RuntimeOperationException("Error Refreshing container: " + runtimeId.getId() + "\n\t There as a problem with refreshing your application, please check into the Wildfly Logs for more information.", ex);
}
}
Aggregations