Search in sources :

Example 26 with RestActionReporter

use of org.glassfish.admin.rest.utils.xml.RestActionReporter in project Payara by payara.

the class TemplateExecCommand method executeCommand.

protected CommandResult executeCommand(ParameterMap data) {
    RestActionReporter actionReport = ResourceUtil.runCommand(commandName, data, getSubject());
    ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
    if (exitCode == ActionReport.ExitCode.FAILURE) {
        throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(actionReport.getMessage()).build());
    }
    CommandResult cr = CompositeUtil.instance().getModel(CommandResult.class);
    cr.setMessage(actionReport.getMessage());
    cr.setProperties(actionReport.getTopMessagePart().getProps());
    cr.setExtraProperties(getExtraProperties(actionReport));
    return cr;
}
Also used : RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) WebApplicationException(javax.ws.rs.WebApplicationException) ActionReport(org.glassfish.api.ActionReport)

Example 27 with RestActionReporter

use of org.glassfish.admin.rest.utils.xml.RestActionReporter in project Payara by payara.

the class TemplateListOfResource method createResource.

// called in case of POST on application resource (deployment).
// resourceToCreate is the name attribute if provided.
private Response createResource(HashMap<String, String> data, String resourceToCreate) {
    try {
        if (data.containsKey("error")) {
            String errorMessage = localStrings.getLocalString("rest.request.parsing.error", "Unable to parse the input entity. Please check the syntax.");
            return Response.status(400).entity(ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, errorMessage, requestHeaders, uriInfo)).build();
        }
        ResourceUtil.purgeEmptyEntries(data);
        // Command to execute
        String commandName = getPostCommand();
        ResourceUtil.defineDefaultParameters(data);
        if ((resourceToCreate == null) || (resourceToCreate.equals(""))) {
            String newResourceName = data.get("DEFAULT");
            if (newResourceName != null) {
                if (newResourceName.contains("/")) {
                    newResourceName = Util.getName(newResourceName, '/');
                } else {
                    if (newResourceName.contains("\\")) {
                        newResourceName = Util.getName(newResourceName, '\\');
                    }
                }
                resourceToCreate = uriInfo.getAbsolutePath() + "/" + newResourceName;
            }
        } else {
            resourceToCreate = uriInfo.getAbsolutePath() + "/" + resourceToCreate;
        }
        if (null != commandName) {
            RestActionReporter actionReport = ResourceUtil.runCommand(commandName, data, getSubject());
            ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
            if (exitCode != ActionReport.ExitCode.FAILURE) {
                String successMessage = localStrings.getLocalString("rest.resource.create.message", "\"{0}\" created successfully.", new Object[] { resourceToCreate });
                return Response.ok().entity(ResourceUtil.getActionReportResult(actionReport, successMessage, requestHeaders, uriInfo)).build();
            }
            String errorMessage = getErrorMessage(data, actionReport);
            return Response.status(400).entity(ResourceUtil.getActionReportResult(actionReport, errorMessage, requestHeaders, uriInfo)).build();
        }
        String message = localStrings.getLocalString("rest.resource.post.forbidden", "POST on \"{0}\" is forbidden.", new Object[] { resourceToCreate });
        return Response.status(403).entity(ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, message, requestHeaders, uriInfo)).build();
    } catch (Exception e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) WebApplicationException(javax.ws.rs.WebApplicationException) ActionReport(org.glassfish.api.ActionReport) MultiException(org.glassfish.hk2.api.MultiException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 28 with RestActionReporter

use of org.glassfish.admin.rest.utils.xml.RestActionReporter in project Payara by payara.

the class TemplateListOfResource method createResource.

@POST
// create
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED })
public Response createResource(HashMap<String, String> data) {
    if (data == null) {
        data = new HashMap<String, String>();
    }
    try {
        if (data.containsKey("error")) {
            String errorMessage = localStrings.getLocalString("rest.request.parsing.error", "Unable to parse the input entity. Please check the syntax.");
            ActionReportResult arr = ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, errorMessage, requestHeaders, uriInfo);
            return Response.status(400).entity(arr).build();
        }
        ResourceUtil.purgeEmptyEntries(data);
        // Command to execute
        String commandName = getPostCommand();
        String resourceToCreate = uriInfo.getAbsolutePath() + "/";
        if (null != commandName) {
            // adjusting for DEFAULT is required only while executing a CLI command
            ResourceUtil.adjustParameters(data);
            if (data.containsKey("name")) {
                resourceToCreate += data.get("name");
            } else {
                resourceToCreate += data.get("DEFAULT");
            }
            RestActionReporter actionReport = ResourceUtil.runCommand(commandName, data, getSubject());
            ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
            if (exitCode != ActionReport.ExitCode.FAILURE) {
                String successMessage = localStrings.getLocalString("rest.resource.create.message", "\"{0}\" created successfully.", resourceToCreate);
                ActionReportResult arr = ResourceUtil.getActionReportResult(actionReport, successMessage, requestHeaders, uriInfo);
                return Response.ok(arr).build();
            }
            String errorMessage = getErrorMessage(data, actionReport);
            ActionReportResult arr = ResourceUtil.getActionReportResult(actionReport, errorMessage, requestHeaders, uriInfo);
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(arr).build();
        } else {
            ActionReportResult arr = ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, "No CRUD Create possible.", requestHeaders, uriInfo);
            return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(arr).build();
        }
    } catch (Exception e) {
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) WebApplicationException(javax.ws.rs.WebApplicationException) ActionReport(org.glassfish.api.ActionReport) MultiException(org.glassfish.hk2.api.MultiException) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 29 with RestActionReporter

