001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wfs.servlets;
006:
007: import org.apache.commons.codec.binary.Base64;
008: import java.io.BufferedReader;
009: import java.io.BufferedWriter;
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.io.OutputStream;
013: import java.io.OutputStreamWriter;
014: import java.io.PrintWriter;
015: import java.net.URL;
016: import java.net.URLDecoder;
017: import javax.servlet.ServletConfig;
018: import javax.servlet.ServletException;
019: import javax.servlet.http.HttpServlet;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: /**
024: * Simple tester for WFS post requests. Can be called two ways. If called with
025: * no parameters, it displays the form, otherwise it displays the result page.
026: *
027: * @author Doug Cates: Moxi Media Inc.
028: * @version 1.0
029: */
030: public class TestWfsPost extends HttpServlet {
031: /**
032: * Initializes the servlet.
033: *
034: * @param config DOCUMENT ME!
035: *
036: * @throws ServletException DOCUMENT ME!
037: */
038: public void init(ServletConfig config) throws ServletException {
039: super .init(config);
040: }
041:
042: /**
043: * Destroys the servlet.
044: */
045: public void destroy() {
046: }
047:
048: /**
049: * Handles the HTTP <code>GET</code> method.
050: *
051: * @param request servlet request
052: * @param response servlet response
053: *
054: * @throws ServletException DOCUMENT ME!
055: * @throws IOException DOCUMENT ME!
056: */
057: protected void doGet(HttpServletRequest request,
058: HttpServletResponse response) throws ServletException,
059: IOException {
060: processRequest(request, response);
061: }
062:
063: /**
064: * Handles the HTTP <code>POST</code> method.
065: *
066: * @param request servlet request
067: * @param response servlet response
068: *
069: * @throws ServletException DOCUMENT ME!
070: * @throws IOException DOCUMENT ME!
071: */
072: protected void doPost(HttpServletRequest request,
073: HttpServletResponse response) throws ServletException,
074: IOException {
075: processRequest(request, response);
076: }
077:
078: /**
079: * Returns a short description of the servlet.
080: *
081: * @return DOCUMENT ME!
082: */
083: public String getServletInfo() {
084: return "Tests a WFS post request using a form entry.";
085: }
086:
087: /**
088: * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
089: * methods.
090: *
091: * @param request servlet request
092: * @param response servlet response
093: *
094: * @throws ServletException DOCUMENT ME!
095: * @throws IOException DOCUMENT ME!
096: */
097: protected void processRequest(HttpServletRequest request,
098: HttpServletResponse response) throws ServletException,
099: IOException {
100: String requestString = request.getParameter("body");
101: String urlString = request.getParameter("url");
102: boolean doGet = (requestString == null)
103: || requestString.trim().equals("");
104:
105: if ((urlString == null)) {
106: PrintWriter out = response.getWriter();
107: StringBuffer urlInfo = request.getRequestURL();
108:
109: if (urlInfo.indexOf("?") != -1) {
110: urlInfo.delete(urlInfo.indexOf("?"), urlInfo.length());
111: }
112:
113: String geoserverUrl = urlInfo.substring(0, urlInfo.indexOf(
114: "/", 8))
115: + request.getContextPath();
116: response.setContentType("text/html");
117: out
118: .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
119: out.println("<html>");
120: out.println("<head>");
121: out.println("<title>TestWfsPost</title>");
122: out.println("</head>");
123: out.println("<script language=\"JavaScript\">");
124: out.println("function doNothing() {");
125: out.println("}");
126: out.println("function sendRequest() {");
127: out.println(" if (checkURL()==true) {");
128: out.print(" document.frm.action = \"");
129: out.print(urlInfo.toString());
130: out.print("\";\n");
131: out.println(" document.frm.target = \"_blank\";");
132: out.println(" document.frm.submit();");
133: out.println(" }");
134: out.println("}");
135: out.println("function checkURL() {");
136: out.println(" if (document.frm.url.value==\"\") {");
137: out
138: .println(" alert(\"Please give URL before you sumbit this form!\");");
139: out.println(" return false;");
140: out.println(" } else {");
141: out.println(" return true;");
142: out.println(" }");
143: out.println("}");
144: out.println("function clearRequest() {");
145: out.println("document.frm.body.value = \"\";");
146: out.println("}");
147: out.println("</script>");
148: out.println("<body>");
149: out
150: .println("<form name=\"frm\" action=\"JavaScript:doNothing()\" method=\"POST\">");
151: out
152: .println("<table align=\"center\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">");
153: out.println("<tr>");
154: out.println("<td><b>URL:</b></td>");
155: out.print("<td><input name=\"url\" value=\"");
156: out.print(geoserverUrl);
157: out
158: .print("/wfs/GetFeature\" size=\"70\" MAXLENGTH=\"100\"/></td>\n");
159: out.println("</tr>");
160: out.println("<tr>");
161: out.println("<td><b>Request:</b></td>");
162: out
163: .println("<td><textarea cols=\"60\" rows=\"24\" name=\"body\"></textarea></td>");
164: out.println("</tr>");
165: out.println("</table>");
166: out.println("<table align=\"center\">");
167: out.println("<tr>");
168: out
169: .println("<td><input type=\"button\" value=\"Clear\" onclick=\"clearRequest()\"></td>");
170: out
171: .println("<td><input type=\"button\" value=\"Submit\" onclick=\"sendRequest()\"></td>");
172: out.println("<td></td>");
173: out.println("</tr>");
174: out.println("</table>");
175: out.println("</form>");
176: out.println("</body>");
177: out.println("</html>");
178: out.close();
179: } else {
180: response.setContentType("application/xml");
181:
182: BufferedReader xmlIn = null;
183: PrintWriter xmlOut = null;
184: StringBuffer sbf = new StringBuffer();
185: String resp = null;
186:
187: try {
188: URL u = new URL(urlString);
189: java.net.HttpURLConnection acon = (java.net.HttpURLConnection) u
190: .openConnection();
191: acon.setAllowUserInteraction(false);
192:
193: if (!doGet) {
194: //System.out.println("set to post");
195: acon.setRequestMethod("POST");
196: acon.setRequestProperty("Content-Type",
197: "application/xml");
198: } else {
199: //System.out.println("set to get");
200: acon.setRequestMethod("GET");
201: }
202:
203: acon.setDoOutput(true);
204: acon.setDoInput(true);
205: acon.setUseCaches(false);
206:
207: //SISfixed - if there was authentication info in the request,
208: // Pass it along the way to the target URL
209: //DJB: applied patch in GEOS-335
210: String authHeader = request.getHeader("Authorization");
211:
212: String username = request.getParameter("username");
213:
214: if ((username != null) && !username.trim().equals("")) {
215: String password = request.getParameter("password");
216: String up = username + ":" + password;
217: byte[] encoded = Base64.encodeBase64(up.getBytes());
218: authHeader = "Basic " + new String(encoded);
219: }
220:
221: if (authHeader != null) {
222: acon
223: .setRequestProperty("Authorization",
224: authHeader);
225: }
226:
227: if (!doGet) {
228: xmlOut = new PrintWriter(new BufferedWriter(
229: new OutputStreamWriter(acon
230: .getOutputStream())));
231: xmlOut = new java.io.PrintWriter(acon
232: .getOutputStream());
233:
234: xmlOut.write(requestString);
235: xmlOut.flush();
236: }
237:
238: // Above 400 they're all error codes, see:
239: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
240: if (acon.getResponseCode() >= 400) {
241: PrintWriter out = response.getWriter();
242: out
243: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
244: out.println("<servlet-exception>");
245: out.println("HTTP response: "
246: + acon.getResponseCode()
247: + "\n"
248: + URLDecoder.decode(acon
249: .getResponseMessage(), "UTF-8"));
250: out.println("</servlet-exception>");
251: out.close();
252: } else {
253: // xmlIn = new BufferedReader(new InputStreamReader(
254: // acon.getInputStream()));
255: // String line;
256:
257: // System.out.println("got encoding from acon: "
258: // + acon.getContentType());
259: response.setContentType(acon.getContentType());
260: response.setHeader("Content-disposition", acon
261: .getHeaderField("Content-disposition"));
262:
263: OutputStream output = response.getOutputStream();
264: int c;
265: InputStream in = acon.getInputStream();
266:
267: while ((c = in.read()) != -1)
268: output.write(c);
269:
270: in.close();
271: output.close();
272: }
273:
274: //while ((line = xmlIn.readLine()) != null) {
275: // out.print(line);
276: //}
277: } catch (Exception e) {
278: PrintWriter out = response.getWriter();
279: out
280: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
281: out.println("<servlet-exception>");
282: out.println(e.toString());
283: out.println("</servlet-exception>");
284: out.close();
285: } finally {
286: try {
287: if (xmlIn != null) {
288: xmlIn.close();
289: }
290: } catch (Exception e1) {
291: PrintWriter out = response.getWriter();
292: out
293: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
294: out.println("<servlet-exception>");
295: out.println(e1.toString());
296: out.println("</servlet-exception>");
297: out.close();
298: }
299:
300: try {
301: if (xmlOut != null) {
302: xmlOut.close();
303: }
304: } catch (Exception e2) {
305: PrintWriter out = response.getWriter();
306: out
307: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
308: out.println("<servlet-exception>");
309: out.println(e2.toString());
310: out.println("</servlet-exception>");
311: out.close();
312: }
313: }
314: }
315: }
316: }
|