Class RequestManager

java.lang.Object
jadex.publishservice.impl.RequestManager

public class RequestManager extends Object
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.
  • Field Details

    • DEFAULT_HOST

      public static final String DEFAULT_HOST
      The default host name.
      See Also:
    • DEFAULT_PORT

      public static final int DEFAULT_PORT
      The default port.
      See Also:
    • DEFAULT_APP

      public static final String DEFAULT_APP
      The default app name.
      See Also:
    • DEFAULT_COMPLETECONTEXT

      public static final String DEFAULT_COMPLETECONTEXT
      The default hostportappcontext.
      See Also:
    • CONVERSATION_TIMEOUT

      public static long CONVERSATION_TIMEOUT
      The conversation timeout.
    • ASYNC_CONTEXT_INFO

      public static final String ASYNC_CONTEXT_INFO
      Async context info.
      See Also:
    • HEADER_JADEX_VERSION

      public static final String HEADER_JADEX_VERSION
      Http header for the Jadex version.
      See Also:
    • HEADER_JADEX_CALLID

      public static final String HEADER_JADEX_CALLID
      Http header for the call id (req and resp).
      See Also:
    • HEADER_JADEX_CALLFINISHED

      public static final String HEADER_JADEX_CALLFINISHED
      Http header for the call id siganlling that this is the last response (resp).
      See Also:
    • HEADER_JADEX_MAX

      public static final String HEADER_JADEX_MAX
      Http header for max value of intermediate future.
      See Also:
    • HEADER_JADEX_CLIENTTIMEOUT

      public static final String HEADER_JADEX_CLIENTTIMEOUT
      Http header for the client side timeout of calls (req).
      See Also:
    • HEADER_JADEX_ALIVE

      public static final String HEADER_JADEX_ALIVE
      Http header for the client side to indicate that conversation is still alive/ongoing.
      See Also:
    • HEADER_JADEX_SSEALIVE

      public static final String HEADER_JADEX_SSEALIVE
      See Also:
    • HEADER_JADEX_TERMINATE

      public static final String HEADER_JADEX_TERMINATE
      Http header to terminate the call (req).
      See Also:
    • HEADER_JADEX_LOGIN

      public static final String HEADER_JADEX_LOGIN
      Http header to login to and gain admin access (req).
      See Also:
    • HEADER_JADEX_LOGOUT

      public static final String HEADER_JADEX_LOGOUT
      See Also:
    • HEADER_JADEX_ISLOGGEDIN

      public static final String HEADER_JADEX_ISLOGGEDIN
      See Also:
    • FINISHED

      public static final String FINISHED
      Finished result marker.
      See Also:
    • RANDOM

      public static final String RANDOM
      URL parameter random request.
      See Also:
    • CONTENTTYPE

      public static final String CONTENTTYPE
      URL parameter type request.
      See Also:
    • ACCEPT

      public static final String ACCEPT
      URL parameter accept request.
      See Also:
    • PARAMETER_MEDIATYPES

      public static final List<String> PARAMETER_MEDIATYPES
      Some basic media types for service invocations.
    • conversationinfos

      protected Map<String,RequestManager.ConversationInfo> conversationinfos
      Info about an ongoing conversation, i.e. Jadex future, session etc.
    • sseevents

      protected List<RequestManager.SSEEvent> sseevents
      SSE events that could not directly be sent.
    • sessions

      protected Map<String,Map<String,Object>> 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

      protected Map<String,RequestManager.SSEInfo> sseinfos
      Infos about the sse sources.
    • converters

      protected jadex.collection.MultiCollection<String,jadex.common.transformation.IObjectStringConverter> converters
      The media type converters.
    • loginsec

      protected boolean loginsec
      Login security of or off.
    • jsonser

      protected jadex.serialization.serializers.JadexJsonSerializer jsonser
      The json processor.
    • serser

      protected jadex.serialization.ISerializationServices serser
      The serialization services.
    • instance

      protected static RequestManager instance
  • Constructor Details

    • RequestManager

      public RequestManager(jadex.serialization.ISerializationServices serser)
  • Method Details

    • createInstance

      public static void createInstance(jadex.serialization.ISerializationServices serser)
    • getInstance

      public static RequestManager 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

      public jadex.future.IFuture<Void> setLoginSecurity(boolean sec)
      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 request
      create - Flag if shall be created.
      Returns:
      The session.
    • getSession

      public Map<String,Object> getSession(String sessionid, boolean create)
      Get a session.
      Parameters:
      create - Flag if shall be created.
      request - The request
      Returns:
      The session.
    • getSSEContextFromSession

      public jakarta.servlet.AsyncContext getSSEContextFromSession(String sessionid)
    • putSSEContextInSession

      public void putSSEContextInSession(String sessionid, jakarta.servlet.AsyncContext context)
    • setInSession

      public void setInSession(String sessionid, String name, Object value)
    • getSession

      public Map<String,Object> getSession(String sessionid)
    • putSession

      public void putSession(String sessionid, Map<String,Object> session)
    • removeSession

      public void removeSession(String sessionid)
    • getSessionId

      public static String getSessionId(jakarta.servlet.http.HttpServletRequest request)
      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

      protected void updateTimestamps(String sessionid)
      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

      protected RequestManager.ConversationInfo getConversation(String callid)
    • addConversation

      protected void addConversation(String callid, RequestManager.ConversationInfo cinfo)
    • hasConversation

      protected boolean hasConversation(String callid)
    • getSSEInfo

      protected RequestManager.SSEInfo getSSEInfo(String sessionid)
    • addSSEInfo

      protected void addSSEInfo(RequestManager.SSEInfo info)
    • removeSSEInfo

      protected void removeSSEInfo(String sessionid)
    • 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

      protected void sendDelayedSSEEvents(Map<String,Object> session)
      Send the delayed events which have been collected during connection loss. Must check if callid belongs to still ongoing call (could be terminated).
    • addSSEEvent

      protected void addSSEEvent(RequestManager.SSEEvent event)
    • logout

      public jadex.future.IFuture<Boolean> logout(jakarta.servlet.http.HttpServletRequest request)
      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

      public boolean isLoggedIn(String callid)
      Test if a the web user is logged in.
      Parameters:
      callid - The callid of the request.
      Returns:
      True, if is logged in.
    • getAsyncContextInfo

      protected IAsyncContextInfo getAsyncContextInfo(jakarta.servlet.http.HttpServletRequest request)
      Get the async
    • addMimeTypes

      public static void addMimeTypes(Object cs, List<String> types)
    • 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

      public static Object convertJsonValue(String val, Class<?> type, ClassLoader cl, boolean tomap)
      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

      public Object convertParameter(Object val, Class<?> target)
      Convert a (string) parameter
      Parameters:
      val -
      target -
      Returns:
    • convertParameter

      protected Object convertParameter(List<String> sr, Object val, Class<?> targetclazz)
      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

      protected Object mapResult(Method method, Object ret)
      Map a result using the result mapper.
    • writeResponse

      protected void writeResponse(RequestManager.ResponseInfo ri)
      Write the response (header and content).
    • writeResponseHeader

      protected List<String> writeResponseHeader(RequestManager.ResponseInfo ri)
    • writeResponseContent

      protected void writeResponseContent(RequestManager.ResponseInfo ri)
      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

      protected String createSSEJson(RequestManager.SSEEvent event)
      Create an sse event for a JavaSSE event.
    • getRequestTimeout

      public static long getRequestTimeout(jakarta.servlet.http.HttpServletRequest request)
      Get the request timeout.
    • parseMimetypes

      public static List<String> parseMimetypes(String mts)
      todo: make statically accessible Copied from Jadex ForwardFilter
    • getParameterInfos

      public jadex.common.Tuple2<List<jadex.common.Tuple2<String,String>>,Map<String,Class<?>>> getParameterInfos(Method method)
      Get metainfo about parameters from the target method via annotations.
    • extractCallerValues

      public Map<String,String> extractCallerValues(Object request)
      Extract caller values like ip and browser.
      Parameters:
      request - The requrest.
      vals - The values.
    • getClassLoader

      protected ClassLoader getClassLoader()
    • getServletUrl

      public static String getServletUrl(jakarta.servlet.http.HttpServletRequest req)
      Get the servlet base url.
      Parameters:
      req - The request.
      Returns:
      The servlet base url.
    • getServletHost

      public static String getServletHost(jakarta.servlet.http.HttpServletRequest req)
      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

      public String loadFunctionJS()
    • loadStyleCSS

      public String loadStyleCSS()
    • guessRestType

      public RequestManager.MappingInfo.HttpMethod guessRestType(Method method)
      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

      public String getPath()
      Get the path to my package as path
      Returns:
      The path.
    • splitQueryString

      public static Map<String,Object> splitQueryString(String query) throws Exception
      Split the query and save the order.
      Throws:
      Exception
    • addEntry

      protected static void addEntry(Map<String,Object> ret, String key, Object val)
      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).