Search in sources :

Example 6 with ExitCode

use of org.glassfish.api.ActionReport.ExitCode in project Payara by payara.

the class SystemPropertiesCliResource method createProperties.

/**
 * Create some system properties using the create-system-properties asadmin
 * command.
 *
 * @param parent the name of the parent object of the target
 * @param data   a map of properties to create
 * @return the result of the command
 */
protected Response createProperties(String parent, Map<String, String> data) {
    String propertiesString = convertPropertyMapToString(data);
    data = new HashMap<String, String>();
    data.put("DEFAULT", propertiesString);
    data.put("target", (parent == null) ? getParent(uriInfo) : parent);
    RestActionReporter actionReport = ResourceUtil.runCommand("create-system-properties", data, getSubject());
    ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
    ActionReportResult results = new ActionReportResult(commandName, actionReport, new OptionsResult());
    int status = HttpURLConnection.HTTP_OK;
    /*200 - ok*/
    if (exitCode == ActionReport.ExitCode.FAILURE) {
        status = HttpURLConnection.HTTP_INTERNAL_ERROR;
    }
    return Response.status(status).entity(results).build();
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ExitCode(org.glassfish.api.ActionReport.ExitCode) ActionReport(org.glassfish.api.ActionReport) OptionsResult(org.glassfish.admin.rest.results.OptionsResult)

Example 7 with ExitCode

use of org.glassfish.api.ActionReport.ExitCode in project Payara by payara.

the class SystemPropertiesCliResource method deleteProperty.

/**
 * Delete a system property using the delete-system-property asadmin command.
 *
 * @param parent   the name of the parent object of the target
 * @param propName the name of the property to delete
 * @return the result of the command
 */
protected Response deleteProperty(String parent, String propName) {
    ParameterMap pm = new ParameterMap();
    pm.add("DEFAULT", propName);
    pm.add("target", (parent == null) ? getParent(uriInfo) : parent);
    RestActionReporter actionReport = ResourceUtil.runCommand("delete-system-property", pm, getSubject());
    ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
    ActionReportResult results = new ActionReportResult(commandName, actionReport, new OptionsResult());
    int status = HttpURLConnection.HTTP_OK;
    /*200 - ok*/
    if (exitCode == ActionReport.ExitCode.FAILURE) {
        status = HttpURLConnection.HTTP_INTERNAL_ERROR;
    }
    return Response.status(status).entity(results).build();
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ParameterMap(org.glassfish.api.admin.ParameterMap) ExitCode(org.glassfish.api.ActionReport.ExitCode) ActionReport(org.glassfish.api.ActionReport) OptionsResult(org.glassfish.admin.rest.results.OptionsResult)

Example 8 with ExitCode

use of org.glassfish.api.ActionReport.ExitCode in project Payara by payara.

the class RestUtil method parseResponse.

public static Map<String, Object> parseResponse(RestResponse response, HandlerContext handlerCtx, String endpoint, Object attrs, boolean quiet, boolean throwException) {
    // Parse the response
    String message = "";
    ExitCode exitCode = ExitCode.FAILURE;
    Object maskedAttr = attrs;
    if ((attrs != null) && (attrs instanceof Map)) {
        maskedAttr = maskOffPassword((Map<String, Object>) attrs);
    }
    if (response != null) {
        try {
            // int status = response.getResponseCode();
            Map<String, Object> responseMap = response.getResponse();
            if (responseMap.get("data") != null) {
                String exitCodeStr = (String) ((Map<String, Object>) responseMap.get("data")).get("exit_code");
                exitCode = (exitCodeStr != null) ? ExitCode.valueOf(exitCodeStr) : ExitCode.SUCCESS;
            }
            // Get the message for both WARNING and FAILURE exit_code
            if (exitCode != ExitCode.SUCCESS) {
                Map<String, Object> dataMap = (Map<String, Object>) responseMap.get("data");
                if (dataMap != null) {
                    message = getMessage(dataMap);
                    List<Map<String, Object>> subReports = (List<Map<String, Object>>) dataMap.get("subReports");
                    if (subReports != null) {
                        StringBuilder sb = new StringBuilder("");
                        for (Map<String, Object> oneSubReport : subReports) {
                            sb.append(" ").append(getMessage(oneSubReport));
                        }
                        message = message + sb.toString();
                    }
                } else {
                    Object msgs = responseMap.get("message");
                    if (msgs == null) {
                        // According to security guideline, we shouldn't expose the endpoint to user for the error.
                        // message =  "REST Request '"  + endpoint + "' failed with response code '" + status + "'.";
                        message = "";
                    } else if (msgs instanceof List) {
                        StringBuilder builder = new StringBuilder("");
                        for (Object obj : ((List<Object>) msgs)) {
                            if ((obj instanceof Map) && ((Map<String, Object>) obj).containsKey("message")) {
                                obj = ((Map<String, Object>) obj).get("message");
                            }
                            builder.append(obj.toString());
                        }
                        message = builder.toString();
                    } else if (msgs instanceof Map) {
                        message = ((Map<String, Object>) msgs).get("message").toString();
                    } else {
                        throw new RuntimeException("Unexpected message type.");
                    }
                }
            }
            switch(exitCode) {
                case FAILURE:
                    {
                        // If this is called from jsf, stop processing/show error.
                        if (throwException) {
                            if (handlerCtx != null) {
                                GuiUtil.handleError(handlerCtx, message);
                                if (!quiet) {
                                    Logger logger = GuiUtil.getLogger();
                                    logger.severe(GuiUtil.getCommonMessage("LOG_REQUEST_RESULT", new Object[] { exitCode, endpoint, maskedAttr }));
                                    if (logger.isLoggable(Level.FINEST)) {
                                        logger.finest("response.getResponseBody(): " + response.getResponseBody());
                                    }
                                }
                                return new HashMap();
                            } else {
                                // If handlerCtx is not passed in, it means the caller (java handler) wants to handle this exception itself.
                                throw new RuntimeException(message);
                            }
                        } else {
                            // Issue Number :13312 handling the case when throwException is false.
                            if (!quiet) {
                                Logger logger = GuiUtil.getLogger();
                                logger.severe(GuiUtil.getCommonMessage("LOG_REQUEST_RESULT", new Object[] { exitCode, endpoint, maskedAttr }));
                                if (logger.isLoggable(Level.FINEST)) {
                                    logger.finest("response.getResponseBody(): " + response.getResponseBody());
                                }
                            }
                            return responseMap;
                        }
                    }
                case WARNING:
                    {
                        GuiUtil.prepareAlert("warning", GuiUtil.getCommonMessage("msg.command.warning"), message);
                        GuiUtil.getLogger().warning(GuiUtil.getCommonMessage("LOG_REQUEST_RESULT", new Object[] { exitCode, endpoint, maskedAttr }));
                        return responseMap;
                    }
                case SUCCESS:
                    {
                        return responseMap;
                    }
            }
        } catch (Exception ex) {
            if (!quiet) {
                Logger logger = GuiUtil.getLogger();
                logger.severe(GuiUtil.getCommonMessage("LOG_REQUEST_RESULT", new Object[] { exitCode, endpoint, maskedAttr }));
                if (logger.isLoggable(Level.FINEST)) {
                    logger.log(Level.FINEST, "response.getResponseBody(): {0}", response.getResponseBody());
                }
            }
            if (handlerCtx != null) {
                // instead of dumping the exception on screen.
                if (throwException) {
                    if ("".equals(message)) {
                        GuiUtil.handleException(handlerCtx, ex);
                    } else {
                        GuiUtil.handleError(handlerCtx, message);
                    }
                }
            } else {
                // if this is called by other java handler, we tell the called handle the exception.
                if ("".equals(message)) {
                    throw new RuntimeException(ex);
                } else {
                    throw new RuntimeException(message, ex);
                }
            }
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ExitCode(org.glassfish.api.ActionReport.ExitCode) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) Logger(java.util.logging.Logger) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TreeMap(java.util.TreeMap) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 9 with ExitCode

use of org.glassfish.api.ActionReport.ExitCode in project Payara by payara.

the class RestUtil method hasWarning.

public static boolean hasWarning(Map responseMap) {
    if (responseMap.get("data") != null) {
        String exitCodeStr = (String) ((Map) responseMap.get("data")).get("exit_code");
        ExitCode exitCode = (exitCodeStr != null) ? ExitCode.valueOf(exitCodeStr) : ExitCode.SUCCESS;
        return (exitCode == ExitCode.WARNING);
    }
    return false;
}
Also used : ExitCode(org.glassfish.api.ActionReport.ExitCode)

Example 10 with ExitCode

use of org.glassfish.api.ActionReport.ExitCode in project Payara by payara.

the class RestartHttpListenersCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    boolean isAll = all != null && all.booleanValue();
    if (isAll && target == null) {
        ExitCode exitCode = ClusterOperationUtil.replicateCommand("restart-http-listeners", FailurePolicy.Ignore, FailurePolicy.Ignore, FailurePolicy.Error, domain.getAllTargets(), context, new ParameterMap(), locator);
        report.setActionExitCode(exitCode);
    }
    if (report.hasFailures()) {
        return;
    }
    if (target != null && isAll) {
        report.setMessage("--all used together with --target and is ignored.");
        report.setActionExitCode(ExitCode.WARNING);
    }
    if (target == null) {
        target = SystemPropertyConstants.DAS_SERVER_NAME;
    }
    Config config = targetUtil.getConfig(target);
    for (NetworkListener listener : config.getNetworkConfig().getNetworkListeners().getNetworkListener()) {
        try {
            if (!"admin-listener".equals(listener.getName())) {
                service.restartNetworkListener(listener, 10, TimeUnit.SECONDS);
            }
        } catch (Exception ex) {
            report.setMessage(MessageFormat.format("Failed to restart listener {0}, {1}", listener.getName(), (ex.getMessage() == null ? "No reason given" : ex.getMessage())));
            report.setActionExitCode(ExitCode.FAILURE);
            report.setFailureCause(ex);
            return;
        }
        if (!report.hasFailures() && !report.hasWarnings()) {
            report.setActionExitCode(ExitCode.SUCCESS);
        }
    }
}
Also used : Config(com.sun.enterprise.config.serverbeans.Config) ExitCode(org.glassfish.api.ActionReport.ExitCode) ParameterMap(org.glassfish.api.admin.ParameterMap) ActionReport(org.glassfish.api.ActionReport) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Aggregations

ExitCode (org.glassfish.api.ActionReport.ExitCode)10 RestActionReporter (org.glassfish.admin.rest.utils.xml.RestActionReporter)6 ActionReport (org.glassfish.api.ActionReport)6 WebApplicationException (javax.ws.rs.WebApplicationException)3 ActionReportResult (org.glassfish.admin.rest.results.ActionReportResult)3 OptionsResult (org.glassfish.admin.rest.results.OptionsResult)2 ParameterMap (org.glassfish.api.admin.ParameterMap)2 ConfigBean (org.jvnet.hk2.config.ConfigBean)2 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)2 Config (com.sun.enterprise.config.serverbeans.Config)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Logger (java.util.logging.Logger)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Response (javax.ws.rs.core.Response)1