Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Kowbathullah Gnaniyar
Redirect to Mobile Community
November 25, 2011 1:02 PM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts

Hi all,

I need to redirect all the users to new community if they access the portal from mobile devices. Here are my technical trials with different way.

LoginPreAction and DefaultLandingPage

I have tried with LoginPreAction and DefaultLandingPage action. But both are not helpful as My requirement is to redirect before the Login since both files are being called after login only.

StartupAction

I tried with StartUpAction . But we cant get the request object in this file. so cant go with this.

company.default.home.url

company.default.home.url will be called whenever user requests the portal all the time. But I need to filter it whether Browser / Mobile Device. So this will not helpful

ServicePreAction

So I have a only one choice is ServicePreAction. for that, I have developed ServicePreAction Hooks to redirect the request to New community whenever request comes from Mobile Devices. I have identified the Mobile Devices from user-agents and redirecting to New Community. But i got a error like " TOO MANY REDIRECTS" since it is being called 22 times each and every request.

Have anybody come across this issue? It would be great if you throw some lights on this.

Here is the code from ServicePreAction to redirect to new community:

 1protected void doRun(
 2                    HttpServletRequest request, HttpServletResponse response)
 3            throws Exception {
 4
 5           // logic to find out the Mobile device//
 6                       boolean mobileDevice = <blah blah blah >
 7          
 8               
 9           if(mobileDevice ) {
10                    response.sendRedirect("/web/mobile-community/home");
11           }
12    }


- Gnaniyar Zubair
Josef Šustáček
RE: Redirect to Mobile Community
November 26, 2011 9:06 AM
Answer

Josef Šustáček

Rank: New Member

Posts: 22

Join Date: October 5, 2009

Recent Posts

Hi,

I'm not sure, but aren't you missing some condition to not redirect the request when you already are requesting "/web/mobile-community/home"?

Because response.sendRedirect() triggers new HTTP request from browser to Liferay, which again gets through ServicePreAction, gets recognized as "mobile client" and gets redirect... and so on.

Simple change to something like:

1
2if(mobileDevice && ! "/web/mobile-community/home".equals(request.getPath())) {
3    response.sendRedirect("/web/mobile-community/home");
4}
Kowbathullah Gnaniyar
RE: Redirect to Mobile Community
November 27, 2011 10:33 AM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts

Josef Šustáček:
Hi,

I'm not sure, but aren't you missing some condition to not redirect the request when you already are requesting "/web/mobile-community/home"?

Because response.sendRedirect() triggers new HTTP request from browser to Liferay, which again gets through ServicePreAction, gets recognized as "mobile client" and gets redirect... and so on.

Simple change to something like:

1
2if(mobileDevice && ! "/web/mobile-community/home".equals(request.getPath())) {
3    response.sendRedirect("/web/mobile-community/home");
4}



Hi Josef,

Thanks for your reply. But i dont find any method like request.getPath(). u mean getPathInfo() . ?

just leave that mobile device redirection josef. Even if we give response.sendRedirect("/web/mobile-community/home") without any condition, it gives that same error. Without any condition if i try like this but OOPS same error.

 1public class CustomServicePreAction extends Action {
 2
 3    @Override
 4    public void run(HttpServletRequest request, HttpServletResponse response) {
 5        try {
 6            doRun(request, response);
 7        }
 8        catch (Exception e) {
 9            if (_log.isWarnEnabled()) {
10                _log.warn(e, e);
11            }
12        }
13    }
14
15    protected void doRun(
16            HttpServletRequest request, HttpServletResponse response)
17        throws Exception {
18   
19            response.sendRedirect("/web/mobile-community/home");
20       
21        }
22
23               private static Log _log = LogFactoryUtil.getLog(CustomServicePreAction.class);
24    }





- Gnaniyar Zubair
Kowbathullah Gnaniyar
RE: Redirect to Mobile Community
November 26, 2011 10:52 AM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts


Thanks Josef. your points helped me to fix this issue. Hope I have resolved that issue. Testing it now.

Will post entire code here once I finished the Unit Testing with Mobile devices.

Thanks

- Gnaniyar Zubair
Kowbathullah Gnaniyar
RE: Redirect to Mobile Community
November 27, 2011 10:34 AM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts

Thanks Josef. Successfully resolved this issue.

As I said, ServicePreAction has been called many times for each and every request . when we request the redirection URL, we should take care that redirection URL has not been called repeatedly whenever ServicePreAction loads.
So, we need to make sure that CurrentURL and Redirection URL should not be the same when request gets through ServicePreAction .

