Search in sources :

Example 1 with ActionReportResult

use of org.glassfish.admin.rest.results.ActionReportResult in project Payara by payara.

the class MonitoringResource method getChildNodes.

@GET
@Path("domain{path:.*}")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getChildNodes(@PathParam("path") List<PathSegment> pathSegments) {
    Response.ResponseBuilder responseBuilder = Response.status(OK);
    RestActionReporter ar = new RestActionReporter();
    ar.setActionDescription("Monitoring Data");
    ar.setMessage("");
    ar.setSuccess();
    String currentInstanceName = System.getProperty("com.sun.aas.instanceName");
    // TODO this needs to come from an API. Check with admin team
    boolean isRunningOnDAS = "server".equals(currentInstanceName);
    MonitoringRuntimeDataRegistry monitoringRegistry = habitat.getRemoteLocator().getService(MonitoringRuntimeDataRegistry.class);
    TreeNode rootNode = monitoringRegistry.get(currentInstanceName);
    // The pathSegments will always contain "domain". Discard it
    pathSegments = pathSegments.subList(1, pathSegments.size());
    if (!pathSegments.isEmpty()) {
        PathSegment lastSegment = pathSegments.get(pathSegments.size() - 1);
        if (lastSegment.getPath().isEmpty()) {
            // if there is a trailing '/' (like monitoring/domain/), a spurious pathSegment is added. Discard it.
            pathSegments = pathSegments.subList(0, pathSegments.size() - 1);
        }
    }
    if (!pathSegments.isEmpty()) {
        String firstPathElement = pathSegments.get(0).getPath();
        if (firstPathElement.equals(currentInstanceName)) {
            // Query for current instance. Execute it
            // iterate over pathsegments and build a dotted name to look up in monitoring registry
            StringBuilder pathInMonitoringRegistry = new StringBuilder();
            for (PathSegment pathSegment : pathSegments.subList(1, pathSegments.size())) {
                if (pathInMonitoringRegistry.length() > 0) {
                    pathInMonitoringRegistry.append('.');
                }
                // Need to escape '.' before passing it to monitoring code
                pathInMonitoringRegistry.append(pathSegment.getPath().replaceAll("\\.", "\\\\."));
            }
            TreeNode resultNode = pathInMonitoringRegistry.length() > 0 && rootNode != null ? rootNode.getNode(pathInMonitoringRegistry.toString()) : rootNode;
            if (resultNode != null) {
                List<TreeNode> list = new ArrayList<TreeNode>();
                if (resultNode.hasChildNodes()) {
                    list.addAll(resultNode.getEnabledChildNodes());
                } else {
                    list.add(resultNode);
                }
                constructEntity(list, ar);
                responseBuilder.entity(new ActionReportResult(ar));
            } else {
                // No monitoring data, so nothing to list
                responseBuilder.status(NOT_FOUND);
                ar.setFailure();
                responseBuilder.entity(new ActionReportResult(ar));
            }
        } else {
            // firstPathElement != currentInstanceName => A proxy request
            if (isRunningOnDAS) {
                // Attempt to forward to instance if running on Das
                // TODO validate that firstPathElement corresponds to a valid server name
                Properties proxiedResponse = new MonitoringProxyImpl().proxyRequest(uriInfo, Util.getJerseyClient(), habitat.getRemoteLocator());
                ar.setExtraProperties(proxiedResponse);
                responseBuilder.entity(new ActionReportResult(ar));
            } else {
                // Not running on DAS and firstPathElement != currentInstanceName => Reject the request as invalid
                return Response.status(FORBIDDEN).build();
            }
        }
    } else {
        // Called for /monitoring/domain/
        List<TreeNode> list = new ArrayList<TreeNode>();
        if (rootNode != null) {
            // Add currentInstance to response
            list.add(rootNode);
        }
        constructEntity(list, ar);
        if (isRunningOnDAS) {
            // Add links to instances from the cluster
            Domain domain = habitat.getRemoteLocator().getService(Domain.class);
            Map<String, String> links = (Map<String, String>) ar.getExtraProperties().get("childResources");
            for (Server s : domain.getServers().getServer()) {
                if (!s.getName().equals("server")) {
                    // add all non 'server' instances
                    links.put(s.getName(), getElementLink(uriInfo, s.getName()));
                }
            }
        }
        responseBuilder.entity(new ActionReportResult(ar));
    }
    return responseBuilder.build();
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) Server(com.sun.enterprise.config.serverbeans.Server) MonitoringRuntimeDataRegistry(org.glassfish.flashlight.MonitoringRuntimeDataRegistry) ArrayList(java.util.ArrayList) PathSegment(javax.ws.rs.core.PathSegment) Properties(java.util.Properties) Response(javax.ws.rs.core.Response) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) TreeNode(org.glassfish.flashlight.datatree.TreeNode) Domain(com.sun.enterprise.config.serverbeans.Domain) Map(java.util.Map) TreeMap(java.util.TreeMap) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with ActionReportResult

