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: 6.2024.6
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.
 */
// Portions Copyright [2019-2021] [Payara Foundation and/or its affiliates]

package org.glassfish.admin.rest.provider;

import org.glassfish.api.ActionReport.ExitCode;
import com.sun.enterprise.admin.report.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 jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.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 {
    
    private static final String ANCHOR_OPEN = "").append(message).append(HEADING_END);
        }

        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("
  • ").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(ANCHOR_OPEN) .append(link.getValue()) .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)) {//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(HEADING_END); } if (ar.getActionExitCode() != ExitCode.SUCCESS) { result.append("

    Exit Code: ").append(ar.getActionExitCode().toString()).append(HEADING_END); } 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.isEmpty()) { result.append(processChildren(children)); } List subReports = ar.getSubActionsReport(); if (!subReports.isEmpty()) { 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