A library that allows you to define Mia-Platform custom services in Java easily.
This library aims to provide an easy way to integrate the Platform.
It is able to integrate either the CRUD and other REST services. The underlying library which enables the possibility to operate with external service is Retrofit, although the user (developer) wouldn't use this in his code.
This plugin is framework-independent. Every functionality is built using plain Java code.
Before getting your service proxy, you need to inject the appropriate InitServiceOptions
instance:
- Default
InitServiceOptions
(port: 3000, protocol: HTTP)InitServiceOptions initOptions = new InitServiceOptions();
- Custom
InitServiceOptions
: you can build an instance withInitServiceOptions.builder()
. For example, you can set custom headers (including Mia Platform headers):Map<String, String> customHeaders = ... // headers InitServiceOptions initOptions = InitServiceOptions.builder().headers(customHeaders).build();
To get a service proxy, you can use the following methods from class ServiceClientFactory
:
-
getDirectServiceProxy
, to directly communicate with a specific microserviceService serviceClient = ServiceClientFactory.getDirectServiceProxy("my-microservice", initOptions);
-
getServiceProxy
, to use microservice gateway:Service serviceClient = ServiceClientFactory.getServiceProxy(initOptions);
An instance of class PreDecoratorRequest
can be used as input to the decorator handler.
The utility functions exposed by the PreDecoratorRequest
instance can be used to access the original request:
getOriginalRequestMethod()
- returns the original request methodgetOriginalRequestPath()
- returns the path of the original requestgetOriginalRequestHeaders()
- returns the headers of the original requestgetOriginalRequestQuery()
- returns the querystring of the original requestgetOriginalRequestBody()
- returns the body of the original request
In addition to the methods described above, the PreDecoratorRequest
instance exposes an interface to modify the original request,
which will come forward to the target service. This interface is accessible using the instance method
changeOriginalRequest
which returns a builder for PreDecoratorRequestProxy
object with following methods:
setMethod(String newMethod)
- modify the method of the original requestsetPath(String newPath)
- modify the path of the original requestsetHeaders(Map<String, String> newHeaders)
- modify the headers of the original requestsetQuery (Map<String, String> newQuery)
- modify the querystring of the original requestsetBody(Serializable newBody)
- change the body of the original request
To leave the original request unchanged, you can instead use leaveOriginalRequestUnmodified
function.
Both the result of changeOriginalRequest
building operation and the one of leaveOriginalRequestUnmodified
call can be passed to static method
DecoratorResponseFactory.makePreDecoratorResponse(PreDecoratorRequest preDecoratorRequest)
.
This method returns an instance of DecoratorResponse
, which represents the response that should be returned.
To abort the decorator chain, you can obtain the related DecoratorResponse
instance by calling the method
DecoratorResponseFactory.abortChain(int finalStatusCode, Serializable finalBody, Map<String, String> finalHeaders)
.
An instance of class PostDecoratorRequest
can be used as input to the decorator handler.
The utility functions exposed by the PostDecoratorRequest
instance can be used to access both the original request and the original response:
getOriginalRequestMethod()
- returns the original request methodgetOriginalRequestPath()
- returns the path of the original requestgetOriginalRequestHeaders()
- returns the headers of the original requestgetOriginalRequestQuery()
- returns the querystring of the original requestgetOriginalRequestBody()
- returns the body of the original requestgetOriginalResponseBody()
- returns the body of the original responsegetOriginalResponseHeaders()
- returns the headers of the original responsegetOriginalResponseStatusCode()
- returns the status code of the original response
In addition to the methods described above, the PostDecoratorRequest
instance exposes an interface to modify the original response,
which will come forwarded to the original caller. This interface is accessible using the instance method
changeOriginalResponse
which returns a builder for PostDecoratorRequestProxy
object with following methods:
setHeaders(Map<String, String> newHeaders)
- modify the headers of the original responsesetBody(Serializable newBody)
- change the body of the original responsesetStatusCode(int statusCode)
- change the status code of the original response
To leave the original response unchanged, the leaveOriginalResponseUnmodified
function can be used instead.
Both the result of changeOriginalRequest
building operation and the one of leaveOriginalRequestUnmodified
call can be passed to static method
DecoratorResponseFactory.makePostDecoratorResponse(PostDecoratorRequest postDecoratorRequest)
.
This method returns an instance of DecoratorResponse
, which represents the response that should be returned.
To abort the decorator chain, you can obtain the related DecoratorResponse
instance by calling the method
DecoratorResponseFactory.abortChain(int finalStatusCode, Serializable finalBody, Map<String, String> finalHeaders)
.