001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.platforms.servers.instances.tomcat;
009:
010: //base classes
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.io.OutputStream;
014: import java.io.PrintWriter;
015: import javax.servlet.RequestDispatcher;
016: import javax.servlet.ServletContext;
017: import javax.servlet.ServletException;
018: import javax.servlet.http.HttpServlet;
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021: import javax.servlet.http.HttpSession;
022: import javax.servlet.jsp.PageContext;
023: import javax.xml.parsers.ParserConfigurationException;
024: import org.xml.sax.SAXException;
025:
026: //project specific classes
027: import org.jfolder.common.StandardDataTypes;
028: import org.jfolder.common.UnexpectedSystemException;
029: import org.jfolder.common.files.WebPageTagMacro;
030: import org.jfolder.common.files.WebPageTagMacroHelper;
031: import org.jfolder.common.function.BaseWebPageTag;
032: import org.jfolder.common.function.ReturnValueAndClassForWebPageTagContext;
033: import org.jfolder.common.function.WebPageTagPreferences;
034: import org.jfolder.common.tagging.ConceptTagCharacteristic;
035: import org.jfolder.common.tagging.ConceptTagCharacteristicHolder;
036: import org.jfolder.common.tagging.ConceptTagFlagsHelper;
037: import org.jfolder.common.tagging.ConceptTagHelper;
038: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagContext;
039: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagException;
040: import org.jfolder.common.tagging.ReturnableConceptTag;
041: import org.jfolder.common.tagging.RootConceptTagHolder;
042: import org.jfolder.common.tagging.ValueAndClassForConceptTag;
043: import org.jfolder.common.utils.misc.MiscHelper;
044: import org.jfolder.common.utils.web.WebComponentHolder;
045: import org.jfolder.platforms.stores.base.ApplicationDataSourceResolver;
046: import org.jfolder.platforms.stores.base.SystemStoreFactory;
047: import org.jfolder.platforms.stores.base.SystemStoreSessionManager;
048: import org.jfolder.platforms.stores.base.SystemStoreSessionWithinWebPage;
049: import org.jfolder.web.page.BinaryOutputFromWebPageTag;
050: import org.jfolder.web.page.RootWebPageTag;
051:
052: //other classes
053:
054: public class TomcatWebPageBinaryServlet extends HttpServlet {
055:
056: public TomcatWebPageBinaryServlet() {
057: }
058:
059: public void service(HttpServletRequest inRequest,
060: HttpServletResponse inResponse) throws IOException,
061: ServletException {
062:
063: //PrintWriter pw = inResponse.getWriter();
064: //pw.println("<html><body>TomcatWebPageServlet</body></html>");
065: //pw.flush();
066: //pw.close();
067:
068: //declare session
069: HttpSession hs = inRequest.getSession();
070:
071: //application
072: String destination = inRequest.getServletPath();
073: //if (destination.startsWith("/")) {
074: // destination = destination.substring(1);
075: //}
076: if (destination.endsWith("/")) {
077: //
078: destination = destination.substring(0,
079: destination.length() - 1);
080: }
081: //destination
082: //String servletPath = inRequest.getContextPath();
083: //if (servletPath.startsWith("/")) {
084: // servletPath = servletPath.substring(1);
085: //}
086: //if (servletPath.endsWith("/")) {
087: // servletPath =
088: // servletPath.substring(0, servletPath.length() - 1);
089: //}
090:
091: //MiscHelper.println("About to dispatch to parent.jsp");
092: //inResponse.setContentType("text/html");
093: //
094: //String destinationPage =
095: // (new TomcatServer()).getRelativeWebPageLocation(destination);
096: RootWebPageTag localRwpt = getBinaryRcth(destination,
097: getServletContext());
098: //
099: ConceptTagCharacteristicHolder rootCtcharh = localRwpt
100: .getFirstChild();
101: WebPageTagPreferences rootWptp = (WebPageTagPreferences) rootCtcharh
102: .getPreferences();
103: BinaryOutputFromWebPageTag rootBofwpt = ((BinaryOutputFromWebPageTag) rootCtcharh
104: .getConceptTag());
105: //
106: WebComponentHolder rootWch = WebComponentHolder.newInstance(
107: getServletContext(), inRequest, inResponse, inRequest
108: .getSession());
109: DetermineValueAndClassConceptTagContext dvacctc = ReturnValueAndClassForWebPageTagContext
110: .newInstance(rootWptp, rootWch);
111: //
112: process(rootBofwpt, inResponse, dvacctc);
113: //
114: //RequestDispatcher rd =
115: // inRequest.getRequestDispatcher(destinationPage);
116: //rd.include(inRequest, inResponse);
117: //MiscHelper.println("Finished to dispatch to parent.jsp");
118: }
119:
120: //
121: private final static void process(
122: BinaryOutputFromWebPageTag inBofwpt,
123: HttpServletResponse inResponse,
124: DetermineValueAndClassConceptTagContext inDvacctc)
125: throws IOException {
126: //
127: try {
128: //
129: String mimeType = inBofwpt.getMimeType();
130: inResponse.setContentType(mimeType);
131: //
132: byte output[] = inBofwpt.determineBinaryOutput(inDvacctc);
133: //
134: OutputStream os = inResponse.getOutputStream();
135: os.write(output);
136: os.flush();
137: os.close();
138: //
139:
140: } catch (DetermineValueAndClassConceptTagException inDvaccte) {
141: MiscHelper.writeMessage(TomcatWebPageBinaryServlet.class,
142: inDvaccte);
143: throw new UnexpectedSystemException(inDvaccte);
144: }
145: }
146:
147: //
148: //
149: //
150: //
151: public final static RootWebPageTag getBinaryRcth(String inPath,
152: ServletContext inSc) {
153:
154: try {
155: //
156: RootWebPageTag outValue = null;
157: //
158: //
159: //ServletContext sc = pc.getServletContext();
160:
161: ApplicationDataSourceResolver adsr = SystemStoreFactory
162: .getApplicationDataSourceResolver();
163: String macroLocation = adsr
164: .getActualWebTemplateLocation(inPath);
165: //
166: //
167: InputStream is = inSc.getResourceAsStream(macroLocation);
168: byte webMacroByteContent[] = MiscHelper
169: .fromInputStreamToBytesWithoutClose(is);
170: String webMacroContent = MiscHelper
171: .fromBytesToString(webMacroByteContent);
172: is.close();
173: //
174: RootConceptTagHolder localRcth = BaseWebPageTag
175: .getCachedRcth(
176: webMacroContent,
177: WebPageTagPreferences.DEFAULT_ROOT_CONCEPT_TAG_CLASS_NAME);
178: Object o = (localRcth.getFirstChildConceptTag());
179: //
180: //
181: outValue = ((RootWebPageTag) o);
182: //
183: //
184: return outValue;
185: }
186: //catch (DetermineValueAndClassConceptTagException dvaccte) {
187: // inDvacctc.setDetermineException(dvaccte);
188: //}
189: catch (IOException ioe) {
190: throw new UnexpectedSystemException(ioe);
191: } catch (ParserConfigurationException pce) {
192: throw new UnexpectedSystemException(pce);
193: } catch (SAXException saxe) {
194: throw new UnexpectedSystemException(saxe);
195: }
196: }
197: }
|