use of org.glassfish.admin.rest.results.ActionReportResult in project Payara by payara.

the class PropertiesBagResource method clearThenSaveProperties.

protected ActionReportResult clearThenSaveProperties(List<Map<String, String>> properties) {
    RestActionReporter ar = new RestActionReporter();
    ar.setActionDescription("property");
    try {
        TranslatedConfigView.doSubstitution.set(Boolean.FALSE);
        Map<String, Property> existing = getExistingProperties();
        deleteMissingProperties(existing, properties);
        Map<String, String> data = new LinkedHashMap<String, String>();
        for (Map<String, String> property : properties) {
            Property existingProp = existing.get(property.get("name"));
            String unescapedName = Object.class.cast(property.get("name")).toString();
            String escapedName = getEscapedPropertyName(unescapedName);
            String value = Object.class.cast(property.get("value")).toString();
            String unescapedValue = value.replaceAll("\\\\", "");
            String description = null;
            if (property.get(description) != null) {
                description = Object.class.cast(property.get("description")).toString();
            }
            // the prop name can not contain .
            // need to remove the . test when http://java.net/jira/browse/GLASSFISH-15418  is fixed
            boolean canSaveDesc = !((Object) property.get("name")).toString().contains(".");
            if ((existingProp == null) || !unescapedValue.equals(existingProp.getValue())) {
                data.put(escapedName, ((Object) property.get("value")).toString());
                if (canSaveDesc && (description != null)) {
                    data.put(escapedName + ".description", ((Object) description).toString());
                }
            }
            // update the description only if not null/blank
            if ((description != null) && (existingProp != null)) {
                if (!"".equals(description) && (!description.equals(existingProp.getDescription()))) {
                    if (canSaveDesc) {
                        data.put(escapedName + ".description", description);
                    }
                }
            }
        }
        if (!data.isEmpty()) {
            Util.applyChanges(data, uriInfo, getSubject());
        }
        String successMessage = localStrings.getLocalString("rest.resource.update.message", "\"{0}\" updated successfully.", new Object[] { uriInfo.getAbsolutePath() });
        ar.setSuccess();
        ar.setMessage(successMessage);
    } catch (Exception ex) {
        if (ex.getCause() instanceof ValidationException) {
            ar.setFailure();
            ar.setFailureCause(ex);
            ar.setMessage(ex.getLocalizedMessage());
        } else {
            logger.log(Level.FINE, "Error processing properties", ex);
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    } finally {
        TranslatedConfigView.doSubstitution.set(Boolean.TRUE);
    }
    return new ActionReportResult("properties", ar, new OptionsResult(Util.getResourceName(uriInfo)));
}
Also used : ValidationException(javax.validation.ValidationException) ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) WebApplicationException(javax.ws.rs.WebApplicationException) ValidationException(javax.validation.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) OptionsResult(org.glassfish.admin.rest.results.OptionsResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) Property(org.jvnet.hk2.config.types.Property)

Example 3 with ActionReportResult

use of org.glassfish.admin.rest.results.ActionReportResult in project Payara by payara.

the class PropertiesBagResource method get.

@GET
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Object get() {
    List<Dom> entities = getEntity();
    if (entities == null) {
        // empty dom list
        return new GetResultList(new ArrayList(), "", new String[][] {}, new OptionsResult(Util.getResourceName(uriInfo)));
    }
    RestActionReporter ar = new RestActionReporter();
    ar.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    ar.setActionDescription("property");
    List properties = new ArrayList();
    for (Dom child : entities) {
        Map<String, String> entry = new HashMap<String, String>();
        entry.put("name", child.attribute("name"));
        entry.put("value", child.attribute("value"));
        String description = child.attribute("description");
        if (description != null) {
            entry.put("description", description);
        }
        properties.add(entry);
    }
    Properties extraProperties = new Properties();
    extraProperties.put("properties", properties);
    ar.setExtraProperties(extraProperties);
    return new ActionReportResult("properties", ar, new OptionsResult(Util.getResourceName(uriInfo)));
}
Also used : Dom(org.jvnet.hk2.config.Dom) ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) GetResultList(org.glassfish.admin.rest.results.GetResultList) GetResultList(org.glassfish.admin.rest.results.GetResultList) OptionsResult(org.glassfish.admin.rest.results.OptionsResult) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with ActionReportResult

