View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.enterprise.context.RequestScoped;
26  import javax.faces.context.ExternalContext;
27  import javax.faces.context.FacesContext;
28  import javax.inject.Named;
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpSession;
32  import java.io.IOException;
33  import java.lang.invoke.MethodHandles;
34  
35  @Named
36  @RequestScoped
37  public class LoginController {
38  
39    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40  
41    private String username;
42    private String password;
43  
44    public Outcome login() throws ServletException {
45      final FacesContext facesContext = FacesContext.getCurrentInstance();
46      final ExternalContext externalContext = facesContext.getExternalContext();
47      final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
48  
49      LOG.info("Try to login user: '{}'", username);
50      request.login(username, password);
51      LOG.info("Successful login user: '{}'", username);
52  
53      return Outcome.CONCEPT_SECURITY_ROLES_XLOGIN;
54    }
55  
56    public String logout() throws ServletException, IOException {
57      final FacesContext facesContext = FacesContext.getCurrentInstance();
58      final ExternalContext externalContext = facesContext.getExternalContext();
59      final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
60  
61      request.logout();
62  
63      return facesContext.getViewRoot().getViewId();
64    }
65  
66    public String resetSession() throws IOException {
67      LOG.info("Resetting the session.");
68      final FacesContext facesContext = FacesContext.getCurrentInstance();
69      final HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
70      if (session != null) {
71        session.invalidate();
72      }
73      final ExternalContext externalContext = facesContext.getExternalContext();
74  /* XXX reset theme doesn't work
75      CookieUtils.removeThemeNameCookie(
76          (HttpServletRequest)externalContext.getRequest(),
77          (HttpServletResponse) externalContext.getResponse());
78  */
79      externalContext.redirect(externalContext.getRequestContextPath() + "/");
80      facesContext.responseComplete();
81      return null;
82    }
83  
84    public String getUsername() {
85      return username;
86    }
87  
88    public void setUsername(final String username) {
89      this.username = username;
90    }
91  
92    public String getPassword() {
93      return password;
94    }
95  
96    public void setPassword(final String password) {
97      this.password = password;
98    }
99  }