1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.openid.action;
16  
17  import com.liferay.portal.action.OpenIdRequestAction;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.servlet.SessionErrors;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  import org.openid4java.OpenIDException;
43  
44  /**
45   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   */
49  public class ViewAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          String cmd = actionRequest.getParameter(Constants.CMD);
57  
58          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
59              WebKeys.THEME_DISPLAY);
60  
61          if (actionRequest.getRemoteUser() != null) {
62              actionResponse.sendRedirect(themeDisplay.getPathMain());
63          }
64          else if (Validator.isNotNull(cmd)) {
65              try {
66                  login(themeDisplay, actionRequest, actionResponse);
67              }
68              catch (Exception e) {
69                  if (e instanceof OpenIDException) {
70                      if (_log.isInfoEnabled()) {
71                          _log.info(
72                              "Error communicating with OpenID provider: " +
73                                  e.getMessage());
74                      }
75  
76                      SessionErrors.add(actionRequest, e.getClass().getName());
77                  }
78                  else {
79                      PortalUtil.sendError(e, actionRequest, actionResponse);
80                  }
81              }
82          }
83      }
84  
85      public ActionForward render(
86              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
87              RenderRequest renderRequest, RenderResponse renderResponse)
88          throws Exception {
89  
90          return mapping.findForward("portlet.open_id.view");
91      }
92  
93      protected void login(
94              ThemeDisplay themeDisplay, ActionRequest actionRequest,
95              ActionResponse actionResponse)
96          throws Exception {
97  
98          HttpServletRequest request = PortalUtil.getHttpServletRequest(
99              actionRequest);
100         HttpServletResponse response = PortalUtil.getHttpServletResponse(
101             actionResponse);
102 
103         String openId = ParamUtil.getString(actionRequest, "openId");
104 
105         OpenIdRequestAction.sendOpenIdRequest(
106             themeDisplay, request, response, openId);
107     }
108 
109     private static Log _log = LogFactoryUtil.getLog(ViewAction.class);
110 
111 }