001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.language.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Contact;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.Portal;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.PropsValues;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.admin.util.AdminUtil;
037    
038    import java.util.List;
039    import java.util.Locale;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.RenderRequest;
045    import javax.portlet.RenderResponse;
046    
047    import javax.servlet.http.HttpServletRequest;
048    import javax.servlet.http.HttpServletResponse;
049    import javax.servlet.http.HttpSession;
050    
051    import org.apache.struts.Globals;
052    import org.apache.struts.action.ActionForm;
053    import org.apache.struts.action.ActionForward;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     */
059    public class ViewAction extends PortletAction {
060    
061            @Override
062            public void processAction(
063                            ActionMapping actionMapping, ActionForm actionForm,
064                            PortletConfig portletConfig, ActionRequest actionRequest,
065                            ActionResponse actionResponse)
066                    throws Exception {
067    
068                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
069                            actionRequest);
070                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
071                            actionResponse);
072                    HttpSession session = request.getSession();
073    
074                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
075                            WebKeys.THEME_DISPLAY);
076    
077                    String languageId = ParamUtil.getString(actionRequest, "languageId");
078    
079                    Locale locale = LocaleUtil.fromLanguageId(languageId);
080    
081                    List<Locale> availableLocales = ListUtil.fromArray(
082                            LanguageUtil.getAvailableLocales(themeDisplay.getSiteGroupId()));
083    
084                    if (availableLocales.contains(locale)) {
085                            boolean persistState = ParamUtil.getBoolean(
086                                    request, "persistState", true);
087    
088                            if (themeDisplay.isSignedIn() && (persistState)) {
089                                    User user = themeDisplay.getUser();
090    
091                                    Contact contact = user.getContact();
092    
093                                    AdminUtil.updateUser(
094                                            actionRequest, user.getUserId(), user.getScreenName(),
095                                            user.getEmailAddress(), user.getFacebookId(),
096                                            user.getOpenId(), languageId, user.getTimeZoneId(),
097                                            user.getGreeting(), user.getComments(), contact.getSmsSn(),
098                                            contact.getAimSn(), contact.getFacebookSn(),
099                                            contact.getIcqSn(), contact.getJabberSn(),
100                                            contact.getMsnSn(), contact.getMySpaceSn(),
101                                            contact.getSkypeSn(), contact.getTwitterSn(),
102                                            contact.getYmSn());
103                            }
104    
105                            session.setAttribute(Globals.LOCALE_KEY, locale);
106    
107                            LanguageUtil.updateCookie(request, response, locale);
108                    }
109    
110                    // Send redirect
111    
112                    String redirect = ParamUtil.getString(actionRequest, "redirect");
113    
114                    String layoutURL = StringPool.BLANK;
115                    String queryString = StringPool.BLANK;
116    
117                    int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
118    
119                    if (pos == -1) {
120                            pos = redirect.indexOf(StringPool.QUESTION);
121                    }
122    
123                    if (pos != -1) {
124                            layoutURL = redirect.substring(0, pos);
125                            queryString = redirect.substring(pos);
126                    }
127                    else {
128                            layoutURL = redirect;
129                    }
130    
131                    Layout layout = themeDisplay.getLayout();
132    
133                    Group group = layout.getGroup();
134    
135                    if (isGroupFriendlyURL(group, layout, layoutURL, locale)) {
136                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
137                                    redirect = layoutURL;
138                            }
139                            else {
140                                    redirect = PortalUtil.getGroupFriendlyURL(
141                                            group, layout.isPrivateLayout(), themeDisplay, locale);
142                            }
143                    }
144                    else {
145                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
146                                    if (themeDisplay.isI18n()) {
147                                            redirect = layout.getFriendlyURL(locale);
148                                    }
149                                    else {
150                                            redirect = PortalUtil.getLayoutURL(
151                                                    layout, themeDisplay, locale);
152                                    }
153                            }
154                            else {
155                                    redirect = PortalUtil.getLayoutFriendlyURL(
156                                            layout, themeDisplay, locale);
157                            }
158                    }
159    
160                    int lifecycle = GetterUtil.getInteger(
161                            HttpUtil.getParameter(queryString, "p_p_lifecycle", false));
162    
163                    if (lifecycle == 0) {
164                            redirect = redirect + queryString;
165                    }
166    
167                    actionResponse.sendRedirect(redirect);
168            }
169    
170            @Override
171            public ActionForward render(
172                            ActionMapping actionMapping, ActionForm actionForm,
173                            PortletConfig portletConfig, RenderRequest renderRequest,
174                            RenderResponse renderResponse)
175                    throws Exception {
176    
177                    return actionMapping.findForward("portlet.language.view");
178            }
179    
180            @Override
181            protected boolean isCheckMethodOnProcessAction() {
182                    return _CHECK_METHOD_ON_PROCESS_ACTION;
183            }
184    
185            protected boolean isGroupFriendlyURL(
186                    Group group, Layout layout, String layoutURL, Locale locale) {
187    
188                    if (Validator.isNull(layoutURL)) {
189                            return true;
190                    }
191    
192                    int pos = layoutURL.lastIndexOf(CharPool.SLASH);
193    
194                    String layoutURLLanguageId = layoutURL.substring(pos + 1);
195    
196                    Locale layoutURLLocale = LocaleUtil.fromLanguageId(
197                            layoutURLLanguageId, true, false);
198    
199                    if (layoutURLLocale != null) {
200                            return true;
201                    }
202    
203                    if (PortalUtil.isGroupFriendlyURL(
204                                    layoutURL, group.getFriendlyURL(),
205                                    layout.getFriendlyURL(locale))) {
206    
207                            return true;
208                    }
209    
210                    return false;
211            }
212    
213            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
214    
215    }