KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
14  * (c) Copyright 2002 eInnovation Inc.
15  * All Rights Reserved.
16  */

17 package com.openedit.generators;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import com.openedit.Generator;
28 import com.openedit.OpenEditException;
29 import com.openedit.WebPageRequest;
30 import com.openedit.error.ContentNotAvailableException;
31 import com.openedit.page.Page;
32 import com.openedit.util.FileUtils;
33
34 /**
35  * This generator uses file steam to process a request for a page
36  *
37  * @author Chris Burkey
38  */

39 public class FileGenerator extends BaseGenerator implements Generator
40 {
41     private static Log log = LogFactory.getLog(FileGenerator.class);
42     
43     public void generate( WebPageRequest inContext, Page inPage, Output inOut ) throws OpenEditException
44     {
45         if( !inPage.exists() )
46         {
47             String JavaDoc vir = inPage.get("virtual");
48             if ( !Boolean.parseBoolean(vir) )
49             {
50                 log.info("Missing: " +inPage.getPath());
51                 throw new ContentNotAvailableException("Missing: " +inPage.getPath(),inPage.getPath());
52             }
53             else
54             {
55                 return; //do nothing
56
}
57         }
58         HttpServletResponse JavaDoc res = inContext.getResponse();
59         //only bother if we are the content page and not in development
60
if ( res != null && inContext.getContentPage() == inPage )
61         {
62             res.setDateHeader("Last-Modified",inPage.getLastModified().getTime());
63             long now = System.currentTimeMillis();
64             boolean cache = true;
65             String JavaDoc nocache = inContext.getRequestParameter("cache");
66             if( nocache != null )
67             {
68                 cache = Boolean.parseBoolean(nocache);
69             }
70             else
71             {
72                 //is this recenlty modified?
73
//3333333recent99 + 24 hours (mil * sec * min * hours) will be more than now
74
cache = inPage.getLastModified().getTime() + (1000 * 60 * 60 * 24 ) < now;
75             }
76             
77             if( cache )
78             {
79                 res.setDateHeader("Expires", now + (1000 * 60 * 60 * 24 )); //sec * min * hour * 48 Hours
80
}
81             else
82             {
83                 res.setDateHeader("Expires", now - (1000 * 60 * 60 * 24)); //expired 24 hours ago
84
}
85             //sometimes we can specify the length of the document
86
if ( inPage.isBinary() )
87             { //we can set the length unless there is a decorator on it somehow
88
int len = (int)inPage.getContentItem().getLength();
89                 if ( len != -1)
90                 {
91                     res.setContentLength(len);
92                 }
93             }
94         }
95         InputStream JavaDoc in = inPage.getInputStream();
96
97         try
98         {
99             if ( inPage.isBinary() )
100             {
101                 getOutputFiller().fill(in, inOut.getStream());
102             }
103             else
104             {
105                 InputStreamReader JavaDoc reader = null;
106                 if ( inPage.getCharacterEncoding() != null )
107                 {
108                     reader = new InputStreamReader JavaDoc( in, inPage.getCharacterEncoding() );
109                 }
110                 else
111                 {
112                     reader = new InputStreamReader JavaDoc( in );
113                 }
114                 getOutputFiller().fill(reader, inOut.getWriter());
115             }
116         }
117         catch ( Exception JavaDoc eof )
118         {
119             if( ignoreError(eof))
120             {
121                 //ignored
122
return;
123             }
124             throw new OpenEditException(eof);
125         }
126         finally
127         {
128             FileUtils.safeClose(in);
129         }
130         
131     }
132     public boolean canGenerate(WebPageRequest inReq)
133     {
134         return true;
135     }
136 }
137
Popular Tags