View Javadoc

1   /*
2    *  ginp - Java Web Application for Viewing Photo Collections
3    *  Copyright (C) 2004  Douglas John Culnane <doug@culnane.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sf.ginp.commands;
20  
21  import java.io.File;
22  import java.io.FileWriter;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.util.Vector;
26  
27  import net.sf.ginp.config.Configuration;
28  import net.sf.ginp.CommandParameter;
29  import net.sf.ginp.GinpModel;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   *  Class to collect and write folder and picture data to xml data files.
35   *
36   * @author     Doug Culnane
37   * @version    $Revision: 303 $
38   */
39  public class UploadConfig implements Command {
40  
41  	private Log log = LogFactory.getLog(UploadConfig.class);
42          
43      /**
44       *  Description of the Method
45       *
46       * @param  model   Description of the Parameter
47       * @param  params  Description of the Parameter
48       */
49      public void action(GinpModel model, Vector params) {
50  
51          // Check we do not have a state of bad config.
52          if (Configuration.configOK()) {
53              return;
54          }
55          
56          File confFile = new File(Configuration.getConfigfilelocation());
57          
58          // check that file is not there.
59          if (confFile.exists()) {
60              log.error("Config File there but NOT OK.  Fix it or remove it."); 
61              return;
62          }
63          
64          for (int i = 0; i < params.size(); i++) {
65              CommandParameter  param  = (CommandParameter) params.get(i);
66              log.debug(param.getName() + " = " + param.getValue());
67              if (param.getName().equals("configfile")) {
68                  try {
69                      InputStream       is = (InputStream) param.getObject();
70                      InputStreamReader isr = new InputStreamReader(is);
71                      FileWriter        fw = new FileWriter(confFile);
72                      int b = isr.read();
73                      while ( b != -1) {
74                         fw.write(b);
75                         b = isr.read();
76                      }
77                      fw.flush();
78                  } catch (Exception ex){
79                      log.error("Error writing Config File from upload stream.", ex);
80                  }
81              }
82          }
83          if (Configuration.configOK()) {
84              log.info("Uploaded valid Config");    
85          }
86          model.setUserName("admin");
87          model.setCurrentPage("setup2.jsp");
88      }
89  }
90