1 19 20 package com.sslexplorer.upgrade; 21 22 import java.io.File ; 23 import java.sql.Connection ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.Statement ; 27 28 public class WebForwardsUpgrade extends AbstractDatabaseUpgrade { 29 30 WebForwardsUpgrade(File oldDbDir, File 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 oldConx, Connection newConx) throws Exception { 39 upgrader.info("Migrating all web forwards"); 41 Statement stmt = oldConx.createStatement(); 42 try { 43 ResultSet rs = stmt.executeQuery("SELECT * FROM WEBFORWARD"); 44 try { 45 while (rs.next()) { 46 String shortName = rs.getString("SHORT_NAME"); 47 String username = rs.getString("USERNAME"); 48 shortName = shortName + " (" + (username.equals("") ? "System" : username) + ")"; 49 PreparedStatement ps = newConx.prepareStatement("SELECT * FROM WEBFORWARD WHERE SHORT_NAME = ?"); 50 boolean found = false; 51 try { 52 ps.setString(1, shortName); 53 ResultSet 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 newConx, Connection oldConx, ResultSet rs, String shortName) throws Exception { 80 81 PreparedStatement 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 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 if (type != 0) { 97 PreparedStatement ps2 = newConx.prepareStatement("SELECT ID FROM WEBFORWARD WHERE SHORT_NAME = ?"); 98 try { 99 ps2.setString(1, shortName); 100 ResultSet 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 updateReplacement(upgrader, newConx, oldConx, oldResourceId, newResourceId); 108 } else if (type == 2) { 109 updateReverse(upgrader, newConx, oldConx, oldResourceId, newResourceId); 111 } else { 112 upgrader.warn("Invalid type " + type + "."); 113 } 114 } else { 115 throw new Exception ("Failed to get new resource Id"); 116 } 117 } finally { 118 rs2.close(); 119 } 120 } finally { 121 ps2.close(); 122 } 123 } 124 } catch (Exception 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 newConx, Connection oldConx, int oldResourceId, int newResourceId) throws Exception { 132 PreparedStatement ps3 = oldConx.prepareStatement("SELECT * FROM SECURE_PROXY_OPTIONS WHERE WEBFORWARD_ID = ?"); 133 try { 134 ps3.setInt(1, oldResourceId); 135 ResultSet rs3 = ps3.executeQuery(); 136 try { 137 while (rs3.next()) { 138 PreparedStatement 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 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 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 newConx, Connection oldConx, int oldResourceId, int newResourceId) throws Exception { 171 PreparedStatement ps3 = oldConx.prepareStatement("SELECT * FROM REVERSE_PROXY_OPTIONS WHERE WEBFORWARD_ID = ?"); 172 try { 173 ps3.setInt(1, oldResourceId); 174 ResultSet rs3 = ps3.executeQuery(); 175 try { 176 while (rs3.next()) { 177 PreparedStatement 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 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 rs3 = ps3.executeQuery(); 211 try { 212 while (rs3.next()) { 213 PreparedStatement ps4 = newConx.prepareStatement( 214 "INSERT INTO REVERSE_PROXY_PATHS (PATH,WEBFORWARD_ID) " + 215 "VALUES(?,?)"); 216 try { 217 String 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 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 |