1 package com.genimen.djeneric.web.controller; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 7 import javax.servlet.ServletContext ; 8 import javax.servlet.ServletException ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 import javax.servlet.http.HttpSession ; 12 import javax.xml.parsers.FactoryConfigurationError ; 13 import javax.xml.parsers.ParserConfigurationException ; 14 import javax.xml.transform.TransformerException ; 15 16 import org.apache.commons.fileupload.FileUploadException; 17 import org.xml.sax.SAXException ; 18 19 import com.genimen.djeneric.repository.exceptions.DjenericException; 20 import com.genimen.djeneric.web.actors.FilterValidator; 21 import com.genimen.djeneric.web.actors.Querier; 22 import com.genimen.djeneric.web.util.DjenericSessionManager; 23 import com.metaparadigm.jsonrpc.JSONRPCBridge; 24 25 public class DjenericController 26 { 27 private static final String COMMAND_EXTENSION = "dje"; 28 private static final String COMMAND_LOGIN = "/login."; 29 private static final String COMMAND_MAIN = "/main."; 30 private static final String COMMAND_TREE = "/tree."; 31 private static final String COMMAND_IMPORT = "/import."; 32 private static final String COMMAND_EXPORT = "/export."; 33 private static final String COMMAND_EXPORT_ALT = "/export"; 34 private static final String COMMAND_CHOOSE = "/choose."; 35 private static final String COMMAND_FILTER = "/filter."; 36 private static final String COMMAND_DIALOG = "/dialog."; 37 private static final String COMMAND_SETFILTER = "/setfilter."; 38 public static final String DJENERIC_SESSION_MANAGER = "DjenericSessionManager"; 39 40 ServletContext _context; 41 LoginCommand _loginCmd; 42 TreeCommand _treeCmd; 43 ExportCommand _exportCmd; 44 ImportCommand _importCmd; 45 FilterCommand _filterCmd; 46 SetFilterCommand _setfilterCmd; 47 MainCommand _mainRendererCmd; 48 DialogCommand _dialogCmd; 49 ChooseCommand _chooseCmd; 50 51 public DjenericController(ServletContext context) 52 { 53 _context = context; 54 _loginCmd = new LoginCommand(); 55 _treeCmd = new TreeCommand(); 56 _filterCmd = new FilterCommand(); 57 _mainRendererCmd = new MainCommand(); 58 _setfilterCmd = new SetFilterCommand(); 59 _exportCmd = new ExportCommand(); 60 _importCmd = new ImportCommand(); 61 _dialogCmd = new DialogCommand(); 62 _chooseCmd = new ChooseCommand(); 63 } 64 65 public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException , 66 ServletException 67 { 68 String method = request.getMethod(); 69 70 try 71 { 72 DjenericSessionManager sessionManager = getSessionManager(request); 73 try 74 { 75 dispatch(method, sessionManager, request, response); 76 } 77 finally 78 { 79 if (sessionManager != null) sessionManager.releaseDatasources(); 80 } 81 } 82 catch (Exception e) 83 { 84 throw new ServletException (e); 85 } 86 } 87 88 protected void dispatch(String method, DjenericSessionManager sessionManager, HttpServletRequest request, 89 HttpServletResponse response) throws IOException , ServletException , DjenericException, 90 FactoryConfigurationError , ParserConfigurationException , FileUploadException, SAXException , TransformerException 91 { 92 String path = request.getServletPath().toLowerCase(); 93 94 if ("GET".equals(method)) 95 { 96 if (!sessionManager.isLoggedIn()) 97 { 98 _loginCmd.get(sessionManager, request, response); 99 } 100 else if (path.startsWith(COMMAND_TREE)) 101 { 102 _treeCmd.get(sessionManager, request, response); 103 } 104 else if (path.startsWith(COMMAND_IMPORT)) 105 { 106 _importCmd.get(sessionManager, request, response); 107 } 108 else if (path.startsWith(COMMAND_EXPORT)) 109 { 110 _exportCmd.get(sessionManager, request, response); 111 } 112 else if (path.equals(COMMAND_EXPORT_ALT)) 113 { 114 _exportCmd.get(sessionManager, request, response); 115 } 116 else if (path.startsWith(COMMAND_DIALOG)) 117 { 118 _dialogCmd.get(sessionManager, request, response); 119 } 120 else if (path.startsWith(COMMAND_CHOOSE)) 121 { 122 _chooseCmd.get(sessionManager, request, response); 123 } 124 else if (path.startsWith(COMMAND_FILTER)) 125 { 126 _filterCmd.get(sessionManager, request, response); 127 } 128 else if (path.startsWith(COMMAND_SETFILTER)) 129 { 130 _setfilterCmd.get(sessionManager, request, response); 131 _treeCmd.get(sessionManager, request, response); 132 } 133 else if (path.startsWith(COMMAND_MAIN)) 134 { 135 _mainRendererCmd.get(sessionManager, request, response); 136 } 137 } 138 else if ("POST".equals(method)) 139 { 140 if (!sessionManager.isLoggedIn()) 142 { 143 if (_loginCmd.post(sessionManager, request, response)) 144 { 145 String url = constructForwardUrl(request, response, COMMAND_MAIN); 147 response.sendRedirect(url); 148 } 149 } 150 else if (path.startsWith(COMMAND_FILTER)) 151 { 152 _filterCmd.post(sessionManager, request, response); 153 } 154 else if (path.startsWith(COMMAND_IMPORT)) 155 { 156 _importCmd.post(sessionManager, request, response); 157 } 158 } 159 } 160 161 protected String constructForwardUrl(HttpServletRequest request, HttpServletResponse response, String nextPath) 162 { 163 String urlString = request.getContextPath() + nextPath + COMMAND_EXTENSION; 164 return response.encodeRedirectURL(urlString); 165 } 166 167 protected DjenericSessionManager getSessionManager(HttpServletRequest request) throws FileNotFoundException , 168 DjenericException 169 { 170 HttpSession session = request.getSession(); 171 DjenericSessionManager mgr = (DjenericSessionManager) session.getAttribute(DJENERIC_SESSION_MANAGER); 172 if (mgr == null) 173 { 174 mgr = initSessionManager(session); 175 initJson(session, mgr); 176 } 177 178 return mgr; 179 } 180 181 private DjenericSessionManager initSessionManager(HttpSession session) throws FileNotFoundException , 182 DjenericException 183 { 184 DjenericSessionManager mgr; 185 InputStream repositories = _context.getResourceAsStream("WEB-INF/repositories.xml"); 186 if (repositories == null) throw new FileNotFoundException ("WEB-INF/repositories.xml not found"); 187 mgr = new DjenericSessionManager(repositories); 188 session.setAttribute(DJENERIC_SESSION_MANAGER, mgr); 189 return mgr; 190 } 191 192 private void initJson(HttpSession session, DjenericSessionManager mgr) 193 { 194 synchronized (session) 195 { 196 JSONRPCBridge bridge = (JSONRPCBridge) session.getAttribute("JSONRPCBridge"); 197 if (bridge == null) 198 { 199 bridge = new com.metaparadigm.jsonrpc.JSONRPCBridge(); 200 session.setAttribute("JSONRPCBridge", bridge); 201 } 202 bridge.registerObject("filterValidator", new FilterValidator(mgr)); 203 bridge.registerObject("querier", new Querier(mgr)); 204 } 205 } 206 } | Popular Tags |