1 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 ; 34 import javax.servlet.FilterChain ; 35 import javax.servlet.FilterConfig ; 36 import javax.servlet.RequestDispatcher ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.ServletRequest ; 39 import javax.servlet.ServletResponse ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import javax.servlet.http.HttpSession ; 43 import java.io.IOException ; 44 import java.io.UnsupportedEncodingException ; 45 import java.net.URL ; 46 import java.util.HashMap ; 47 import java.util.Map ; 48 import java.util.Properties ; 49 import java.util.prefs.Preferences ; 50 51 public class AdminInitFilter implements Filter { 52 53 private final static String DEFAULT_ENCODING = "UTF-8"; 54 55 protected final static String ATT_AUTHENTICATED = "authenticated"; 56 protected final static String ATT_SERVERCONFIG = "serverconfig"; 57 protected final static String ATT_CONFIG = "config"; 58 protected final static String ATT_STEP = "step"; 59 protected final static String ATT_ERRORS = "errors"; 60 protected final static String ATT_APPS = "applications"; 61 62 protected final static String PARAM_INSTALL = "install"; 63 protected final static String PARAM_EXPERT = "expert"; 64 65 protected Properties serverPrefsDefaults = new Properties (); 66 protected AdminXmlRpcClient adminClient; 67 68 public void init(FilterConfig config) throws ServletException { 69 try { 71 serverPrefsDefaults.load(AdminInitFilter.class.getResourceAsStream("/conf/snipsnap.conf")); 72 } catch (IOException e) { 73 System.err.println("AdminInitFilter: Unable to read server defaults: " + e.getMessage()); 74 e.printStackTrace(); 75 } 76 77 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 78 try { 79 String url = serverPrefs.get(ServerConfiguration.ADMIN_URL, 80 serverPrefsDefaults.getProperty(ServerConfiguration.ADMIN_URL)); 81 String user = serverPrefs.get(ServerConfiguration.ADMIN_USER, "admin"); 82 String pass = serverPrefs.get(ServerConfiguration.ADMIN_PASS, null); 83 adminClient = new AdminXmlRpcClient(url, user, pass); 84 } catch (Exception e) { 85 System.out.println("!! Unable to create XML-RPC client, check system preferences:"); 86 throw new ServletException (e); 87 } 88 } 89 90 public void destroy() { 91 } 93 94 public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException , ServletException { 95 HttpServletRequest request = (HttpServletRequest ) req; 97 98 try { 101 request.setCharacterEncoding(DEFAULT_ENCODING); 102 request = new EncRequestWrapper(request, request.getCharacterEncoding()); 103 } catch (UnsupportedEncodingException e) { 104 System.err.println("AdminInitFilter: unsupported encoding: " + e); 105 } 106 107 HttpSession session = request.getSession(); 109 Properties config = null; 110 Map errors = new HashMap (); 111 112 String path = request.getServletPath(); 113 if (null == path || "".equals(path)) { 114 System.out.println("Redirecting '" + path + "' -> " + request.getContextPath() + "/"); 115 ((HttpServletResponse ) response).sendRedirect(request.getContextPath() + "/"); 116 return; 117 } 118 119 if (!(path.startsWith("/images") || path.endsWith(".css"))) { 121 RequestDispatcher dispatcher = request.getRequestDispatcher("main.jsp"); 122 String step = null; 123 124 if (!"true".equals(session.getAttribute(ATT_AUTHENTICATED))) { 126 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 127 String serverPass = serverPrefs.get(ServerConfiguration.ADMIN_PASS, ""); 128 String 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 applications = null; 141 if (null == step) { 142 config = (Properties ) session.getAttribute(ATT_CONFIG); 143 if (null == config) { 144 config = new Properties (); 145 config.load(AdminInitFilter.class.getResourceAsStream("/org/snipsnap/config/globals.conf")); 146 } 147 148 String host = request.getParameter(Configuration.APP_HOST); 149 String port = request.getParameter(Configuration.APP_PORT); 150 String 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 (applications.size())); 165 } catch (XmlRpcException e) { 166 System.err.println("AdminInitFilter: error retrieving existing applications: " + e); 167 e.printStackTrace(); 168 } catch (IOException 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 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 ) response).sendRedirect(url.toString()); 184 session.removeAttribute(ATT_CONFIG); 185 return; 186 } 187 } catch (Exception 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 chain.doFilter(request, response); 206 } 207 208 protected URL install(String host, String port, String path) throws Exception { 209 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
|