1 25 package org.snipsnap.net.filter; 26 27 import org.radeox.util.i18n.ResourceManager; 28 import org.radeox.util.logging.LogHandler; 29 import org.radeox.util.logging.Logger; 30 import org.snipsnap.app.Application; 31 import org.snipsnap.app.ApplicationManager; 32 import org.snipsnap.config.Configuration; 33 import org.snipsnap.config.ConfigurationManager; 34 import org.snipsnap.config.ConfigurationProxy; 35 import org.snipsnap.config.Globals; 36 import org.snipsnap.config.ServerConfiguration; 37 import org.snipsnap.container.Components; 38 import org.snipsnap.container.SessionService; 39 import org.snipsnap.snip.Snip; 40 import org.snipsnap.snip.SnipLink; 41 import org.snipsnap.snip.SnipSpace; 42 import org.snipsnap.snip.SnipSpaceFactory; 43 import org.snipsnap.user.Digest; 44 import org.snipsnap.user.User; 45 46 import javax.servlet.Filter ; 47 import javax.servlet.FilterChain ; 48 import javax.servlet.FilterConfig ; 49 import javax.servlet.RequestDispatcher ; 50 import javax.servlet.ServletContext ; 51 import javax.servlet.ServletException ; 52 import javax.servlet.ServletRequest ; 53 import javax.servlet.ServletResponse ; 54 import javax.servlet.http.HttpServletRequest ; 55 import javax.servlet.http.HttpServletResponse ; 56 import javax.servlet.http.HttpSession ; 57 import java.io.BufferedReader ; 58 import java.io.ByteArrayInputStream ; 59 import java.io.File ; 60 import java.io.FileInputStream ; 61 import java.io.FileOutputStream ; 62 import java.io.IOException ; 63 import java.io.InputStreamReader ; 64 import java.net.URL ; 65 import java.util.Collection ; 66 import java.util.Date ; 67 import java.util.HashMap ; 68 import java.util.Iterator ; 69 import java.util.Map ; 70 71 78 public class InitFilter implements Filter { 79 private Globals globals = null; 81 private boolean startUpDone = false; 82 83 public void init(FilterConfig filterConfig) throws ServletException { 84 ServletContext context = filterConfig.getServletContext(); 85 86 globals = ConfigurationProxy.getInstance(); 88 89 String configParam = (String ) context.getAttribute(ServerConfiguration.INIT_PARAM); 91 if (null == configParam) { 92 System.out.println("SnipSnap " + globals.getVersion()); 93 BufferedReader br = new BufferedReader (new InputStreamReader (getClass().getResourceAsStream("/defaults/copyright.txt"))); 94 try { 95 String line; 96 while (null != (line = br.readLine())) { 97 System.out.println(line); 98 } 99 } catch (IOException e) { 100 System.out.println("Copyright (c) Fraunhofer Gesellschaft"); 102 System.out.println("Fraunhofer Institute for Computer Architecture and Software Technology"); 103 System.out.println("All Rights Reserved. See License Agreement for terms and conditions of use."); 104 } 105 106 String initParam = context.getInitParameter(ServerConfiguration.INIT_PARAM); 107 if (null != initParam) { 108 configParam = context.getRealPath(initParam); 109 } 110 } 111 if (null == configParam) { 112 configParam = context.getRealPath("/WEB-INF/application.conf"); 113 } 114 115 116 globals = ConfigurationProxy.getInstance(); 118 try { 119 globals.loadGlobals(new FileInputStream (configParam)); 120 } catch (Exception e) { 121 System.err.println("InitFilter: unable to load globals: " + configParam + ": " + e.getMessage()); 122 } 123 124 String installKey = globals.getInstallKey(); 125 if (null == installKey || "".equals(installKey)) { 126 globals.setInstallKey(Digest.getDigest("" + new Date ()).substring(0, 5).toLowerCase()); 127 System.out.println(">> Your installation key is '" + globals.getInstallKey() + "'"); 128 System.out.println(">> Remember it, you will need this key to install new instances."); 129 try { 130 globals.storeGlobals(new FileOutputStream (configParam)); 131 } catch (Exception e) { 132 System.err.println("InitFilter: unable to store install key: " + e.getMessage()); 133 } 134 } 135 136 globals.setWebInfDir(new File (context.getRealPath("WEB-INF"))); 137 138 String logger = globals.getLogger(); 140 try { 141 Logger.setHandler((LogHandler) Class.forName(logger).newInstance()); 142 } catch (Exception e) { 143 System.err.println("InitFilter: LogHandler not found: " + logger); 144 } 145 146 if (!globals.isInstalled()) { 147 System.out.println(">> Please finish the installation, visit"); 148 System.out.println(">> " + ((Configuration) globals).getUrl() + "?key=" + globals.getInstallKey()); 149 } else { 150 loadApplicationContexts(); 151 } 152 startUpDone = true; 153 } 154 155 private void loadApplicationContexts() { 156 ApplicationManager appManager = (ApplicationManager) Components.getComponent(ApplicationManager.class); 157 Collection prefixes = appManager.getPrefixes(); 158 Iterator prefixIt = prefixes.iterator(); 159 Application app = Application.get(); 160 int okCount = 0; 161 boolean weblogsPing = false; 162 while (prefixIt.hasNext()) { 163 String prefix = (String ) prefixIt.next(); 164 String appOid = appManager.getApplication(prefix); 165 app.storeObject(Application.OID, appOid); 166 167 System.out.print(">> Loading: " + prefix + " "); 168 Configuration appConfig = ConfigurationProxy.newInstance(); 169 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 170 if (space.exists(Configuration.SNIPSNAP_CONFIG)) { 171 Snip configSnip = space.load(Configuration.SNIPSNAP_CONFIG); 172 String configContent = configSnip.getContent(); 173 try { 174 appConfig.load(new ByteArrayInputStream (configContent.getBytes())); 175 okCount++; 176 System.out.print("(" + appConfig.getName() + ", " + appConfig.getUrl() + ")"); 177 } catch (IOException e) { 178 System.out.print("ERROR: " + e.getMessage()); 179 continue; 180 } 181 ConfigurationManager.getInstance().addConfiguration(appOid, appConfig); 182 } else { 183 System.out.print("(NOT CONFIGURED)"); 184 } 185 weblogsPing = appConfig.allow(Configuration.APP_PERM_WEBLOGSPING); 186 System.out.println(); 187 } 188 if (weblogsPing) { 189 System.out.println(">> WARNING: Weblogs ping is enabled for some instances.\n" + 190 ">> This means that SnipSnap sends notifications to hosts on the internet\n" + 191 ">> when your weblog changes. To turn this off take a look at the FAQ at\n" + 192 ">> http://snipsnap.org/space/faq"); 193 } 194 System.out.println(">> Installation key: " + globals.getInstallKey()); 195 System.out.println(">> Loaded " + okCount + " instances (" + (prefixes.size() - okCount) + " not configured)."); 196 } 197 198 public void destroy() { 199 } 201 202 public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException , ServletException { 203 HttpServletRequest request = (HttpServletRequest ) req; 205 206 if (!startUpDone) { 207 ((HttpServletResponse ) response).sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, 208 "Startup in progress, please wait ..."); 209 return; 210 } 211 212 String path = request.getServletPath(); 213 214 HttpSession session = request.getSession(); 215 Application app = Application.forceGet(); 216 ConfigurationManager configManager = ConfigurationManager.getInstance(); 217 ApplicationManager appManager = null; 218 219 ResourceManager resourceManager = ResourceManager.forceGet(); 221 resourceManager.setLocale(request.getLocale(), request.getLocales()); 222 223 if (globals.isInstalled()) { 224 appManager = (ApplicationManager) Components.getComponent(ApplicationManager.class); 225 } 226 227 String prefix = "/"; 228 String appOid = null; 229 230 path = request.getServletPath(); 232 if (path != null && path.length() > 1) { 233 int prefixEnd = path.indexOf("/", 1); 234 String checkPrefix = null; 235 if (prefixEnd > 1) { 236 checkPrefix = path.substring(0, prefixEnd); 237 } else { 238 checkPrefix = path; 239 } 240 if (appManager != null && appManager.getPrefixes().contains(checkPrefix)) { 241 prefix = checkPrefix; 242 } 243 } 244 245 Configuration appConfig = null; 246 if (globals.isInstalled()) { 248 appOid = appManager.getApplication(prefix); 249 appConfig = configManager.getConfiguration(appOid); 250 app.setConfiguration(appConfig); 251 app.storeObject(Application.OID, appOid); 252 } 253 254 if (path.startsWith("/RPC2")) { 256 if (globals.isInstalled()) { 257 chain.doFilter(request, response); 258 } else { 259 ((HttpServletResponse ) response).sendError(HttpServletResponse.SC_PRECONDITION_FAILED, 260 "Please finish database installation first."); 261 } 262 return; 263 } 264 265 if (appConfig != null && "true".equals(appConfig.getRealAutodetect())) { 267 String xForwardedHost = request.getHeader("X-Forwarded-Host"); 270 if (xForwardedHost != null) { 271 String protocol = appConfig.get(Configuration.APP_REAL_PROTOCOL, "http"); 272 String contextPath = appConfig.get(Configuration.APP_REAL_PATH, ""); 273 274 int colonIndex = xForwardedHost.indexOf(':'); 275 String host = xForwardedHost; 276 if (colonIndex != -1) { 277 host = host.substring(0, colonIndex); 278 int port = Integer.parseInt(xForwardedHost.substring(colonIndex + 1)); 279 app.storeObject(Application.URL, new URL (protocol, host, port, contextPath)); 280 } else { 281 app.storeObject(Application.URL, new URL (protocol, host, contextPath)); 282 } 283 } else { 284 String protocol = new URL (request.getRequestURL().toString()).getProtocol(); 285 String host = request.getServerName(); 286 int port = request.getServerPort(); 287 String contextPath = request.getContextPath() + ("/".equals(prefix) ? "" : prefix); 288 289 if (port != 80) { 290 app.storeObject(Application.URL, new URL (protocol, host, port, contextPath)); 291 } else { 292 app.storeObject(Application.URL, new URL (protocol, host, contextPath)); 293 } 294 } 295 } 297 298 if (null == appManager || null == appConfig || !appConfig.isConfigured()) { 301 if (path == null || !(path.startsWith("/admin") || path.startsWith("/images"))) { 302 String queryString = request.getQueryString(); 303 queryString = (null == queryString || "".equals(queryString) ? "" : queryString + "&") + "prefix=" + prefix; 304 ((HttpServletResponse ) response).sendRedirect(request.getContextPath() + "/admin/configure?" + queryString); 305 return; 306 } 307 } else { 308 if (!"/".equals(prefix)) { 309 path = path.substring(prefix.length()); 310 } 311 312 request.setAttribute(Configuration.APP_PREFIX, prefix); 313 314 session.setAttribute("app", app); 315 session.setAttribute("space", SnipSpaceFactory.getInstance()); 316 317 SessionService service = (SessionService) Components.getComponent(SessionService.class); 319 User user = service.getUser(request, (HttpServletResponse ) response); 320 if(null == user) { 321 return; 323 } 324 app.setUser(user, session); 325 326 Iterator paramIt = request.getParameterMap().keySet().iterator(); 327 Map paramMap = new HashMap (); 328 while (paramIt.hasNext()) { 329 String key = (String ) paramIt.next(); 330 paramMap.put(key, request.getParameter(key)); 331 } 332 String uri = (String ) request.getAttribute("URI"); 333 if (uri != null) { 334 paramMap.put("URI", appConfig.getUrl(uri)); 335 } else { 336 String pathInfo = request.getPathInfo(); 337 paramMap.put("URI", appConfig.getUrl((path != null ? path : "") + 338 (pathInfo != null ? pathInfo : ""))); 339 } 340 paramMap.put("RSS", appConfig.getUrl("/exec/rss")); 341 paramMap.put("request", request); 342 app.setParameters(paramMap); 343 } 344 345 if (!"/".equals(prefix)) { 346 if ("".equals(path)) { 347 path = "index.jsp"; 348 } 349 if (request.getClass().getName().startsWith("org.mortbay")) { 351 path = SnipLink.encode(path); 352 } 353 RequestDispatcher dispatcher = request.getRequestDispatcher(path); 355 if (dispatcher != null) { 356 dispatcher.forward(request, response); 357 return; 358 } 359 } 360 361 chain.doFilter(request, response); 363 } 364 } 365 | Popular Tags |