Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 82 83 84 85 86 87 < 88 > 89 90 91 92 93 94 .. 166 >> Next

001 package org.javapitfalls.item27;
002
003 import java.io.*;
004 import java.util.*;
005 import javax.servlet.*;
006 import javax.servlet.http.*;
007 import org.javapitfalls.item27.*;
008
009 public class ControllerServlet extends HttpServlet {
010
011 String direction;
012 String pageNumber;
013 String username;
014 String password;
015 boolean loggedIn = false;
016 ldapAuthenticate la;
017 authenticateHelper helper;
018
019 public void service(HttpServletRequest req,
020 HttpServletResponse res)
021 throws ServletException, java io.IOException {
022
023 direction = req.getParameter("direction");
024 pageNumber = req.getParameter("page");
025 username = req.getParameter("username");
026 password = req.getParameter("password");
027 if (loggedIn) {
028
029 if ( (username != la.getUsername()) ||
030 (password != la.getPassword()) ) {
031 loggedIn = false;
032 }
033 }
034 if (!loggedIn) {
035 if ((username != null) && (password != null) &&
036 (!username.equals("")) && (!password.equals(""))) {
037
Listing 27.1 ControllerServlet.java (continued)
238 Item 27
On line 38, an instance of the authentication application named ldapAuthenticate is passed the username and password entered by the user from the login.jsp file dispatched on line 83. When valid entries have been made by the user, the profile data collected from the LDAP authentication and query will be passed along to the database through the helper class authenticateHelper.java. This process is shown in
Figure 27.1.
038 la = new ldapAuthenticate(username, password);
039
040 if ( (la.validUser()) && (la.getSearchCount() > 0) ) {
041
042 loggedIn = true;
043 helper.setDB(la.getEmail());
044
045 } else {
046
047 System.out.println("ERROR: Invalid user.");
048 getServletConfig().getServletContext().
049 getNamedDispatcher("InvalidUser").forward(req, res);
050 }
051 }
052
053 }
054
055 if (loggedIn) {
056
057 if (pageNumber == null) {
058
059 pageNumber = "Page1";
060
061 } else {
062
Listing 27.1 (continued)
The isUserInRole method checks for authenticated users in the application so that
proper navigation can take place. This role information is passed from the LDAP direc-
tory and the JNDI realm that was implemented to expose those roles. If a user is deter-
mined to possess an admin role, that user will be forwarded to the Page4.jsp.
063 if (req.isUserInRole("admin")) {
064
065 pageNumber = "Page4";
066
067 } else if (req.isUserInRole("manager")) {
068
069 pageNumber = "Page3";
Listing 27.1 (continued)
Transactional LDAP-Don't Make that Commitment 239
070
071 } else if (req.isUserInRole("tester")) {
072
073 pageNumber = "Page2";
074
075 } else {
076
077 pageNumber = "Pagel";
078
079 }
080
081 }
082
083 getServletConfig().getServletContext().
084 getNamedDispatcher(pageNumber).forward(req, res);
085
086 } else {
087
088 System.out.println("Login error...");
089 getServletConfig().getServletContext().
090 getNamedDispatcher("Login").forward(req, res);
091
092 }
093
094 }
095
Listing 27.1 (continued)
The Front Controller pattern was employed in this application indicated by the Servlet controller, the dispatcher process shown on Lines 48, 83, and 89. Also part of that pattern implementation is a helper routine shown in the init() method on lines 97 and 98. This one-time call determines the database properties and performs connection and query update operations on profile data that needs to be pushed to the backend data store and predetermined and user-defined personalization preferences.
095 public void init() throws ServletException {
096
097 helper = new authenticateHelper();
098 helper.getDBProperties();
099
100 }
101
102 }
103
Listing 27.2 authenticateHelper.java
240 Item 27
The authenticateHelper class shown in Listing 27.3 loads the database.proper-ties file. It then extracts the key properties and stores them in instance variables:
01 package org.javapitfalls.item27;
02
03 import java.io.*;
04 import java.net.*;
05 import java.util.*;
06 import javax.servlet.*;
07 import javax.servlet.http.*;
08
09 public class authenticateHelper extends HttpServlet {
10
11 String driver;
12 String dbname;
13 String username;
14 String password;
15
16 authenticateHelper() {}
17
18 public void getDBProperties() {
19
20 URL url = null;
21 Properties props = new Properties();
22
23 try {
24 url = this.getClass().getClassLoader().
25 getResource("database.properties");
26 props.load( new FileInputStream(url.getFile()) );
27 // Get properties
28 driver = props.getProperty("driver");
29 dbname = props.getProperty("dbname");
30 username = props.getProperty("username");
31 password = props.getProperty("password");
32 }
33 catch(Exception e) {
34 System.out.println("ERROR:" + e.toString());
35 }
36
37 }
Previous << 1 .. 82 83 84 85 86 87 < 88 > 89 90 91 92 93 94 .. 166 >> Next