How to use a custom requesthandler

1
I have been trying for several hours now, and I'm done with it. Documentation in very limited and in my opinion poor. I have been reading the article about the requesthandler, but I think this is not complete at all. I mean I miss information how to implement a custom request handler. Based on the forum threath I tried to implement something like this, but I have been struggling with error for many hours, so it is time to ask for help. I also read this blog, but although the text is clear I MISS information. My questions are, WHERE do I create the class file (which folder) and what is in it. I takes to much time to write all the error's down. In the documentation I cannot find anything about how to implement something like this. Maybe someone got the following information: Where to put the custom handler file What name has to be used. I got errors on naming the file has a different name then the class I use (filename = reloadSecurity, class = public class ReloadSecurityRequestHandler overwriting the requestHandler). How to call the request handler ( I currently have a MF which is called at startup. Code I user: / Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments. package myfirstmodule.actions; import com.mendix.core.Core; import com.mendix.externalinterface.connector.RequestHandler; import com.mendix.m2ee.api.IMxRuntimeRequest; import com.mendix.m2ee.api.IMxRuntimeResponse; import com.mendix.m2ee.log.ILogNode; import com.mendix.systemwideinterfaces.core.UserAction; import com.mendix.systemwideinterfaces.core.ISession; import com.mendix.systemwideinterfaces.core.IContext; import myfirstmodule.ReloadSecurityRequestHandler; /** * Bedoeld om te zorgen dat bij een autorisatieaanpassing de security reset om de goede menukeuzes te tonen */ public class InitSecurity extends UserAction<boolean> { public InitSecurity() { super(); } @Override public Boolean executeAction() throws Exception { // BEGIN USER CODE Core.addRequestHandler("reloadSecurity/", new ReloadSecurityRequestHandler()); return true; // END USER CODE } /** * Returns a string representation of this action */ @Override public String toString() { return "InitSecurity"; } // BEGIN EXTRA CODE // END EXTRA CODE } This code is called in the startup Requesthandler // BEGIN EXTRA CODE public class ReloadSecurityRequestHandler extends RequestHandler { @Override public void processRequest(IMxRuntimeRequest request, IMxRuntimeResponse response, String arg2) throws Exception { String curSession = request.getCookie(XAS_SESSION_ID); String userName = request.getParameter("userName"); ISession currentMxSession = Core.getActiveSession(userName); if(currentMxSession.getId().toString().equals(curSession)) { IContext systemContext = Core.createSystemContext(); ISession newSession = Core.initializeSession(systemContext, Core.getUser(systemContext, userName), curSession,"nl_NL"); response.addCookie(XAS_SESSION_ID, newSession.getId().toString(),"/" ,"" ,-1 ); response.addCookie(XAS_ID, "0." + Core.getXASId(),"/" ,"" ,-1); Core.logout(currentMxSession); } response.setStatus(IMxRuntimeResponse.SEE_OTHER); response.addHeader("location", "../index.html"); } private static final String XAS_ID = "XASID"; } saved in a file called "reloadSecurity.java" in de home folder of the project. So one folder higher than actions, dataset and proxies. When compiling I get: Buildfile: C:\Users\Stephan\Documents\My Projects\ProefProject\deployment\build.xml compile: [javac] Compiling 2 source files to C:\Users\Stephan\Documents\My Projects\ProefProject\deployment\model\lib\bin [javac] C:\Users\Stephan\Documents\My Projects\ProefProject\javasource\myfirstmodule\reloadSecurity.java:13: class ReloadSecurityRequestHandler is public, should be declared in a file named ReloadSecurityRequestHandler.java [javac] public class ReloadSecurityRequestHandler extends RequestHandler [javac] ^ [javac] C:\Users\Stephan\Documents\My Projects\ProefProject\javasource\myfirstmodule\actions\InitSecurity.java:20: cannot find symbol [javac] symbol : class ReloadSecurityRequestHandler [javac] location: package myfirstmodule [javac] import myfirstmodule.ReloadSecurityRequestHandler; [javac] ^ [javac] C:\Users\Stephan\Documents\My Projects\ProefProject\javasource\myfirstmodule\actions\InitSecurity.java:36: cannot find symbol [javac] symbol : class ReloadSecurityRequestHandler [javac] location: class myfirstmodule.actions.InitSecurity [javac] Core.addRequestHandler("reloadSecurity/", new ReloadSecurityRequestHandler()); [javac] ^ [javac] 3 errors BUILD FAILED C:\Users\Stephan\Documents\My Projects\ProefProject\deployment\build.xml:62: Compile failed; see the compiler error output for details. I have been unable to get to work. Any help appreciated.
asked
3 answers
1

Stephan,

You just create 1 java action from the modeler. In the user code section add the requesthandler code:

// BEGIN USER CODE
Core.addRequestHandler("reloadSecurity/", new ReloadSecurityRequestHandler());
return true;
// END USER CODE

then in the extra code section you add:

    public class ReloadSecurityRequestHandler extends RequestHandler { @Override public void processRequest(IMxRuntimeRequest request, IMxRuntimeResponse response, String arg2) throws Exception {

    String curSession = request.getCookie(XAS_SESSION_ID);
    System.out.println("current session: "+curSession);
    String userName = request.getParameter("userName");
    ISession currentMxSession = Core.getActiveSession(userName);

    if(currentMxSession.getId().toString().equals(curSession)){
        IContext systemContext = Core.createSystemContext();

        ISession newSession = Core.initializeSession(systemContext, Core.getUser(systemContext, userName), curSession,"nl_NL");
        response.addCookie(XAS_SESSION_ID, newSession.getId().toString(),"/" ,"" ,-1 );
        response.addCookie(XAS_ID, "0." + Core.getXASId(),"/" ,"" ,-1);

        Core.logout(currentMxSession);
    }

    response.setStatus(IMxRuntimeResponse.SEE_OTHER);
    response.addHeader("location", "../index.html");
}

private static final String XAS_ID = "XASID";
}

This java action you call in a microflow that you add as the after startup MF and the same code is called after changing the user role (this is in your second microflow).

answered
0

Hi Erwin, thanks for the reply. We already tried what you suggested, even followed the instructions use by Herbert in his post. However we still get a file not found error.

According the blog mentioned in my first post one should be able to call the registered handler like

http://localhost:8080/name-of-handler

If I try this in our app, still got the file-not-found error

http://localhost:8080:reloadSecurity?UserName={name-of-user}

So either we don't configure is right of miss some other info. Any thoughts?

answered
0

Hi Erwin,

just wanted to let you know that the problem was not the java-file but the redirect URL we use (according to Herberts post). This URL was http://localhost:8080:reloadSecurity?UserName={name-of-user} but should be http://localhost:8080:reloadSecurity/reloadSecurity?UserName={name-of-user} to get it to work.

So I guess we can say this issue has been solved. Thanks for the help.

answered