Package org.glassfish.admin.rest.utils
Class ResourceUtil
- java.lang.Object
-
- org.glassfish.admin.rest.utils.ResourceUtil
-
public class ResourceUtil extends Object
Resource utilities class. Used by resource templates,TemplateListOfResource
andTemplateRestResource
- Author:
- Rajeshwar Patil
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addCommandLog(RestActionReporter ar, String commandName, ParameterMap parameters)
static void
addMethodMetaData(ActionReport ar, Map<String,MethodMetaData> mmd)
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.static void
addQueryString(jakarta.ws.rs.core.MultivaluedMap<String,String> qs, Properties data)
static void
adjustParameters(Map<String,String> data)
Adjust the input parameters.static Subject
authenticateViaAdminRealm(org.glassfish.hk2.api.ServiceLocator habitat, org.glassfish.grizzly.http.server.Request req, String remoteHost)
Authenticate the given req as originated from given remoteHost against admin realm.static Map
buildMethodMetadataMap(MethodMetaData mmd)
static boolean
canShowDeprecatedItems(org.glassfish.hk2.api.ServiceLocator habitat)
static boolean
commandIsPresent(org.glassfish.hk2.api.ServiceLocator habitat, String commandName)
static String
convertToXMLName(String name)
protected static void
copyParameterMetaDataAttribute(ParameterMetaData from, ParameterMetaData to, String key)
static void
defineDefaultParameters(Map<String,String> data)
Adjust the input parameters.static String
encodeString(String text)
static ActionReportResult
getActionReportResult(RestActionReporter ar, String message, jakarta.ws.rs.core.HttpHeaders requestHeaders, jakarta.ws.rs.core.UriInfo uriInfo)
static ActionReportResult
getActionReportResult(ActionReport.ExitCode status, String message, jakarta.ws.rs.core.HttpHeaders requestHeaders, jakarta.ws.rs.core.UriInfo uriInfo)
static String
getAttributeBooleanMethodName(String attributeName)
static String
getAttributeMethodName(String attributeName)
protected static byte[]
getBytesFromStream(InputStream is)
static String
getCommand(RestRedirect.OpType type, ConfigModel model)
Returns the name of the command associated with this resource,if any, for the given operation.static List<Map<String,String>>
getCommandLinks(String[][] commandResourcesPaths)
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 deletedprotected static void
getInterfaces(Class<?> clazz, List<Class<?>> interfaces)
static MethodMetaData
getMethodMetaData(String command, Map<String,String> commandParamsToSkip, org.glassfish.hk2.api.ServiceLocator habitat)
Constructs and returns the resource method meta-data.static MethodMetaData
getMethodMetaData(String command, org.glassfish.hk2.api.ServiceLocator habitat)
Constructs and returns the resource method meta-data.static MethodMetaData
getMethodMetaData(ConfigModel configBeanModel)
Constructs and returns the resource method meta-data.static MethodMetaData
getMethodMetaData2(Dom parent, ConfigModel childModel, int parameterType)
static String
getParameterList(ParameterMap parameters)
static Collection<CommandModel.ParamModel>
getParamMetaData(String commandName, Collection<String> commandParamsToSkip, org.glassfish.hk2.api.ServiceLocator habitat)
Constructs and returns the parameter meta-data.static Collection<CommandModel.ParamModel>
getParamMetaData(String commandName, org.glassfish.hk2.api.ServiceLocator habitat)
Constructs and returns the parameter meta-data.static List<ConfigModel>
getRealChildConfigModels(ConfigModel childModel, DomDocument domDocument)
static Map<String,String>
getResourceLinks(List<Dom> proxyList, jakarta.ws.rs.core.UriInfo uriInfo)
static Map<String,String>
getResourceLinks(Dom dom, jakarta.ws.rs.core.UriInfo uriInfo, boolean canShowDeprecated)
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.static RestConfig
getRestConfig(org.glassfish.hk2.api.ServiceLocator habitat)
static String
getResultType(jakarta.ws.rs.core.HttpHeaders requestHeaders)
static String
getUnqualifiedTypeName(String qualifiedTypeName)
static boolean
isAuthorized(org.glassfish.hk2.api.ServiceLocator habitat, Subject subject, String resource, String action)
Indicates whether the subject can perform the action on the resource.static boolean
isDeprecated(ConfigModel model)
static boolean
isOnlyATag(ConfigModel model)
static Map<String,String>
processJvmOptions(Map<String,String> jvmOptions, boolean removeVersioning)
Creates a new rearranged map of JVM options.static void
purgeEmptyEntries(Map<String,String> data)
Removes entries with empty value from the given Mapstatic void
resolveParamValues(Map<String,String> commandParams, jakarta.ws.rs.core.UriInfo uriInfo)
static RestActionReporter
runCommand(String commandName, Map<String,String> parameters, Subject subject)
Executes the specified __asadmin command.static RestActionReporter
runCommand(String commandName, ParameterMap parameters, Subject subject)
Executes the specified __asadmin command.static RestActionReporter
runCommand(String commandName, ParameterMap parameters, Subject subject, boolean managedJob)
Executes the specified __asadmin command.static org.glassfish.jersey.media.sse.EventOutput
runCommandWithSse(String commandName, ParameterMap parameters, Subject subject, SseCommandHelper.ActionReportProcessor processor)
static Map<String,String>
translateCamelCasedNamesToXMLNames(Map<String,String> sourceData)
-
-
-
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 executeparameters
- the command parameterssubject
- Subject- Returns:
- ActionReport object with command execute status details.
-
runCommandWithSse
public static org.glassfish.jersey.media.sse.EventOutput runCommandWithSse(String commandName, ParameterMap parameters, Subject subject, SseCommandHelper.ActionReportProcessor processor)
-
addCommandLog
public static void addCommandLog(RestActionReporter ar, String commandName, ParameterMap parameters)
-
getParameterList
public static String getParameterList(ParameterMap parameters)
-
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 methodhabitat
- 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 methodcommandParamsToSkip
- 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.
-
getMethodMetaData2
public static MethodMetaData getMethodMetaData2(Dom parent, ConfigModel childModel, int parameterType)
-
copyParameterMetaDataAttribute
protected static void copyParameterMetaDataAttribute(ParameterMetaData from, ParameterMetaData to, String key)
-
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 methodhabitat
- 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 methodcommandParamsToSkip
- 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 responsemessage
- message for the responserequestHeaders
- request headers of the request- Returns:
- Response the response object to be returned to the client
-
getActionReportResult
public static ActionReportResult getActionReportResult(ActionReport.ExitCode status, String message, jakarta.ws.rs.core.HttpHeaders requestHeaders, jakarta.ws.rs.core.UriInfo uriInfo)
-
getActionReportResult
public static ActionReportResult getActionReportResult(RestActionReporter ar, String message, jakarta.ws.rs.core.HttpHeaders requestHeaders, jakarta.ws.rs.core.UriInfo uriInfo)
-
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)
-
getAttributeBooleanMethodName
public static String getAttributeBooleanMethodName(String attributeName)
-
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)
-
getRealChildConfigModels
public static List<ConfigModel> getRealChildConfigModels(ConfigModel childModel, DomDocument domDocument)
-
getResourceLinks
public static Map<String,String> getResourceLinks(List<Dom> proxyList, jakarta.ws.rs.core.UriInfo uriInfo)
-
getCommandLinks
public static List<Map<String,String>> getCommandLinks(String[][] commandResourcesPaths)
-
addMethodMetaData
public static void addMethodMetaData(ActionReport ar, Map<String,MethodMetaData> mmd)
-
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 servicessubject
- the Subject to be qualifiedresource
- the resource affected by the actionaction
- 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. Optionstarget
andprofiler
are forwarded 1:1. All other options are joined in the result map for keyid
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 ornull
. 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 tooremoveVersioning
- settrue
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 existingtarget
andprofiler
keys are kept same as in input map.
-
-