use of org.glassfish.admin.rest.results.ActionReportResult in project Payara by payara.

the class TemplateListOfResource method buildActionReportResult.

protected ActionReportResult buildActionReportResult() {
    if (entity == null) {
        // wrong resource
        String errorMessage = localStrings.getLocalString("rest.resource.erromessage.noentity", "Resource not found.");
        return ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, errorMessage, requestHeaders, uriInfo);
    }
    RestActionReporter ar = new RestActionReporter();
    final String typeKey = (decode(getName(uriInfo.getPath(), '/')));
    ar.setActionDescription(typeKey);
    OptionsResult optionsResult = new OptionsResult(Util.getResourceName(uriInfo));
    Map<String, MethodMetaData> mmd = getMethodMetaData();
    optionsResult.putMethodMetaData("GET", mmd.get("GET"));
    optionsResult.putMethodMetaData("POST", mmd.get("POST"));
    ResourceUtil.addMethodMetaData(ar, mmd);
    ar.getExtraProperties().put("childResources", ResourceUtil.getResourceLinks(getEntity(), uriInfo));
    ar.getExtraProperties().put("commands", ResourceUtil.getCommandLinks(getCommandResourcesPaths()));
    // an internal impl detail, so it can wait
    return new ActionReportResult(ar, optionsResult);
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) MethodMetaData(org.glassfish.admin.rest.provider.MethodMetaData) OptionsResult(org.glassfish.admin.rest.results.OptionsResult)

Example 5 with ActionReportResult

use of org.glassfish.admin.rest.results.ActionReportResult in project Payara by payara.

the class TemplateRestResource method buildActionReportResult.

protected ActionReportResult buildActionReportResult(boolean showEntityValues) {
    RestActionReporter ar = new RestActionReporter();
    ar.setExtraProperties(new Properties());
    ConfigBean entity = (ConfigBean) getEntity();
    if (childID != null) {
        ar.setActionDescription(childID);
    } else if (childModel != null) {
        ar.setActionDescription(childModel.getTagName());
    }
    if (showEntityValues) {
        if (entity != null) {
            ar.getExtraProperties().put("entity", getAttributes(entity));
        }
    }
    OptionsResult optionsResult = new OptionsResult(Util.getResourceName(uriInfo));
    Map<String, MethodMetaData> mmd = getMethodMetaData();
    optionsResult.putMethodMetaData("GET", mmd.get("GET"));
    optionsResult.putMethodMetaData("POST", mmd.get("POST"));
    optionsResult.putMethodMetaData("DELETE", mmd.get("DELETE"));
    ResourceUtil.addMethodMetaData(ar, mmd);
    if (entity != null) {
        ar.getExtraProperties().put("childResources", ResourceUtil.getResourceLinks(entity, uriInfo, ResourceUtil.canShowDeprecatedItems(locatorBridge.getRemoteLocator())));
    }
    ar.getExtraProperties().put("commands", ResourceUtil.getCommandLinks(getCommandResourcesPaths()));
    return new ActionReportResult(ar, entity, optionsResult);
}
Also used : ActionReportResult(org.glassfish.admin.rest.results.ActionReportResult) RestActionReporter(org.glassfish.admin.rest.utils.xml.RestActionReporter) ConfigBean(org.jvnet.hk2.config.ConfigBean) MethodMetaData(org.glassfish.admin.rest.provider.MethodMetaData) Properties(java.util.Properties) OptionsResult(org.glassfish.admin.rest.results.OptionsResult)

Aggregations

ActionReportResult (org.glassfish.admin.rest.results.ActionReportResult)24 RestActionReporter (org.glassfish.admin.rest.utils.xml.RestActionReporter)23 OptionsResult (org.glassfish.admin.rest.results.OptionsResult)13 Produces (javax.ws.rs.Produces)9 GET (javax.ws.rs.GET)7 ArrayList (java.util.ArrayList)5 Consumes (javax.ws.rs.Consumes)5 MethodMetaData (org.glassfish.admin.rest.provider.MethodMetaData)5 ActionReport (org.glassfish.api.ActionReport)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Properties (java.util.Properties)4 Test (org.testng.annotations.Test)3 Domain (com.sun.enterprise.config.serverbeans.Domain)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 POST (javax.ws.rs.POST)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Response (javax.ws.rs.core.Response)2 ActionReportResultHtmlProvider (org.glassfish.admin.rest.provider.ActionReportResultHtmlProvider)2