Using java Aspectj/Annotation to log http request in Spring Boot - rest

I have created an Aspectj that is triggered when methods are annotated with #AuditLog.
The purpose is to log all the request parameters and body. The issue that I'm having is that inside the Aspectj the InputStream has already been consumed, probably by spring when creating the #RequestBody parameters in the rest method.
I don't want to use a filter with a wrapper since I want the least invasive modification to the actual code and also I only need to log certain methods.
Can this be done inside the Aspectj?
Edit. More Info added.
I wanted to do this in The AspectJ because I don't need to log all the methods, I also need to read information from the properties file and added to the log message. This log has to be saved to a queue for later processing.
A could use a wrapper inside the AspectJ but the InsputStream has already been consumed.

Related

Make #RefreshScope default

If I annotate a bean as #RefreshScope, I can get a new instance of it after its configuration changes (eg. by triggering the refresh by calling /refresh).
But this is exactly I'd like for each of my beans: why would I change a configuration file and then expect the configuration to take effect for some part of the application immediately and for some part only after restart?
So the question is whether it's possible to apply it as a default scope?
Also in a typical Spring Boot application a lot gets auto-configured (eg. datasource), and without a default scope, I'd have to build the beans myself and annotate them properly. (edit: #ConfigurationProperties are automatically refreshed, and since a Spring Boot Datasource auto configuration is based on that, it is refreshed indeed without #RefreshScope)
What am I missing here?
https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_environment_changes and https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_refresh_scope has all the answers.
#ConfigurationProperties are automatically refreshed when /refresh is called, so beans using these properties get the fresh values, for the others and #Value there's #RefreshScope.
I don't think making #RefreshScope default is possible.
The beans annotated with #RefreshScope don't get automatically refreshed after a configuration is changed. It gets refreshed only after the cache entry is invalidated.
From the docs:
Refresh scope beans are lazy proxies that initialize when they are used (i.e., when a method is called), and the scope acts as a cache of initialized values. To force a bean to re-initialize on the next method call, you just need to invalidate its cache entry.
One way to invalidate the cache is by using the /refresh endpoint.
It's valid to note that a refresh-scoped bean can lead to unexpected behavior, please refer to the docs mentioned above to understand why this is not the default behavior.

How can I get a list of eventprocessor in axon 3.1.1

I am using Axon 3.1.1 and wanted to know,
How can I get a list of eventprocessor in my configuration file,
I went through the springAmQPmessageSource file but still not sure how to exactly do it.
So that I can pass my event to appropriate eventhandler on Query side.
List<Consumer<List<? extends EventMessage<?>>>> eventProcessors = new CopyOnWriteArrayList<>();
Updated
I was retrieving message from kafka topic and wanted to wire them to specific eventhandler but since I am not able to get evenprocessors, I am not able to do that.
Can you please tell me how to do it, if I am using Axon 3.0.5
If you're using the SpringAmqpMessageSource, you will not need to retrieve the list of eventProcessors you've shared, as Axon will automatically subscribe all the event handling components to it for you.
Subsequently, the events the Message Source receives will automatically be pushed to all the listeners in your query side.
As this is all covered as Axon infrastructure under the hood, there is no one-off way to pull them out of it for your own use (other than potentially wiring them yourself).
Hence, you shouldn't have to do this yourself.
But, maybe I'm missing an obvious point here.
Could you elaborate a little more why you need the list of handlers in the first place?

Hit REST end point on startup - weblogic + ATG

