001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.spring.JWicSpringController
017: * Created on 20.05.2005
018: * $Id: JWicSpringController.java,v 1.2 2007/04/12 17:04:11 cosote Exp $
019: */
020: package de.jwic.spring;
021:
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.Map;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.springframework.beans.BeansException;
033: import org.springframework.web.context.WebApplicationContext;
034: import org.springframework.web.servlet.ModelAndView;
035: import org.springframework.web.servlet.mvc.AbstractController;
036:
037: import de.jwic.base.ConfigurationTool;
038: import de.jwic.base.IApplicationSetup;
039: import de.jwic.base.JWicRuntime;
040: import de.jwic.upload.Upload;
041: import de.jwic.web.IApplicationSetupProvider;
042: import de.jwic.web.IAuthenticator;
043: import de.jwic.web.UploadHttpServletRequest;
044: import de.jwic.web.WebEngine;
045:
046: /**
047: * Starts applications and dispatches incoming events to those applications.
048: * The application setup is loaded from the spring component container.
049: *
050: * @author Florian Lippisch
051: * @version $Revision: 1.2 $
052: */
053: public class JWicSpringController extends AbstractController implements
054: IApplicationSetupProvider {
055:
056: protected final Log log = LogFactory.getLog(getClass());
057:
058: private JWicRuntime jRuntime = null;
059: private int uploadLimit = 1024 * 1024 * 16; // 16 mb
060: private WebEngine engine = null;
061:
062: private IAuthenticator authenticator = null;
063: private String loginPage = null;
064:
065: /**
066: * Default constructor.
067: */
068: public JWicSpringController() {
069: jRuntime = JWicRuntime.getJWicRuntime();
070: }
071:
072: /* (non-Javadoc)
073: * @see org.springframework.context.support.ApplicationObjectSupport#initApplicationContext()
074: */
075: protected void initApplicationContext() throws BeansException {
076: super .initApplicationContext();
077:
078: ServletContext srvCtx = getServletContext();
079: String webAppRoot = srvCtx.getRealPath("/");
080: jRuntime.setRootPath(webAppRoot);
081: ConfigurationTool.setRootPath(webAppRoot);
082: try {
083: engine = new WebEngine(this , webAppRoot);
084: } catch (Exception e) {
085: throw new RuntimeException("Error initializing WebEngine");
086: }
087: if (loginPage != null) {
088: engine.setLoginPage(loginPage);
089: }
090: if (authenticator != null) {
091: engine.setAuthenticator(authenticator);
092: }
093: }
094:
095: /* (non-Javadoc)
096: * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
097: */
098: public ModelAndView handleRequestInternal(HttpServletRequest req,
099: HttpServletResponse res) throws Exception {
100:
101: // Store the context path initialy in the JWicRuntime to allow
102: // other tools to build full path links.
103: if (jRuntime.getContextPath() == null) {
104: jRuntime.setContextPath(req.getContextPath());
105: }
106:
107: Upload upload = null;
108: if (req.getMethod().equals("POST")
109: && req.getContentType().startsWith("multipart")) {
110: // parse data using "multipart" mode
111: upload = new Upload(req, ".", uploadLimit, 1 * 1024 * 1024);
112: // get parameters from the stream and set them as AgoraRequest parameters.
113: // fill webform
114: Map fields = upload.getParams();
115: req = new UploadHttpServletRequest(req, fields);
116: }
117:
118: engine.handleRequest(req, res, upload);
119:
120: return null;
121: }
122:
123: /**
124: * @return Returns the uploadLimit.
125: */
126: public int getUploadLimit() {
127: return uploadLimit;
128: }
129:
130: /**
131: * @param uploadLimit The uploadLimit to set.
132: */
133: public void setUploadLimit(int uploadLimit) {
134: this .uploadLimit = uploadLimit;
135: }
136:
137: /**
138: * @return Returns the authenticator.
139: */
140: public IAuthenticator getAuthenticator() {
141: return authenticator;
142: }
143:
144: /**
145: * @param authenticator The authenticator to set.
146: */
147: public void setAuthenticator(IAuthenticator authenticator) {
148: this .authenticator = authenticator;
149: }
150:
151: /**
152: * @return Returns the loginPage.
153: */
154: public String getLoginPage() {
155: return loginPage;
156: }
157:
158: /**
159: * @param loginPage The loginPage to set.
160: */
161: public void setLoginPage(String loginPage) {
162: this .loginPage = loginPage;
163: }
164:
165: /* (non-Javadoc)
166: * @see de.jwic.web.IApplicationSetupProvider#createApplicationSetup(javax.servlet.http.HttpServletRequest)
167: */
168: public IApplicationSetup createApplicationSetup(
169: HttpServletRequest request) throws IOException {
170: // load the application setup
171: WebApplicationContext context = getWebApplicationContext();
172:
173: String beanId = getFileName(request);
174: if (!context.containsBean(beanId)) {
175: throw new FileNotFoundException("Application with id "
176: + beanId
177: + " not found/specified in application context.");
178: }
179:
180: return (IApplicationSetup) context.getBean(beanId);
181:
182: }
183:
184: /**
185: * @param req
186: * @return
187: */
188: private String getFileName(HttpServletRequest req) {
189: String path = req.getServletPath();
190: int i = path.lastIndexOf('/');
191: if (i != -1) {
192: return path.substring(i + 1);
193: }
194: return path;
195: }
196:
197: }
|