View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.controller.impl;
17  
18  import javax.servlet.FilterChain;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.seasar.cubby.action.ActionResult;
23  import org.seasar.cubby.controller.ActionContext;
24  import org.seasar.cubby.controller.ActionDef;
25  import org.seasar.cubby.controller.ActionProcessor;
26  import org.seasar.cubby.convention.CubbyConvention;
27  import org.seasar.cubby.exception.ActionRuntimeException;
28  import org.seasar.cubby.filter.CubbyHttpServletRequestWrapper;
29  import org.seasar.framework.log.Logger;
30  
31  public class ActionProcessorImpl implements ActionProcessor {
32  
33  	private static final Logger logger = Logger
34  			.getLogger(ActionProcessorImpl.class);
35  
36  	private ActionContext context;
37  
38  	private CubbyConvention cubbyConvention;
39  
40  	public void setActionContext(final ActionContext context) {
41  		this.context = context;
42  	}
43  
44  	public void setCubbyConvention(final CubbyConvention cubbyConvention) {
45  		this.cubbyConvention = cubbyConvention;
46  	}
47  
48  	public ActionResult process(final HttpServletRequest request,
49  			final HttpServletResponse response, final FilterChain chain)
50  			throws Exception {
51  		final ActionDef actionDef = cubbyConvention
52  				.fromPathToActionDef(request);
53  		if (actionDef != null) {
54  			context.initialize(actionDef);
55  			if (logger.isDebugEnabled()) {
56  				logger
57  						.log("DCUB0004",
58  								new Object[] { request.getRequestURI() });
59  				logger.log("DCUB0005", new Object[] { context.getMethod() });
60  			}
61  			final ActionResult result = context.invoke();
62  			if (result == null) {
63  				throw new ActionRuntimeException("ECUB0001",
64  						new Object[] { context.getMethod() });
65  			}
66  			final HttpServletRequest wrappedRequest = new CubbyHttpServletRequestWrapper(
67  					request, context);
68  			result.execute(context, wrappedRequest, response);
69  			return result;
70  		} else {
71  			final HttpServletRequest wrappedRequest = new CubbyHttpServletRequestWrapper(
72  					request, context);
73  			chain.doFilter(wrappedRequest, response);
74  			return null;
75  		}
76  	}
77  
78  }