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