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

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

The newest version!
/*
 * Copyright (c) 2023 Contributors to the Eclipse Foundation
 * Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.admin.rest.provider;

import com.sun.appserv.server.util.Version;

import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.UriInfo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.glassfish.admin.rest.Constants;
import org.glassfish.admin.rest.results.OptionsResult;
import org.glassfish.admin.rest.utils.ResourceUtil;
import org.glassfish.admin.rest.utils.Util;
import org.glassfish.external.statistics.Statistic;
import org.glassfish.external.statistics.impl.StatisticImpl;
import org.jvnet.hk2.config.ConfigBean;

/**
 *
 * @author Pajeshwar Patil
 * @author Ludovic Champenois [email protected]
 */
public class ProviderUtil {
    public static final String KEY_CHILD_RESOURCE = "childResource";
    public static final String KEY_CHILD_RESOURCES = "childResources";
    public static final String KEY_COMMAND = "command";
    public static final String KEY_COMMANDS = "commands";
    public static final String KEY_ENTITY = "entity";
    public static final String KEY_METHODS = "methods";

    /**
     * Produce a string in double quotes with backslash sequences in all the right places.
     */
    public static String quote(String string) {
        if (string == null || string.length() == 0) {
            return "\"\"";
        }

        char b;
        char c = 0;
        int i;
        int len = string.length();
        StringBuilder sb = new StringBuilder(len + 4);
        String t;

        sb.append('"');
        for (i = 0; i < len; i += 1) {
            b = c;
            c = string.charAt(i);
            switch (c) {
            case '\\':
            case '"':
                sb.append('\\');
                sb.append(c);
                break;
            case '/':
                if (b == '<') {
                    sb.append('\\');
                }
                sb.append(c);
                break;
            case '\b':
                sb.append("\\b");
                break;
            case '\t':
                sb.append("\\t");
                break;
            case '\n':
                sb.append("\\n");
                break;
            case '\f':
                sb.append("\\f");
                break;
            case '\r':
                sb.append("\\r");
                break;
            default:
                if (c < ' ') {
                    t = "000" + Integer.toHexString(c);
                    sb.append("\\u").append(t.substring(t.length() - 4));
                } else {
                    sb.append(c);
                }
            }
        }
        sb.append('"');
        return sb.toString();
    }

    protected static String slashToDash(String string) {
        if (string != null && !string.isEmpty()) {
            return string.replaceAll("/", "-");
        } else {
            return string;
        }
    }

    static public String getElementLink(UriInfo uriInfo, String elementName) {
        return uriInfo.getRequestUriBuilder().segment(elementName).build().toASCIIString();

    }

    static protected String getStartXmlElement(String name) {
        assert ((name != null) && name.length() > 0);
        String result = "<";
        result = result + name;
        result = result + ">";
        return result;
    }

    static protected String getEndXmlElement(String name) {
        assert ((name != null) && name.length() > 0);
        String result = "<";
        result = result + "/";
        result = result + name;
        result = result + ">";
        return result;
    }

