KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > admin > AdminInitFilter


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.admin;
26
27 import org.apache.xmlrpc.XmlRpcException;
28 import org.snipsnap.config.Configuration;
29 import org.snipsnap.config.ServerConfiguration;
30 import org.snipsnap.net.filter.EncRequestWrapper;
31 import org.snipsnap.server.AdminXmlRpcClient;
32
33 import javax.servlet.Filter JavaDoc;
34 import javax.servlet.FilterChain JavaDoc;
35 import javax.servlet.FilterConfig JavaDoc;
36 import javax.servlet.RequestDispatcher JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.ServletRequest JavaDoc;
39 import javax.servlet.ServletResponse JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import javax.servlet.http.HttpSession JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.UnsupportedEncodingException JavaDoc;
45 import java.net.URL JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.Properties JavaDoc;
49 import java.util.prefs.Preferences JavaDoc;
50
51 public class AdminInitFilter implements Filter JavaDoc {
52
53   private final static String JavaDoc DEFAULT_ENCODING = "UTF-8";
54
55   protected final static String JavaDoc ATT_AUTHENTICATED = "authenticated";
56   protected final static String JavaDoc ATT_SERVERCONFIG = "serverconfig";
57   protected final static String JavaDoc ATT_CONFIG = "config";
58   protected final static String JavaDoc ATT_STEP = "step";
59   protected final static String JavaDoc ATT_ERRORS = "errors";
60   protected final static String JavaDoc ATT_APPS = "applications";
61
62   protected final static String JavaDoc PARAM_INSTALL = "install";
63   protected final static String JavaDoc PARAM_EXPERT = "expert";
64
65   protected Properties JavaDoc serverPrefsDefaults = new Properties JavaDoc();
66   protected AdminXmlRpcClient adminClient;
67
68   public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
69     // load defaults (should not be necessary ...)
70
try {
71       serverPrefsDefaults.load(AdminInitFilter.class.getResourceAsStream("/conf/snipsnap.conf"));
72     } catch (IOException JavaDoc e) {
73       System.err.println("AdminInitFilter: Unable to read server defaults: " + e.getMessage());
74       e.printStackTrace();
75     }
76
77     Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
78     try {
79       String JavaDoc url = serverPrefs.get(ServerConfiguration.ADMIN_URL,
80                                    serverPrefsDefaults.getProperty(ServerConfiguration.ADMIN_URL));
81       String JavaDoc user = serverPrefs.get(ServerConfiguration.ADMIN_USER, "admin");
82       String JavaDoc pass = serverPrefs.get(ServerConfiguration.ADMIN_PASS, null);
83       adminClient = new AdminXmlRpcClient(url, user, pass);
84     } catch (Exception JavaDoc e) {
85       System.out.println("!! Unable to create XML-RPC client, check system preferences:");
86       throw new ServletException JavaDoc(e);
87     }
88   }
89
90   public void destroy() {
91     //config = null;
92
}
93
94   public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
95     // make sure it's an http servlet request
96
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
97
98     // make sure the request has a correct character encoding
99
// the enc-wrapper ensures some methods return correct strings too
100
try {
101       request.setCharacterEncoding(DEFAULT_ENCODING);
102       request = new EncRequestWrapper(request, request.getCharacterEncoding());
103     } catch (UnsupportedEncodingException JavaDoc e) {
104       System.err.println("AdminInitFilter: unsupported encoding: " + e);
105     }
106
107     // get or create session and application object
108
HttpSession JavaDoc session = request.getSession();
109     Properties JavaDoc config = null;
110     Map JavaDoc errors = new HashMap JavaDoc();
111
112     String JavaDoc path = request.getServletPath();
113     if (null == path || "".equals(path)) {
114       System.out.println("Redirecting '" + path + "' -> " + request.getContextPath() + "/");
115       ((HttpServletResponse JavaDoc) response).sendRedirect(request.getContextPath() + "/");
116       return;
117     }
118
119     // except css files and images everything is protected
120
if (!(path.startsWith("/images") || path.endsWith(".css"))) {
121       RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("main.jsp");
122       String JavaDoc step = null;
123
124       // check authentication and verify session
125
if (!"true".equals(session.getAttribute(ATT_AUTHENTICATED))) {
126         Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
127         String JavaDoc serverPass = serverPrefs.get(ServerConfiguration.ADMIN_PASS, "");
128         String JavaDoc installPass = path;
129         if (installPass == null || "".equals(installPass) || "/".equals(installPass)) {
130           installPass = "/" + request.getParameter("password");
131         }
132
133         if (installPass == null || "".equals(installPass) || !serverPass.equals(installPass.substring(1))) {
134           step = "login";
135         } else {
136           session.setAttribute(ATT_AUTHENTICATED, "true");
137         }
138       }
139
140       Map JavaDoc applications = null;
141       if (null == step) {
142         config = (Properties JavaDoc) session.getAttribute(ATT_CONFIG);
143         if (null == config) {
144           config = new Properties JavaDoc();
145           config.load(AdminInitFilter.class.getResourceAsStream("/org/snipsnap/config/globals.conf"));
146         }
147
148         String JavaDoc host = request.getParameter(Configuration.APP_HOST);
149         String JavaDoc port = request.getParameter(Configuration.APP_PORT);
150         String JavaDoc contextPath = request.getParameter(Configuration.APP_PATH);
151
152         if (null != host && !"".equals(host)) {
153           config.setProperty(Configuration.APP_HOST, host);
154         }
155         if (null != port && !"".equals(port)) {
156           config.setProperty(Configuration.APP_PORT, port);
157         }
158         if (null != contextPath && !"".equals(contextPath)) {
159           config.setProperty(Configuration.APP_PATH, contextPath);
160         }
161
162         try {
163           applications = adminClient.getApplications();
164           request.setAttribute(ATT_APPS, new Integer JavaDoc(applications.size()));
165         } catch (XmlRpcException e) {
166           System.err.println("AdminInitFilter: error retrieving existing applications: " + e);
167           e.printStackTrace();
168         } catch (IOException JavaDoc e) {
169           System.err.println("AdminInitFilter: unable to contact server: " + e);
170           e.printStackTrace();
171         }
172
173         if (null != request.getParameter(PARAM_EXPERT) ||
174             (null == request.getParameter(PARAM_INSTALL) && (applications != null && applications.size() > 0))) {
175           step = "install";
176         } else {
177           URL JavaDoc url = null;
178           try {
179             url = install(config.getProperty(Configuration.APP_HOST),
180                           config.getProperty(Configuration.APP_PORT),
181                           config.getProperty(Configuration.APP_PATH));
182             if (url != null) {
183               ((HttpServletResponse JavaDoc) response).sendRedirect(url.toString());
184               session.removeAttribute(ATT_CONFIG);
185               return;
186             }
187           } catch (Exception JavaDoc e) {
188             e.printStackTrace();
189             System.out.println(e);
190             System.out.println(e.getCause());
191             errors.put(e.getMessage(), "unknown");
192           }
193           step = "install";
194         }
195       }
196
197       session.setAttribute(ATT_CONFIG, config);
198       request.setAttribute(ATT_ERRORS, errors);
199       request.setAttribute(ATT_STEP, step);
200       dispatcher.forward(request, response);
201       return;
202     }
203
204     // apply the chain
205
chain.doFilter(request, response);
206   }
207
208   protected URL JavaDoc install(String JavaDoc host, String JavaDoc port, String JavaDoc path) throws Exception JavaDoc {
209     //System.out.println("install("+host+", "+port+", "+path+")");
210
try {
211       return adminClient.install(host + "_" + port + "_" + path.replace('/', '_'), host, port, path);
212     } catch (XmlRpcException e) {
213       throw e;
214     }
215   }
216 }
217
Popular Tags