Search in sources :

Example 1 with WildflyClientException

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ModelNode(org.jboss.dmr.ModelNode) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 2 with WildflyClientException

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 3 with WildflyClientException

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) AuthScope(org.apache.http.auth.AuthScope) ModelNode(org.jboss.dmr.ModelNode)

Example 4 with WildflyClientException

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());
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) JsonObject(com.google.gson.JsonObject) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) Date(java.util.Date) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) StringEntity(org.apache.http.entity.StringEntity) JsonElement(com.google.gson.JsonElement) AuthScope(org.apache.http.auth.AuthScope) ModelNode(org.jboss.dmr.ModelNode) JsonParser(com.google.gson.JsonParser)

Example 5 with WildflyClientException

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);
    }
}
Also used : WildflyRuntime(org.guvnor.ala.wildfly.model.WildflyRuntime) WildflyClientException(org.guvnor.ala.wildfly.access.exceptions.WildflyClientException) RuntimeOperationException(org.guvnor.ala.exceptions.RuntimeOperationException) WildflyRuntimeState(org.guvnor.ala.wildfly.model.WildflyRuntimeState) WildflyAppState(org.guvnor.ala.wildfly.access.WildflyAppState)

Aggregations

WildflyClientException (org.guvnor.ala.wildfly.access.exceptions.WildflyClientException)11 IOException (java.io.IOException)6 AuthScope (org.apache.http.auth.AuthScope)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 HttpPost (org.apache.http.client.methods.HttpPost)6 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 ModelNode (org.jboss.dmr.ModelNode)6 HttpResponse (org.apache.http.HttpResponse)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 StringEntity (org.apache.http.entity.StringEntity)5 RuntimeOperationException (org.guvnor.ala.exceptions.RuntimeOperationException)4 WildflyRuntime (org.guvnor.ala.wildfly.model.WildflyRuntime)4 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Date (java.util.Date)1 HttpEntity (org.apache.http.HttpEntity)1