In this Example, I have redirected to "/web/mobile-community/home"

When requests goes into ServicePreAction, it checks the redirection URL (getRedirect(themeDisplay)) is not same like CurrentURL

1[b]!getRedirect(themeDisplay).equals(currentURL)[/b]


so, Request gets into ServicePreAction, It will not take that same URL again and again which will avoid the "TOO MANY REDIRECTION ERROR" and redirecting properly.

Here is the code:
 1[font=Verdana]
 2
 3          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY);
 4            
 5          String currentURL = PortalUtil.getCurrentURL(request);
 6
 7          String redirect = null;
 8          
 9          boolean isMobileDevice = //  Logic to identify the mobile device //
10         
11           if(((!getRedirect(themeDisplay).equals(currentURL)) && (isMobileDevice))) {
12            response.sendRedirect(getRedirectURL(themeDisplay));
13           }[/font]
Tanweer .
RE: Redirect to Mobile Community
November 27, 2011 10:13 AM
Answer

Tanweer .

Rank: Expert

Posts: 319

Join Date: March 11, 2010

Recent Posts

Gnaniyar Zubair:

Thanks Josef. your points helped me to fix this issue. Hope I have resolved that issue. Testing it now.

Will post entire code here once I finished the Unit Testing with Mobile devices.

Thanks

- Gnaniyar Zubair


Zubair Anna,

Can you please post your solution, so that it helps other member of the forum looking for the same issue.

Regards,
Tanweer
mPowerian-BoschLer
Kowbathullah Gnaniyar
RE: Redirect to Mobile Community
November 27, 2011 10:19 AM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts

Tanweer Ahmed Ansari:
Gnaniyar Zubair:

Thanks Josef. your points helped me to fix this issue. Hope I have resolved that issue. Testing it now.

Will post entire code here once I finished the Unit Testing with Mobile devices.

Thanks

- Gnaniyar Zubair


Zubair Anna,

Can you please post your solution, so that it helps other member of the forum looking for the same issue.

Regards,
Tanweer
mPowerian-BoschLer



Hi brother,

I have given my solution how i achieved that?

Are you looking for entire code?

- Gnaniyar Zubair
Josef Šustáček
RE: Redirect to Mobile Community
December 7, 2011 12:15 PM
Answer

Josef Šustáček

Rank: New Member

Posts: 22

Join Date: October 5, 2009

Recent Posts

Great, that's what I meant, I only wrote some meta-code, I didn't check the presence of
1request.getPath()
is Servlet specs :-)
abdul quddhus
RE: Redirect to Mobile Community
February 26, 2012 11:16 AM
Answer

abdul quddhus

Rank: New Member

Posts: 6

Join Date: January 19, 2010

Recent Posts

Gnaniyar Zubair:
Thanks Josef. Successfully resolved this issue.

As I said, ServicePreAction has been called many times for each and every request . when we request the redirection URL, we should take care that redirection URL has not been called repeatedly whenever ServicePreAction loads.
So, we need to make sure that CurrentURL and Redirection URL should not be the same when request gets through ServicePreAction .

In this Example, I have redirected to "/web/mobile-community/home"

When requests goes into ServicePreAction, it checks the redirection URL (getRedirect(themeDisplay)) is not same like CurrentURL

1[b]!getRedirect(themeDisplay).equals(currentURL)[/b]


so, Request gets into ServicePreAction, It will not take that same URL again and again which will avoid the "TOO MANY REDIRECTION ERROR" and redirecting properly.

Here is the code:
 1[font=Verdana]
 2
 3          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY);
 4            
 5          String currentURL = PortalUtil.getCurrentURL(request);
 6
 7          String redirect = null;
 8          
 9          boolean isMobileDevice = //  Logic to identify the mobile device //
10         
11           if(((!getRedirect(themeDisplay).equals(currentURL)) && (isMobileDevice))) {
12            response.sendRedirect(getRedirectURL(themeDisplay));
13           }[/font]


hello, what is the value you are checking in getRedirect() method?
Dhandapani S
RE: Redirect to Mobile Community
February 27, 2012 9:10 PM
Answer

Dhandapani S

Rank: Regular Member

Posts: 170

Join Date: March 24, 2009

Recent Posts

Zubair,

Can u plz share the code snippet .....

