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();
}
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();
}
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;
}
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;
}
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);
}
}
}
Aggregations