View Javadoc

1   /*
2    * ------------------------------------------------------------------------------
3    * Hermes FTP Server
4    * Copyright (c) 2005-2007 Lars Behnke
5    * ------------------------------------------------------------------------------
6    * 
7    * This file is part of Hermes FTP Server.
8    * 
9    * Hermes FTP Server is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   * 
14   * Hermes FTP Server is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Hermes FTP Server; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22   * ------------------------------------------------------------------------------
23   */
24  
25  package net.sf.hermesftp.console;
26  
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.servlet.Servlet;
31  
32  import net.sf.hermesftp.exception.FtpConfigException;
33  import net.sf.hermesftp.exception.FtpConsoleException;
34  import net.sf.hermesftp.usermanager.UserManager;
35  import net.sf.hermesftp.usermanager.model.UserData;
36  
37  import org.mortbay.jetty.Server;
38  import org.mortbay.jetty.handler.ContextHandlerCollection;
39  import org.mortbay.jetty.security.Constraint;
40  import org.mortbay.jetty.security.ConstraintMapping;
41  import org.mortbay.jetty.security.HashUserRealm;
42  import org.mortbay.jetty.security.SecurityHandler;
43  import org.mortbay.jetty.security.UserRealm;
44  import org.mortbay.jetty.servlet.Context;
45  import org.mortbay.jetty.servlet.ServletHolder;
46  
47  /***
48   * Web based administration console. The console is powered by an embedded jetty web server.
49   * 
50   * @author Administrator
51   */
52  public class ConsoleServerImpl implements ConsoleServer {
53  
54      private static final int     DEFAULT_PORT = 9988;
55  
56      private int                  port         = DEFAULT_PORT;
57  
58      private Map<String, Servlet> servlets;
59  
60      private UserManager          userManager;
61  
62      private UserRealm            realm;
63  
64      /***
65       * {@inheritDoc}
66       */
67      public void start() throws FtpConsoleException {
68  
69          try {
70              Server server = new Server(getPort());
71              ContextHandlerCollection contexts = new ContextHandlerCollection();
72              server.setHandler(contexts);
73              Context ctx = new Context(contexts, "/", Context.SESSIONS | Context.SECURITY);
74              configureSecurity(ctx);
75              configureServlets(ctx);
76              server.start();
77          } catch (Exception e) {
78              throw new FtpConsoleException("Starting web server failed: " + e);
79          }
80      }
81  
82      private void configureServlets(Context ctx) {
83          Set<Map.Entry<String, Servlet>> entrySet = getServlets().entrySet();
84          for (Map.Entry<String, Servlet> entry : entrySet) {
85              String path = entry.getKey().toString();
86              Servlet servlet = (Servlet) entry.getValue();
87              ctx.addServlet(new ServletHolder(servlet), path);
88  
89          }
90      }
91  
92      /***
93       * Configures the Console security. Per default basic authentication is enforced.
94       * 
95       * @param ctx The Web context.
96       * @throws FtpConfigException Error in configuration.
97       */
98      protected void configureSecurity(Context ctx) throws FtpConfigException {
99          SecurityHandler sh = ctx.getSecurityHandler();
100         if (!userManager.isLoaded()) {
101             userManager.load();
102         }
103         configureRealm(realm);
104         sh.setUserRealm(realm);
105         Constraint constraint = new Constraint();
106         constraint.setName(Constraint.__BASIC_AUTH);
107         constraint.setRoles(new String[] {ConsoleConstants.ROLE_ADMIN});
108         constraint.setAuthenticate(true);
109         ConstraintMapping cm = new ConstraintMapping();
110         cm.setConstraint(constraint);
111         cm.setPathSpec("/*");
112         sh.setConstraintMappings(new ConstraintMapping[] {cm});
113     }
114 
115     /***
116      * Configures the security realm.
117      * 
118      * @param realm The realm to configure.
119      * @throws FtpConfigException Error due to an flow in the configuration.
120      */
121     protected void configureRealm(UserRealm realm) throws FtpConfigException {
122         if (realm instanceof HashUserRealm) {
123             HashUserRealm hur = (HashUserRealm) realm;
124             for (UserData ud : userManager.getUserDataList()) {
125                 hur.put(ud.getUid(), new ConsolePassword(ud.getPassword()));
126                 if (ud.isAdminRole()) {
127                     hur.addUserToRole(ud.getUid(), ConsoleConstants.ROLE_ADMIN);
128                 }
129             }
130         }
131     }
132 
133     /***
134      * Getter method for property <code>port</code>.
135      * 
136      * @return Property <code>port</code>.
137      */
138     public int getPort() {
139         return port;
140     }
141 
142     /***
143      * Setter methode for property <code>port</code>.
144      * 
145      * @param port Value for <code>port</code>.
146      */
147     public void setPort(int port) {
148         this.port = port;
149     }
150 
151     /***
152      * Getter methode for property <code>realm</code>.
153      * 
154      * @return Property <code>realm</code>.
155      */
156     public UserRealm getRealm() {
157         return realm;
158     }
159 
160     /***
161      * Setter methode for property <code>realm</code>.
162      * 
163      * @param realm Value for <code>realm</code>.
164      */
165     public void setRealm(UserRealm realm) {
166         this.realm = realm;
167     }
168 
169     /***
170      * Getter method for property <code>servlets</code>.
171      * 
172      * @return Property <code>servlets</code>.
173      */
174     public Map<String, Servlet> getServlets() {
175         return servlets;
176     }
177 
178     /***
179      * Setter methode for property <code>servlets</code>.
180      * 
181      * @param servlets Value for <code>servlets</code>.
182      */
183     public void setServlets(Map<String, Servlet> servlets) {
184         this.servlets = servlets;
185     }
186 
187     /***
188      * Getter method for property <code>userManager</code>.
189      * 
190      * @return Property <code>userManager</code>.
191      */
192     public UserManager getUserManager() {
193         return userManager;
194     }
195 
196     /***
197      * Setter methode for property <code>userManager</code>.
198      * 
199      * @param userManager Value for <code>userManager</code>.
200      */
201     public void setUserManager(UserManager userManager) {
202         this.userManager = userManager;
203     }
204 
205 }