Class ResourceUtil


  • public class ResourceUtil
    extends Object
    Resource utilities class. Used by resource templates, TemplateListOfResource and TemplateRestResource
    Author:
    Rajeshwar Patil
    • Method Detail

      • getBytesFromStream

        protected static byte[] getBytesFromStream​(InputStream is)
      • adjustParameters

        public static void adjustParameters​(Map<String,​String> data)
        Adjust the input parameters. In case of POST and DELETE methods, user can provide name, id or DEFAULT parameter for primary parameter(i.e the object to create or delete). This method is used to rename primary parameter name to DEFAULT irrespective of what user provides.
      • defineDefaultParameters

        public static void defineDefaultParameters​(Map<String,​String> data)
        Adjust the input parameters. In case of POST and DELETE methods, user can provide id or DEFAULT parameter for primary parameter(i.e the object to create or delete). This method is used to rename primary parameter name to DEFAULT irrespective of what user provides.
      • getCommand

        public static String getCommand​(RestRedirect.OpType type,
                                        ConfigModel model)
        Returns the name of the command associated with this resource,if any, for the given operation.
        Parameters:
        type - the given resource operation
        Returns:
        String the associated command name for the given operation.
      • runCommand

        public static RestActionReporter runCommand​(String commandName,
                                                    ParameterMap parameters,
                                                    Subject subject)
        Executes the specified __asadmin command.
        Parameters:
        commandName -
        parameters -
        subject -
        Returns:
      • runCommand

        public static RestActionReporter runCommand​(String commandName,
                                                    ParameterMap parameters,
                                                    Subject subject,
                                                    boolean managedJob)
        Executes the specified __asadmin command.
        Parameters:
        commandName -
        parameters -
        subject -
        managedJob -
        Returns:
      • runCommand

        public static RestActionReporter runCommand​(String commandName,
                                                    Map<String,​String> parameters,
                                                    Subject subject)
        Executes the specified __asadmin command.
        Parameters:
        commandName - the command to execute
        parameters - the command parameters
        subject - Subject
        Returns:
        ActionReport object with command execute status details.
      • encodeString

        public static String encodeString​(String text)
      • getMethodMetaData

        public static MethodMetaData getMethodMetaData​(String command,
                                                       org.glassfish.hk2.api.ServiceLocator habitat)
        Constructs and returns the resource method meta-data.
        Parameters:
        command - the command associated with the resource method
        habitat - the habitat
        Returns:
        MethodMetaData the meta-data store for the resource method.
      • getMethodMetaData

        public static MethodMetaData getMethodMetaData​(String command,
                                                       Map<String,​String> commandParamsToSkip,
                                                       org.glassfish.hk2.api.ServiceLocator habitat)
        Constructs and returns the resource method meta-data.
        Parameters:
        command - the command associated with the resource method
        commandParamsToSkip - the command parameters for which not to include the meta-data.
        habitat - the habitat
        Returns:
        MethodMetaData the meta-data store for the resource method.
      • resolveParamValues

        public static void resolveParamValues​(Map<String,​String> commandParams,
                                              jakarta.ws.rs.core.UriInfo uriInfo)
      • getMethodMetaData

        public static MethodMetaData getMethodMetaData​(ConfigModel configBeanModel)
        Constructs and returns the resource method meta-data. This method is called to get meta-data in case of update method (POST).
        Parameters:
        configBeanModel - the config bean associated with the resource.
        Returns:
        MethodMetaData the meta-data store for the resource method.
      • getInterfaces

        protected static void getInterfaces​(Class<?> clazz,
                                            List<Class<?>> interfaces)
      • commandIsPresent

        public static boolean commandIsPresent​(org.glassfish.hk2.api.ServiceLocator habitat,
                                               String commandName)
      • getParamMetaData

        public static Collection<CommandModel.ParamModel> getParamMetaData​(String commandName,
                                                                           org.glassfish.hk2.api.ServiceLocator habitat)
        Constructs and returns the parameter meta-data.
        Parameters:
        commandName - the command associated with the resource method
        habitat - the habitat
        Returns:
        Collection the meta-data for the parameter of the resource method.
      • getParamMetaData

        public static Collection<CommandModel.ParamModel> getParamMetaData​(String commandName,
                                                                           Collection<String> commandParamsToSkip,
                                                                           org.glassfish.hk2.api.ServiceLocator habitat)
        Constructs and returns the parameter meta-data.
        Parameters:
        commandName - the command associated with the resource method
        commandParamsToSkip - the command parameters for which not to include the meta-data.
        habitat - the habitat
        Returns:
        Collection the meta-data for the parameter of the resource method.
      • purgeEmptyEntries

        public static void purgeEmptyEntries​(Map<String,​String> data)
        Removes entries with empty value from the given Map
        Parameters:
        data - data to remove empty entries from
      • getResponse

        public static jakarta.ws.rs.core.Response getResponse​(int status,
                                                              String message,
                                                              jakarta.ws.rs.core.HttpHeaders requestHeaders,
                                                              jakarta.ws.rs.core.UriInfo uriInfo)
        Constructs and returns the appropriate response object based on the client.
        Parameters:
        status - the http status code for the response
        message - message for the response
        requestHeaders - request headers of the request
        Returns:
        Response the response object to be returned to the client
      • getDeleteResponse

        public static jakarta.ws.rs.core.Response getDeleteResponse​(int status,
                                                                    String message,
                                                                    jakarta.ws.rs.core.HttpHeaders requestHeaders,
                                                                    jakarta.ws.rs.core.UriInfo uriInfo)
        special case for the delete operation: we need to give back the URI of the parent since the resource we are on is deleted
        Parameters:
        status -
        message -
        requestHeaders -
        uriInfo -
        Returns:
      • addQueryString

        public static void addQueryString​(jakarta.ws.rs.core.MultivaluedMap<String,​String> qs,
                                          Map<String,​String> data)

        This method takes any query string parameters and adds them to the specified map. This is used, for example, with the delete operation when cascading deletes are required:

        DELETE http://localhost:4848/.../foo?cascade=true

        The reason we need to use query parameters versus "body" variables is the limitation that HttpURLConnection has in this regard.

        Parameters:
        data -
      • addQueryString

        public static void addQueryString​(jakarta.ws.rs.core.MultivaluedMap<String,​String> qs,
                                          Properties data)
      • getAttributeMethodName

        public static String getAttributeMethodName​(String attributeName)
      • getAttributeBooleanMethodName

        public static String getAttributeBooleanMethodName​(String attributeName)
      • convertToXMLName

        public static String convertToXMLName​(String name)
      • translateCamelCasedNamesToXMLNames

        public static Map<String,​String> translateCamelCasedNamesToXMLNames​(Map<String,​String> sourceData)
        Returns:
        A copy of given sourceData where key of each entry from it is converted to xml name
      • getResultType

        public static String getResultType​(jakarta.ws.rs.core.HttpHeaders requestHeaders)
      • buildMethodMetadataMap

        public static Map buildMethodMetadataMap​(MethodMetaData mmd)
      • isDeprecated

        public static boolean isDeprecated​(ConfigModel model)
      • getResourceLinks

        public static Map<String,​String> getResourceLinks​(Dom dom,
                                                                jakarta.ws.rs.core.UriInfo uriInfo,
                                                                boolean canShowDeprecated)
      • getUnqualifiedTypeName

        public static String getUnqualifiedTypeName​(String qualifiedTypeName)
        Parameters:
        qualifiedTypeName -
        Returns:
        unqualified type name for given qualified type name. This is a substring of qualifiedTypeName after last "."
      • isOnlyATag

        public static boolean isOnlyATag​(ConfigModel model)
      • getResourceLinks

        public static Map<String,​String> getResourceLinks​(List<Dom> proxyList,
                                                                jakarta.ws.rs.core.UriInfo uriInfo)
      • getRestConfig

        public static RestConfig getRestConfig​(org.glassfish.hk2.api.ServiceLocator habitat)
      • canShowDeprecatedItems

        public static boolean canShowDeprecatedItems​(org.glassfish.hk2.api.ServiceLocator habitat)
      • authenticateViaAdminRealm

        public static Subject authenticateViaAdminRealm​(org.glassfish.hk2.api.ServiceLocator habitat,
                                                        org.glassfish.grizzly.http.server.Request req,
                                                        String remoteHost)
                                                 throws LoginException,
                                                        IOException
        Authenticate the given req as originated from given remoteHost against admin realm.
        Returns:
        subject identifying the user/client
        Throws:
        LoginException
        IOException
      • isAuthorized

        public static boolean isAuthorized​(org.glassfish.hk2.api.ServiceLocator habitat,
                                           Subject subject,
                                           String resource,
                                           String action)
                                    throws URISyntaxException
        Indicates whether the subject can perform the action on the resource.
        Parameters:
        habitat - ServiceLocator for finding services
        subject - the Subject to be qualified
        resource - the resource affected by the action
        action - the action being attempted by the subject on the resource
        Returns:
        true if the subject is allowed to perform the action, false otherwise
        Throws:
        URISyntaxException
      • processJvmOptions

        public static Map<String,​String> processJvmOptions​(Map<String,​String> jvmOptions,
                                                                 boolean removeVersioning)
        Creates a new rearranged map of JVM options. Options target and profiler are forwarded 1:1. All other options are joined in the result map for key id and are separated by semi-colon. An input key may include a value. In such case key and value are divided by an equals sign: key=value. In case of an empty value the key may end with an equals sign: key=. If the value is given as part of the key the input value should be empty or null. A value may itself contain equals signs. Keys ending with a backslash the backslash is stripped away. This is a backwards compatibility behaviour addressing remains of escaped equals sign that isn't properly unescaped prior to splitting in all path. The resulting option only uses an equals sign between key and value in case the value is non-empty. This is independent of whether the value was extracted from the input key or input value.
        Parameters:
        jvmOptions - a set of jvm options given as key-value pairs, keys are allowed to contain values too
        removeVersioning - set true to erase any JVM version prefix from the keys, false to keep keys as they are.
        Returns:
        a map where most options are joined into one expression for key id. If existing target and profiler keys are kept same as in input map.