Package jadex.extension.rs.publish
Class AbstractRestPublishService
- java.lang.Object
-
- jadex.extension.rs.publish.AbstractRestPublishService
-
- All Implemented Interfaces:
IPublishService
,IWebPublishService
- Direct Known Subclasses:
ExternalRestPublishService
public abstract class AbstractRestPublishService extends java.lang.Object implements IWebPublishService
Publish service without Jersey directly using different containers (in concrete impls).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractRestPublishService.ConversationInfo
Struct for storing info about a request and the results.static class
AbstractRestPublishService.MappingInfo
static class
AbstractRestPublishService.ResponseInfo
Info struct for response.static class
AbstractRestPublishService.SSEEvent
SSE event data class.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ASYNC_CONTEXT_INFO
Async context info.protected jadex.platform.service.serialization.serializers.JadexBinarySerializer
binser
The binary processor.protected IInternalAccess
component
The component.protected java.util.Map<java.lang.String,AbstractRestPublishService.ConversationInfo>
conversationinfos
Info about an ongoing conversation, i.e.protected MultiCollection<java.lang.String,IObjectStringConverter>
converters
The media type converters.static java.lang.String
FINISHED
Finished result marker.static java.lang.String
HEADER_JADEX_CALLFINISHED
Http header for the call id siganlling that this is the last response (resp).static java.lang.String
HEADER_JADEX_CALLID
Http header for the call id (req and resp).static java.lang.String
HEADER_JADEX_CLIENTTIMEOUT
Http header for the client side timeout of calls (req).static java.lang.String
HEADER_JADEX_ISLOGGEDIN
static java.lang.String
HEADER_JADEX_LOGIN
Http header to login to the platform and gain admin access (req).static java.lang.String
HEADER_JADEX_LOGOUT
static java.lang.String
HEADER_JADEX_MAX
Http header for max value of intermediate future.static java.lang.String
HEADER_JADEX_TERMINATE
Http header to terminate the call (req).static java.lang.String
HEADER_JADEX_VERSION
Http header for the Jadex version.protected jadex.platform.service.serialization.serializers.JadexJsonSerializer
jsonser
The json processor.protected boolean
loginsec
Login security of or off.static java.util.List<java.lang.String>
PARAMETER_MEDIATYPES
Some basic media types for service invocations.protected java.util.List<AbstractRestPublishService.SSEEvent>
sseevents
SSE events that could not directly be sent.-
Fields inherited from interface jadex.bridge.service.types.publish.IPublishService
DEFAULT_RSPUBLISH_COMPONENTS, PUBLISH_RS, PUBLISH_WS
-
-
Constructor Summary
Constructors Constructor Description AbstractRestPublishService()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addConverter(java.lang.String[] mediatypes, IObjectStringConverter converter)
Add a converter for one or multiple types.protected static void
addEntry(java.util.Map<java.lang.String,java.lang.Object> ret, java.lang.String key, java.lang.Object val)
protected void
complete(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
static java.lang.Object
convertJsonValue(java.lang.String val, java.lang.Class<?> type, java.lang.ClassLoader cl, boolean tomap)
Convert a json string to a java object.java.lang.Object
convertParameter(java.lang.Object val, java.lang.Class<?> target)
Convert a (string) parameterprotected java.lang.Object
convertParameter(java.util.List<java.lang.String> sr, java.lang.String val, java.lang.Class<?> targetclazz)
Convert a parameter string to an object if is json or xml.protected AbstractRestPublishService.SSEEvent
createSSEEvent(AbstractRestPublishService.ResponseInfo ri)
Create an sse event.protected AbstractRestPublishService.SSEEvent
createSSEEvent(java.lang.Object result, boolean finished, java.lang.String callid, java.lang.Integer max, java.lang.String exceptiontype)
Create an sse event.protected java.lang.String
createSSEJson(AbstractRestPublishService.SSEEvent event)
IFuture<PathManager<AbstractRestPublishService.MappingInfo>>
evaluateMapping(IServiceIdentifier sid, PublishInfo pi)
Evaluate the service interface and generate mappings.java.util.Map<java.lang.String,java.lang.String>
extractCallerValues(java.lang.Object request)
Extract caller values like ip and browser.static java.lang.Object[]
generateInParameters(java.util.Map<java.lang.String,java.lang.Object> inparamsmap, Tuple2<java.util.List<Tuple2<java.lang.String,java.lang.String>>,java.util.Map<java.lang.String,java.lang.Class<?>>> pinfos, java.lang.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 asyncjava.lang.String
getCleanPublishId(java.lang.String id)
Get the cleaned publish id.abstract java.lang.Object
getHttpServer(java.net.URI uri, PublishInfo info)
Get or start an api to the http server.Tuple2<java.util.List<Tuple2<java.lang.String,java.lang.String>>,java.util.Map<java.lang.String,java.lang.Class<?>>>
getParameterInfos(java.lang.reflect.Method method)
Get metainfo about parameters from the target method via annotations.static long
getRequestTimeout(jakarta.servlet.http.HttpServletRequest request)
Get the request timeout.java.lang.String
getServiceInfo(java.lang.Object service, java.lang.String baseuri, PathManager<AbstractRestPublishService.MappingInfo> mappings)
Functionality blueprint for get service info web method.static java.lang.String
getServletHost(jakarta.servlet.http.HttpServletRequest req)
Get the servlet base url.static java.lang.String
getServletUrl(jakarta.servlet.http.HttpServletRequest req)
Get the servlet base url.AbstractRestPublishService.MappingInfo.HttpMethod
guessRestType(java.lang.reflect.Method method)
Guess the http type (GET, POST, PUT, DELETE, ...) of a method.void
handleRequest(IService service, PathManager<AbstractRestPublishService.MappingInfo> pm, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, java.lang.Object[] others)
Handle a web request.boolean
hasStringConvertableParameters(java.lang.reflect.Method method, java.lang.Class<?> rettype, java.lang.Class<?>[] paramtypes)
Test if a method has parameters that are all convertible from string.IFuture<java.lang.Void>
init()
The service init.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.IFuture<java.lang.Boolean>
isLoggedIn(java.lang.String callid)
Test if a the web user is logged in.IFuture<java.lang.Boolean>
isSupported(java.lang.String publishtype)
Test if publishing a specific type is supported (e.g.java.lang.String
loadFunctionJS()
IFuture<byte[]>
loadJadexJS()
Load jadex.jsjava.lang.String
loadStyleCSS()
IFuture<java.lang.Boolean>
login(jakarta.servlet.http.HttpServletRequest request, java.lang.String secret)
Log in to the platform.IFuture<java.lang.Boolean>
logout(jakarta.servlet.http.HttpServletRequest request)
Logout from the platform.static void
main(java.lang.String[] args)
protected Tuple2<AbstractRestPublishService.MappingInfo,java.lang.Object[]>
mapParameters(jakarta.servlet.http.HttpServletRequest request, java.util.Collection<AbstractRestPublishService.MappingInfo> mis, java.util.List<java.util.Map<java.lang.String,java.lang.String>> bindings)
Map the incoming uri/post/multipart parameters to the service target parameter types.protected java.lang.Object
mapResult(java.lang.reflect.Method method, java.lang.Object ret)
Map a result using the result mapper.static java.util.List<java.lang.String>
parseMimetypes(java.lang.String mts)
todo: make statically accessible Copied from Jadex ForwardFilterabstract IFuture<java.lang.Void>
publishService(IServiceIdentifier serviceid, PublishInfo info)
Publish a service.void
removeConverter(java.lang.String[] mediatypes, IObjectStringConverter converter)
Remove a converter.protected void
sendDelayedSSEEvents(jakarta.servlet.http.HttpSession session)
IFuture<java.lang.Void>
setLoginSecurity(boolean sec)
Turn on or off the login security.static java.util.Map<java.lang.String,java.lang.Object>
splitQueryString(java.lang.String query)
Split the query and save the order.protected void
writeResponse(AbstractRestPublishService.ResponseInfo ri)
Write the response (header and content).protected void
writeResponseContent(AbstractRestPublishService.ResponseInfo ri)
Write the response content.protected java.util.List<java.lang.String>
writeResponseHeader(AbstractRestPublishService.ResponseInfo ri)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jadex.bridge.service.types.publish.IPublishService
unpublishService
-
Methods inherited from interface jadex.bridge.service.types.publish.IWebPublishService
publishHMTLPage, publishResources
-
-
-
-
Field Detail
-
ASYNC_CONTEXT_INFO
public static final java.lang.String ASYNC_CONTEXT_INFO
Async context info.- See Also:
- Constant Field Values
-
HEADER_JADEX_VERSION
public static final java.lang.String HEADER_JADEX_VERSION
Http header for the Jadex version.- See Also:
- Constant Field Values
-
HEADER_JADEX_CALLID
public static final java.lang.String HEADER_JADEX_CALLID
Http header for the call id (req and resp).- See Also:
- Constant Field Values
-
HEADER_JADEX_CALLFINISHED
public static final java.lang.String HEADER_JADEX_CALLFINISHED
Http header for the call id siganlling that this is the last response (resp).- See Also:
- Constant Field Values
-
HEADER_JADEX_MAX
public static final java.lang.String HEADER_JADEX_MAX
Http header for max value of intermediate future.- See Also:
- Constant Field Values
-
HEADER_JADEX_CLIENTTIMEOUT
public static final java.lang.String HEADER_JADEX_CLIENTTIMEOUT
Http header for the client side timeout of calls (req).- See Also:
- Constant Field Values
-
HEADER_JADEX_TERMINATE
public static final java.lang.String HEADER_JADEX_TERMINATE
Http header to terminate the call (req).- See Also:
- Constant Field Values
-
HEADER_JADEX_LOGIN
public static final java.lang.String HEADER_JADEX_LOGIN
Http header to login to the platform and gain admin access (req).- See Also:
- Constant Field Values
-
HEADER_JADEX_LOGOUT
public static final java.lang.String HEADER_JADEX_LOGOUT
- See Also:
- Constant Field Values
-
HEADER_JADEX_ISLOGGEDIN
public static final java.lang.String HEADER_JADEX_ISLOGGEDIN
- See Also:
- Constant Field Values
-
FINISHED
public static final java.lang.String FINISHED
Finished result marker.- See Also:
- Constant Field Values
-
PARAMETER_MEDIATYPES
public static java.util.List<java.lang.String> PARAMETER_MEDIATYPES
Some basic media types for service invocations.
-
component
protected IInternalAccess component
The component.
-
conversationinfos
protected java.util.Map<java.lang.String,AbstractRestPublishService.ConversationInfo> conversationinfos
Info about an ongoing conversation, i.e. Jadex future, session etc.
-
sseevents
protected java.util.List<AbstractRestPublishService.SSEEvent> sseevents
SSE events that could not directly be sent.
-
converters
protected MultiCollection<java.lang.String,IObjectStringConverter> converters
The media type converters.
-
loginsec
protected boolean loginsec
Login security of or off.
-
jsonser
protected jadex.platform.service.serialization.serializers.JadexJsonSerializer jsonser
The json processor.
-
binser
protected jadex.platform.service.serialization.serializers.JadexBinarySerializer binser
The binary processor.
-
-
Method Detail
-
init
public IFuture<java.lang.Void> init()
The service init.
-
setLoginSecurity
public IFuture<java.lang.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.- Specified by:
setLoginSecurity
in interfaceIWebPublishService
- Parameters:
sec
- On or off.
-
addConverter
public void addConverter(java.lang.String[] mediatypes, IObjectStringConverter converter)
Add a converter for one or multiple types.
-
removeConverter
public void removeConverter(java.lang.String[] mediatypes, IObjectStringConverter converter)
Remove a converter.- Parameters:
converter
- The converter.
-
isSupported
public IFuture<java.lang.Boolean> isSupported(java.lang.String publishtype)
Test if publishing a specific type is supported (e.g. web service).- Specified by:
isSupported
in interfaceIPublishService
- Parameters:
publishtype
- The type to test.- Returns:
- True, if can be published.
-
handleRequest
public void handleRequest(IService service, PathManager<AbstractRestPublishService.MappingInfo> pm, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, java.lang.Object[] others) throws java.io.IOException, jakarta.servlet.ServletException
Handle a web request.- Parameters:
service
- The service.mappings
- The collected mapping infos for the service.request
- The request.response
- The response.- Throws:
java.io.IOException
jakarta.servlet.ServletException
-
sendDelayedSSEEvents
protected void sendDelayedSSEEvents(jakarta.servlet.http.HttpSession session)
-
publishService
public abstract IFuture<java.lang.Void> publishService(IServiceIdentifier serviceid, PublishInfo info)
Publish a service.- Specified by:
publishService
in interfaceIPublishService
- Parameters:
cl
- The classloader.service
- The original service.pid
- The publish id (e.g. url or name).
-
getHttpServer
public abstract java.lang.Object getHttpServer(java.net.URI uri, PublishInfo info)
Get or start an api to the http server.
-
login
public IFuture<java.lang.Boolean> login(jakarta.servlet.http.HttpServletRequest request, java.lang.String secret)
Log in to the platform.- Parameters:
request
- The request.secret
- The platform secret.- Returns:
- True, if login was successful.
-
logout
public IFuture<java.lang.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 IFuture<java.lang.Boolean> isLoggedIn(java.lang.String callid)
Test if a the web user is logged in.- Specified by:
isLoggedIn
in interfaceIWebPublishService
- 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
-
mapParameters
protected Tuple2<AbstractRestPublishService.MappingInfo,java.lang.Object[]> mapParameters(jakarta.servlet.http.HttpServletRequest request, java.util.Collection<AbstractRestPublishService.MappingInfo> mis, java.util.List<java.util.Map<java.lang.String,java.lang.String>> bindings)
Map the incoming uri/post/multipart parameters to the service target parameter types.
-
generateInParameters
public static java.lang.Object[] generateInParameters(java.util.Map<java.lang.String,java.lang.Object> inparamsmap, Tuple2<java.util.List<Tuple2<java.lang.String,java.lang.String>>,java.util.Map<java.lang.String,java.lang.Class<?>>> pinfos, java.lang.Class<?>[] types)
Generate in parameters that are correct wrt order and number of targetparameter (must convert types possibly).
-
convertJsonValue
public static java.lang.Object convertJsonValue(java.lang.String val, java.lang.Class<?> type, java.lang.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 java.lang.Object convertParameter(java.lang.Object val, java.lang.Class<?> target)
Convert a (string) parameter- Parameters:
val
-target
-- Returns:
-
convertParameter
protected java.lang.Object convertParameter(java.util.List<java.lang.String> sr, java.lang.String val, java.lang.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 java.lang.Object mapResult(java.lang.reflect.Method method, java.lang.Object ret)
Map a result using the result mapper.
-
writeResponse
protected void writeResponse(AbstractRestPublishService.ResponseInfo ri)
Write the response (header and content).
-
writeResponseHeader
protected java.util.List<java.lang.String> writeResponseHeader(AbstractRestPublishService.ResponseInfo ri)
-
writeResponseContent
protected void writeResponseContent(AbstractRestPublishService.ResponseInfo ri)
Write the response content.
-
createSSEEvent
protected AbstractRestPublishService.SSEEvent createSSEEvent(AbstractRestPublishService.ResponseInfo ri)
Create an sse event.- Parameters:
result
-finished
-callid
-max
-- Returns:
-
createSSEEvent
protected AbstractRestPublishService.SSEEvent createSSEEvent(java.lang.Object result, boolean finished, java.lang.String callid, java.lang.Integer max, java.lang.String exceptiontype)
Create an sse event.
-
createSSEJson
protected java.lang.String createSSEJson(AbstractRestPublishService.SSEEvent event)
-
getRequestTimeout
public static long getRequestTimeout(jakarta.servlet.http.HttpServletRequest request)
Get the request timeout.
-
parseMimetypes
public static java.util.List<java.lang.String> parseMimetypes(java.lang.String mts)
todo: make statically accessible Copied from Jadex ForwardFilter
-
splitQueryString
public static java.util.Map<java.lang.String,java.lang.Object> splitQueryString(java.lang.String query) throws java.lang.Exception
Split the query and save the order.- Throws:
java.lang.Exception
-
main
public static void main(java.lang.String[] args) throws java.lang.Exception
- Throws:
java.lang.Exception
-
addEntry
protected static void addEntry(java.util.Map<java.lang.String,java.lang.Object> ret, java.lang.String key, java.lang.Object val)
- Parameters:
ret
-key
-val
-
-
evaluateMapping
public IFuture<PathManager<AbstractRestPublishService.MappingInfo>> evaluateMapping(IServiceIdentifier sid, PublishInfo pi)
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).
-
getServletUrl
public static java.lang.String getServletUrl(jakarta.servlet.http.HttpServletRequest req)
Get the servlet base url.- Parameters:
req
- The request.- Returns:
- The servlet base url.
-
getServletHost
public static java.lang.String getServletHost(jakarta.servlet.http.HttpServletRequest req)
Get the servlet base url.- Parameters:
req
- The request.- Returns:
- The servlet base url.
-
getServiceInfo
public java.lang.String getServiceInfo(java.lang.Object service, java.lang.String baseuri, PathManager<AbstractRestPublishService.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 java.lang.String loadFunctionJS()
-
loadStyleCSS
public java.lang.String loadStyleCSS()
-
guessRestType
public AbstractRestPublishService.MappingInfo.HttpMethod guessRestType(java.lang.reflect.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(java.lang.reflect.Method method, java.lang.Class<?> rettype, java.lang.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 IFuture<byte[]> loadJadexJS()
Load jadex.js- Returns:
- The text from the file.
-
getParameterInfos
public Tuple2<java.util.List<Tuple2<java.lang.String,java.lang.String>>,java.util.Map<java.lang.String,java.lang.Class<?>>> getParameterInfos(java.lang.reflect.Method method)
Get metainfo about parameters from the target method via annotations.
-
extractCallerValues
public java.util.Map<java.lang.String,java.lang.String> extractCallerValues(java.lang.Object request)
Extract caller values like ip and browser.- Parameters:
request
- The requrest.vals
- The values.
-
getCleanPublishId
public java.lang.String getCleanPublishId(java.lang.String id)
Get the cleaned publish id. Square brackets for the optional host and context part are removed.
-
-