View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.login;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServlet;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.jetspeed.Jetspeed;
30  import org.apache.jetspeed.PortalReservedParameters;
31  import org.apache.jetspeed.engine.Engine;
32  import org.apache.jetspeed.exception.JetspeedException;
33  import org.apache.jetspeed.request.RequestContext;
34  import org.apache.jetspeed.request.RequestContextComponent;
35  
36  /***
37   * LoginServlet
38   * 
39   * @author <a href="mailto:ate@douma.nu">Ate Douma </a>
40   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke Sugaya</a>
41   * @version $Id: LoginServlet.java 516881 2007-03-11 10:34:21Z ate $
42   */
43  public class LoginServlet extends HttpServlet
44  {
45      private static final Log log = LogFactory.getLog(LoginServlet.class);
46  
47      public void doGet(HttpServletRequest request,
48              HttpServletResponse response) throws IOException, ServletException
49      {
50          HttpSession session = request.getSession(true);
51  
52          if (request.getUserPrincipal() != null)
53          {
54              String destination = (String) session
55                      .getAttribute(LoginConstants.DESTINATION);
56              if (destination == null)
57                      destination = request.getContextPath() + "/";
58  
59              response.sendRedirect(response.encodeURL(destination));
60          }
61  
62          if (Jetspeed.getEngine() != null)
63          {
64              request.setAttribute(PortalReservedParameters.PIPELINE, PortalReservedParameters.LOGIN_PIPELINE);
65              Engine engine = Jetspeed.getEngine();
66              try
67              {
68                  RequestContextComponent contextComponent = (RequestContextComponent) Jetspeed.getComponentManager()
69                          .getComponent(RequestContextComponent.class);
70                  RequestContext context = contextComponent.create(request, response, getServletConfig());
71                  engine.service(context);
72                  contextComponent.release(context);
73              }
74              catch (JetspeedException e)
75              {
76                  log.warn("Jetspeed engine does not work properly.", e);
77                  // forward to JetspeedServlet 
78                  response.sendRedirect(response.encodeURL(request.getContextPath() + "/"));
79              }
80          }
81          else
82          {
83              // forward to JetspeedServlet to create Engine
84              response.sendRedirect(response.encodeURL(request.getContextPath() + "/"));
85          }
86      }
87  
88      public final void doPost(HttpServletRequest request,
89              HttpServletResponse response) throws IOException, ServletException
90      {
91          doGet(request, response);
92      }
93  }