001    /**
002     * Copyright (c) 2000-2011 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.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    }