Grails - Access request object in domain class - grails

Is there a possibility to access the properties of the request object within a method of a domain class?
Thus, I would like to access request.getRemoteAddr() inside beforeInsert() and beforeUpdate() of my domain base class in order to save the IP-Address automatically and would not have to code it within every controller.
Thank you in advance.

You shouldn't access the request or session in your domain class directly, as you can never know, in which context the domain object gets saved/updated. This is by design.
If the situation is really desperate, you can use a workaround:
import org.springframework.web.context.request.RequestContextHolder
def request = RequestContextHolder.currentRequestAttributes()

Related

How to Store Object into Application Level Context in Grails

I want to store HashMap Object likeHashMap<String,MyClass> contextHashMap = new HashMap<String,MyClass> (); which will be accessible through out the application like as we store the object/variables in ApplicationContext of Struts.
So that I can Change or read the Data from this Variable whenever I need.
It isn’t really clear what you need but one option would be to store the data in a singleton service and inject that service wherever it is needed. That is a super simple solution.
You can also create a simple singleton Spring-bean of type Map (ConcurrentHashMap) and also inject into any Grails artefact:
resources.groovy:
beans = {
contextHashMap( ConcurrentHashMap )
}
and inject:
class ExampleController {
def contextHashMap
…
}

How to access session variables from a Draper decorator in Rails?

I want to be able to access a session variable from the Decorator. Now I can't do so, nor can I access controller instance variables, let's say #session_variable.
Is there a clean way to achieve this?
Thanks!
When I need any object other than a controller to have access to request information, I like to use what I think of as the Context pattern. It looks like this:
Write a singleton class that has only the interface that your decorator needs (following the Interface Segregation Principle). As an example, let's say that your decorator needs to know whether the user is logged in. Make a LoginContext singleton with an instance method user_is_logged_in?. The decorator can find out whether a the user is logged in by calling LoginContext.instance.user_is_logged_in?.
Add an before_filter to your ApplicationController that sets the singleton's user_is_logged_in attribute to true or false according to the session before running the action. Optionally, if you want to make sure that nothing uses the LoginContext outside of a request, make the filter an around_filter and set the attribute to nil after running the action, and write the user_is_logged_in? accessor so that it raises an error if the attribute is nil.
The above is for single-threaded Rails application servers, but you can write the same functionality around a thread-specific singleton if you use a threaded application server.

Where to put object-cache in Grails?

I want to use some caching from the Guava-library in my Grails app. Is a service class with a non-static field and some getter the best place to put this cache into? Or should it be static or declared somewhere else?
Basic Example:
class TestService {
def getCachedValue(Test test) {
return testCache.get(test)
}
def testCache = new CacheBuilder()
.maximumSize(2000)
.weakKeys()
.weakValues()
.expireAfterWrite(30, TimeUnit.SECONDS)
.build(
new CacheLoader<Test, Date>() {
...
Using a service is the best idea for this. However, making it static is a bit unnecessary since by default services are singletons.
The fact a service is a singleton, and is exposed to not only your controllers but other artifacts within your Grails application make it a perfect fit for accessing an object cache. A single point of access.

Why use a ShoppingCart as a Stateful Sesson Bean?

One typical example of using a Stateful Session Bean is through a ShoppingCart example. We create a bean instance of the ShoppingCart class, then store this instance within a HttpSession.
However, the same can be achieved easily with the ShoppingCart class being a normal Java class (or a stateless session bean). A request of adding a product comes in, we create a cart object, then put that cart object inside a HttpSession.
So, I don't see the point of using a stateful session bean ShoppingCart here. And in general, a stateful session bean doesn't seem to play any significant roles.
I would rephrase your question:
Why should I use mechanism for automatic session based bean management (maintaining one instance per session, persisting inside session) if I can implement it myself?
Yes, you can implement it yourself and it would be pretty simple. However you can just use Java EE mechanisms for that.

Grails singleton scoped controller and request object

I understand that grails 2 now supports singleton scoped controllers. I have a requirement to use such a controller and I have a question about the request object (and other implicit objects)
The grails documentation always refers to the request object as an instance variable but in a singleton scoped controller it would not be safe to use such an object. What is the recommended way to handle the implicit objects in a singleton scoped controller? I tried modifying the controller's action signature to accept the request object as a parameter (similar to standard spring MVC) and this appears to work:
class MyController {
static scope = "singleton"
def list(request) {
// do something
}
}
However I've also noticed that this.request still exists, so how can I be sure that the parameter request is not just a reference to this.request?
In either case it would not matter. The request object is bound using a ThreadLocal variable, so it would in all cases be safe to access concurrently.

Resources