Source code: kelp/webapp/presentation/FormServlet.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22 package kelp.webapp.presentation;
23
24 // Xerces imports
25 import org.w3c.dom.html.HTMLInputElement;
26
27 // Servlet imports
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 // Standard imports
35 import java.io.IOException;
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.FileOutputStream;
39 import java.util.Properties;
40
41 /**
42 * <p>
43 * This presentation object dynamically creates an HTML page
44 * showing the greeting from the application configuration file.
45 * </p>
46 */
47 public class FormServlet extends HttpServlet {
48 public void doPost(HttpServletRequest request,
49 HttpServletResponse response) throws ServletException,
50 IOException {
51 doGet(request, response);
52 }
53
54 public void doGet(HttpServletRequest request,
55 HttpServletResponse response) throws ServletException,
56 IOException {
57 FormHTML page = null;
58 ServletOutputStream out;
59 Properties data = null;
60 byte[] buffer;
61
62 data = initData(request, data);
63 data = saveParameters(request, data);
64 page = createPage(data);
65 buffer = page.toDocument().getBytes();
66 response.setContentType( "text/html" );
67 response.setContentLength( buffer.length );
68 out = response.getOutputStream();
69 out.write(buffer);
70 out.flush();
71 response.flushBuffer();
72 }
73
74 private Properties saveParameters(HttpServletRequest request,
75 Properties data) {
76 String paramFirst = null;
77 String paramLast = null;
78
79 try {
80 paramFirst = request.getParameter("first");
81 paramLast = request.getParameter("last");
82 } catch (Exception e) {
83 paramFirst = null;
84 paramLast = null;
85 }
86 if (paramFirst != null && paramLast != null) {
87 data.put("first", paramFirst);
88 data.put("last", paramLast);
89 writePropertyFile(data);
90 }
91 return data;
92 }
93
94 public FormHTML createPage(Properties data) {
95 String first = null;
96 String last = null;
97 HTMLInputElement inputFirst = null;
98 HTMLInputElement inputLast = null;
99 FormHTML page = null;
100
101 page = new FormHTML();
102 first = (String) data.get("first");
103 last = (String) data.get("last");
104 inputFirst = page.getElementFirst();
105 inputLast = page.getElementLast();
106 inputFirst.setValue(first);
107 inputLast.setValue(last);
108 return page;
109 }
110
111 private void writePropertyFile(Properties data) {
112 FileOutputStream out = null;
113 String dataPath = null;
114
115 dataPath = getDataPath();
116 try {
117 out = new FileOutputStream(dataPath, false);
118 data.store(out, "Data for Form page");
119 out.close();
120 } catch (java.io.IOException e) {
121 e.printStackTrace();
122 }
123 }
124
125 private Properties readPropertyFile(Properties data) {
126 FileInputStream is = null;
127 String dataPath = null;
128
129 dataPath = getDataPath();
130 try {
131 is = new FileInputStream(dataPath);
132 data = new Properties();
133 data.load(is);
134 is.close();
135 } catch (Exception e) {
136
137 // create deault
138 e.printStackTrace();
139 data = new Properties();
140 data.put("first", "Luke");
141 data.put("last", "Skywalker");
142 }
143 return data;
144 }
145
146 private Properties initData(HttpServletRequest request, Properties data) {
147 data = (Properties) request.getSession().getAttribute("data");
148 if (data == null) {
149 data = readPropertyFile(data);
150 }
151 return data;
152 }
153
154 private String getDataPath() {
155 StringBuffer pathBuf = new StringBuffer();
156 String testPath = getInitParameter("testDataPath");
157
158 if (testPath == null) {
159 System.out.println("init-parm not found: testDataPath");
160 } else {
161 pathBuf.append(testPath);
162
163 // Remove quotes
164 int lastAt = -1;
165
166 lastAt = pathBuf.length() - 1;
167 if (pathBuf.charAt(lastAt) == '"') {
168 pathBuf.deleteCharAt(lastAt);
169 }
170 if (pathBuf.charAt(0) == '"') {
171 pathBuf.deleteCharAt(0);
172 }
173 pathBuf.append('/');
174 pathBuf.append("form.properties");
175 }
176 return pathBuf.toString();
177 }
178
179 }