net.sf.ginp.commands.UploadConfig.java Source code

Java tutorial

Introduction

Here is the source code for net.sf.ginp.commands.UploadConfig.java

Source

/*
 *  ginp - Java Web Application for Viewing Photo Collections
 *  Copyright (C) 2004  Douglas John Culnane <doug@culnane.net>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 */
package net.sf.ginp.commands;

import net.sf.ginp.CommandParameter;
import net.sf.ginp.GinpModel;
import net.sf.ginp.config.Configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.Vector;

/**
 *  Class to collect and write folder and picture data to xml data files.
 *
 * @author     Doug Culnane
 * @version    $Revision: 338
 */
public class UploadConfig implements Command {
    /**
     *  apache Commons Logger specific to this class.
     */
    private Log log = LogFactory.getLog(UploadConfig.class);

    /**
     *  Handle admin request to upload an new configuration file.
     *
     * @param  model   Description of the Parameter
     * @param  params  Description of the Parameter
     */
    public final void action(final GinpModel model, final Vector params) {
        // Check we do not have a state of bad config.
        if (Configuration.configOK()) {
            return;
        }

        File confFile = new File(Configuration.getConfigfilelocation());

        // check that file is not there.
        if (confFile.exists()) {
            log.error("Config File there but NOT OK.  Fix it or remove it.");

            return;
        }

        for (int i = 0; i < params.size(); i++) {
            CommandParameter param = (CommandParameter) params.get(i);
            log.debug(param.getName() + " = " + param.getValue());

            if (param.getName().equals("configfile")) {
                try {
                    InputStream is = (InputStream) param.getObject();
                    InputStreamReader isr = new InputStreamReader(is);
                    FileWriter fw = new FileWriter(confFile);
                    int b = isr.read();

                    while (b != -1) {
                        fw.write(b);
                        b = isr.read();
                    }

                    fw.flush();
                } catch (Exception ex) {
                    log.error("Error writing Config File from upload stream.", ex);
                }
            }
        }

        if (Configuration.configOK()) {
            log.info("Uploaded valid Config");
        }

        model.setUserName("admin");
        model.setCurrentPage("setup2.jsp");
    }
}