Regards
Dhans
Kowbathullah Gnaniyar
RE: Redirect to Mobile Community
March 29, 2012 2:44 PM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts

Dhandapani S:
Zubair,

Can u plz share the code snippet .....

Regards
Dhans



Hi Dhandapani,

I have done through VM file instead of hook hence i was getting again TOO MANY REDIRECT ERROR . Also I wanted to avoid ServicePreAction as it has been called repeatedly for each request. So, It would be better approach to write the User agent detection logic in Velocity file instead of Hook.


set this property in portal.ext.properties file

1default.mobile.community.page=<your.mobile.community.url>


Write this below code in init_custom.vm of your portal's Guest Theme :


 1#set ($isIphone = $request.getHeader("User-Agent").toLowerCase().indexOf("iphone") != -1)
 2#set ($isIpad = $request.getHeader("User-Agent").toLowerCase().indexOf("ipad") != -1)
 3#set ($isAndroid = $request.getHeader("User-Agent").toLowerCase().indexOf("android") != -1)
 4#set ($isMobile = $request.getHeader("User-Agent").toLowerCase().indexOf("mobile") != -1)
 5
 6#set ($isMobileDevice = ($isIphone||$isIpod ||$isIpad ||$isBlackBerry ||$isAndroid ||$isMobile))
 7
 8#set ($mobileCommunityURL = $propsUtil.get("default.mobile.community.page"))
 9
10#if ($isMobileDevice)
11<script type="text/javascript">
12    window.location.href = '$mobileCommunityURL';
13</script>
14#end


Also you can get the user agent identification through this API :

1boolean isIphone = BrowserSnifferUtil.isIphone(request);


We can write this redirection logic through javascript too. will share that script some other time as my eyes are redirecting to sleeping now. emoticon

HTH

- Gnaniyar Zubair
Patrizia D
RE: Redirect to Mobile Community
January 23, 2013 1:34 AM
Answer

Patrizia D

Rank: New Member

Posts: 6

Join Date: April 21, 2010

Recent Posts

Hey, how did you get the redirection URL (getRedirect (themeDisplay))? When I am in a mobile page (ex. web / guest / home-mobile) and I click logout, the mobile redirect rule never makes to the logout action and instead redirects me back to my app, still signed on. From a mobile I can not log out. Please can you help me?
Kowbathullah Gnaniyar
RE: Redirect to Mobile Community
January 23, 2013 3:46 AM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts

Patrizia D:
Hey, how did you get the redirection URL (getRedirect (themeDisplay))? When I am in a mobile page (ex. web / guest / home-mobile) and I click logout, the mobile redirect rule never makes to the logout action and instead redirects me back to my app, still signed on. From a mobile I can not log out. Please can you help me?



Hi Patrizia,


I have given Redirection URL of mobile community in property file and reading from property. Here is my code for getting entire logout URL :

1 protected String getRedirect(ThemeDisplay themeDisplay,String mobPagePath)
2    throws Exception {
3            
4            String redirect = themeDisplay.getPathFriendlyURLPublic()+mobPagePath;
5
6              return redirect;
7    }


#mobPagePath: receiving from property file.

HTH

- Gnaniyar Zubair
Patrizia D
RE: Redirect to Mobile Community
January 25, 2013 3:13 AM
Answer

Patrizia D

Rank: New Member

Posts: 6

Join Date: April 21, 2010

Recent Posts

Hi Gnaniyar, thanks for reply!

Mmmm ..... I still have some problems. I'll explain.
I have created a mobile rule on the homepage (web/en/home) that does a simple redirect to /web/guest/home-mobile. Logout from this page does not work. I do not understand why! I got a error like "TOO MANY redirects." I changed ServicePreAction so but it does not work.

Can you help me please?Thanks

PD
Yogesh Kumar
RE: Redirect to Mobile Community
March 27, 2013 2:48 AM
Answer

Yogesh Kumar

Rank: New Member

Posts: 1

Join Date: March 27, 2013

Recent Posts

Hi Gnaniyar Zubair,

I have problem to create liferay mobile theme . how shall i create mobile theme ? plz suggest me.
Kowbathullah Gnaniyar
RE: Redirect to Mobile Community
March 27, 2013 3:18 AM
Answer

Kowbathullah Gnaniyar

Rank: Liferay Master

Posts: 605

Join Date: December 19, 2007

Recent Posts

Too Many Redirection issue due to ServicePreAction only. will have a look your code if you share.