Retrieve redirected URL

0
Hi,   In an application we are currently developing anonymous users can access the login page via a number of URL's which all redirect to our application. We want to make the content of the page dependent on the redirected URL. Currently we retrieve this URL and set an attribute based on it through JavaScript. The problem we encountered is that this occurs after the actual rendering of the page. Is there some way to access this URL in a microflow instead so we can set attributes and conditional visibility based on these attributes before the page renders?
asked
2 answers
1

You can write a custom request handler that handles request on a certain path (for example http://example.mendixcloud.com/login/). Register the login request handler in the afterstartup microflow to make sure it is always available and then redirect users to your custom request handler on /login/.

In your request handler you can create an object which you can retrieve later on, for example by associating it with the current user.

Note that since this will probaly be the first request the user will make to mendix, you need to create an anonymous session for the user and return the appropriate cookies. Also make sure to redirect the user to the index.html page, otherwise Mendix won't be able to open any pages.

For an example of a request handler for this purpose:

public class HomePageRequestHandler extends RequestHandler {

    private static final String MICROFLOWACTIONNAME = "UserManagement.IVK_OpenLoginPage";
    public static final String PATH = "login/";
    private static final String	DEFAULT_INDEX_LOCATION	= "index.html";
    private static final String XAS_ID = "XASID";
    @Override
    protected void processRequest(IMxRuntimeRequest iMxRuntimeRequest, IMxRuntimeResponse iMxRuntimeResponse, String s) throws Exception {
        ISession session = getSessionFromRequest(iMxRuntimeRequest);
        // there might not be a session at this point, so manually create a session
        if(session == null)
        {
        	session = createGuestSession(iMxRuntimeResponse);
        }
        IContext context = session.createContext();

        // get url from request
        HttpServletRequest servletRequest = iMxRuntimeRequest.getHttpServletRequest();
        StringBuffer url = servletRequest.getRequestURL();

        //remove path to leave domain
        int i = url.indexOf(PATH);
        url.delete(i, i + url.length());

        IContext sysContext = Core.createSystemContext();

        // initiate url object
        URLObject urlObject = new URLObject(sysContext);
        urlObject.setURL(url.toString());
        urlObject.setURLObject_User(sysContext, User.initialize(context, session.getUser().getMendixObject()));
        Core.commit(sysContext, urlObject.getMendixObject());

        // return redirect to index.html
        iMxRuntimeResponse.setStatus(IMxRuntimeResponse.SEE_OTHER);
        iMxRuntimeResponse.addHeader("location", url.toString() + DEFAULT_INDEX_LOCATION);
    }
    
	private ISession createGuestSession(IMxRuntimeResponse response) throws Exception {
		if (!Core.getConfiguration().getEnableGuestLogin())
			throw new Exception("Guest login is not enabled");
		
		ISession session = Core.initializeGuestSession();
		setCookies(response, session);
		
		return session;
	}

	private void setCookies(IMxRuntimeResponse response, ISession session) {
		response.addCookie(Core.getConfiguration().getSessionIdCookieName(), session.getId().toString(),  "/", "", -1, true);
		response.addCookie(XAS_ID, "0."+Core.getXASId(),"/", "", -1, true);			 
	}
}

 

answered
0

You should probably upgrade to 6.7+ and create multiple login pages. You can then redirect to the appropriate page which you've defined the URL for and you can have full control from there. Here's a snippet of how the URL parameter works in pages:

 

URL

Added in Mendix 6.7.0

The URL of the page can be used to directly navigate to the page, e.g. from external links or bookmarks. It will be shown in the address bar of the browser when you visit the page. When navigating to a page without a URL configured, the last visited URL is shown. Note that the full URL of the page will be the base URL of your application followed by /p and then by the configured URL of the page, e.g. http://example.mendixcloud.com/p/home_page. Only pages that do not need an object can have a URL.

answered