Class RESTWorkItemHandler

  • All Implemented Interfaces:
    org.kie.api.runtime.process.WorkItemHandler, org.kie.internal.runtime.Cacheable

    @Wid(widfile="RestDefinitions.wid",name="Rest",displayName="Rest",defaultHandler="mvel: new org.jbpm.process.workitem.rest.RESTWorkItemHandler()",documentation="jbpm-workitems-rest/index.html",category="jbpm-workitems-rest",icon="defaultresticon.png",parameters={@WidParameter(name="Url"),@WidParameter(name="Method"),@WidParameter(name="HandleResponseErrors"),@WidParameter(name="ResultClass"),@WidParameter(name="AcceptHeader"),@WidParameter(name="AcceptCharset"),@WidParameter(name="Headers"),@WidParameter(name="AuthType"),@WidParameter(name="ConnectTimeout"),@WidParameter(name="ReadTimeout"),@WidParameter(name="Content"),@WidParameter(name="ContentData"),@WidParameter(name="Username"),@WidParameter(name="Password"),@WidParameter(name="AuthUrl"),@WidParameter(name="ContentType"),@WidParameter(name="ContentTypeCharset"),@WidParameter(name="Cookie"),@WidParameter(name="CookiePath")},results=@WidResult(name="Result",runtimeType="java.lang.Object"),mavenDepends=@WidMavenDepends(group="org.jbpm",artifact="jbpm-workitems-rest",version="7.67.0.Final"),serviceInfo=@WidService(category="REST",description="Perform REST calls",keywords="rest,call",action=@WidAction(title="Perform a Rest call"),authinfo=@WidAuth(required=true,params={"username","password"},paramsdescription={"User Name","Password"})))
    public class RESTWorkItemHandler
    extends org.jbpm.process.workitem.core.AbstractLogOrThrowWorkItemHandler
    implements org.kie.internal.runtime.Cacheable
    WorkItemHandler that is capable of interacting with REST service. Supports both types of services secured (that requires authentication) and open (no authentication). Authentication methods currently supported:
    • BASIC
    • FORM BASED
    Authentication information can be given on handler initialization and can be overridden via work item parameters. All other configuration options must be given via work item parameters map:
    • Url - resource location to be invoked - mandatory
    • Method - HTTP method that will be executed - defaults to GET
    • ContentType - data type in case of sending data - mandatory for POST,PUT
    • ContentData - actual data to be sent - mandatory for POST,PUT
    • ConnectTimeout - connection time out - default to 60 seconds
    • ReadTimeout - read time out - default to 60 seconds
    • Username - user name for authentication - overrides one given on handler initialization)
    • Password - password for authentication - overrides one given on handler initialization)
    • AuthUrl - url that is handling authentication (usually j_security_check url)
    • HandleResponseErrors - optional parameter that instructs handler to throw errors in case of non successful response codes (other than 2XX)
    • ResultClass - fully qualified class name of the class that response should be transformed to, if not given string format will be returned
    • AcceptHeader - accept header value
    • Headers - additional HTTP Headers in format: header1=val1;header2=val2
    • Cookie - Cookie need to send while accessing REST API in format: cookieParam1=cookieParam1_Value;cookieParam2=cookieParam2_Value
    • CookiePath - Define path for which the cookie is valid - default set to /
    • Constructor Detail

      • RESTWorkItemHandler

        public RESTWorkItemHandler()
        Used when no authentication is required
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(boolean doCacheClient)
        Used when no authentication is required
        Parameters:
        doCacheClient - true/false setting for client cache
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute)
        Used when no authentication is required
        Parameters:
        doCacheClient - - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   boolean doCacheClient)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        doCacheClient - - true/false setting for client cache
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   String authUrl)
        Dedicated constructor when FORM BASED authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        authUrl - - authentication url to be used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   String authUrl,
                                   boolean doCacheClient)
        Dedicated constructor when FORM BASED authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        authUrl - - authentication url to be used
        doCacheClient - - true/false setting for client cache
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   String authUrl,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute)
        Dedicated constructor when FORM BASED authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        authUrl - - authentication url to be used
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(ClassLoader classLoader)
        Used when no authentication is required
        Parameters:
        classLoader - - given classloader
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(ClassLoader classLoader,
                                   boolean doCacheClient)
        Used when no authentication is required
        Parameters:
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(ClassLoader classLoader,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute)
        Used when no authentication is required
        Parameters:
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   ClassLoader classLoader)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        classLoader - - given classloader
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   ClassLoader classLoader,
                                   boolean doCacheClient)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   ClassLoader classLoader,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   String authUrl,
                                   ClassLoader classLoader)
        Dedicated constructor when FORM BASED authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        authUrl - - authentication url to be used
        classLoader - - given classloader
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   String authUrl,
                                   ClassLoader classLoader,
                                   boolean doCacheClient)
        Dedicated constructor when FORM BASED authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        authUrl - - authentication url to be used
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   String authUrl,
                                   ClassLoader classLoader,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute)
        Dedicated constructor when FORM BASED authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        authUrl - - authentication url to be used
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String handlingProcessId,
                                   String handlingStrategy,
                                   String username,
                                   String password)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
        username - - user name to be used for authentication
        password - - password to be used for authentication
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String handlingProcessId,
                                   String handlingStrategy,
                                   String username,
                                   String password,
                                   boolean doCacheClient)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
        username - - user name to be used for authentication
        password - - password to be used for authentication
        doCacheClient - - true/false setting for client cache
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String handlingProcessId,
                                   String handlingStrategy,
                                   String username,
                                   String password,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
        username - - user name to be used for authentication
        password - - password to be used for authentication
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(ClassLoader classLoader,
                                   String handlingProcessId,
                                   String handlingStrategy)
        Used when no authentication is required
        Parameters:
        classLoader - - given classloader
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(ClassLoader classLoader,
                                   boolean doCacheClient,
                                   String handlingProcessId,
                                   String handlingStrategy)
        Used when no authentication is required
        Parameters:
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(ClassLoader classLoader,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute,
                                   String handlingProcessId,
                                   String handlingStrategy)
        Used when no authentication is required
        Parameters:
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   ClassLoader classLoader,
                                   String handlingProcessId,
                                   String handlingStrategy)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        classLoader - - given classloader
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   ClassLoader classLoader,
                                   boolean doCacheClient,
                                   String handlingProcessId,
                                   String handlingStrategy)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
      • RESTWorkItemHandler

        public RESTWorkItemHandler​(String username,
                                   String password,
                                   ClassLoader classLoader,
                                   boolean doCacheClient,
                                   int totalPoolConnections,
                                   int maxPoolConnectionsPerRoute,
                                   String handlingProcessId,
                                   String handlingStrategy)
        Dedicated constructor when BASIC authentication method shall be used
        Parameters:
        username - - user name to be used for authentication
        password - - password to be used for authentication
        classLoader - - given classloader
        doCacheClient - - true/false setting for client cache
        totalPoolConnections - - max pool connections if caching client is used
        maxPoolConnectionsPerRoute - - max pool connections per route if caching client is used
        handlingProcessId - - process id to handle exception
        handlingStrategy - - strategy to be applied after handling exception process is completed
    • Method Detail

      • getAuthUrl

        public String getAuthUrl()
      • getDoCacheClient

        public boolean getDoCacheClient()
      • executeWorkItem

        public void executeWorkItem​(org.kie.api.runtime.process.WorkItem workItem,
                                    org.kie.api.runtime.process.WorkItemManager manager)
        Specified by:
        executeWorkItem in interface org.kie.api.runtime.process.WorkItemHandler
      • getParamAsInt

        protected Integer getParamAsInt​(Object param)
      • setCharset

        protected void setCharset​(org.apache.http.client.methods.RequestBuilder builder,
                                  String charset)
      • addAcceptHeader

        protected void addAcceptHeader​(org.apache.http.client.methods.RequestBuilder builder,
                                       String value)
      • setCharset

        protected void setCharset​(org.apache.http.client.methods.HttpRequestBase theMethod,
                                  String charset)
      • addAcceptHeader

        protected void addAcceptHeader​(org.apache.http.client.methods.HttpRequestBase theMethod,
                                       String value)
      • setBody

        protected void setBody​(org.apache.http.client.methods.RequestBuilder builder,
                               Map<String,​Object> params)
      • setBody

        protected void setBody​(org.apache.http.client.methods.HttpRequestBase theMethod,
                               Map<String,​Object> params)
      • transformRequest

        protected String transformRequest​(Object data,
                                          String contentType)
      • doRequestWithAuthorization

        protected org.apache.http.HttpResponse doRequestWithAuthorization​(org.apache.http.client.HttpClient httpclient,
                                                                          org.apache.http.client.methods.RequestBuilder requestBuilder,
                                                                          Map<String,​Object> params,
                                                                          RESTWorkItemHandler.AuthenticationType type)
        This method does the actual request, including the setup for authorization.

        It is not responsible for cleaning up after the last request that it does.

        It is responsible for cleaning up after all previous request, such as for form-based authentication, that happen.
        Parameters:
        httpclient - The HttpClient instance
        requestBuilder - The RequestBuilder instance
        params - The parameters that may be needed for authentication
        Returns:
        A HttpResponse instance from which we can extract the content
      • doRequestWithAuthorization

        protected org.apache.http.HttpResponse doRequestWithAuthorization​(org.apache.http.client.HttpClient httpclient,
                                                                          org.apache.http.client.methods.HttpRequestBase httpMethod,
                                                                          Map<String,​Object> params,
                                                                          RESTWorkItemHandler.AuthenticationType type)
      • abortWorkItem

        public void abortWorkItem​(org.kie.api.runtime.process.WorkItem workItem,
                                  org.kie.api.runtime.process.WorkItemManager manager)
        Specified by:
        abortWorkItem in interface org.kie.api.runtime.process.WorkItemHandler
      • getHttpClient

        protected org.apache.http.client.HttpClient getHttpClient​(Integer readTimeout,
                                                                  Integer connectTimeout,
                                                                  String url,
                                                                  String cookie,
                                                                  String cookiePath)
      • addCookie

        protected org.apache.http.impl.client.BasicCookieStore addCookie​(String url,
                                                                         String cookie,
                                                                         String cookiePath)
      • getNewPooledHttpClient

        protected org.apache.http.impl.client.CloseableHttpClient getNewPooledHttpClient​(Integer readTimeout,
                                                                                         Integer connectTimeout)
      • close

        public void close()
        Specified by:
        close in interface org.kie.internal.runtime.Cacheable