I have a rest endpoint which would start the scheduler of loading a XML to memory. Whenever I hit that rest endpoint, it loads the XML in memory and would return the XML after its ready (would take 10 - 15 seconds). When the same endpoint is accessed again, it would return the cached XML. Everything works fine but for now I have to manually hit the endpoint for the scheduler to start. Is there a way to hit the endpoint automatically via a simple code in startup? Or is there any other solution for this?
Normally, a component in the Nucleus is instantiated at first access, not at system start-up.
The way to have anything done at start-up in ATG is to create your component, and then to add its nucleus path to the list of initial services in the /Initial component (or from one of the many other Initial components changed off of it)
The component should be globally scoped. Because /Initial is instantiated at start-up, the services it references will also be instantiated as dependencies.
If your component is a POJO, then the no argument constructor will be invoked on component start-up, then the setX method will be called for each property with a value defined in its properties file.
If your component is extended from Generic Service, then additionally, beforeSet and afterSet methods will be called, before and after the set methods are invoked, if they exist, and finally doStartUp will be called.
This is all part of the fundamental lifecycle of components that the Nucleus manages.
This gives you a number of hooks with which to invoke your custom code.
Now, in your question, you ask how to call a REST endpoint at start-up. However, I believe what you actually want to ask is how to ensure that a particular piece of code gets executed at system start-up. A REST endpoint is how you are triggering it today, manually, from outside the Nucleus. But that does not mean that it must call a REST end point if it is to be automatically called at start up.
The easiest way to achieve what you want is
define a class that extends GenericService
override the doStartUp method
put the code you want to execute in this method, or invoke the code on another component from here
define a globally scoped component for the class
Add the component to the initialServices property of the Initial component
Restart the server and check that your code is being called at start-up. Put some debug statements in, and switch debug logging on in your layer.
Note, you may actually also want to think about whether you really need to invoke your code at system start-up. Anything in initial services adds to the start time of the server. Depending on your requirements, it may be better to do it on first access of your application service rather than at server start-up.

Request Filter Attribute not executing on ServiceStack

I'm running ServiceStack version 4.x and I've created a custom Request Filter Attribute (it inherits from RequestFilterAttribute).
I have some class methods using this custom attribute with ApplyTo parameter. Whenever I use normal HTTP calls the filter gets executed perfectly, the problem comes when one of my services calls internaly a method from another service, the filter then is not executed.
According to the documentation that can be found here:
Order of operations
For non HTTP calls, Request filters with priority >= 0 will be executed right before Action filters.
I have no idea why this is not working, maybe is a bug on the implementation or I misunderstood the documentation.
Any idea how to solve this or a workaround?
Note the non-HTTP Global Request Filters is linked to the Messaging Global Request and Response Filters docs which refer to the MQ Request Filters, i.e:
appHost.GlobalMessageRequestFilters
appHost.GlobalMessageResponseFilters
Only the appHost.PreRequestFilters are executed everywhere, i.e. for every Raw HTTP Handler, HTTP or MQ Service, etc.

In a Spring WS, how can I handle SaajSoapEnvelopeException and get a copy of the SOAP after an exception?

That is "After and only After" an Exception since I've been told to get it up front is a performance hit we don't want because that would be for each and every call rather than just for an Exception. Makes sense of course but I sure don't see how it can be done AFTER an Exception.
Use case goes like this: Some guys, perhaps bad guys, send us some bad SOAP and the dispatcher chucks out a SaajSoapEnvelopeException. How do I handle this gracefully?
So far what I have is an extension of MessageDispatcherServlet with an override of the doService() method. The web.xml file has been updated to show this Dispatcher for Spring's config. Within this override, surround the call to the super method with a try/catch and you catch the Exception but the problem here is that the stream for the HttpServletRequest is already closed, so you can't get the SOAP from here, AFIK.
Another problem is that I can't get a marshaller to wire in here. I have Java faults generated from our WSDL I would like to use but I think there is a problem with wiring marshallers in a non-endpoint class. Perhaps something about the namespace? I probably need to read up on how these work.
Bottum line: Is it possible to get the SOAP after an Exception or is it possible to predict that there will be an Exception so that I can grab it up front? Also, how can I get a fault marshaller into this Dispatcher or will I have to BS up a text version of the fault?
I'm fairly new to Web Services and what I know so far is mostly CXF. So far, I'm not much impressed with Spring WS. They have a long ways to go yet, IMHO. The fact that I can't get my WSDL from the service due to a known bug having to do with XSD references in WSDL not getting properly renamed to match the bean, is particularly annoying.
Have you tried a EndpointExceptionResolver?
For instance, I have used one to catch and translate authentication exceptions.
You'll probably need an EndpointInterceptor as well to wrap the exception resolver.