001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.AuthTokenUtil;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.struts.ActionConstants;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.PortletURLImpl;
031    
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletURL;
034    
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    import org.apache.struts.action.Action;
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Mika Koivisto
045     */
046    public class VerifyEmailAddressAction extends Action {
047    
048            @Override
049            public ActionForward execute(
050                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
051                            HttpServletResponse response)
052                    throws Exception {
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    String cmd = ParamUtil.getString(request, Constants.CMD);
058    
059                    if (Validator.isNull(cmd)) {
060                            return mapping.findForward("portal.verify_email_address");
061                    }
062    
063                    try {
064                            verifyEmailAddress(request, response, themeDisplay);
065    
066                            if (!themeDisplay.isSignedIn()) {
067                                    PortletURL portletURL = new PortletURLImpl(
068                                            request, PortletKeys.LOGIN, themeDisplay.getPlid(),
069                                            PortletRequest.RENDER_PHASE);
070    
071                                    response.sendRedirect(portletURL.toString());
072    
073                                    return null;
074                            }
075                            else {
076                                    return mapping.findForward(ActionConstants.COMMON_REFERER);
077                            }
078                    }
079                    catch (Exception e) {
080                            if (e instanceof PortalException ||
081                                    e instanceof SystemException) {
082    
083                                    SessionErrors.add(request, e.getClass().getName());
084    
085                                    return mapping.findForward("portal.verify_email_address");
086                            }
087                            else {
088                                    PortalUtil.sendError(e, request, response);
089    
090                                    return null;
091                            }
092                    }
093            }
094    
095            protected void verifyEmailAddress(
096                            HttpServletRequest request, HttpServletResponse response,
097                            ThemeDisplay themeDisplay)
098                    throws Exception {
099    
100                    AuthTokenUtil.check(request);
101    
102                    String ticketKey = ParamUtil.getString(request, "ticketKey");
103    
104                    UserLocalServiceUtil.verifyEmailAddress(ticketKey);
105            }
106    
107    }