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