Get the current Play session() in Java

Juan Gudiño

I Have this class:

package ds;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import play.mvc.Http;

public class MyRoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected String determineCurrentLookupKey() {
        return Http.Context.current().session().get("currentDB");
    }
}

But when I want to access into the Play session I have this error:

Caused by: java.lang.RuntimeException: There is no HTTP Context available from here.
at play.mvc.Http$Context.current(Http.java:34) ~[play_2.10-2.3.10.jar:2.3.10]
at play.mvc.Controller.session(Controller.java:72) ~[play_2.10-2.3.10.jar:2.3.10]

I also tried with HttpExecution.defaultContext() and his HttpExecutionContext but it cannot be cast to the Http.Context that is what I need.

I was thinking about get the request header but certainly I don't know how to handle it from my class and determine the session from the request

rsan

You can't access the Play Context in that tier cause is out of scope. In the documentation of the AbstractRoutingDatasource:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.html

The main description of the class says:

Abstract DataSource implementation that routes getConnection() calls to one of various target DataSources based on a lookup key. The latter is usually (but not necessarily) determined through some thread-bound transaction context.

So this class is suggesting that a way to get information of the current context should be using a Thread-Bound Transaction Context.

Now, Is PlayFramework Thread Context Safe? Reading Play documentation:

https://www.playframework.com/documentation/2.3.x/ThreadPools#Java-thread-locals

Java code in Play uses a thread local to find out about contextual information such as the current HTTP request. Scala code doesn’t need to use thread locals because it can use implicit parameters to pass context instead. Threads locals are used in Java so that Java code can access contextual information without needing to pass context parameters everywhere.

So, if you are using a Java implementation, you can use ThreadLocals as context channel between components. Be careful if you create your own thread pools because there's a warning in the same documentation:

The default objects wrap the default user thread pool. If you want to do your own threading then you should use the HttpExecution class’s helper methods to get an ExecutionContextExecutor object yourself.

But that won't be a problem if you are not using custom thread pool in your app.

Said that, what you have to do is:

  1. Define a ThreadLocalContext for the object that you want to use as router.

  2. Put in the context. (You can do it in the controller, in the security controller if you are using an authorization framework like Deadbolt or even implementing a new request filter.)

  3. Reading in the AbstractRoutingDataSource the ThreadLocal context.

Important! Don't forget to clean the Thread-Local or you can face a memory Leak.

Step 1:

public class RequestContext {

   private static final ThreadLocal<String> contextHolder = 
            new ThreadLocal<String>();

   public static void setRoutingKey(String key) {
      contextHolder.set(key);
   }

   public static String getRoutingKey() {
      return (String) contextHolder.get();
   }

   public static void clearRoutingKey() {
      contextHolder.remove();
   }
}

Step 2:

//Demostrative code, not tested, not even compiled
public static void myController() {
   RoutingContext.setRoutingKey(Play.Context.request()); 
   return bla;
}

Step 3:

@Override
protected Object determineCurrentLookupKey() {
  String datasource = RoutingContext.getRoutingKey();
  RoutingContext.clearRoutingKey();
  return datasource;
}

Regards!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get the current Play session() in Java

From Dev

Get id of current session

From Dev

Play framework 2.2: How to get current controller and action in java

From Dev

Play framework 2.2: How to get current controller and action in java

From Dev

Get Current Session Value in JavaScript?

From Dev

Get session of current user on trigger

From Dev

How to get the current session's dateformat

From Dev

Get current Windows session ID in PowerShell

From Dev

Get first name from current session PHP

From Dev

Get current memory usage of a tmux session

From Dev

How to get the current Mongoid session or database

From Dev

Nette Framework - Get Current Session ID

From Dev

Get current language session when filling a survey

From Dev

Play Framework: How to get the current port number

From Dev

remain logged in for whole session play framework (java)

From Dev

Converting a Scala Session Object to a Java Session Object in Play Framework

From Dev

Java get current time in Israel

From Dev

Get date in current timezone in java

From Dev

Get current line number Java

From Dev

Get Current Day in Milliseconds In Java

From Dev

Get the current value of an ArraList - Java

From Dev

How to get the current session user in Jenkins on a Groovy parameter

From Dev

Get users current page when session times out

From Dev

How do I get the application to display in the current session

From Dev

Get current ssh session's originating IP without being superuser

From Dev

How to get the current library name from session object

From Dev

Is there any way to get information about current session from gv$session in oracle?

From Dev

How can I get the current session information from express-session?

From Dev

Java hibernate session.get inside constructor

Related Related

  1. 1

    Get the current Play session() in Java

  2. 2

    Get id of current session

  3. 3

    Play framework 2.2: How to get current controller and action in java

  4. 4

    Play framework 2.2: How to get current controller and action in java

  5. 5

    Get Current Session Value in JavaScript?

  6. 6

    Get session of current user on trigger

  7. 7

    How to get the current session's dateformat

  8. 8

    Get current Windows session ID in PowerShell

  9. 9

    Get first name from current session PHP

  10. 10

    Get current memory usage of a tmux session

  11. 11

    How to get the current Mongoid session or database

  12. 12

    Nette Framework - Get Current Session ID

  13. 13

    Get current language session when filling a survey

  14. 14

    Play Framework: How to get the current port number

  15. 15

    remain logged in for whole session play framework (java)

  16. 16

    Converting a Scala Session Object to a Java Session Object in Play Framework

  17. 17

    Java get current time in Israel

  18. 18

    Get date in current timezone in java

  19. 19

    Get current line number Java

  20. 20

    Get Current Day in Milliseconds In Java

  21. 21

    Get the current value of an ArraList - Java

  22. 22

    How to get the current session user in Jenkins on a Groovy parameter

  23. 23

    Get users current page when session times out

  24. 24

    How do I get the application to display in the current session

  25. 25

    Get current ssh session's originating IP without being superuser

  26. 26

    How to get the current library name from session object

  27. 27

    Is there any way to get information about current session from gv$session in oracle?

  28. 28

    How can I get the current session information from express-session?

  29. 29

    Java hibernate session.get inside constructor

HotTag

Archive