All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.admin.rest.provider.ActionReportResultHtmlProvider Maven / Gradle / Ivy

There is a newer version: 4.1.2.181
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2010-2013 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package org.glassfish.admin.rest.provider;

import org.glassfish.api.ActionReport.ExitCode;
import com.sun.enterprise.v3.common.ActionReporter;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.glassfish.admin.rest.results.ActionReportResult;
import org.glassfish.admin.rest.utils.xml.RestActionReporter;
import org.glassfish.api.ActionReport;
import org.jvnet.hk2.config.ConfigBean;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import static org.glassfish.admin.rest.provider.ProviderUtil.getHtmlForComponent;
import static org.glassfish.admin.rest.provider.ProviderUtil.getHtmlRespresentationsForCommand;
import static org.glassfish.admin.rest.provider.ProviderUtil.getHint;
import org.glassfish.admin.rest.utils.ResourceUtil;

/**
 * @author Ludovic Champenois
 */
@Provider
@Produces(MediaType.TEXT_HTML)
public class ActionReportResultHtmlProvider extends BaseProvider {
    public ActionReportResultHtmlProvider() {
        super(ActionReportResult.class, MediaType.TEXT_HTML_TYPE);
    }

    @Override
    public String getContent(ActionReportResult proxy) {
        RestActionReporter ar = (RestActionReporter) proxy.getActionReport();
        StringBuilder result = new StringBuilder(ProviderUtil.getHtmlHeader(getBaseUri()));
        final String message = ResourceUtil.encodeString(ar.getCombinedMessage());

        if (!message.isEmpty()) {
            result.append("

").append(message).append("

"); } if (proxy.isError()) { result.append("

").append(ar.getActionDescription()).append(" Error:

") .append(proxy.getErrorMessage()); } else { final Map childResources = (Map) ar.getExtraProperties().get("childResources"); final List> commands = (List>) ar.getExtraProperties().get("commands"); final MethodMetaData postMetaData = proxy.getMetaData().getMethodMetaData("POST"); final MethodMetaData deleteMetaData = proxy.getMetaData().getMethodMetaData("DELETE"); final MethodMetaData getMetaData = proxy.getMetaData().getMethodMetaData("GET"); final ConfigBean entity = proxy.getEntity(); if ((proxy.getCommandDisplayName()!=null) &&(getMetaData!=null)) {//for commands, we want the output of the command before the form if (entity==null) {//show extra properties only for non entity pages result.append(processReport(ar)); } } if ((postMetaData != null) && (entity == null)) { String postCommand = getHtmlRespresentationsForCommand(postMetaData, "POST", ( proxy.getCommandDisplayName()==null )? "Create" : proxy.getCommandDisplayName(), uriInfo.get()); result.append(getHtmlForComponent(postCommand, "Create " + ar.getActionDescription(), "")); } if ((deleteMetaData != null) && (entity == null)) { String deleteCommand = getHtmlRespresentationsForCommand(deleteMetaData, "DELETE", ( proxy.getCommandDisplayName()==null )? "Delete" : proxy.getCommandDisplayName(), uriInfo.get()); result.append(getHtmlForComponent(deleteCommand, "Delete " + ar.getActionDescription(), "")); } if ((getMetaData != null) && (entity == null) &&(proxy.getCommandDisplayName()!=null )) { String getCommand = getHtmlRespresentationsForCommand(getMetaData, "GET", ( proxy.getCommandDisplayName()==null )? "Get" : proxy.getCommandDisplayName(), uriInfo.get()); result.append(getHtmlForComponent(getCommand, "Get " + ar.getActionDescription(), "")); } if (entity != null) { String attributes = ProviderUtil.getHtmlRepresentationForAttributes(proxy.getEntity(), uriInfo.get()); result.append(ProviderUtil.getHtmlForComponent(attributes, ar.getActionDescription() + " Attributes", "")); String deleteCommand = ProviderUtil.getHtmlRespresentationsForCommand(proxy.getMetaData().getMethodMetaData("DELETE"), "DELETE", (proxy.getCommandDisplayName() == null) ? "Delete" : proxy.getCommandDisplayName(), uriInfo.get()); result.append(ProviderUtil.getHtmlForComponent(deleteCommand, "Delete " + entity.model.getTagName(), "")); } else if (proxy.getLeafContent()!=null){ //it is a single leaf @Element String content = "
"+ "
"+ ""+ "
"+ ""+ "
"+ "


"); for (Map.Entry entry : (Set) vals.entrySet()) { Object object = entry.getValue(); if (object == null) { //do nothing } else if (object instanceof Collection) { if (!((Collection) object).isEmpty()) { Collection c = ((Collection) object); Iterator i = c.iterator(); result.append("
  • ").append(entry.getKey()); result.append("
      "); while (i.hasNext()) { result.append("
    • ").append(getHtmlRepresentation(i.next())).append("
    • "); } result.append("
    "); result.append("
  • "); } } else if (object instanceof Map) { if (!((Map) object).isEmpty()) { Map m = (Map) object; if (vals.size() != 1) {//add a link if more than 1 child result.append("
  • ").append("" + entry.getKey() + ""); } else { result.append("
  • ").append(entry.getKey()); } result.append("
      "); for (Map.Entry anEntry : (Set) m.entrySet()) { final String htmlRepresentation = getHtmlRepresentation(anEntry.getValue()); if (htmlRepresentation != null) { result.append("
    • ").append(anEntry.getKey()).append(" : ").append(htmlRepresentation).append("
    • "); } } result.append("
    "); result.append("
  • "); } } else { result.append("
  • ").append(entry.getKey()).append(" : ").append(object.toString()).append("
  • "); } } result.append(""); } else {//no values to show... give an hint if ((childResources == null) || (childResources.isEmpty())) { if ((uriInfo !=null)&&(uriInfo.get().getPath().equalsIgnoreCase("domain"))) { result.append(getHint(uriInfo.get(), MediaType.TEXT_HTML)); } } } } if ((childResources != null)&&(!childResources.isEmpty())) { String childResourceLinks = getResourcesLinks(childResources); result.append(ProviderUtil.getHtmlForComponent(childResourceLinks, "Child Resources", "")); } if ((commands != null) &&(!commands.isEmpty())) { String commandLinks = getCommandLinks(commands); result.append(ProviderUtil.getHtmlForComponent(commandLinks, "Commands", "")); } } return result.append("
    ").toString(); } protected String getBaseUri() { if ((uriInfo != null) && (uriInfo.get() != null)) { return uriInfo.get().getBaseUri().toASCIIString(); } return ""; } protected String getResourcesLinks(Map childResources) { StringBuilder links = new StringBuilder("
    "); for (Map.Entry link : childResources.entrySet()) { links.append("") .append(link.getKey()) .append("
    "); } return links.append("

    ").toString(); } protected String getCommandLinks(List> commands) { StringBuilder result = new StringBuilder("
    "); boolean showHiddenCommands = canShowHiddenCommands(); for (Map commandList : commands) { String command = commandList.get("command"); String path = commandList.get("path"); if (path.startsWith("_")&&(showHiddenCommands==false)) {//hidden cli command name result.append(""); } } return result.append("

    ").toString(); } protected String processReport(ActionReporter ar) { StringBuilder result = new StringBuilder(); String des=ar.getActionDescription(); //check for no description, make it blank if (des==null){ des=""; } final String message = ResourceUtil.encodeString((ar instanceof RestActionReporter) ? ((RestActionReporter)ar).getCombinedMessage() : ar.getMessage()); if (message!=null){ result.append("

    ") .append(des) .append(" output:

    ") .append("
    ")
                    .append(message)
                    .append("
    ") .append("

    "); } if (ar.getActionExitCode() != ExitCode.SUCCESS) { result.append("

    Exit Code: ").append(ar.getActionExitCode().toString()).append("

    "); } Properties properties = ar.getTopMessagePart().getProps(); if (!properties.isEmpty()) { result.append(processProperties(properties)); } Properties extraProperties = ar.getExtraProperties(); if ((extraProperties != null) && (!extraProperties.isEmpty())) { if ((extraProperties.size()==1)&&(extraProperties.get("methods")!=null)){ //do not show only methods metadata in html, not really needed } else { result.append(getExtraProperties(extraProperties)); } } List children = ar.getTopMessagePart().getChildren(); if (children.size() > 0) { result.append(processChildren(children)); } List subReports = ar.getSubActionsReport(); if (subReports.size() > 0) { result.append(processSubReports(subReports)); } return result.toString(); } protected String processProperties(Properties props) { StringBuilder result = new StringBuilder("

    Properties

    "); result.append(getHtml(props)); return result.append("").toString(); } protected String getExtraProperties(Properties props) { StringBuilder result = new StringBuilder("

    Extra Properties

    "); result.append(getHtml(props)); return result.toString(); } protected String processChildren(List parts) { StringBuilder result = new StringBuilder("

    Children

      "); for (ActionReport.MessagePart part : parts) { result.append("
    • ") .append("") .append(""); List children = part.getChildren(); if (children.size() > 0) { result.append(""); } result.append("
      Message") .append(part.getMessage()) .append("
      Properties") .append(getHtml(part.getProps())) .append("
      Children") .append(processChildren(part.getChildren())) .append("
    • "); } return result.append("
    ").toString(); } protected String processSubReports(List subReports) { StringBuilder result = new StringBuilder("

    Sub Reports

      "); for (ActionReporter subReport : subReports) { result.append(processReport(subReport)); } return result.append("
    ").toString(); } protected String getHtmlRepresentation(Object object) { String result = null; if (object == null) { return ""; } else if (object instanceof Collection) { if (!((Collection)object).isEmpty()) { result = getHtml((Collection) object); } } else if (object instanceof Map) { if (!((Map)object).isEmpty()) { result = getHtml((Map) object); } } else { result = object.toString(); } return result; } protected String getHtml(Collection c) { StringBuilder result = new StringBuilder("
      "); Iterator i = c.iterator(); while (i.hasNext()) { result.append("
    • ") .append(getHtmlRepresentation(i.next())) .append("
    • "); } return result.append("
    ").toString(); } protected String getHtml(Map map) { StringBuilder result = new StringBuilder(); if (!map.isEmpty()) { result.append(""); for (Map.Entry entry : (Set) map.entrySet()) { final String htmlRepresentation = getHtmlRepresentation(entry.getValue()); if (htmlRepresentation != null) { result.append(""); } } result.append("
    keyvalue
    ") .append(entry.getKey()) .append("") .append(htmlRepresentation) .append("
    "); } return result.toString(); } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy