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.geotools.validation.dto.PlugInDTO;
011: import org.geotools.validation.dto.TestSuiteDTO;
012: import org.geotools.validation.xml.XMLWriter;
013: import org.vfny.geoserver.global.ConfigurationException;
014: import org.vfny.geoserver.global.GeoserverDataDirectory;
015: import org.vfny.geoserver.global.UserContainer;
016: import org.vfny.geoserver.global.dto.DataDTO;
017: import org.vfny.geoserver.global.dto.GeoServerDTO;
018: import org.vfny.geoserver.global.dto.WCSDTO;
019: import org.vfny.geoserver.global.dto.WFSDTO;
020: import org.vfny.geoserver.global.dto.WMSDTO;
021: import org.vfny.geoserver.global.xml.XMLConfigWriter;
022: import org.vfny.geoserver.global.xml.XMLConfigWriter.WriterUtils;
023: import java.io.File;
024: import java.io.FileWriter;
025: import java.io.IOException;
026: import java.util.Iterator;
027: import java.util.Map;
028: import javax.servlet.ServletContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: /**
034: * Save GeoServer state to XML.
035: *
036: * <p>
037: * This is a propert ConfigAction - you need to be logged in for this to work.
038: * </p>
039: */
040: public class SaveXMLAction extends ConfigAction {
041: public ActionForward execute(ActionMapping mapping,
042: ActionForm form, UserContainer user,
043: HttpServletRequest request, HttpServletResponse response)
044: throws IOException, ServletException {
045: ActionForward r1 = saveGeoserver(mapping, form, request,
046: response);
047: ActionForward r2 = saveValidation(mapping, form, request,
048: response);
049:
050: getApplicationState(request).notifiySaveXML();
051:
052: return mapping.findForward("config");
053: }
054:
055: private ActionForward saveGeoserver(ActionMapping mapping,
056: ActionForm form,
057: //UserContainer user,
058: HttpServletRequest request, HttpServletResponse response)
059: throws IOException, ServletException {
060: ServletContext sc = request.getSession().getServletContext();
061:
062: //File rootDir = new File(sc.getRealPath("/"));
063: File rootDir = GeoserverDataDirectory
064: .getGeoserverDataDirectory();
065:
066: try {
067: XMLConfigWriter.store((WCSDTO) getWCS(request).toDTO(),
068: (WMSDTO) getWMS(request).toDTO(), (WFSDTO) getWFS(
069: request).toDTO(), (GeoServerDTO) getWFS(
070: request).getGeoServer().toDTO(),
071: (DataDTO) getWFS(request).getData().toDTO(),
072: rootDir);
073: } catch (ConfigurationException e) {
074: e.printStackTrace();
075: throw new ServletException(e);
076: }
077:
078: // We need to stash the current page?
079: // or can we use null or something?
080: //
081: return mapping.findForward("config");
082: }
083:
084: private ActionForward saveValidation(ActionMapping mapping,
085: ActionForm form,
086: //UserContainer user,
087: HttpServletRequest request, HttpServletResponse response)
088: throws IOException, ServletException {
089: ServletContext sc = request.getSession().getServletContext();
090:
091: //CH: changed for geoserver_data_dir, forgotten first round.
092: File rootDir = GeoserverDataDirectory
093: .getGeoserverDataDirectory();
094:
095: File dataDir;
096:
097: // if (GeoserverDataDirectory.isTrueDataDir()) {
098: dataDir = rootDir;
099:
100: // } else {
101: // dataDir = new File(rootDir, "data/");
102: // }
103: File plugInDir;
104: File validationDir;
105:
106: try {
107: plugInDir = WriterUtils.initWriteFile(new File(dataDir,
108: "plugIns"), true);
109: validationDir = WriterUtils.initWriteFile(new File(dataDir,
110: "validation"), true);
111: } catch (ConfigurationException confE) {
112: throw new ServletException(confE);
113: }
114:
115: Map plugIns = (Map) getWFS(request).getValidation()
116: .toPlugInDTO();
117: Map testSuites = (Map) getWFS(request).getValidation()
118: .toTestSuiteDTO();
119:
120: Iterator i = null;
121:
122: if (plugIns != null) {
123: i = plugIns.keySet().iterator();
124:
125: while (i.hasNext()) {
126: PlugInDTO dto = null;
127: Object key = null;
128:
129: try {
130: key = i.next();
131: dto = (PlugInDTO) plugIns.get(key);
132:
133: String fName = dto.getName().replaceAll(" ", "")
134: + ".xml";
135: File pFile = WriterUtils.initWriteFile(new File(
136: plugInDir, fName), false);
137: FileWriter fw = new FileWriter(pFile);
138: XMLWriter.writePlugIn(dto, fw);
139: fw.close();
140: } catch (Exception e) {
141: e.printStackTrace();
142: throw new ServletException(e);
143: }
144: }
145: }
146:
147: // deletes of plug ins here
148:
149: /*File[] pluginFL = plugInDir.listFiles();
150: for(int j=0;j<pluginFL.length;j++){
151: String flName = pluginFL[j].getName();
152: flName = flName.substring(0,flName.length()-4);
153: if(plugIns.get(flName)==null){
154: // delete this
155: pluginFL[j].delete();
156: }
157: }*/
158: if (testSuites != null) {
159: i = testSuites.keySet().iterator();
160:
161: while (i.hasNext()) {
162: TestSuiteDTO dto = null;
163:
164: try {
165: dto = (TestSuiteDTO) testSuites.get(i.next());
166:
167: String fName = dto.getName().replaceAll(" ", "")
168: + ".xml";
169: File pFile = WriterUtils.initWriteFile(new File(
170: validationDir, fName), false);
171: FileWriter fw = new FileWriter(pFile); //new File(validationDir,
172: //dto.getName().replaceAll(" ", "") + ".xml"));
173:
174: XMLWriter.writeTestSuite(dto, fw);
175: fw.close();
176: } catch (Exception e) {
177: System.err.println(dto.getClass());
178: e.printStackTrace();
179: throw new ServletException(e);
180: }
181: }
182: }
183:
184: // deletes of testSuites here
185: File[] testsFL = validationDir.listFiles();
186:
187: if (testsFL != null) {
188: for (int j = 0; j < testsFL.length; j++) {
189: boolean found = false;
190: i = testSuites.keySet().iterator();
191:
192: while (!found && i.hasNext())
193: found = (((TestSuiteDTO) testSuites.get(i.next()))
194: .getName().replaceAll(" ", "") + ".xml")
195: .equals(testsFL[j].getName());
196:
197: if (!found) {
198: testsFL[j].delete();
199: }
200: }
201: }
202:
203: getApplicationState(request).notifiySaveXML();
204:
205: // We need to stash the current page?
206: // or can we use null or something?
207: //
208: return mapping.findForward("config.validation");
209: }
210: }
|