1   /**
2    * Copyright (c) 2000-2008 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.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
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  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  import org.openid4java.OpenIDException;
51  
52  /**
53   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Jorge Ferrer
56   *
57   */
58  public class ViewAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = actionRequest.getParameter(Constants.CMD);
66  
67          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
68              WebKeys.THEME_DISPLAY);
69  
70          if (actionRequest.getRemoteUser() != null) {
71              actionResponse.sendRedirect(themeDisplay.getPathMain());
72          }
73          else if (Validator.isNotNull(cmd)) {
74              try {
75                  login(themeDisplay, actionRequest, actionResponse);
76              }
77              catch (Exception e) {
78                  if (e instanceof OpenIDException) {
79                      if (_log.isInfoEnabled()) {
80                          _log.info(
81                              "Error communicating with OpenID provider: " +
82                                  e.getMessage());
83                      }
84  
85                      SessionErrors.add(actionRequest, e.getClass().getName());
86                  }
87                  else {
88                      PortalUtil.sendError(e, actionRequest, actionResponse);
89                  }
90              }
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
96              RenderRequest renderRequest, RenderResponse renderResponse)
97          throws Exception {
98  
99          return mapping.findForward("portlet.open_id.view");
100     }
101 
102     protected void login(
103             ThemeDisplay themeDisplay, ActionRequest actionRequest,
104             ActionResponse actionResponse)
105         throws Exception {
106 
107         HttpServletRequest request = PortalUtil.getHttpServletRequest(
108             actionRequest);
109         HttpServletResponse response = PortalUtil.getHttpServletResponse(
110             actionResponse);
111 
112         String openId = ParamUtil.getString(actionRequest, "openId");
113 
114         OpenIdRequestAction.sendOpenIdRequest(
115             themeDisplay, request, response, openId);
116     }
117 
118     private static Log _log = LogFactory.getLog(ViewAction.class);
119 
120 }