Package jadex.publishservice.impl
Class RequestManager
java.lang.Object
jadex.publishservice.impl.RequestManager
The request manager is used to handle REST web requests.
It follows tha Jadex manager pattern, i.e. it is a singleton accessible
also from static contexts.
- Requests with jadex.js contained will be delivered the jadex.js file.
- Requests on the base url of a published service will be delivered an
overview site for the service.
The request manager has a sophisticated mapping mechanisms for request
paramaters and tries to map naturally as much as possible.
There is (nearly) a 1:1 relationship between sessions and sseinfos. Each jadex.js
uses the "jadex" cookie for session id. The cookie is path wide (from a path all subpaths)
so that multiple jadex.js and thus applications can coexist using different sessions.
Sessions do not need explicit timeouts as SSE pings are sent to the see clients.
Each client responds via a call to baseurl/ssealive path, which must be routed to
the request manager.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Struct for storing info about a request and the results.static class
static class
class
Info struct for response.static class
SSE event data class.static class
Struct for storing info about a request and the results. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
URL parameter accept request.static final String
Async context info.static final String
URL parameter type request.static long
The conversation timeout.protected Map
<String, RequestManager.ConversationInfo> Info about an ongoing conversation, i.e.protected jadex.collection.MultiCollection
<String, jadex.common.transformation.IObjectStringConverter> The media type converters.static final String
The default app name.static final String
The default hostportappcontext.static final String
The default host name.static final int
The default port.static final String
Finished result marker.static final String
Http header for the client side to indicate that conversation is still alive/ongoing.static final String
Http header for the call id siganlling that this is the last response (resp).static final String
Http header for the call id (req and resp).static final String
Http header for the client side timeout of calls (req).static final String
static final String
Http header to login to and gain admin access (req).static final String
static final String
Http header for max value of intermediate future.static final String
static final String
Http header to terminate the call (req).static final String
Http header for the Jadex version.protected static RequestManager
protected jadex.serialization.serializers.JadexJsonSerializer
The json processor.protected boolean
Login security of or off.Some basic media types for service invocations.static final String
URL parameter random request.protected jadex.serialization.ISerializationServices
The serialization services.The sessions.protected List
<RequestManager.SSEEvent> SSE events that could not directly be sent.protected Map
<String, RequestManager.SSEInfo> Infos about the sse sources. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addConversation
(String callid, RequestManager.ConversationInfo cinfo) protected static void
static void
addMimeTypes
(Object cs, List<String> types) protected void
protected void
protected void
complete
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) static Object
convertJsonValue
(String val, Class<?> type, ClassLoader cl, boolean tomap) Convert a json string to a java object.convertParameter
(Object val, Class<?> target) Convert a (string) parameterprotected Object
convertParameter
(List<String> sr, Object val, Class<?> targetclazz) Convert a parameter string to an object if is json or xml.static void
createInstance
(jadex.serialization.ISerializationServices serser) protected RequestManager.SSEEvent
Create an sse event.protected RequestManager.SSEEvent
createSSEEvent
(Object result, boolean finished, String callid, Integer max, String exceptiontype) Create an sse event.protected String
Create an sse event for a JavaSSE event.evaluateMapping
(jadex.providedservice.IServiceIdentifier sid, PublishInfo pi, ClassLoader cl) Evaluate the service interface and generate mappings.extractCallerValues
(Object request) Extract caller values like ip and browser.static Object[]
generateInParameters
(Map<String, Object> inparamsmap, jadex.common.Tuple2<List<jadex.common.Tuple2<String, String>>, Map<String, Class<?>>> pinfos, Class<?>[] types) Generate in parameters that are correct wrt order and number of targetparameter (must convert types possibly).protected IAsyncContextInfo
getAsyncContextInfo
(jakarta.servlet.http.HttpServletRequest request) Get the asyncprotected ClassLoader
protected RequestManager.ConversationInfo
getConversation
(String callid) protected Collection
<jadex.common.transformation.IObjectStringConverter> getConverters
(String mediatype) Get the converters.static RequestManager
getParameterInfos
(Method method) Get metainfo about parameters from the target method via annotations.getPath()
Get the path to my package as pathstatic long
getRequestTimeout
(jakarta.servlet.http.HttpServletRequest request) Get the request timeout.jadex.serialization.ISerializationServices
getServiceInfo
(Object service, String baseuri, PathManager<RequestManager.MappingInfo> mappings) Functionality blueprint for get service info web method.static String
getServletHost
(jakarta.servlet.http.HttpServletRequest req) Get the servlet base url.static String
getServletUrl
(jakarta.servlet.http.HttpServletRequest req) Get the servlet base url.getSession
(jakarta.servlet.http.HttpServletRequest request, boolean create) Get a session.getSession
(String sessionid) getSession
(String sessionid, boolean create) Get a session.static String
getSessionId
(jakarta.servlet.http.HttpServletRequest request) Get a session id.jakarta.servlet.AsyncContext
getSSEContextFromSession
(String sessionid) protected RequestManager.SSEInfo
getSSEInfo
(String sessionid) guessRestType
(Method method) Guess the http type (GET, POST, PUT, DELETE, ...) of a method.void
handleRequest
(jadex.providedservice.IService service, PathManager<RequestManager.MappingInfo> pm, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object[] others) Handle a web request.protected boolean
hasConversation
(String callid) boolean
hasStringConvertableParameters
(Method method, Class<?> rettype, Class<?>[] paramtypes) Test if a method has parameters that are all convertible from string.protected boolean
isComplete
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) boolean
isLoggedIn
(jakarta.servlet.http.HttpServletRequest request) Test if a the web user is logged in.boolean
isLoggedIn
(String callid) Test if a the web user is logged in.jadex.future.IFuture
<byte[]> Load jadex.jsjadex.future.IFuture
<Boolean> Log in to the platform.jadex.future.IFuture
<Boolean> logout
(jakarta.servlet.http.HttpServletRequest request) Logout from the platform.protected jadex.common.Tuple2
<RequestManager.MappingInfo, Object[]> mapParameters
(jakarta.servlet.http.HttpServletRequest request, Collection<RequestManager.MappingInfo> mis, List<Map<String, String>> bindings) Map the incoming uri/post/multipart parameters to the service target parameter types.protected Object
Map a result using the result mapper.parseMimetypes
(String mts) todo: make statically accessible Copied from Jadex ForwardFilterprotected void
Prune the timeouted conversations.void
putSession
(String sessionid, Map<String, Object> session) void
putSSEContextInSession
(String sessionid, jakarta.servlet.AsyncContext context) void
removeSession
(String sessionid) protected void
removeSSEInfo
(String sessionid) protected void
sendDelayedSSEEvents
(Map<String, Object> session) Send the delayed events which have been collected during connection loss.protected void
setCORSHeader
(jakarta.servlet.http.HttpServletResponse response) Set the cors header in the response.void
setInSession
(String sessionid, String name, Object value) jadex.future.IFuture
<Void> setLoginSecurity
(boolean sec) Turn on or off the login security.protected void
setNoCachingHeader
(jakarta.servlet.http.HttpServletResponse response) Set the cache header in the response.splitQueryString
(String query) Split the query and save the order.protected void
terminateConversation
(RequestManager.ConversationInfo cinfo, Exception ex, boolean clientterm) protected void
updateTimestamps
(String sessionid) Update timestamps of all conversations that belong to a session / sse source.protected void
Write the response (header and content).protected void
Write the response content.
-
Field Details
-
DEFAULT_HOST
The default host name.- See Also:
-
DEFAULT_PORT
public static final int DEFAULT_PORTThe default port.- See Also:
-
DEFAULT_APP
The default app name.- See Also:
-
DEFAULT_COMPLETECONTEXT
The default hostportappcontext.- See Also:
-
CONVERSATION_TIMEOUT
public static long CONVERSATION_TIMEOUTThe conversation timeout. -
ASYNC_CONTEXT_INFO
Async context info.- See Also:
-
HEADER_JADEX_VERSION
Http header for the Jadex version.- See Also:
-
HEADER_JADEX_CALLID
Http header for the call id (req and resp).- See Also:
-
HEADER_JADEX_CALLFINISHED
Http header for the call id siganlling that this is the last response (resp).- See Also:
-
HEADER_JADEX_MAX
Http header for max value of intermediate future.- See Also:
-
HEADER_JADEX_CLIENTTIMEOUT
Http header for the client side timeout of calls (req).- See Also:
-
HEADER_JADEX_ALIVE
Http header for the client side to indicate that conversation is still alive/ongoing.- See Also:
-
HEADER_JADEX_SSEALIVE
- See Also:
-
HEADER_JADEX_TERMINATE
Http header to terminate the call (req).- See Also:
-
HEADER_JADEX_LOGIN
Http header to login to and gain admin access (req).- See Also:
-
HEADER_JADEX_LOGOUT
- See Also:
-
HEADER_JADEX_ISLOGGEDIN
- See Also:
-
FINISHED
Finished result marker.- See Also:
-
RANDOM
URL parameter random request.- See Also:
-
CONTENTTYPE
URL parameter type request.- See Also:
-
ACCEPT
URL parameter accept request.- See Also:
-
PARAMETER_MEDIATYPES
Some basic media types for service invocations. -
conversationinfos
Info about an ongoing conversation, i.e. Jadex future, session etc. -
sseevents
SSE events that could not directly be sent. -
sessions
The sessions. Cannot use built-in due to problem with concurrent inital requests (and also after platform restart). Two requests (sse, subscribeX) arrive at server with the same jsessionid -> session not known, server creates a new one for both with different ids. https://community.oracle.com/tech/developers/discussion/1455774/multiple-sessions-created. -
sseinfos
Infos about the sse sources. -
converters
protected jadex.collection.MultiCollection<String,jadex.common.transformation.IObjectStringConverter> convertersThe media type converters. -
loginsec
protected boolean loginsecLogin security of or off. -
jsonser
protected jadex.serialization.serializers.JadexJsonSerializer jsonserThe json processor. -
serser
protected jadex.serialization.ISerializationServices serserThe serialization services. -
instance
-
-
Constructor Details
-
RequestManager
public RequestManager(jadex.serialization.ISerializationServices serser)
-
-
Method Details
-
createInstance
public static void createInstance(jadex.serialization.ISerializationServices serser) -
getInstance
-
getSerializationServices
public jadex.serialization.ISerializationServices getSerializationServices() -
getConverters
protected Collection<jadex.common.transformation.IObjectStringConverter> getConverters(String mediatype) Get the converters. Needs not to be synchronized as long as only gets are performed. -
setLoginSecurity
Turn on or off the login security. If true one has to log in with platform secret before using published services.- Parameters:
sec
- On or off.
-
getSession
public Map<String,Object> getSession(jakarta.servlet.http.HttpServletRequest request, boolean create) Get a session.- Parameters:
request
- The requestcreate
- Flag if shall be created.- Returns:
- The session.
-
getSession
Get a session.- Parameters:
create
- Flag if shall be created.request
- The request- Returns:
- The session.
-
getSSEContextFromSession
-
putSSEContextInSession
-
setInSession
-
getSession
-
putSession
-
removeSession
-
getSessionId
Get a session id.- Parameters:
request
- The request- Returns:
- The session id.
-
handleRequest
public void handleRequest(jadex.providedservice.IService service, PathManager<RequestManager.MappingInfo> pm, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object[] others) throws IOException, jakarta.servlet.ServletException Handle a web request.- Parameters:
service
- The service.request
- The request.response
- The response.mappings
- The collected mapping infos for the service.- Throws:
IOException
jakarta.servlet.ServletException
-
pruneObsoleteConversations
protected void pruneObsoleteConversations()Prune the timeouted conversations. -
updateTimestamps
Update timestamps of all conversations that belong to a session / sse source.- Parameters:
sessionid
- The session id.
-
terminateConversation
protected void terminateConversation(RequestManager.ConversationInfo cinfo, Exception ex, boolean clientterm) - Parameters:
cinfo
-ex
-
-
getConversation
-
addConversation
-
hasConversation
-
getSSEInfo
-
addSSEInfo
-
removeSSEInfo
-
setCORSHeader
protected void setCORSHeader(jakarta.servlet.http.HttpServletResponse response) Set the cors header in the response.- Parameters:
response
- The response.
-
setNoCachingHeader
protected void setNoCachingHeader(jakarta.servlet.http.HttpServletResponse response) Set the cache header in the response.- Parameters:
response
- The response.
-
sendDelayedSSEEvents
Send the delayed events which have been collected during connection loss. Must check if callid belongs to still ongoing call (could be terminated). -
addSSEEvent
-
logout
Logout from the platform.- Parameters:
secret
- The platform secret.- Returns:
- True, if login was successful.
-
isLoggedIn
public boolean isLoggedIn(jakarta.servlet.http.HttpServletRequest request) Test if a the web user is logged in.- Parameters:
request
- The web request.- Returns:
- True, if is logged in.
-
isLoggedIn
Test if a the web user is logged in.- Parameters:
callid
- The callid of the request.- Returns:
- True, if is logged in.
-
getAsyncContextInfo
Get the async -
addMimeTypes
-
mapParameters
protected jadex.common.Tuple2<RequestManager.MappingInfo,Object[]> mapParameters(jakarta.servlet.http.HttpServletRequest request, Collection<RequestManager.MappingInfo> mis, List<Map<String, String>> bindings) Map the incoming uri/post/multipart parameters to the service target parameter types. -
generateInParameters
public static Object[] generateInParameters(Map<String, Object> inparamsmap, jadex.common.Tuple2<List<jadex.common.Tuple2<String, String>>, Map<String, Class<?>>> pinfos, Class<?>[] types) Generate in parameters that are correct wrt order and number of targetparameter (must convert types possibly). -
convertJsonValue
Convert a json string to a java object.- Parameters:
val
- The json string.type
- The target class.cl
- The classloader.tomap
- Flag, if a (nested) map should be read (only possible if type is map too).
-
convertParameter
Convert a (string) parameter- Parameters:
val
-target
-- Returns:
-
convertParameter
Convert a parameter string to an object if is json or xml.- Parameters:
sr
- The media types.val
- The string value.- Returns:
- The decoded object.
-
mapResult
Map a result using the result mapper. -
writeResponse
Write the response (header and content). -
writeResponseHeader
-
writeResponseContent
Write the response content. -
createSSEEvent
Create an sse event.- Parameters:
result
-finished
-callid
-max
-- Returns:
-
createSSEEvent
protected RequestManager.SSEEvent createSSEEvent(Object result, boolean finished, String callid, Integer max, String exceptiontype) Create an sse event. -
createSSEJson
Create an sse event for a JavaSSE event. -
getRequestTimeout
public static long getRequestTimeout(jakarta.servlet.http.HttpServletRequest request) Get the request timeout. -
parseMimetypes
todo: make statically accessible Copied from Jadex ForwardFilter -
getParameterInfos
public jadex.common.Tuple2<List<jadex.common.Tuple2<String,String>>, getParameterInfosMap<String, Class<?>>> (Method method) Get metainfo about parameters from the target method via annotations. -
extractCallerValues
Extract caller values like ip and browser.- Parameters:
request
- The requrest.vals
- The values.
-
getClassLoader
-
getServletUrl
Get the servlet base url.- Parameters:
req
- The request.- Returns:
- The servlet base url.
-
getServletHost
Get the servlet base url.- Parameters:
req
- The request.- Returns:
- The servlet base url.
-
getServiceInfo
public String getServiceInfo(Object service, String baseuri, PathManager<RequestManager.MappingInfo> mappings) Functionality blueprint for get service info web method. Creates a html page with css for style and javascript for ajax post requests. The service info site contains a section for each published method.- Parameters:
params
- The parameters.- Returns:
- The result.
-
loadFunctionJS
-
loadStyleCSS
-
guessRestType
Guess the http type (GET, POST, PUT, DELETE, ...) of a method.- Parameters:
method
- The method.- Returns:
- The rs annotation of the method type to use
-
hasStringConvertableParameters
public boolean hasStringConvertableParameters(Method method, Class<?> rettype, Class<?>[] paramtypes) Test if a method has parameters that are all convertible from string.- Parameters:
method
- The method.rettype
- The return types (possibly unwrapped from future type).paramtypes
- The parameter types.- Returns:
- True, if is convertible.
-
complete
protected void complete(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) - Parameters:
request
-cinfo
-
-
isComplete
protected boolean isComplete(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) - Parameters:
request
-cinfo
-
-
loadJadexJS
public jadex.future.IFuture<byte[]> loadJadexJS()Load jadex.js- Returns:
- The text from the file.
-
getPath
Get the path to my package as path- Returns:
- The path.
-
splitQueryString
Split the query and save the order.- Throws:
Exception
-
addEntry
- Parameters:
ret
-key
-val
-
-
login
public jadex.future.IFuture<Boolean> login(jakarta.servlet.http.HttpServletRequest request, String secret) Log in to the platform.- Parameters:
request
- The request.secret
- The platform secret.- Returns:
- True, if login was successful.
-
evaluateMapping
public PathManager<RequestManager.MappingInfo> evaluateMapping(jadex.providedservice.IServiceIdentifier sid, PublishInfo pi, ClassLoader cl) Evaluate the service interface and generate mappings. Return a multicollection in which for each path name the possible methods are contained (can be more than one due to different parameters).
-