KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > DefaultWebContext


1 /*
2  * Copyright 2005 Joe Walker
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.io.StringWriter JavaDoc;
20
21 import javax.servlet.ServletConfig JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.directwebremoting.Container;
29 import org.directwebremoting.ScriptSession;
30 import org.directwebremoting.WebContext;
31 import org.directwebremoting.extend.RealScriptSession;
32 import org.directwebremoting.extend.ScriptSessionManager;
33 import org.directwebremoting.util.SwallowingHttpServletResponse;
34 import org.directwebremoting.util.VersionUtil;
35
36 /**
37  * A default implementation of WebContext
38  * @author Joe Walker [joe at getahead dot ltd dot uk]
39  */

40 public class DefaultWebContext extends DefaultServerContext implements WebContext
41 {
42     /**
43      * Create a new DefaultWebContext
44      * @param request The incoming http request
45      * @param response The outgoing http reply
46      * @param config The servlet configuration
47      * @param context The servlet context
48      * @param container The IoC container
49      * @see org.directwebremoting.WebContextFactory.WebContextBuilder#set(HttpServletRequest, HttpServletResponse, ServletConfig, ServletContext, Container)
50      */

51     public DefaultWebContext(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ServletConfig JavaDoc config, ServletContext JavaDoc context, Container container)
52     {
53         super(config, context, container);
54
55         this.request = request;
56         this.response = response;
57     }
58
59     /* (non-Javadoc)
60      * @see org.directwebremoting.WebContext#setCurrentPageInformation(java.lang.String, java.lang.String)
61      */

62     public void setCurrentPageInformation(String JavaDoc page, String JavaDoc scriptSessionId)
63     {
64         this.scriptSessionId = scriptSessionId;
65         this.page = page;
66     }
67
68     /* (non-Javadoc)
69      * @see org.directwebremoting.WebContext#getCurrentPage()
70      */

71     public String JavaDoc getCurrentPage()
72     {
73         return page;
74     }
75
76     /* (non-Javadoc)
77      * @see org.directwebremoting.WebContext#getScriptSession()
78      */

79     public ScriptSession getScriptSession()
80     {
81         ScriptSessionManager manager = getScriptSessionManager();
82
83         RealScriptSession scriptSession = manager.getScriptSession(scriptSessionId);
84         manager.setPageForScriptSession(scriptSession, page);
85
86         return scriptSession;
87     }
88
89     /* (non-Javadoc)
90      * @see org.directwebremoting.WebContext#getSession()
91      */

92     public HttpSession JavaDoc getSession()
93     {
94         return request.getSession();
95     }
96
97     /* (non-Javadoc)
98      * @see org.directwebremoting.WebContext#getSession(boolean)
99      */

100     public HttpSession JavaDoc getSession(boolean create)
101     {
102         return request.getSession(create);
103     }
104
105     /* (non-Javadoc)
106      * @see org.directwebremoting.WebContext#getHttpServletRequest()
107      */

108     public HttpServletRequest JavaDoc getHttpServletRequest()
109     {
110         return request;
111     }
112
113     /* (non-Javadoc)
114      * @see org.directwebremoting.WebContext#getHttpServletResponse()
115      */

116     public HttpServletResponse JavaDoc getHttpServletResponse()
117     {
118         return response;
119     }
120
121     /* (non-Javadoc)
122      * @see org.directwebremoting.WebContext#forwardToString(java.lang.String)
123      */

124     public String JavaDoc forwardToString(String JavaDoc url) throws ServletException JavaDoc, IOException JavaDoc
125     {
126         StringWriter JavaDoc sout = new StringWriter JavaDoc();
127         StringBuffer JavaDoc buffer = sout.getBuffer();
128
129         HttpServletResponse JavaDoc realResponse = getHttpServletResponse();
130         HttpServletResponse JavaDoc fakeResponse = new SwallowingHttpServletResponse(realResponse, sout, realResponse.getCharacterEncoding());
131
132         HttpServletRequest JavaDoc realRequest = getHttpServletRequest();
133         realRequest.setAttribute(WebContext.ATTRIBUTE_DWR, Boolean.TRUE);
134
135         getServletContext().getRequestDispatcher(url).forward(realRequest, fakeResponse);
136
137         return buffer.toString();
138     }
139
140     /* (non-Javadoc)
141      * @see org.directwebremoting.WebContext#getVersion()
142      */

143     public String JavaDoc getVersion()
144     {
145         return VersionUtil.getVersion();
146     }
147
148     /**
149      * The unique ID (like a session ID) assigned to the current page
150      */

151     private String JavaDoc scriptSessionId = null;
152
153     /**
154      * The URL of the current page
155      */

156     private String JavaDoc page = null;
157
158     /**
159      * The HttpServletRequest associated with the current request
160      */

161     private HttpServletRequest JavaDoc request = null;
162
163     /**
164      * The HttpServletResponse associated with the current request
165      */

166     private HttpServletResponse JavaDoc response = null;
167 }
168
Popular Tags