001    /**
002     * Copyright (c) 2000-present 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                    if (isGroupFriendlyURL(layout.getGroup(), layout, layoutURL, locale)) {
134                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
135                                    redirect = layoutURL;
136                            }
137                            else {
138                                    redirect = PortalUtil.getGroupFriendlyURL(
139                                            layout.getLayoutSet(), themeDisplay, locale);
140                            }
141                    }
142                    else {
143                            if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
144                                    if (themeDisplay.isI18n()) {
145                                            redirect = layout.getFriendlyURL(locale);
146                                    }
147                                    else {
148                                            redirect = PortalUtil.getLayoutURL(
149                                                    layout, themeDisplay, locale);
150                                    }
151                            }
152                            else {
153                                    redirect = PortalUtil.getLayoutFriendlyURL(
154                                            layout, themeDisplay, locale);
155                            }
156                    }
157    
158                    int lifecycle = GetterUtil.getInteger(
159                            HttpUtil.getParameter(queryString, "p_p_lifecycle", false));
160    
161                    if (lifecycle == 0) {
162                            redirect = redirect + queryString;
163                    }
164    
165                    actionResponse.sendRedirect(redirect);
166            }
167    
168            @Override
169            public ActionForward render(
170                            ActionMapping actionMapping, ActionForm actionForm,
171                            PortletConfig portletConfig, RenderRequest renderRequest,
172                            RenderResponse renderResponse)
173                    throws Exception {
174    
175                    return actionMapping.findForward("portlet.language.view");
176            }
177    
178            @Override
179            protected boolean isCheckMethodOnProcessAction() {
180                    return _CHECK_METHOD_ON_PROCESS_ACTION;
181            }
182    
183            protected boolean isGroupFriendlyURL(
184                    Group group, Layout layout, String layoutURL, Locale locale) {
185    
186                    if (Validator.isNull(layoutURL)) {
187                            return true;
188                    }
189    
190                    int pos = layoutURL.lastIndexOf(CharPool.SLASH);
191    
192                    String layoutURLLanguageId = layoutURL.substring(pos + 1);
193    
194                    Locale layoutURLLocale = LocaleUtil.fromLanguageId(
195                            layoutURLLanguageId, true, false);
196    
197                    if (layoutURLLocale != null) {
198                            return true;
199                    }
200    
201                    if (PortalUtil.isGroupFriendlyURL(
202                                    layoutURL, group.getFriendlyURL(),
203                                    layout.getFriendlyURL(locale))) {
204    
205                            return true;
206                    }
207    
208                    return false;
209            }
210    
211            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
212    
213    }