use of org.glassfish.admin.rest.utils.xml.RestActionReporter in project Payara by payara.

the class TemplateRestResource method doCreateOrUpdate.

/**
 * This method performs the creation or updating of an entity, regardless of the
 * request's mime type.  If an error occurs, a <code>WebApplicationException</code>
 * is thrown, so if the method returns, the create/update was successful.
 * @param data
 * @return
 */
protected RestActionReporter doCreateOrUpdate(HashMap<String, String> data) {
    if (data == null) {
        data = new HashMap<String, String>();
    }
    try {
        // data.remove("submit");
        removeAttributesToBeSkipped(data);
        if (data.containsKey("error")) {
            throw new WebApplicationException(Response.status(400).entity(ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, localStrings.getLocalString("rest.request.parsing.error", "Unable to parse the input entity. Please check the syntax."), requestHeaders, uriInfo)).build());
        }
        ResourceUtil.purgeEmptyEntries(data);
        // client POST request for delete operation to DELETE method.
        if ("__deleteoperation".equals(data.get("operation"))) {
            data.remove("operation");
            delete(data);
            return new RestActionReporter();
        }
        // just update it.
        data = ResourceUtil.translateCamelCasedNamesToXMLNames(data);
        RestActionReporter ar = Util.applyChanges(data, uriInfo, getSubject());
        if (ar.getActionExitCode() != ActionReport.ExitCode.SUCCESS) {
            // i18n
            throwError(Status.BAD_REQUEST, "Could not apply changes:  " + ar.getMessage());
        }
        return ar;
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception ex) {
        throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) MultiException(org.glassfish.hk2.api.MultiException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 30 with RestActionReporter

use of org.glassfish.admin.rest.utils.xml.RestActionReporter in project Payara by payara.

the class FindHttpProtocolResource method get.

@GET
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED })
public ActionReportResult get() {
    Dom dom = getEntity();
    NetworkListener nl = dom.createProxy(NetworkListener.class);
    Protocol p = nl.findHttpProtocol();
    RestActionReporter ar = new RestActionReporter();
    ar.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    ar.getTopMessagePart().getProps().put("protocol", p.getName());
    ActionReportResult result = new ActionReportResult("find-http-protocol", ar, new OptionsResult());
    return result;
}
Also used : Dom(org.jvnet.hk2.config.Dom) ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) Protocol(org.glassfish.grizzly.config.dom.Protocol) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) OptionsResult(org.glassfish.admin.rest.results.OptionsResult) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) GET(javax.ws.rs.GET)

Aggregations

RestActionReporter (org.glassfish.admin.rest.utils.xml.RestActionReporter)44 ActionReportResult (org.glassfish.admin.rest.results.ActionReportResult)23 OptionsResult (org.glassfish.admin.rest.results.OptionsResult)13 ActionReport (org.glassfish.api.ActionReport)12 Produces (javax.ws.rs.Produces)9 WebApplicationException (javax.ws.rs.WebApplicationException)8 HashMap (java.util.HashMap)7 Properties (java.util.Properties)7 GET (javax.ws.rs.GET)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Consumes (javax.ws.rs.Consumes)5 MethodMetaData (org.glassfish.admin.rest.provider.MethodMetaData)5 MessagePart (org.glassfish.api.ActionReport.MessagePart)5 ParameterMap (org.glassfish.api.admin.ParameterMap)5 CommandRunner (org.glassfish.api.admin.CommandRunner)4 Test (org.testng.annotations.Test)4 ActionReporter (com.sun.enterprise.v3.common.ActionReporter)3 List (java.util.List)3 MultiException (org.glassfish.hk2.api.MultiException)3