001: package org.vraptor.scope;
002:
003: import javax.servlet.ServletContext;
004: import javax.servlet.http.HttpServletRequest;
005: import javax.servlet.http.HttpServletResponse;
006:
007: import org.apache.log4j.Logger;
008: import org.vraptor.LogicDefinition;
009: import org.vraptor.LogicRequest;
010: import org.vraptor.core.WebRequest;
011: import org.vraptor.url.RequestInfo;
012:
013: /**
014: * Basic vraptor logic request.
015: *
016: * @author Guilherme Silveira
017: */
018: public class DefaultLogicRequest implements LogicRequest {
019:
020: private static final Logger LOG = Logger
021: .getLogger(DefaultLogicRequest.class);
022:
023: private final ApplicationContext applicationContext;
024:
025: private SessionContext sessionContext;
026:
027: private RequestContext requestContext;
028:
029: private String result;
030:
031: private final RequestInfo info;
032:
033: private final WebRequest request;
034:
035: private LogicDefinition logicDefinition;
036:
037: private final FlashContext flashContext;
038:
039: @Deprecated
040: public DefaultLogicRequest(RequestInfo info, WebRequest request) {
041: this (info, request, null);
042: }
043:
044: public DefaultLogicRequest(RequestInfo info, WebRequest request,
045: LogicDefinition definition) {
046: this .request = request;
047: this .info = info;
048: this .applicationContext = new DefaultApplicationContext(request
049: .getContext());
050: this .logicDefinition = definition;
051: this .flashContext = new FlashContext(request.getRequest());
052: }
053:
054: public ApplicationContext getApplicationContext() {
055: return this .applicationContext;
056: }
057:
058: public SessionContext getSessionContext() {
059: if (this .sessionContext == null) {
060: this .sessionContext = new SessionContext(this .request
061: .getRequest().getSession());
062: }
063: return this .sessionContext;
064: }
065:
066: public RequestContext getRequestContext() {
067: if (this .requestContext == null) {
068: this .requestContext = new DefaultRequestContext(request
069: .getRequest());
070: }
071: return this .requestContext;
072: }
073:
074: public HttpServletRequest getRequest() {
075: return request.getRequest();
076: }
077:
078: public HttpServletResponse getResponse() {
079: return request.getResponse();
080: }
081:
082: public Object findAttribute(String key) {
083: if (getRequestContext().hasAttribute(key)) {
084: return requestContext.getAttribute(key);
085: }
086: if (getSessionContext().hasAttribute(key)) {
087: return sessionContext.getAttribute(key);
088: }
089: if (getApplicationContext().hasAttribute(key)) {
090: return applicationContext.getAttribute(key);
091: }
092: if (LOG.isDebugEnabled()) {
093: LOG.debug("key not found in any contexts: " + key);
094: }
095: return null;
096: }
097:
098: public void confirmResult(String string) {
099: if (this .result != null) {
100: throw new IllegalStateException(
101: "the result for the logic was already set");
102: }
103: this .result = string;
104: }
105:
106: public String getResult() {
107: return this .result;
108: }
109:
110: public RequestInfo getRequestInfo() {
111: return info;
112: }
113:
114: public ServletContext getServletContext() {
115: return request.getContext();
116: }
117:
118: public LogicDefinition getLogicDefinition() {
119: return this .logicDefinition;
120: }
121:
122: public void setLogicDefinition(LogicDefinition logicDefinition) {
123: this .logicDefinition = logicDefinition;
124: }
125:
126: public FlashContext getFlashContext() {
127: return this.flashContext;
128: }
129:
130: }
|