1 19 package org.lucane.webconnector; 20 21 import java.io.File ; 22 import java.sql.Connection ; 23 import java.sql.PreparedStatement ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.http.HttpServletRequest ; 31 32 import org.lucane.common.JarUtils; 33 import org.lucane.common.Logging; 34 import org.lucane.common.Message; 35 import org.lucane.common.concepts.PluginConcept; 36 import org.lucane.common.net.ObjectConnection; 37 import org.lucane.server.Server; 38 import org.lucane.server.Service; 39 import org.lucane.server.ServiceManager; 40 import org.lucane.server.database.DatabaseAbstractionLayer; 41 import org.lucane.server.store.PluginStore; 42 import org.lucane.webconnector.security.LucanePrincipal; 43 import org.lucane.webconnector.security.LucaneRealm; 44 import org.mortbay.http.*; 45 import org.mortbay.http.handler.*; 46 47 public class WebConnector extends Service 48 { 49 public static WebConnector getInstance() 50 { 51 return (WebConnector)ServiceManager.getInstance().getService("org.lucane.webconnector"); 52 } 53 54 private String realmName = "Lucane"; 56 private boolean httpEnabled = false; 57 private int httpPort = 8080; 58 private boolean httpsEnabled = false; 59 private int httpsPort = 8443; 60 private String httpsKeystorePassword = ""; 61 private String httpsKeyPassword = ""; 62 63 private HttpServer httpServer; 65 private HashMap webapps; 66 67 public WebConnector() 68 { 69 this.webapps = new HashMap (); 70 } 71 72 76 public void install() 77 { 78 try { 79 String dbDescription = getDirectory() + "db-webconnector.xml"; 81 DatabaseAbstractionLayer layer = Server.getInstance().getDBLayer(); 82 layer.getTableCreator().createFromXml(dbDescription); 83 84 Connection c = layer.getConnection(); 86 PreparedStatement insert = c.prepareStatement("INSERT INTO WebConnector VALUES(?, ?, ?, ?, ?, ?, ?)"); 87 insert.setString(1, "Lucane"); 88 insert.setInt(2, 0); 89 insert.setInt(3, 8080); 90 insert.setInt(4, 0); 91 insert.setInt(5, 8443); 92 insert.setString(6, ""); 93 insert.setString(7, ""); 94 insert.execute(); 95 insert.close(); 96 c.close(); 97 } catch (Exception e) { 98 Logging.getLogger().severe("Unable to install WebConnector !"); 99 e.printStackTrace(); 100 } 101 } 102 103 104 public void init(Server parent) 105 { 106 try { 107 this.readConfig(); 108 } catch(SQLException sqle) { 109 Logging.getLogger().warning("Unable to read WebConnector configuration : " + sqle); 110 } 111 112 if(!this.httpEnabled && !this.httpsEnabled) 113 { 114 Logging.getLogger().info("WebConnector is disabled"); 115 return; 116 } 117 118 119 120 this.httpServer = new HttpServer(); 122 123 if(this.httpEnabled) 125 { 126 Logging.getLogger().info("Starting http WebConnector on port " + httpPort + " (" + realmName + ")"); 127 SocketListener listener=new SocketListener(); 128 listener.setPort(httpPort); 129 httpServer.addListener(listener); 130 } 131 132 if(this.httpsEnabled) 135 { 136 Logging.getLogger().info("Starting https WebConnector on port " + httpsPort + " (" + realmName + ")"); 137 File keystore = new File ("etc/webconnector.ssl"); 138 if(keystore.exists()) 139 { 140 SunJsseListener jsse = new SunJsseListener(); 141 jsse.setKeystore(keystore.getPath()); 142 jsse.setPassword(httpsKeystorePassword); 143 jsse.setKeyPassword(httpsKeyPassword); 144 jsse.setPort(httpsPort); 145 httpServer.addListener(jsse); 146 } 147 } 148 149 HttpContext root = new HttpContext(); 151 root.setAttribute(HttpContext.__ErrorHandler, new ErrorPageHandler()); 152 root.setClassLoader(WebConnector.class.getClassLoader()); 153 root.setContextPath("/"); 154 root.setResourceBase(getDirectory() + WebApp.WEB_DIR); 155 UserRealm realm = new LucaneRealm(realmName, httpPort); 156 root.setRealm(realm); 157 158 Authenticator authenticator = new BasicAuthenticator(); 159 root.setAuthenticator(authenticator); 160 161 SecurityConstraint security = new SecurityConstraint(); 162 security.setAuthenticate(true); 163 security.addRole(SecurityConstraint.ANY_ROLE); 164 root.addSecurityConstraint("/", security); 165 166 root.addHandler(new SecurityHandler()); 167 root.addHandler(new JspHandler(null)); 168 root.addHandler(new ResourceHandler()); 169 root.addHandler(new NotFoundHandler()); 170 httpServer.addContext(root); 171 172 ContextFactory factory = new ContextFactory(realm, authenticator, security); 174 try { 175 String baseUrl = System.getProperty("user.dir") + "/" + Server.APPLICATIONS_DIRECTORY; 176 Iterator plugins = parent.getStore().getPluginStore().getAllPlugins(); 177 while(plugins.hasNext()) 178 { 179 PluginConcept plugin = (PluginConcept)plugins.next(); 180 String name = plugin.getName(); 181 String jarUrl = baseUrl + name + ".jar"; 182 if(JarUtils.isWebEnabled(jarUrl)) { 183 WebApp app = new WebApp(name, null); 184 webapps.put(name, app); 185 httpServer.addContext(factory.createApplicationContext(app)); 186 httpServer.addContext(factory.createImageContext(app)); 187 } 188 } 189 } catch(Exception e) { 190 Logging.getLogger().warning("Unable to get plugins !"); 191 e.printStackTrace(); 192 } 193 194 195 try { 197 httpServer.start(); 198 } catch(Exception e) { 199 e.printStackTrace(); 200 } 201 } 202 203 204 public void process(ObjectConnection oc, Message message) 205 { 206 } 207 208 public void shutdown() 209 { 210 if(httpServer != null) 211 { 212 try { 213 httpServer.stop(); 214 } catch (InterruptedException e) { 215 Logging.getLogger().warning("Unable to stop http server: " + e); 216 } 217 } 218 } 219 220 public WebApp getRootWebApp(LucanePrincipal user) 221 { 222 return new WebApp(getName(), user); 223 } 224 225 public WebApp getWebApp(ServletConfig config, HttpServletRequest request) 226 throws Exception 227 { 228 return getWebApp(config, (LucanePrincipal)request.getUserPrincipal()); 229 } 230 231 public WebApp getWebApp(ServletConfig config, LucanePrincipal user) 232 throws Exception 233 { 234 String name = config.getInitParameter(WebApp.APPNAME_PARAM); 235 return getWebApp(name, user); 236 } 237 238 public WebApp getWebApp(String name, LucanePrincipal user) 239 throws Exception 240 { 241 WebApp app = (WebApp)webapps.get(name); 242 if(app != null) 243 { 244 PluginStore pluginStore = Server.getInstance().getStore().getPluginStore(); 245 PluginConcept plugin = pluginStore.getPlugin(name); 246 if(pluginStore.isAuthorizedPlugin(user.getUser(), plugin)) 247 return new WebApp(name, user); 248 else 249 throw new IllegalAccessException ("User " + user.getName() + " has no access to plugin: " + name); 250 } 251 252 return null; 253 } 254 255 private void readConfig() 256 throws SQLException 257 { 258 DatabaseAbstractionLayer layer = Server.getInstance().getDBLayer(); 259 260 if(!layer.hasTable("WebConnector")) 262 return; 263 264 Connection c = layer.getConnection(); 265 PreparedStatement select = c.prepareStatement( 266 "SELECT * FROM WebConnector"); 267 268 ResultSet r = select.executeQuery(); 269 if(r.next()) 270 { 271 this.realmName = r.getString(1); 272 this.httpEnabled = (r.getInt(2) == 1); 273 this.httpPort = r.getInt(3); 274 this.httpsEnabled = (r.getInt(4) == 1); 275 this.httpsPort = r.getInt(5); 276 this.httpsKeystorePassword = r.getString(6); 277 this.httpsKeyPassword = r.getString(7); 278 } 279 280 r.close(); 281 select.close(); 282 c.close(); 283 } 284 } 285 | Popular Tags |