KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > upgrade > WebForwardsUpgrade


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.upgrade;
21
22 import java.io.File JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 public class WebForwardsUpgrade extends AbstractDatabaseUpgrade {
29
30     WebForwardsUpgrade(File JavaDoc oldDbDir, File JavaDoc newDbDir) {
31         super("Web Forwards", "Copies all web forwards to the new format. " +
32             "All three types are migrated. Any user created web " +
33             "forwards are also copied (with the owner username appended to " +
34             "the new resource name). The resource will not be attached to " +
35             "any policies.", true, "explorer_configuration", "explorer_configuration", oldDbDir, newDbDir);
36     }
37
38     public void doUpgrade(Upgrader upgrader, Connection JavaDoc oldConx, Connection JavaDoc newConx) throws Exception JavaDoc {
39         // // Auth Schemes
40
upgrader.info("Migrating all web forwards");
41         Statement JavaDoc stmt = oldConx.createStatement();
42         try {
43             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM WEBFORWARD");
44             try {
45                 while (rs.next()) {
46                     String JavaDoc shortName = rs.getString("SHORT_NAME");
47                     String JavaDoc username = rs.getString("USERNAME");
48                     shortName = shortName + " (" + (username.equals("") ? "System" : username) + ")";
49                     PreparedStatement JavaDoc ps = newConx.prepareStatement("SELECT * FROM WEBFORWARD WHERE SHORT_NAME = ?");
50                     boolean found = false;
51                     try {
52                         ps.setString(1, shortName);
53                         ResultSet JavaDoc rs2 = ps.executeQuery();
54                         try {
55                             if (rs2.next()) {
56                                 found = true;
57                             }
58                         } finally {
59                             rs2.close();
60                         }
61                     } finally {
62                         ps.close();
63                     }
64                     if (found) {
65                         upgrader.warn(" Network place '" + shortName + "' already exists, skipping");
66                     } else {
67                         insertWebForward(upgrader, newConx, oldConx, rs, shortName);
68                     }
69                 }
70             } finally {
71                 rs.close();
72             }
73         } finally {
74             stmt.close();
75         }
76
77     }
78
79     void insertWebForward(Upgrader upgrader, Connection JavaDoc newConx, Connection JavaDoc oldConx, ResultSet JavaDoc rs, String JavaDoc shortName) throws Exception JavaDoc {
80
81         PreparedStatement JavaDoc ps = newConx.prepareStatement("INSERT INTO WEBFORWARD (DESTINATION_URL,"
82                         + "TYPE, SHORT_NAME, DESCRIPTION, CATEGORY, " + "PARENT_RESOURCE_PERMISSION, "
83                         + "DATE_CREATED, DATE_AMENDED) VALUES " + "(?,?,?,?,?,?,NOW(),NOW())");
84         try {
85             upgrader.info(" " + shortName);
86             ps.setString(1, rs.getString("DESTINATION_URL"));
87             int type = rs.getInt("TYPE");
88             ps.setInt(2, type);
89             ps.setString(3, shortName);
90             String JavaDoc description = rs.getString("DESCRIPTION");
91             ps.setString(4, description.equals("") ? shortName : description);
92             ps.setString(5, rs.getString("CATEGORY"));
93             ps.setInt(6, 0);
94             ps.execute();
95             // Tunneled, nothing additional
96
if (type != 0) {
97                 PreparedStatement JavaDoc ps2 = newConx.prepareStatement("SELECT ID FROM WEBFORWARD WHERE SHORT_NAME = ?");
98                 try {
99                     ps2.setString(1, shortName);
100                     ResultSet JavaDoc rs2 = ps2.executeQuery();
101                     try {
102                         if (rs2.next()) {
103                             int oldResourceId = rs.getInt("ID");
104                             int newResourceId = rs2.getInt("ID");
105                             if (type == 1) {
106                                 // replacement
107
updateReplacement(upgrader, newConx, oldConx, oldResourceId, newResourceId);
108                             } else if (type == 2) {
109                                 // reverse
110
updateReverse(upgrader, newConx, oldConx, oldResourceId, newResourceId);
111                             } else {
112                                 upgrader.warn("Invalid type " + type + ".");
113                             }
114                         } else {
115                             throw new Exception JavaDoc("Failed to get new resource Id");
116                         }
117                     } finally {
118                         rs2.close();
119                     }
120                 } finally {
121                     ps2.close();
122                 }
123             }
124         } catch (Exception JavaDoc e) {
125             upgrader.warn("Failed to insert web forward " + shortName + ". Probably already exists.");
126         } finally {
127             ps.close();
128         }
129     }
130
131     void updateReplacement(Upgrader upgrader, Connection JavaDoc newConx, Connection JavaDoc oldConx, int oldResourceId, int newResourceId) throws Exception JavaDoc {
132         PreparedStatement JavaDoc ps3 = oldConx.prepareStatement("SELECT * FROM SECURE_PROXY_OPTIONS WHERE WEBFORWARD_ID = ?");
133         try {
134             ps3.setInt(1, oldResourceId);
135             ResultSet JavaDoc rs3 = ps3.executeQuery();
136             try {
137                 while (rs3.next()) {
138                     PreparedStatement JavaDoc ps4 = newConx.prepareStatement(
139                         "INSERT INTO SECURE_PROXY_OPTIONS (WEBFORWARD_ID," +
140                         "ENCODING,AUTHENTICATION_USERNAME,AUTHENTICATION_PASSWORD," +
141                         "PREFERRED_AUTHENTICATION_SCHEME,FORM_TYPE,FORM_PARAMETERS," +
142                         "RESTRICT_TO_HOSTS) VALUES(?,?,?,?,?,?,?,?)");
143                     try {
144                         ps4.setInt(1, newResourceId);
145                         String JavaDoc encoding = rs3.getString("ENCODING");
146                         ps4.setString(2, encoding == null || encoding.equals("") ? "Default" : encoding);
147                         ps4.setString(3, rs3.getString("AUTHENTICATION_USERNAME"));
148                         ps4.setString(4, rs3.getString("AUTHENTICATION_PASSWORD"));
149                         ps4.setString(5, rs3.getString("PREFERRED_AUTHENTICATION_SCHEME"));
150                         ps4.setString(6, "NONE");
151                         ps4.setString(7, "");
152                         ps4.setString(8, "");
153                         try {
154                             ps4.execute();
155                         } catch (Exception JavaDoc e) {
156                             upgrader.warn("Failed to insert replacement proxy options. Probably already exists.");
157                         }
158                     } finally {
159                         ps4.close();
160                     }
161                 }
162             } finally {
163                 rs3.close();
164             }
165         } finally {
166             ps3.close();
167         }
168     }
169
170     void updateReverse(Upgrader upgrader, Connection JavaDoc newConx, Connection JavaDoc oldConx, int oldResourceId, int newResourceId) throws Exception JavaDoc {
171         PreparedStatement JavaDoc ps3 = oldConx.prepareStatement("SELECT * FROM REVERSE_PROXY_OPTIONS WHERE WEBFORWARD_ID = ?");
172         try {
173             ps3.setInt(1, oldResourceId);
174             ResultSet JavaDoc rs3 = ps3.executeQuery();
175             try {
176                 while (rs3.next()) {
177                     PreparedStatement JavaDoc ps4 = newConx.prepareStatement(
178                         "INSERT INTO REVERSE_PROXY_OPTIONS (WEBFORWARD_ID," +
179                         "AUTHENTICATION_USERNAME,AUTHENTICATION_PASSWORD," +
180                         "PREFERRED_AUTHENTICATION_SCHEME,ACTIVE_DNS,HOST_HEADER," +
181                         "FORM_TYPE,FORM_PARAMETERS) VALUES(?,?,?,?,?,?,?,?)");
182                     try {
183                         ps4.setInt(1, newResourceId);
184                         ps4.setString(2, rs3.getString("AUTHENTICATION_USERNAME"));
185                         ps4.setString(3, rs3.getString("AUTHENTICATION_PASSWORD"));
186                         ps4.setString(4, rs3.getString("PREFERRED_AUTHENTICATION_SCHEME"));
187                         ps4.setInt(5, rs3.getInt("ACTIVE_DNS"));
188                         ps4.setString(6, rs3.getString("HOST_HEADER"));
189                         ps4.setString(7, "NONE");
190                         ps4.setString(8, "");
191                         try {
192                             ps4.execute();
193                         } catch (Exception JavaDoc e) {
194                             upgrader.warn("Failed to insert replacement proxy options. Probably already exists.");
195                         }
196                     } finally {
197                         ps4.close();
198                     }
199                 }
200             } finally {
201                 rs3.close();
202             }
203         } finally {
204             ps3.close();
205         }
206         upgrader.info(" Doing paths");
207         ps3 = oldConx.prepareStatement("SELECT * FROM REVERSE_PROXY_PATHS WHERE WEBFORWARD_ID = ?");
208         try {
209             ps3.setInt(1, oldResourceId);
210             ResultSet JavaDoc rs3 = ps3.executeQuery();
211             try {
212                 while (rs3.next()) {
213                     PreparedStatement JavaDoc ps4 = newConx.prepareStatement(
214                         "INSERT INTO REVERSE_PROXY_PATHS (PATH,WEBFORWARD_ID) " +
215                         "VALUES(?,?)");
216                     try {
217                         String JavaDoc path = rs3.getString("PATH");
218                         upgrader.info(" [" + path + "]");
219                         ps4.setString(1, path);
220                         ps4.setInt(2, newResourceId);
221                         try {
222                             ps4.execute();
223                         } catch (Exception JavaDoc e) {
224                             upgrader.warn("Failed to insert replacement proxy path. Probably already exists.");
225                         }
226                     } finally {
227                         ps4.close();
228                     }
229                 }
230             } finally {
231                 rs3.close();
232             }
233         } finally {
234             ps3.close();
235         }
236     }
237 }
238
Popular Tags