Search in sources :

Example 41 with RestActionReporter

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

the class EncodingTest method encodeAsXml.

@Test
public void encodeAsXml() {
    RestActionReporter ar = buildActionReport();
    ActionReportResultXmlProvider provider = new ActionReportResultXmlProvider();
    ActionReportResult result = new ActionReportResult("test", ar);
    String xml = provider.getContent(result);
    Map responseMap = MarshallingUtils.buildMapFromDocument(xml);
    assertEquals(7, responseMap.size());
    assertEquals(4, ((Map) responseMap.get("extraProperties")).size());
    assertTrue(responseMap.get("children") instanceof List);
    assertTrue(responseMap.get("subReports") instanceof List);
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ArrayList(java.util.ArrayList) List(java.util.List) ActionReportResultXmlProvider(org.glassfish.admin.rest.provider.ActionReportResultXmlProvider) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 42 with RestActionReporter

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

the class CompositeUtil method executeCommand.

/**
 * Execute an <code>AdminCommand</code> with the specified parameters.
 *
 * @param command
 * @param parameters
 * @param throwBadRequest (vs. NOT_FOUND)
 * @param throwOnWarning  (vs.ignore warning)
 * @return
 */
public ActionReporter executeCommand(Subject subject, String command, ParameterMap parameters, Status status, boolean includeFailureMessage, boolean throwOnWarning, boolean managed) {
    RestActionReporter ar = ResourceUtil.runCommand(command, parameters, subject, managed);
    ExitCode code = ar.getActionExitCode();
    if (code.equals(ExitCode.FAILURE) || (code.equals(ExitCode.WARNING) && throwOnWarning)) {
        Throwable t = ar.getFailureCause();
        if (t instanceof SecurityException) {
            throw new WebApplicationException(Response.status(Status.UNAUTHORIZED).build());
        }
        if (includeFailureMessage) {
            throw new WebApplicationException(Response.status(status).entity(ar.getCombinedMessage()).build());
        } else {
            throw new WebApplicationException(status);
        }
    }
    return ar;
}
Also used : RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) WebApplicationException(javax.ws.rs.WebApplicationException) ExitCode(org.glassfish.api.ActionReport.ExitCode)

Example 43 with RestActionReporter

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

the class ActionReportJsonProvider method processReport.

/**
 * Converts an ActionReport into a JsonObject
 * @param ar
 * @return
 * @throws JsonException
 */
protected JsonObject processReport(ActionReporter ar) throws JsonException {
    JsonObjectBuilder result = Json.createObjectBuilder();
    if (ar instanceof RestActionReporter) {
        result.add("message", ((RestActionReporter) ar).getCombinedMessage());
    } else {
        String message = decodeEol(ar.getMessage());
        if (message != null) {
            result.add("message", message);
        }
    }
    String desc = ar.getActionDescription();
    if (desc != null) {
        result.add("command", ar.getActionDescription());
    } else {
        result.add("command", JsonValue.NULL);
    }
    result.add("exit_code", ar.getActionExitCode().toString());
    Properties properties = ar.getTopMessagePart().getProps();
    if ((properties != null) && (!properties.isEmpty())) {
        JsonObject propBuilder = Json.createObjectBuilder((Map) properties).build();
        result.add("properties", propBuilder);
    }
    Properties extraProperties = ar.getExtraProperties();
    if ((extraProperties != null) && (!extraProperties.isEmpty())) {
        result.add("extraProperties", getExtraProperties(extraProperties));
    }
    List<MessagePart> children = ar.getTopMessagePart().getChildren();
    if ((children != null) && (!children.isEmpty())) {
        result.add("children", processChildren(children));
    }
    List<ActionReporter> subReports = ar.getSubActionsReport();
    if ((subReports != null) && (!subReports.isEmpty())) {
        result.add("subReports", processSubReports(subReports));
    }
    return result.build();
}
Also used : RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) MessagePart(org.glassfish.api.ActionReport.MessagePart) JsonObject(javax.json.JsonObject) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ActionReporter(com.sun.enterprise.v3.common.ActionReporter) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 44 with RestActionReporter

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

the class RestAdapter method reportError.

private void reportError(Request req, Response res, int statusCode, String msg) {
    try {
        // TODO: There's a lot of arm waving and flailing here.  I'd like this to be cleaner, but I don't
        // have time at the moment.  jdlee 8/11/10
        // getClientActionReport(req);
        RestActionReporter report = new RestActionReporter();
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setActionDescription("Error");
        report.setMessage(msg);
        BaseProvider<ActionReportResult> provider;
        String type = getAcceptedMimeType(req);
        if (null == type) {
            res.setContentType("text/html");
            provider = new ActionReportResultHtmlProvider();
        } else
            switch(type) {
                case "xml":
                    res.setContentType("application/xml");
                    provider = new ActionReportResultXmlProvider();
                    break;
                case "json":
                    res.setContentType("application/json");
                    provider = new ActionReportResultJsonProvider();
                    break;
                default:
                    res.setContentType("text/html");
                    provider = new ActionReportResultHtmlProvider();
                    break;
            }
        res.setStatus(statusCode);
        res.getOutputStream().write(provider.getContent(new ActionReportResult(report)).getBytes());
        res.getOutputStream().flush();
        res.finish();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ActionReportResultHtmlProvider(org.glassfish.admin.rest.provider.ActionReportResultHtmlProvider) ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ActionReportResultXmlProvider(org.glassfish.admin.rest.provider.ActionReportResultXmlProvider) ActionReportResultJsonProvider(org.glassfish.admin.rest.provider.ActionReportResultJsonProvider) LoginException(javax.security.auth.login.LoginException) EndpointRegistrationException(org.glassfish.api.container.EndpointRegistrationException) RemoteAdminAccessException(org.glassfish.internal.api.RemoteAdminAccessException) IOException(java.io.IOException)

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