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.action;
006:
007: import org.apache.struts.action.ActionForm;
008: import org.apache.struts.action.ActionForward;
009: import org.apache.struts.action.ActionMapping;
010: import org.vfny.geoserver.form.DemoRequestForm;
011: import org.vfny.geoserver.util.Requests;
012: import java.io.BufferedReader;
013: import java.io.File;
014: import java.io.FileReader;
015: import java.io.IOException;
016: import javax.servlet.ServletException;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019:
020: /**
021: * <b>DemoRequestAction</b><br>
022: * Oct 7, 2005<br>
023: *
024: * <b>Purpose:</b><br>
025: * This used to be DemoAction. We changed DemoAction to be a bunch of links, and leave
026: * it as an action in case we want to do something fancier with it later.
027: *
028: * This Action handles all the buttons for the Demo.jsp page.
029: *
030: * <p>
031: * This one is more complicated then usual since not all the actions require
032: * the form bean to be validated! I am going to have to hack a little bit to
033: * make that happen, I may end up making the form bean validation differ
034: * depending on the selected action.
035: * </p>
036: *
037: * <p>
038: * Buttons that make this action go:
039: *
040: * <ul>
041: * <li>
042: * Submit: submit the request specified by url and post fields (Should be done
043: * using Javascript locally)
044: * </li>
045: * <li>
046: * Change: select between the precanned demos
047: * </li>
048: * </ul>
049: *
050: * As usual we will have to uninternationlize the action name provided to us.
051: * </p>
052: *
053: * @author Richard Gould
054: * @author Jody Garnett
055: * @author Brent Owens (The Open Planning Project)
056: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
057: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
058: *
059: * @version
060: */
061: public class DemoRequestAction extends GeoServerAction {
062: public ActionForward execute(ActionMapping mapping,
063: ActionForm form, HttpServletRequest request,
064: HttpServletResponse response) throws IOException,
065: ServletException {
066: DemoRequestForm demoForm = (DemoRequestForm) form;
067:
068: File dir = demoForm.getDir();
069: String demo = demoForm.getDemo();
070: String baseUrl = Requests.getBaseUrl(request, getGeoServer());
071:
072: if (demo == null) {
073: demo = "";
074: }
075:
076: if (demo.equals("")) {
077: demoForm.setUrl(baseUrl);
078: demoForm.setBody("");
079: }
080:
081: if (demo.indexOf('/') != -1 || demo.indexOf('\\') != -1)
082: throw new IllegalArgumentException("Invalid path " + demo);
083:
084: String service = demo.substring(0, demo.indexOf('_'))
085: .toLowerCase();
086: String url = Requests.getBaseUrl(request, getGeoServer())
087: + service;
088:
089: File file = new File(dir, demo);
090: BufferedReader reader = new BufferedReader(new FileReader(file));
091:
092: StringBuffer buf = new StringBuffer();
093:
094: for (String line = reader.readLine(); line != null; line = reader
095: .readLine()) {
096: buf.append(line);
097: buf.append("\n");
098: }
099:
100: if (demo.endsWith(".url")) {
101: demoForm.setUrl(baseUrl + buf.toString());
102: demoForm.setBody("");
103: } else { //demo.endsWith(.xml), but not yet for backwards compatibility.
104: demoForm.setUrl(url);
105: demoForm.setBody(buf.toString());
106: }
107:
108: // return back to the admin demo
109: //
110: return mapping.findForward("welcome.demoRequest");
111: }
112: }
|