Class ChenileExchange

java.lang.Object
org.chenile.core.context.ChenileExchange
All Implemented Interfaces:
Serializable, WarningAware, ChainContextContainer<ChenileExchange>

public class ChenileExchange extends Object implements Serializable, ChainContextContainer<ChenileExchange>, WarningAware
A bidirectional exchange that navigates between different Chenile Interceptors. The exchange contains a body which is the incoming request along with headers.

Commands will set the response or exception in the return journey.

There can be transformers that convert the body to the type acceptable by the service.

Chenile end point modules such as chenile-http must be able to convert their end point specific incoming request (such as HttpServletRequest) into ChenileExchange

They must also be able to convert the returned response into their protocol specific response object (such as HttpServletResponse)

Author:
Raja Shankar Kolluru
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • headers

      private Map<String,Object> headers
      Incoming request headers. Some headers can be added as we navigate through the interception chain
    • multiPartMap

      private Map<String,MultipartFile> multiPartMap
      This is used to handle multipart messages from HTTP.
    • body

      private Object body
      The converted body of the incoming message. This will be set to the actual payload expected by the service. It may be a JSON string to begin with before the transformation framework converts it to the desired payload type
    • bodyType

      private com.fasterxml.jackson.core.type.TypeReference<?> bodyType
      bodyType is populated by the Chenile Transformation framework. The bodyType might vary depending on the URL of the incoming request and also some headers. A body type selector is a special Command<ChenileExchange> that determines the body type depending on the context of the request. (like headers, URL , locale etc.) All incoming JSONs (and other type of strings) are converted into the body type specified here
    • apiInvocation

      private List<Object> apiInvocation
      An object internally used by ServiceInvoker to invoke the API.
    • operationDefinition

      private OperationDefinition operationDefinition
      This is a very important metadata to handle the request. It contains the definition of the Service and the Operation within it. All operations are performed using these data structures. For example this metadata will be used to determine the service that needs to be invoked and the method within the service. This metadata is also used to determine the interceptors that are applicable etc. ChenileServiceDefinition and OperationDefinition are expected to be set by the protocol specific end point. Example: in chenile-http the URL called will be mapped to the ChenileServiceDefinition and OperationDefinition.
    • serviceDefinition

      private ChenileServiceDefinition serviceDefinition
    • logWriter

      private LogWriter logWriter
    • originalSourceReference

      private String originalSourceReference
      This (optional) field allows traceability to the original request. This is populated when the entry point is invoked in a batch mode from a file watcher or a reader from a queue or topic. In this situation, no response is emitted. Hence, we need to know how this request got created in the first place.
      Typical values can be:
    • file1:line1:ref ID -> to indicate that this message was constructed from the file in question etc.
    • mqtt:topic1:id -> to indicate that this messages was created by MQTT by reading the topic in question etc.
    • responseBodyType

      private ParameterizedTypeReference<?> responseBodyType
      used (by chenile-proxy) to convert the response JSON to a response body.
    • response

      private Object response
      The actual response returned by the service or any interceptor. (for example a caching interceptor might set the response and return)
    • exception

      private ErrorNumException exception
      The exception thrown by the service or any of the interceptors
    • httpResponseStatusCode

      private int httpResponseStatusCode
      contains the http response status code
    • responseMessages

      private List<ResponseMessage> responseMessages
      contains all errors and warnings
    • chainContext

      private ChainContext<ChenileExchange> chainContext
      Internal field for the purpose of continuing the interceptor chain. The interceptors can use this to pass control to the next in the line by calling the ChainContext.doContinue() method. Interceptors can also use the ChainContext.savePoint() to save the current point and using ChainContext.resumeFromSavedPoint(ChainContext.SavePoint) to continue the chain from the saved point. This is useful if the rest of the interceptor chain needs to be called multiple times as part of some retry logic.
    • localInvocation

      private boolean localInvocation
    • locale

      private Locale locale
      Calculated locale. This should be populated from the incoming request as per the headers passed to it.
    • invokeMock

      private boolean invokeMock
      This is useful if this request is determined to be a mock request. Hence a mock service may have to be invoked instead of the actual service. In case of HTTP a special header (x-chenile-mock) will be passed for mock requests.
    • serviceReference

      private Object serviceReference
      the service which will be invoked. This would be populated by the ConstructServiceReference interceptor and invoked by ServiceInvoker. By switching the serviceReference, any interceptor can change the target service that will be invoked. But please be sure to recompute the method by calling MethodUtils
    • method

      private Method method
      This field just uses a cached method object for optimization purposes. This needs to be recalculated if the serviceReference is changed by an interceptor.
    • serviceReferenceId

      private String serviceReferenceId
      the spring bean ID of the service being invoked.
  • Constructor Details

    • ChenileExchange

      public ChenileExchange()
    • ChenileExchange

      public ChenileExchange(ChenileExchange that)
  • Method Details

    • getOriginalSourceReference

      public String getOriginalSourceReference()
    • setOriginalSourceReference

      public void setOriginalSourceReference(String originalSourceReference)
    • getOperationDefinition

      public OperationDefinition getOperationDefinition()
    • setOperationDefinition

      public void setOperationDefinition(OperationDefinition operationDefinition)
    • getServiceDefinition

      public ChenileServiceDefinition getServiceDefinition()
    • setServiceDefinition

      public void setServiceDefinition(ChenileServiceDefinition serviceDefinition)
    • getHeaders

      public Map<String,Object> getHeaders()
    • getHeader

      public <T> T getHeader(String name, Object defaultValue, Class<T> type)
    • getHeader

      public Object getHeader(String name, Object defaultValue)
    • getHeader

      public <T> T getHeader(String name, Class<T> type)
    • getHeader

      public Object getHeader(String name)
    • setHeaders

      public void setHeaders(Map<String,Object> headers)
    • setHeader

      public void setHeader(String key, Object value)
    • getBody

      public Object getBody()
    • setBody

      public void setBody(Object body)
    • setResponse

      public void setResponse(Object response)
    • getResponse

      public Object getResponse()
    • getChainContext

      public ChainContext<ChenileExchange> getChainContext()
      Specified by:
      getChainContext in interface ChainContextContainer<ChenileExchange>
    • setChainContext

      public void setChainContext(ChainContext<ChenileExchange> chainContext)
      Specified by:
      setChainContext in interface ChainContextContainer<ChenileExchange>
    • getBodyType

      public com.fasterxml.jackson.core.type.TypeReference<?> getBodyType()
    • setBodyType

      public void setBodyType(com.fasterxml.jackson.core.type.TypeReference<?> bodyType)
    • getApiInvocation

      public List<Object> getApiInvocation()
    • setApiInvocation

      public void setApiInvocation(List<Object> apiInvocation)
    • setLocalInvocation

      public void setLocalInvocation(boolean b)
    • isLocalInvocation

      public boolean isLocalInvocation()
    • setException

      public void setException(Throwable e)
      Call this to set exception in the exchange. This method has side effects! It modifies the responseMessages in the exchange.
      If the exception is null then it removes all traces of errors so far! It empties the response messages in the exchange.
      If the exception is set for the first time, then all the errors in the exception get copied as response messages in the exchange.
      If the exception is already present, then the exception is not over-ridden. Instead, the response messages are added from this exception.
      Parameters:
      e - the exception that needs to be set in Chenile Exchange
    • createErrorNumExceptionIfRequired

      private ErrorNumException createErrorNumExceptionIfRequired(Throwable e)
    • getException

      public ErrorNumException getException()
    • getLocale

      public Locale getLocale()
    • setLocale

      public void setLocale(Locale locale)
    • isInvokeMock

      public boolean isInvokeMock()
    • setInvokeMock

      public void setInvokeMock(boolean invokeMock)
    • getServiceReference

      public Object getServiceReference()
    • setServiceReference

      public void setServiceReference(Object serviceReference)
    • getServiceReferenceId

      public String getServiceReferenceId()
    • setServiceReferenceId

      public void setServiceReferenceId(String serviceReferenceId)
    • setMultiPartMap

      public void setMultiPartMap(Map<String,MultipartFile> multiPartMap)
    • getMultiPartMap

      public Map<String,MultipartFile> getMultiPartMap()
    • getResponseBodyType

      public ParameterizedTypeReference<?> getResponseBodyType()
    • setResponseBodyType

      public void setResponseBodyType(ParameterizedTypeReference<?> responseBodyType)
    • getMethod

      public Method getMethod()
    • setMethod

      public void setMethod(Method method)
    • getHttpResponseStatusCode

      public int getHttpResponseStatusCode()
    • setHttpResponseStatusCode

      public void setHttpResponseStatusCode(int httpResponseStatusCode)
    • getResponseMessages

      public List<ResponseMessage> getResponseMessages()
    • setResponseMessages

      public void setResponseMessages(List<ResponseMessage> responseMessages)
    • getWarningMessages

      public List<ResponseMessage> getWarningMessages()
      Specified by:
      getWarningMessages in interface WarningAware
    • addWarningMessage

      public void addWarningMessage(ResponseMessage m)
      Specified by:
      addWarningMessage in interface WarningAware
    • removeAllWarnings

      public void removeAllWarnings()
      Specified by:
      removeAllWarnings in interface WarningAware
    • getExtensionByAnnotation

      public <T extends Annotation> T getExtensionByAnnotation(Class<T> klass)
    • getLogWriter

      public LogWriter getLogWriter()
    • setLogWriter

      public void setLogWriter(LogWriter logWriter)