KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > generators > FilterGenerator


1 /*
2  Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4  This library is free software; you can redistribute it and/or modify it under the terms
5  of the GNU Lesser General Public License as published by the Free Software Foundation;
6  either version 2.1 of the License, or (at your option) any later version.
7
8  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  See the GNU Lesser General Public License for more details.
11  */

12
13 package com.openedit.generators;
14
15 import java.io.IOException JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17
18 import javax.servlet.FilterChain JavaDoc;
19 import javax.servlet.RequestDispatcher JavaDoc;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.ServletOutputStream JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.openedit.OpenEditException;
31 import com.openedit.WebPageRequest;
32 import com.openedit.page.Page;
33
34 /**
35  * This repository implements a JSP compiler using the {@link NamedDispatcher} class in the
36  * standard servlet API.
37  */

38 public class FilterGenerator extends BaseGenerator
39 {
40     protected static final Log log = LogFactory.getLog(FilterGenerator.class);
41
42     public void generate(WebPageRequest inContext, Page inPage, Output inOut)
43         throws OpenEditException
44     {
45         HttpServletRequest JavaDoc httpRequest = inContext.getRequest();
46         HttpServletResponse JavaDoc httpResponse = inContext.getResponse();
47         Page page = (Page) inContext.getPageValue("page");
48
49         //load the standard context objects
50
httpRequest.setAttribute("context", inContext);
51         httpRequest.setAttribute("pages", inContext.getPageStreamer());
52         httpRequest.setAttribute("content", inContext.getContentPage());
53
54         /* I commented this out since it conflics with existing JSP pages. Blojsom has its own "page" variable, get stuff from the context
55          for (Iterator iter = inContext.getPageMap().entrySet().iterator(); iter.hasNext();)
56          {
57          Map.Entry entry = (Map.Entry) iter.next();
58          String key = (String) entry.getKey();
59          httpRequest.setAttribute(key, inContext.getPageValue(key));
60          }
61          */

62
63         DecoratedServletResponse response = new DecoratedServletResponse(httpResponse,
64             new DecoratedServletOutputStream(inOut));
65
66         DecoratedServletRequest newrequest = new DecoratedServletRequest(httpRequest);
67
68         String JavaDoc requestPath = page.getContentItem().getActualPath();
69         newrequest.setrequestURI(requestPath);
70
71         try
72         {
73             //URLUtilities urls = (URLUtilities)inContext.getPageValue(PageRequestKeys.URL_UTILITIES);
74
if( requestPath.endsWith(".jsp") )
75             {
76                 //A dispatcherallow us to use the base directory and draft modes
77
RequestDispatcher JavaDoc jspDispatcher = httpRequest.getRequestDispatcher( requestPath );
78                 jspDispatcher.include(newrequest, response); //This only applies for jsp pages.
79
}
80             else
81             {
82                 FilterChain JavaDoc chain = (FilterChain JavaDoc) httpRequest.getAttribute("servletchain");
83                 chain.doFilter(newrequest, response); //This will capture output to our existing output class
84
}
85         }
86         catch (ServletException JavaDoc ex)
87         {
88             throw new OpenEditException(ex);
89         }
90         catch (IOException JavaDoc ex)
91         {
92             throw new OpenEditException(ex);
93         }
94     }
95
96     /**
97      * Stub implementation of HttpServletResponse
98      */

99     class DecoratedServletResponse extends HttpServletResponseWrapper JavaDoc
100     {
101         DecoratedServletOutputStream output;
102
103         public DecoratedServletResponse(HttpServletResponse JavaDoc response,
104             DecoratedServletOutputStream inOut)
105         {
106             super(response);
107             this.output = inOut;
108         }
109
110         //Keep this stream open since we want to keep feeding data to the stream
111
public boolean isCommitted()
112         {
113             return false;
114         }
115
116         public void setContentLength(int len)
117         {
118         }
119
120         public void setContentType(java.lang.String JavaDoc type)
121         {
122         }
123
124         //this is for text
125
public PrintWriter JavaDoc getWriter()
126         {
127             PrintWriter JavaDoc out = new PrintWriter JavaDoc(output.getOutput().getWriter())
128             {
129                 public void close()
130                 {
131                     //ignore closes
132
}
133             };
134             return out;
135         }
136
137         //This is binary
138
public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc
139         {
140             return output;
141         }
142
143         public void reset()
144         {
145         }
146
147         public void resetBuffer()
148         {
149         }
150
151     }
152
153     class DecoratedServletRequest extends HttpServletRequestWrapper JavaDoc
154     {
155         protected String JavaDoc fieldRequestURI;
156
157         public DecoratedServletRequest(HttpServletRequest JavaDoc inRequest)
158         {
159             super(inRequest);
160         }
161
162         public void setrequestURI(String JavaDoc inUri)
163         {
164             fieldRequestURI = inUri;
165         }
166         
167         public String JavaDoc getRequestURI()
168         {
169             if (fieldRequestURI != null)
170             {
171                 return fieldRequestURI;
172             }
173             return super.getRequestURI();
174         }
175         
176     }
177
178     class DecoratedServletOutputStream extends ServletOutputStream JavaDoc
179     {
180         Output output;
181
182         public DecoratedServletOutputStream(Output inOut)
183         {
184             this.output = inOut;
185         }
186
187         public void write(int b) throws java.io.IOException JavaDoc
188         {
189             // This method is not used but has to be implemented
190
//this.writer.write(b);
191
output.getStream().write(b);
192         }
193
194         public void write(byte b[]) throws IOException JavaDoc
195         {
196             write(b, 0, b.length);
197         }
198
199         public void write(byte b[], int off, int len) throws IOException JavaDoc
200         {
201             //System.out.println("writing...");
202
output.getStream().write(b, off, len);
203         }
204
205         public void close() throws IOException JavaDoc
206         {
207             //super.close();
208
}
209
210         public void flush() throws IOException JavaDoc
211         {
212             output.getStream().flush();
213         }
214
215         public Output getOutput()
216         {
217             return output;
218         }
219     }
220
221 }
222
Popular Tags