    static public Map getStatistics(Statistic statistic)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        HashMap results = new HashMap();
        Class classObject = statistic.getClass();
        Method[] methods = classObject.getMethods();
        for (Method method : methods) {
            int modifier = method.getModifiers();
            //consider only the public methods
            if (Modifier.isPublic(modifier)) {
                String name = method.getName();
                //considier only the get* methods
                if (name.startsWith("get")) {
                    name = name.substring("get".length());
                    Class returnType = method.getReturnType();
                    //consider only the methods that return primitives or String objects)
                    if (returnType.isPrimitive() || returnType.getName().equals("java.lang.String")) {
                        results.put(name, method.invoke(statistic, null));
                    } else {
                        //control should never reach here
                        //we do not expect statistic object to return object
                        //as value for any of it's stats.
                    }
                }
            }
        }
        return results;
    }

    static public Map getStatistic(Statistic statistic) {
        Map statsMap;
        // Most likely we will get the proxy of the StatisticImpl,
        // reconvert that so you can access getStatisticAsMap method
        if (Proxy.isProxyClass(statistic.getClass())) {
            statsMap = ((StatisticImpl) Proxy.getInvocationHandler(statistic)).getStaticAsMap();
        } else {
            statsMap = ((StatisticImpl) statistic).getStaticAsMap();
        }

        return statsMap;
    }

    static public HashMap getStringMap(Map map) {
        HashMap stringMap = new HashMap<>();
        if (map != null) {
            //Convert attribute value to String if that's not the case.
            //Attribute value can be Boolean, Interger etc.
            String key = null;
            Object value = null;
            //We know keys in the map are always stored as String objects
            for (Map.Entry entry : map.entrySet()) {
                stringMap.put(entry.getKey(), entry.getValue().toString());
            }
            /*
            Iterator iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                key = iterator.next();
                value = map.get(key);
                stringMap.put(key, value.toString());
            }
            */
        }
        return stringMap;
    }

    static protected String getHtmlRepresentationForAttributes(ConfigBean proxy, UriInfo uriInfo) {
        StringBuilder result = new StringBuilder();

        MethodMetaData methodMetaData = ResourceUtil.getMethodMetaData(proxy.model);

        Set parameters = methodMetaData.parameters();
        ParameterMetaData parameterMetaData;

        for (String parameter : parameters) {
            parameterMetaData = methodMetaData.getParameterMetaData(parameter);
            //parameterMetaData contains attributeNames in camelCasedNames convert them to xmlNames to get the attribute's current value
            String xmlAttributeName = ResourceUtil.convertToXMLName(parameter);
            result.append(getHtmlRespresentationForParameter(parameter, parameterMetaData, proxy.attribute(xmlAttributeName)));
        }

        if (result.length() > 0) {
            return "
" + result.toString() + "
" + "
"; } else { return ""; } } static protected String getHtmlRespresentationsForCommand(MethodMetaData methodMetaData, String commandMethod, String commandDisplayName, UriInfo uriInfo) { StringBuilder result = new StringBuilder(); if (methodMetaData != null) { Set parameters = methodMetaData.parameters(); ParameterMetaData parameterMetaData; for (String parameter : parameters) { parameterMetaData = methodMetaData.getParameterMetaData(parameter); if ((methodMetaData.isFileUploadOperation()) && (parameter.equals("id"))) { parameterMetaData.setIsFileParameter(true); } result.append(getHtmlRespresentationForParameter(parameter, parameterMetaData)); } //Fix to diplay component for commands with 0 arguments. //For example, rotate-log or restart. if (result.length() == 0) { result.append(" "); } } //hack-1 : support delete method for html //hardcode "post" instead of commandMethod which should be post or delete. String webMethod = "post"; if (commandMethod.equalsIgnoreCase("get")) { webMethod = "get"; } if (result.length() != 0) { //set encType if file upload operation String encType = methodMetaData.isFileUploadOperation() ? " enctype=\"multipart/form-data\"" : ""; result = new StringBuilder("
").append("
").append(result); //hack-1 : support delete method for html //add hidden field if (commandMethod.equalsIgnoreCase("DELETE")) { result.append("
"); } //hack-2 : put a flag to delte the empty value for CLI parameters... //add hidden field result.append("
"); result.append("
"); result.append("
"); } return result.toString(); } static protected String getHtmlForComponent(String component, String heading, String result) { if ((component != null) && (component.length() > 0)) { result = result + "

" + heading + "

"; result = result + component; result = result + "
"; } return result; } /** * Method to get the hint to display in case module monitoring levels are OFF * * @param uriInfo the uri context object of the input request * @param mediaType the media type of the input request * @return a hit to display when module monitoring levels are all OFF */ static protected String getHint(UriInfo uriInfo, String mediaType) { String result = ""; java.net.URI baseUri = uriInfo.getBaseUri(); String monitoringLevelsConfigUrl = baseUri.getScheme() + "://" + baseUri.getHost() + ":" + baseUri.getPort() + "/management/domain/configs/config/server-config/monitoring-service/module-monitoring-levels"; String name = Util.localStrings.getLocalString("rest.monitoring.levels.hint.heading", "Hint"); String value = Util.localStrings.getLocalString("rest.monitoring.levels.hint.message", "Module monitoring levels may be OFF. To set module monitoring levels please visit following url: {0}", new Object[] { monitoringLevelsConfigUrl }); if (mediaType.equals(MediaType.TEXT_HTML)) { monitoringLevelsConfigUrl = "
" + monitoringLevelsConfigUrl + ""; value = Util.localStrings.getLocalString("rest.monitoring.levels.hint.message", "Module monitoring levels may be OFF. To set module monitoring levels please visit following url: {0}", new Object[] { monitoringLevelsConfigUrl }); result = result + "

" + name + "

"; result = result + value + "
"; result = "
" + result + "
" + "
"; return result; } if (mediaType.equals(MediaType.APPLICATION_JSON)) { result = " " + quote(name) + ":" + jsonValue(value); return result; } if (mediaType.equals(MediaType.APPLICATION_XML)) { result = result + " " + name + "=" + quote(value); return result; } return result; } static public String jsonValue(Object value) { String result = ""; if (value.getClass().getName().equals("java.lang.String")) { result = quote(value.toString()); } else { result = value.toString(); } return result; } static public String getHtmlHeader(String baseUri) { String title = Version.getProductId() + " REST Interface"; String result = "" + title + ""; result = result + getInternalStyleSheet(baseUri); result = result + getAjaxJavascript(baseUri); result = result + ""; result = result + "

" + title + "

"; result = result + "
"; return result; } static protected JSONArray getJsonForMethodMetaData(OptionsResult metaData) throws JSONException { OptionsResultJsonProvider provider = new OptionsResultJsonProvider(); return provider.getRespresenationForMethodMetaData(metaData); } static protected String getJsonForMethodMetaData(OptionsResult metaData, String indent) { OptionsResultJsonProvider provider = new OptionsResultJsonProvider(); return provider.getRespresenationForMethodMetaData(metaData).toString(); } static protected String getXmlForMethodMetaData(OptionsResult metaData, String indent) { OptionsResultXmlProvider provider = new OptionsResultXmlProvider(); return provider.getRespresenationForMethodMetaData(metaData, indent); } static private String getHtmlRespresentationForParameter(String parameter, ParameterMetaData parameterMetaData) { return getHtmlRespresentationForParameter(parameter, parameterMetaData, null); } static private String getHtmlRespresentationForParameter(String parameter, ParameterMetaData parameterMetaData, String parameterValue) { if ("true".equals(parameterMetaData.getAttributeValue(Constants.DEPRECATED))) { return ""; } //set appropriate type of input field. In can be of type file or text //file type is used in case of deploy operation String parameterType = parameterMetaData.isFileParameter() ? "file" : "text"; StringBuilder result = new StringBuilder(); result.append("
"); boolean isBoolean = false; if ((parameterMetaData.getAttributeValue(Constants.TYPE).endsWith(Constants.JAVA_BOOLEAN_TYPE)) || (parameterMetaData.getAttributeValue(Constants.TYPE).equals(Constants.XSD_BOOLEAN_TYPE))) { isBoolean = true; } boolean hasAcceptableValues = false; String acceptableValues = parameterMetaData.getAttributeValue(Constants.ACCEPTABLE_VALUES); if ((acceptableValues != null) && (acceptableValues.length() > 0)) { hasAcceptableValues = true; } boolean hasValue = false; if ((parameterValue == null) || (parameterValue.equals(""))) { String defaultValue = parameterMetaData.getAttributeValue(Constants.DEFAULT_VALUE); if ((defaultValue != null) && (defaultValue.length() > 0)) { parameterValue = defaultValue; } } if ((parameterValue != null) && (parameterValue.length() > 0)) { hasValue = true; } boolean keyAttribute = Boolean.parseBoolean(parameterMetaData.getAttributeValue(Constants.KEY)); if (keyAttribute) { if (hasValue) { result.append("
"); } else { //no value for the key, so we are in create mode, enable the entry field in the ui //control should never reach here. result.append("
"); } } else { if (isBoolean || hasAcceptableValues) { //use combo box result.append("
"); } else { //use text box String field; boolean isList = parameterMetaData.getAttributeValue(Constants.TYPE).equals("interface java.util.List"); if (hasValue) { field = ""; } else { field = ""; } result.append("
").append(field); if (isList) { result.append( "Add row"); } result.append("
"); } } return result.toString(); } /** * This method converts a string into string array, uses the delimeter as the separator character. If the delimiter is * null, uses space as default. */ private static String[] stringToArray(String str, String delimiter) { String[] retString = new String[0]; if (str != null) { if (delimiter == null) { delimiter = " "; } StringTokenizer tokens = new StringTokenizer(str, delimiter); retString = new String[tokens.countTokens()]; int i = 0; while (tokens.hasMoreTokens()) { retString[i++] = tokens.nextToken(); } } return retString; } private static String getInternalStyleSheet(String baseUri) { return " "; } private static String getAjaxJavascript(String baseUri) { return " "; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy