001: /*
002: * EncodingSampleApp
003: *
004: * Enhydra super-servlet presentation object
005: *
006: */
007:
008: package org.enhydra.encodingsample.presentation;
009:
010: // Enhydra SuperServlet imports
011: import com.lutris.appserver.server.httpPresentation.HttpPresentation;
012: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
013: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
014: import com.lutris.mime.ContentHeader;
015: import com.lutris.mime.MimeException;
016: import com.lutris.mime.MimeHeader;
017: import com.lutris.mime.MultipartMimeInput;
018: import com.lutris.mime.MultipartMimeInputStream;
019:
020: // Standard imports
021: import java.io.IOException;
022:
023: import org.enhydra.encodingsample.EncodingSampleApp;
024: import org.enhydra.encodingsample.EncodingSampleSessionData;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.html.HTMLFormElement;
027: import org.w3c.dom.html.HTMLOptionElement;
028: import org.w3c.dom.html.HTMLSelectElement;
029: import org.w3c.dom.html.HTMLTableElement;
030: import org.w3c.dom.html.HTMLTableRowElement;
031:
032: public class EncodingPresentation implements HttpPresentation {
033: private EncodingSampleSessionData sessionData = null;
034:
035: public void run(HttpPresentationComms comms)
036: throws HttpPresentationException, IOException {
037:
038: initSessionData(comms);
039:
040: String[] encodings = EncodingSampleApp.encodings;
041:
042: EncodeHTML encodePage;
043: encodePage = (EncodeHTML) comms.xmlcFactory
044: .create(EncodeHTML.class);
045:
046: if (sessionData.getEncoding() != null) {
047: comms.request.getHttpServletRequest().setCharacterEncoding(
048: sessionData.getEncoding());
049: }
050:
051: String requestMethod = null;
052: String requestRedirect = null;
053: String requestContentType = null;
054: String requestEncoding = sessionData.getEncoding();
055: String textParam = null;
056: String requestFileEncoding = sessionData.getFileEncoding();
057: String event = null;
058:
059: String filePath = null;
060: String fileContent = null;
061:
062: // PARAMETER INITIALIZATION
063: if ("POST".equalsIgnoreCase(comms.request.getMethod())
064: && !"application/x-www-form-urlencoded"
065: .equalsIgnoreCase(comms.request
066: .getContentType())) {
067:
068: String CONTENT_TYPE = "Content-Type";
069: String CONTENT_DISPOSITION = "Content-Disposition";
070: try {
071: String header = comms.request.getHeader(CONTENT_TYPE);
072: if (header != null && !"".equals(header)) {
073: ContentHeader contentHeader = new ContentHeader(
074: header);
075: MultipartMimeInput mInput = new MultipartMimeInput(
076: comms.request.getInputStream(),
077: contentHeader);
078:
079: MultipartMimeInputStream mime = mInput
080: .nextPart(requestEncoding);
081: while (mime != null) {
082: MimeHeader mh = mime
083: .getHeader(CONTENT_DISPOSITION);
084: String paramName = mh.getParameter("name");
085: if ("uploadFile".equals(paramName)) {
086: filePath = mh.getParameter("filename");
087: fileContent = getString(mime,
088: requestFileEncoding);
089: } else if ("requestMethod".equals(paramName)) {
090: requestMethod = getString(mime,
091: requestEncoding);
092: } else if ("requestRedirect".equals(paramName)) {
093: requestRedirect = getString(mime,
094: requestEncoding);
095: } else if ("requestContentType"
096: .equals(paramName)) {
097: requestContentType = getString(mime,
098: requestEncoding);
099: } else if ("requestEncoding".equals(paramName)) {
100: requestEncoding = getString(mime,
101: requestEncoding);
102: if (requestEncoding != null
103: && !"".equals(requestEncoding)
104: && !"null".equals(requestEncoding)) {
105: sessionData
106: .setEncoding(requestEncoding);
107: } else {
108: requestEncoding = sessionData
109: .getEncoding();
110: }
111: } else if ("textParam".equals(paramName)) {
112: textParam = getString(mime, requestEncoding);
113: } else if ("requestFileEncoding"
114: .equals(paramName)) {
115: requestFileEncoding = getString(mime,
116: requestEncoding);
117: if (requestFileEncoding != null
118: && !"".equals(requestFileEncoding)
119: && !"null"
120: .equals(requestFileEncoding)) {
121: sessionData
122: .setFileEncoding(requestFileEncoding);
123: } else {
124: requestFileEncoding = sessionData
125: .getFileEncoding();
126: }
127: } else if ("event".equals(paramName)) {
128: event = getString(mime, requestEncoding);
129: }
130: mime = mInput.nextPart(requestEncoding);
131: }
132: }
133: } catch (MimeException mexc) {
134: mexc.printStackTrace();
135: }
136: } else {
137: try {
138: requestMethod = comms.request
139: .getParameter("requestMethod");
140: requestRedirect = comms.request
141: .getParameter("requestRedirect");
142: requestContentType = comms.request
143: .getParameter("requestContentType");
144: requestEncoding = comms.request
145: .getParameter("requestEncoding");
146: if (requestEncoding != null
147: && !"".equals(requestEncoding)
148: && !"null".equals(requestEncoding)) {
149: sessionData.setEncoding(requestEncoding);
150: } else {
151: requestEncoding = sessionData.getEncoding();
152: }
153: textParam = comms.request.getParameter("textParam");
154: requestFileEncoding = comms.request
155: .getParameter("requestFileEncoding");
156: if (requestFileEncoding != null
157: && !"".equals(requestFileEncoding)
158: && !"null".equals(requestFileEncoding)) {
159: sessionData.setFileEncoding(requestFileEncoding);
160: } else {
161: requestFileEncoding = sessionData.getFileEncoding();
162: }
163: event = comms.request.getParameter("event");
164: } catch (HttpPresentationException ex) {
165: requestMethod = null;
166: requestRedirect = null;
167: requestContentType = null;
168: requestEncoding = null;
169: textParam = null;
170: requestFileEncoding = null;
171: event = null;
172: ex.printStackTrace();
173: }
174: }
175:
176: // REQUEST TABLE PREPARATIONS
177: HTMLOptionElement encodingOption = encodePage
178: .getElementRequestEncodingOption();
179: HTMLOptionElement fileEncodingOption = encodePage
180: .getElementRequestFileEncodingOption();
181:
182: for (int i = 0; i < encodings.length; i++) {
183: HTMLOptionElement clonedOption = (HTMLOptionElement) encodingOption
184: .cloneNode(true);
185: HTMLOptionElement clonedFileOption = (HTMLOptionElement) encodingOption
186: .cloneNode(true);
187:
188: Node optionTextNode = clonedOption.getOwnerDocument()
189: .createTextNode(encodings[i]);
190:
191: clonedOption.appendChild(optionTextNode.cloneNode(true));
192: clonedOption.setValue(encodings[i]);
193:
194: clonedFileOption.appendChild(optionTextNode);
195: clonedFileOption.setValue(encodings[i]);
196:
197: // clonedOption.setSelected(encodings[i].equals(requestEncoding));
198: if (encodings[i].equals(requestEncoding))
199: clonedOption.setAttribute("selected", "selected");
200:
201: encodingOption.getParentNode().appendChild(clonedOption);
202:
203: // clonedFileOption.setSelected(encodings[i].equals(requestFileEncoding));
204: if (encodings[i].equals(requestFileEncoding))
205: clonedFileOption.setAttribute("selected", "selected");
206:
207: fileEncodingOption.getParentNode().appendChild(
208: clonedFileOption);
209: }
210: encodingOption.getParentNode().removeChild(encodingOption);
211: fileEncodingOption.getParentNode().removeChild(
212: fileEncodingOption);
213:
214: HTMLFormElement requestForm = encodePage
215: .getElementRequestForm();
216:
217: HTMLSelectElement redirectSelection = encodePage
218: .getElementRequestRedirect();
219: if ("SERVER".equals(requestRedirect)) {
220: redirectSelection.setSelectedIndex(2);
221: requestForm.setAction("ServerRedirectPresentation.po");
222: } else if ("CLIENT".equals(requestRedirect)) {
223: redirectSelection.setSelectedIndex(1);
224: requestForm.setAction("ClientRedirectPresentation.po");
225: } else {
226: redirectSelection.setSelectedIndex(0);
227: }
228:
229: // DISABLE CLIENT REDIRECTION IF POST IS REQUESTED METHOD
230: if ("POST".equals(requestMethod)) {
231: HTMLOptionElement clientRedirectOption = encodePage
232: .getElementClientRedirectOption();
233: clientRedirectOption.getParentNode().removeChild(
234: clientRedirectOption);
235: }
236:
237: if ("POST".equals(requestMethod)) {
238: // POST
239: requestForm.setMethod("POST");
240:
241: HTMLSelectElement methodSelection = encodePage
242: .getElementRequestMethod();
243: methodSelection.setSelectedIndex(1);
244:
245: if ("multipart".equals(requestContentType)) {
246: requestForm.setEnctype("multipart/form-data");
247:
248: HTMLSelectElement contentSelection = encodePage
249: .getElementRequestContentType();
250: contentSelection.setSelectedIndex(1);
251:
252: HTMLTableRowElement tr = encodePage
253: .getElementParamRow();
254: tr.getParentNode().removeChild(tr);
255: } else {
256: requestForm
257: .setEnctype("application/x-www-form-urlencoded");
258:
259: HTMLTableRowElement tr = encodePage
260: .getElementFileEncodingRow();
261: tr.getParentNode().removeChild(tr);
262: tr = encodePage.getElementFileRow();
263: tr.getParentNode().removeChild(tr);
264: }
265: } else {
266: // GET
267: requestForm.setMethod("GET");
268:
269: HTMLSelectElement methodSelection = encodePage
270: .getElementRequestMethod();
271: methodSelection.setSelectedIndex(0);
272:
273: HTMLTableRowElement tr = encodePage
274: .getElementContentTypeRow();
275: tr.getParentNode().removeChild(tr);
276: tr = encodePage.getElementFileEncodingRow();
277: tr.getParentNode().removeChild(tr);
278: tr = encodePage.getElementFileRow();
279: tr.getParentNode().removeChild(tr);
280:
281: }
282:
283: // RESULT TABLE PREPARATIONS
284: if ("submit".equals(event)) {
285: if (filePath != null && fileContent != null) {
286: setFileDisplay(encodePage, filePath, requestEncoding,
287: fileContent, requestFileEncoding);
288: } else if (textParam != null) {
289: setParameterDisplay(encodePage, textParam,
290: requestEncoding);
291: } else {
292: setDefaultDisplay(encodePage);
293: }
294: } else {
295: setDefaultDisplay(encodePage);
296: }
297:
298: if (sessionData.getEncoding() != null) {
299: comms.response.setEncoding(sessionData.getEncoding());
300: }
301:
302: comms.response.writeDOM(encodePage);
303: }
304:
305: private String getString(MultipartMimeInputStream part,
306: String ecoding) throws IOException {
307: byte[] buf = new byte[1024];
308: StringBuffer res = new StringBuffer(1024);
309: int b = 0;
310: while ((b = part.read(buf, 0, buf.length)) != -1) {
311: try {
312: res.append(new String(buf, 0, b, ecoding));
313: } catch (Exception exc) {
314: res.append(new String(buf, 0, b));
315: }
316: }
317: return res.toString();
318: }
319:
320: private void setParameterDisplay(EncodeHTML encodePage,
321: String parameter, String encoding) {
322: encodePage.setTextParamVal(parameter);
323: encodePage.setTextEncodingVal(encoding);
324:
325: HTMLTableRowElement contentRow = encodePage
326: .getElementResultContentRow();
327: contentRow.getParentNode().removeChild(contentRow);
328:
329: HTMLTableRowElement fileRow = encodePage
330: .getElementResultFileRow();
331: fileRow.getParentNode().removeChild(fileRow);
332:
333: HTMLTableRowElement fileEncodingRow = encodePage
334: .getElementResultFileEncodingRow();
335: fileEncodingRow.getParentNode().removeChild(fileEncodingRow);
336: }
337:
338: private void setFileDisplay(EncodeHTML encodePage, String file,
339: String encoding, String content, String fileEncoding) {
340: encodePage.setTextFilePathVal(file);
341: encodePage.setTextFileContentVal(content);
342: encodePage.setTextEncodingVal(encoding);
343: encodePage.setTextFileEncodingVal(fileEncoding);
344: HTMLTableRowElement paramRow = encodePage
345: .getElementResultParamRow();
346: paramRow.getParentNode().removeChild(paramRow);
347: }
348:
349: private void setDefaultDisplay(EncodeHTML encodePage) {
350: HTMLTableElement table = encodePage.getElementResultTable();
351: table.getParentNode().removeChild(table);
352: table = encodePage.getElementResultHead();
353: table.getParentNode().removeChild(table);
354: }
355:
356: /**
357: * Method to get or create the SessionData object from the user session
358: *
359: * @param comms
360: * HttpPresentationComms
361: */
362: protected void initSessionData(HttpPresentationComms comms) {
363: try {
364: Object obj = comms.sessionData
365: .get(EncodingSampleSessionData.SESSION_KEY);
366: // If we found the session data, save it in a private data member
367: if (null != obj) {
368: this .sessionData = (EncodingSampleSessionData) obj;
369: } else {
370: // If no session data was found, create a new session data
371: // instance
372: this .sessionData = new EncodingSampleSessionData();
373: comms.sessionData.set(
374: EncodingSampleSessionData.SESSION_KEY,
375: this .sessionData);
376: }
377: } catch (Exception ex) {
378: ex.printStackTrace();
379: this .sessionData = new EncodingSampleSessionData();
380: }
381: }
382:
383: }
|