moving cmu handle service files to hs tree
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / hs / ClubShibSQLHandleRepository.java
1 import java.util.*;
2 import java.sql.*;
3 import javax.servlet.http.*;
4
5 public class ClubShibSQLHandleRepository extends HandleRepositoryFactory{
6
7     private Connection con;
8     String DBdriver;
9     String DBuser;
10     String DBpass;
11     String DBdomain;
12     String DBurl;
13     final static String db = "HandleService";
14
15     public ClubShibSQLHandleRepository(HttpServlet HS) 
16         throws HandleException 
17     {
18         DBdriver = HS.getInitParameter("DBdriver");
19         DBuser = HS.getInitParameter("DBuser");
20         DBpass = HS.getInitParameter("DBpass");
21         DBdomain = HS.getInitParameter("DBdomain");
22         DBurl = "jdbc:mysql://"+DBdomain+"/shib"+
23             "?user="+DBuser+"&password="+DBpass+"&autoReconnect=true";
24             
25         try {
26             Class.forName(DBdriver);
27         }
28         catch (Exception ex) {
29             throw new HandleException(HandleException.SQL, ex.getMessage());
30         }
31         try {
32             con = DriverManager.getConnection(DBurl);
33         } 
34         catch (Exception ex) {
35             throw new HandleException(HandleException.SQL, ex.getMessage());
36         }
37
38     }
39
40     public HandleEntry getHandleEntry( String handle )
41         throws HandleException
42     {
43         HandleEntry he = null;
44
45         if (handle == null){
46             throw new HandleException(HandleException.ERR, "ClubShibSQLHandleRepository() requires handle");
47         }
48
49         try{
50             Statement st = con.createStatement();
51             String query = "SELECT * FROM "+db+" WHERE handle=\""+handle+"\"";
52             ResultSet rs = st.executeQuery(query);
53
54             if(rs == null)
55                 throw new HandleException(HandleException.ERR, "null result set for handle: "+handle);
56             
57             while (rs.next()) {
58                 he = new HandleEntry( rs.getString("handle"), 
59                                       rs.getString("username"),
60                                       rs.getString("authType"),
61                                       rs.getLong("authInstant"),
62                                       rs.getLong("expInstant"));
63             }
64             st.close();
65         }
66         catch (SQLException e) {
67             throw new HandleException(HandleException.SQL, e.getMessage());
68         }
69         if ( he == null ) 
70             throw new HandleException(HandleException.ERR, "getHandleEntry() cannot find matching record for handle: "+handle);
71         else
72             return he;
73     }
74     
75
76     public void insertHandleEntry( HandleEntry he )
77         throws HandleException
78     {
79         if ( he == null ) { 
80             throw new HandleException(HandleException.ERR, "InsertHandle() requires HandleEntry arg");
81         }
82
83         String handle = he.getHandle();
84         String username = he.getUsername();
85         String authType = he.getAuthType();
86         long authInstant = he.getAuthInstant();
87         long expInstant = he.getExpInstant();
88
89         try{
90             Statement st = con.createStatement();
91             String update = "INSERT INTO " +db+
92                 " VALUES ( \"" + handle +"\", \""+username+"\", \""+
93                 authType+"\", \""+ authInstant +"\", \""+
94                 expInstant+"\")";
95             st.executeUpdate(update);
96             st.close();
97         }
98         catch (SQLException e) {
99             throw new HandleException(HandleException.SQL, e.getMessage());
100         }
101     }
102
103     public String toHTMLString() 
104         throws HandleException
105     {
106         String HTMLString = new String();
107         
108         try{
109             Statement st = con.createStatement();
110             String query = "SELECT * FROM "+db;
111             ResultSet rs = st.executeQuery(query);
112             HTMLString = "Server = "+DBdomain+"<br>"+
113                 "<table><tr><td><b>handle</b></td>"+
114                 "<td><b>username</b></td>"+
115                 "<td><b>authType</b></td>"+
116                 "<td><b>authInstant</b></td>"+
117                 "<td><b>expInstant</b></td></tr>";
118             while (rs.next()) {
119                 String han = rs.getString(1);
120                 String uid = rs.getString(2);
121                 String authtype = rs.getString(3);
122                 String date_in = rs.getString(4);
123                 String date_exp = rs.getString(5);
124
125                 HTMLString += "<tr><td>"+han+"</td><td>"+uid+"</td>" +
126                 "<td>"+authtype+"</td>"+
127                 "<td>"+date_in+"</td>"+
128                 "<td>"+date_exp+"</td></tr>";
129             }
130             st.close();
131
132             HTMLString += "</table>";
133         }
134         catch (SQLException e) {
135             throw new HandleException(HandleException.SQL, e.getMessage());
136         }
137
138         return HTMLString;
139     }
140     public void destroy() 
141         throws HandleException
142     {
143         try {
144             con.close();
145         }
146         catch (SQLException e) {
147             throw new HandleException(HandleException.SQL, e.getMessage());
148         }
149
150     }   
151         
152 }