*/
public class StylesEditorAction extends ConfigAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
UserContainer user, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
DataConfig config = (DataConfig) getDataConfig();
StylesEditorForm stylesForm = (StylesEditorForm) form;
FormFile file = stylesForm.getSldFile();
final String filename = file.getFileName();
final String styleID = stylesForm.getStyleID();
StyleConfig style = user.getStyle();
boolean doFullValidation = stylesForm.getFullyValidate();
if (stylesForm.getFullyValidateChecked() == false) {
doFullValidation = false;
}
if (doFullValidation)
{
List l = getSchemaExceptions(file,request);
if (l.size() !=0)
{
handleValidationErrors(l,file,stylesForm);
return mapping.findForward("schemaErrors");
}
}
if (style == null) {
// Must of bookmarked? Redirect so they can select
return mapping.findForward("config.data.style");
}
ServletContext sc = getServlet().getServletContext();
//DJB: changed for geoserver_data_dir
//File rootDir = new File(getServlet().getServletContext().getRealPath("/"));
File rootDir = GeoserverDataDirectory.getGeoserverDataDirectory(sc);
File styleDir;
try {
styleDir = GeoserverDataDirectory.findConfigDir(rootDir, "styles");
} catch (ConfigurationException cfe) {
LOGGER.warning("no style dir found, creating new one");
//if for some bizarre reason we don't fine the dir, make a new one.
styleDir = new File(rootDir, "styles");
}
// send content of FormFile to /styles :
// there nothing to keep the styles in memory for XMLConfigWriter.store()
InputStreamReader isr = new InputStreamReader(file.getInputStream());
File newSldFile = new File(styleDir, filename);
//here we do a check to see if the file we are trying to upload is
//overwriting another style file.
LOGGER.fine("new sld file is: " + newSldFile + ", exists: "
+ newSldFile.exists());
if (newSldFile.exists()) {
StyleConfig styleForID = config.getStyle(styleID);
if (styleForID == null) {
//if there is already a file at the location (file.exists()), and
//the system does not have a record of this styleId then it means
//we are trying to add a new sld at a location that would overwrite
//another's sld file.
doFileExistsError(newSldFile, request);
return mapping.findForward("config.data.style.editor");
} else {
//if the system has a record of the file, then we need to check if this
//update is being performed on the correct style id, so we see if the
//file in the system is the same as this one.
File oldFile = styleForID.getFilename();
LOGGER.fine("old file: " + oldFile + ", newFile: " + newSldFile);
if (!oldFile.equals(newSldFile)) {
doFileExistsError(newSldFile, request);
return mapping.findForward("config.data.style.editor");
}
}
}
//When we have time we should put this in a temp file, to be safe, before
//we do the validation, and only write to the real style directory when we
//have things set. If only java had a nice file copy utility.
FileWriter fw = new FileWriter(newSldFile);
char[] tampon = new char[1024];
int charsRead;
while ((charsRead = isr.read(tampon, 0, 1024)) != -1) {
fw.write(tampon, 0, charsRead);
}
fw.flush();
fw.close();
isr.close();
style.setFilename(new File(styleDir, filename));
style.setId(styleID);
StyleFactory factory = StyleFactory.createStyleFactory();
SLDParser styleReader = new SLDParser(factory, newSldFile.toURL());
Style[] readStyles = null;
Style newStyle;
try {
readStyles = styleReader.readXML();
if (readStyles.length == 0) {
//I think our style parser does pretty much no error reporting.
//This is one of the many reasons we need a new SLD parser.
//We could add new exceptions to it, but it's really just
//patching a sinking ship. One option that we could do
//here is do a xerces validating parse, to make sure the
//sld matches the schema before we try to pass it to our
//crappy parser.
String message = "The xml was valid, but couldn't get a Style"
+ " from it. Make sure your style validates against "
+ " the SLD schema";
doStyleParseError(message, newSldFile, request);
return mapping.findForward("config.data.style.editor");
}
newStyle = readStyles[0];
LOGGER.fine("sld is " + newStyle);
} catch (Exception e) {
e.printStackTrace();
String message = (e.getCause() == null) ? e.getLocalizedMessage()
: e.getCause()
.getLocalizedMessage();
doStyleParseError(message, newSldFile, request);
return mapping.findForward("config.data.style.editor");
}
if (newStyle == null) {
throw new RuntimeException("new style equals null"); //I don't
//think this will ever happen, our SLD parser won't return a null.
}
// Do configuration parameters here
config.addStyle(style.getId(), style);
getApplicationState().notifyConfigChanged();
return mapping.findForward("config.data.style");
}