Search in sources :

Example 11 with RestActionReporter

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

the class TemplateRestResource method doDelete.

protected ExitCode doDelete(HashMap<String, String> data) {
    if (data == null) {
        data = new HashMap<String, String>();
    }
    if (entity == null) {
        // wrong resource
        // return Response.status(404).entity(ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, errorMessage, requestHeaders, uriInfo)).build();
        throwError(Status.NOT_FOUND, localStrings.getLocalString("rest.resource.erromessage.noentity", "Resource not found."));
    }
    if (getDeleteCommand() == null) {
        String message = localStrings.getLocalString("rest.resource.delete.forbidden", "DELETE on \"{0}\" is forbidden.", new Object[] { uriInfo.getAbsolutePath() });
        throwError(Status.FORBIDDEN, message);
    }
    if (getDeleteCommand().equals("GENERIC-DELETE")) {
        try {
            ConfigBean p = (ConfigBean) parent;
            if (parent == null) {
                p = (ConfigBean) entity.parent();
            }
            ConfigSupport.deleteChild(p, (ConfigBean) entity);
            return ExitCode.SUCCESS;
        } catch (TransactionFailure ex) {
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
    // do the delete via the command:
    if (data.containsKey("error")) {
        throwError(Status.BAD_REQUEST, localStrings.getLocalString("rest.request.parsing.error", "Unable to parse the input entity. Please check the syntax."));
    }
    ResourceUtil.addQueryString(uriInfo.getQueryParameters(), data);
    ResourceUtil.purgeEmptyEntries(data);
    ResourceUtil.adjustParameters(data);
    if (data.get("DEFAULT") == null) {
        addDefaultParameter(data);
    } else {
        String resourceName = getResourceName(uriInfo.getAbsolutePath().getPath(), "/");
        if (!data.get("DEFAULT").equals(resourceName)) {
            throwError(Status.FORBIDDEN, localStrings.getLocalString("rest.resource.not.deleted", "Resource not deleted. Value of \"name\" should be the name of this resource."));
        }
    }
    RestActionReporter actionReport = runCommand(getDeleteCommand(), data);
    if (actionReport != null) {
        ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
        if (exitCode != ActionReport.ExitCode.FAILURE) {
            return exitCode;
        }
        throwError(Status.BAD_REQUEST, actionReport.getMessage());
    }
    throw new WebApplicationException(handleError(Status.BAD_REQUEST, localStrings.getLocalString("rest.resource.delete.forbidden", "DELETE on \"{0}\" is forbidden.", new Object[] { uriInfo.getAbsolutePath() })));
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) WebApplicationException(javax.ws.rs.WebApplicationException) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ConfigBean(org.jvnet.hk2.config.ConfigBean) ExitCode(org.glassfish.api.ActionReport.ExitCode) ActionReport(org.glassfish.api.ActionReport)

Example 12 with RestActionReporter

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

the class ManagementProxyResource method proxyRequest.

@GET
public ActionReportResult proxyRequest() {
    RestActionReporter ar = new RestActionReporter();
    ar.setActionDescription("Proxied Data");
    ar.setSuccess();
    ActionReportResult result = new ActionReportResult(ar);
    Properties proxiedResponse = new ManagementProxyImpl().proxyRequest(uriInfo, Util.getJerseyClient(), habitat);
    ar.setExtraProperties(proxiedResponse);
    return result;
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) Properties(java.util.Properties) GET(javax.ws.rs.GET)

Example 13 with RestActionReporter

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

the class MonitoredAttributeBagResource method setMonitoredAttributes.

/**
 * Sets the monitored attribute list to the specified list.
 *
 * @param attributes the intended list of attributes.
 * @throws TransactionFailure if an error occurs in removing or adding
 * attributes.
 */
public void setMonitoredAttributes(List<Map<String, String>> attributes) throws TransactionFailure {
    TranslatedConfigView.doSubstitution.set(false);
    List<Map<String, String>> existingAttributes = getMonitoredAttributes();
    // Get a list of attributes that need adding
    List<Map<String, String>> attributesToAdd = new ArrayList<>(attributes);
    // Start with the list of specified attributes, and remove any that are also in the existing list
    Iterator<Map<String, String>> iterator = attributesToAdd.iterator();
    while (iterator.hasNext()) {
        Map<String, String> attributeToAdd = iterator.next();
        for (Map<String, String> existingAttribute : existingAttributes) {
            if (attributesAreEqual(existingAttribute, attributeToAdd)) {
                iterator.remove();
            }
        }
    }
    // Get a list of attributes that need deleting
    List<Map<String, String>> attributesToDelete = new ArrayList<>(existingAttributes);
    // Start with the list of existing attributes, and remove any that aren't also in the specified list
    iterator = attributesToDelete.iterator();
    while (iterator.hasNext()) {
        Map<String, String> attributeToDelete = iterator.next();
        boolean specified = false;
        for (Map<String, String> specifiedAttribute : attributes) {
            if (attributesAreEqual(specifiedAttribute, attributeToDelete)) {
                specified = true;
            }
        }
        if (specified) {
            iterator.remove();
        }
    }
    try {
        // Add all required attributes
        for (Map<String, String> attribute : attributesToAdd) {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("addattribute", String.format("attributeName=%s objectName=%s", attribute.get("attributeName"), attribute.get("objectName")));
            RestActionReporter reporter = ResourceUtil.runCommand("set-monitoring-configuration", parameters, getSubject());
            if (reporter.isFailure()) {
                throw new TransactionFailure(reporter.getMessage());
            }
        }
        // Delete all unrequired attributes
        for (Map<String, String> attribute : attributesToDelete) {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("delattribute", String.format("attributeName=%s objectName=%s", attribute.get("attributeName"), attribute.get("objectName")));
            RestActionReporter reporter = ResourceUtil.runCommand("set-monitoring-configuration", parameters, getSubject());
            if (reporter.isFailure()) {
                throw new TransactionFailure(reporter.getMessage());
            }
        }
    } finally {
        TranslatedConfigView.doSubstitution.set(true);
    }
    synchronized (parent) {
        entity = parent.nodeElements("monitored-attributes");
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter)

Example 14 with RestActionReporter

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

the class MonitoredAttributeBagResource method put.

/**
 * Creates new monitored-attributes. This method deletes all of the existing
 * monitored-attributes.
 *
 * @param attributes the list of monitored-attributes to be created.
 * @return a list of the monitored-attributes after the transaction.
 */
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED })
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public ActionReportResult put(List<Map<String, String>> attributes) {
    RestActionReporter ar = new RestActionReporter();
    ar.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    ar.setActionDescription("monitored-attribute");
    try {
        setMonitoredAttributes(attributes);
        List monitoredAttributes = getMonitoredAttributes();
        Properties extraProperties = new Properties();
        extraProperties.put("monitoredAttributes", monitoredAttributes);
        ar.setExtraProperties(extraProperties);
    } catch (TransactionFailure ex) {
        ar.setActionExitCode(ActionReport.ExitCode.FAILURE);
        ar.setMessage(ex.getMessage());
    }
    return new ActionReportResult(tagName, ar, new OptionsResult(Util.getResourceName(uriInfo)));
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) OptionsResult(org.glassfish.admin.rest.results.OptionsResult) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 15 with RestActionReporter

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

the class MonitoredAttributeBagResource method get.

/**
 * Gets the monitored-attributes.
 *
 * @return a list of the monitored-attributes after the transaction.
 */
@GET
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public ActionReportResult get() {
    RestActionReporter ar = new RestActionReporter();
    ar.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    ar.setActionDescription("monitored-attribute");
    List monitoredAttributes = getMonitoredAttributes();
    Properties extraProperties = new Properties();
    extraProperties.put("monitoredAttributes", monitoredAttributes);
    ar.setExtraProperties(extraProperties);
    return new ActionReportResult(tagName, ar, new OptionsResult(Util.getResourceName(uriInfo)));
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) OptionsResult(org.glassfish.admin.rest.results.OptionsResult) Produces(javax.ws.rs.Produces) 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