1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.openid.action;
24  
25  import com.liferay.portal.action.OpenIdRequestAction;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.struts.ActionConstants;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.util.servlet.SessionErrors;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import javax.servlet.jsp.PageContext;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  import org.openid4java.OpenIDException;
53  
54  /**
55   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Jorge Ferrer
58   *
59   */
60  public class ViewAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig config,
64              ActionRequest req, ActionResponse res)
65          throws Exception {
66  
67          String cmd = req.getParameter(Constants.CMD);
68  
69          ThemeDisplay themeDisplay =
70              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
71  
72          if (req.getRemoteUser() != null) {
73              res.sendRedirect(themeDisplay.getPathMain());
74          }
75          else if (Validator.isNotNull(cmd)) {
76              try {
77                  login(themeDisplay, req, res);
78              }
79              catch (Exception e) {
80                  if (e instanceof OpenIDException) {
81                      if (_log.isInfoEnabled()) {
82                          _log.info(
83                              "Error communicating with OpenID provider: " +
84                                  e.getMessage());
85                      }
86  
87                      SessionErrors.add(req, e.getClass().getName());
88                  }
89                  else {
90                      req.setAttribute(PageContext.EXCEPTION, e);
91  
92                      setForward(req, ActionConstants.COMMON_ERROR);
93                  }
94              }
95          }
96      }
97  
98      public ActionForward render(
99              ActionMapping mapping, ActionForm form, PortletConfig config,
100             RenderRequest req, RenderResponse res)
101         throws Exception {
102 
103         return mapping.findForward("portlet.open_id.view");
104     }
105 
106     protected void login(
107             ThemeDisplay themeDisplay, ActionRequest req, ActionResponse res)
108         throws Exception {
109 
110         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
111         HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(res);
112 
113         String openId = ParamUtil.getString(req, "openId");
114 
115         OpenIdRequestAction.sendOpenIdRequest(
116             themeDisplay, httpReq, httpRes, openId);
117     }
118 
119     private static Log _log = LogFactory.getLog(ViewAction.class);
120 
121 }