Package org.cafesip.jiplet.console.server

Source Code of org.cafesip.jiplet.console.server.RealmUploadHandler

/*
* Created on Jan 6, 2009
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cafesip.jiplet.console.server;

import java.io.File;
import java.util.HashMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cafesip.gwtcomp.server.FileUploadAction;
import org.cafesip.gwtcomp.server.FormResponse;
import org.cafesip.jiplet.utils.FileUtils;

/**
* @author Becky McElroy
*
*/
public class RealmUploadHandler implements FileUploadAction
{
    private String serverPath;

    private HashMap<String, File> fileList;

    private String uploadFilePath;

    public FormResponse onSubmit(HttpServlet servlet, HttpServletRequest request)
    {
        FormResponse response = initialValidation();
        if (response == null)
        {
            response = processSubmission(servlet, request);
        }

        return response;
    }

    private FormResponse initialValidation()
    {
        if (isUpload(serverPath) == true)
        {
            if ((fileList == null) || (fileList.size() == 0))
            {
                return new FormResponse(HttpServletResponse.SC_BAD_REQUEST,
                        "You have not provided a file to upload", true);
            }

            if (fileList.size() > 1)
            {
                return new FormResponse(HttpServletResponse.SC_BAD_REQUEST,
                        "Please select one file/directory to upload", true);
            }
        }
        else
        {
            // it's a path on the server
            File file = new File(serverPath);
            if (file.exists() == false)
            {
                // in the rare case that the file/directory was deleted after
                // the server-side file/dir has been selected from the client
                return new FormResponse(HttpServletResponse.SC_BAD_REQUEST,
                        "The file/directory does not exist on the server", true);
            }

            if ((file.isDirectory() == false)
                    && (serverPath.endsWith(FileUtils.SRR_EXTENSION) == false))
            {
                return new FormResponse(
                        HttpServletResponse.SC_BAD_REQUEST,
                        "The specified path is a file but it does not have the '.srr' extension",
                        true);
            }
        }

        return null;
    }

    private FormResponse processSubmission(HttpServlet servlet,
            HttpServletRequest request)
    {
        File tempFile = null;

        try
        {
            if (isUpload(serverPath) == true)
            {
                File uploadedFile = fileList.values().iterator().next();
                if (uploadedFile.getName().endsWith(FileUtils.SRR_EXTENSION) == false)
                {
                    FileUtils f = new FileUtils();

                    String uploadedFileName = new File(uploadFilePath)
                            .getName();
                    String tempFilePath = new File(uploadedFile.getParent(),
                            uploadedFileName).getAbsolutePath();

                    if (f
                            .copyFile(uploadedFile.getAbsolutePath(),
                                    tempFilePath) == false)
                    {
                        return new FormResponse(
                                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                "There was an error executing your request - the uploaded file copy failed",
                                true);
                    }

                    tempFile = new File(tempFilePath);
                    serverPath = tempFile.getAbsolutePath();
                }
                else
                {
                    serverPath = uploadedFile.getAbsolutePath();
                }
            }

            if ((!FileUtils.validDeployableSrr(serverPath))
                    && (!FileUtils.validDeployableRealmDir(serverPath)))
            {
                return new FormResponse(
                        HttpServletResponse.SC_BAD_REQUEST,
                        "The realm could not be created because the path does not have a valid signature",
                        true);
            }

            RealmMgmt rlm = new RealmMgmt();
            rlm.createRealm(serverPath);

            if (Model.getDeploymentType().equals("JBOSS") == true)
            {
                servlet
                        .log("User "
                                + request.getUserPrincipal().getName()
                                + " initiated the creation of the J2EE realm with file "
                                + new File(serverPath).getName());
                try
                {
                    Thread.sleep(10000L);
                }
                catch (InterruptedException e)
                {
                }
            }
            else
            {
                servlet.log("User " + request.getUserPrincipal().getName()
                        + " created realm with file "
                        + new File(serverPath).getName());
            }

            return new FormResponse(HttpServletResponse.SC_OK, "OK", true);

        }
        catch (Throwable e)
        {
            e.printStackTrace();
            return new FormResponse(
                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getClass()
                            .getName()
                            + ": " + e.getMessage(), true);
        }
        finally
        {
            if (tempFile != null)
            {
                tempFile.delete();
            }
        }
    }

    private boolean isUpload(String serverPath)
    {
        return (serverPath == null) || (serverPath.trim().length() == 0);
    }

    public String getServerPath()
    {
        return serverPath;
    }

    public void setServerPath(String serverPath)
    {
        this.serverPath = serverPath;
    }

    public HashMap<String, File> getFileList()
    {
        return fileList;
    }

    public void setFileList(HashMap<String, File> fileList)
    {
        this.fileList = fileList;
    }

    public String getUploadFilePath()
    {
        return uploadFilePath;
    }

    public void setUploadFilePath(String uploadFilePath)
    {
        this.uploadFilePath = uploadFilePath;
    }

}
TOP

Related Classes of org.cafesip.jiplet.console.server.RealmUploadHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.