Package org.cafesip.jiplet.console.server

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

/*
* Created on Dec 31, 2008
*
* 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 ContextUploadHandler implements FileUploadAction
{
    private String serverContextPath;

    private HashMap<String, File> fileList;

    private String uploadSprPath;

    private String contextNameOverride;

    private String domainName;

    private String selectedConnector;

    private String contextCriteria;

    /*
     * @see
     * org.cafesip.gwtcomp.server.FileUploadAction#onSubmit(javax.servlet.http
     * .HttpServlet, javax.servlet.http.HttpServletRequest)
     */
    public FormResponse onSubmit(HttpServlet servlet, HttpServletRequest request)
    {
        FormResponse response = validateSubmission();
        if (response == null)
        {
            response = processSubmission(servlet, request);
        }

        return response;
    }

    /*
     * @see
     * org.cafesip.gwtcomp.server.FileUploadAction#setFileList(java.util.HashMap
     * )
     */
    public void setFileList(HashMap<String, File> files)
    {
        fileList = files;
    }

    private FormResponse validateSubmission()
    {
        if (isUpload(serverContextPath) == 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(serverContextPath);
            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() == true)
            {
                if ((contextNameOverride != null)
                        && (contextNameOverride.length() > 0))
                {
                    return new FormResponse(
                            HttpServletResponse.SC_BAD_REQUEST,
                            "The specified path is a directory. Therefore the name cannot be overridden",
                            true);
                }
            }
            else if (serverContextPath.endsWith(FileUtils.SPR_EXTENSION) == false)
            {
                return new FormResponse(
                        HttpServletResponse.SC_BAD_REQUEST,
                        "The specified path is a file but it does not have the '.spr' extension",
                        true);
            }
        }

        return null;
    }

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

        if (isUpload(serverContextPath) == false)
        {
            new_name = getContextName(contextNameOverride, serverContextPath);
        }
        else
        {
            File fpath = new File(uploadSprPath);
            new_name = getContextName(contextNameOverride, fpath.getName());
            override_name = new_name;

            File uploadedFile = fileList.values().iterator().next();
            if (uploadedFile.getName().endsWith(FileUtils.SPR_EXTENSION) == false)
            {
                FileUtils f = new FileUtils();
                String tempFilePath = uploadedFile.getAbsolutePath();
                if (f.copyFile(tempFilePath, tempFilePath
                        + FileUtils.SPR_EXTENSION) == 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 + FileUtils.SPR_EXTENSION);
                serverContextPath = tempFile.getAbsolutePath();
            }
            else
            {
                serverContextPath = uploadedFile.getAbsolutePath();
            }
        }

        if ((!FileUtils.validDeployableSpr(serverContextPath))
                && (!FileUtils.validDeployableJipletDir(serverContextPath)))
        {
            return new FormResponse(
                    HttpServletResponse.SC_BAD_REQUEST,
                    "The context could not be created because the path does not have a valid signature",
                    true);
        }

        String cmap = getContextCriteriaXML(domainName, selectedConnector,
                contextCriteria, new_name);

        try
        {
            ContainerMgmt cont = new ContainerMgmt();

            if ((override_name != null) && (override_name.length() > 0))
            {
                cont.createContext(serverContextPath, override_name, cmap);
            }
            else
            {
                cont.createContext(serverContextPath, null, cmap);
            }

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

            return new FormResponse(HttpServletResponse.SC_OK, "OK", true);
        }
        catch (Throwable e)
        {
            servlet.log("Failure creating context: " + e.getClass().getName()
                    + ": " + e.getMessage());
            return new FormResponse(
                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    "Failure creating context: " + e.getMessage(), true);
        }
        finally
        {
            if (tempFile != null)
            {
                tempFile.delete();
            }
        }
    }

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

    private String getContextName(String ctxName, String serverPath)
    {
        String new_name = null;
        if ((ctxName != null) && (ctxName.length() > 0))
        {
            new_name = ctxName;
        }
        else
        {
            new_name = parseContextName(serverPath);
        }

        return new_name;
    }

    private String parseContextName(String name)
    {
        File f = new File(name);
        if (name.endsWith(FileUtils.SPR_EXTENSION) == true)
        {
            String fname = f.getName();
            return fname.substring(0, fname.length()
                    - FileUtils.SPR_EXTENSION.length());
        }
        else
        {
            return f.getName();
        }
    }

    private String getContextCriteriaXML(String domainName,
            String selectedConnector, String contextCriteria, String name)
    {
        String cmap = null;
        if ((domainName != null) && (domainName.trim().length() > 0))
        {
            String domain = domainName.trim().trim();
            // the user has specified a connector name with a domain name
            StringBuffer buffer = new StringBuffer("<context-mappings>\n"
                    + "<context-mapping context=\"" + encodeXMLString(name)
                    + "\"");

            if ((selectedConnector != null) && (selectedConnector.length() > 0))
            {
                buffer.append(" connector=\"" + selectedConnector + "\"");
            }

            buffer.append(">\n<mapping>\n<or>\n");

            buffer.append("<subdomain-of>\n" + " <var>request.uri.host</var>\n"
                    + "<value>" + encodeXMLString(domain)
                    + "</value>\n</subdomain-of>\n");

            buffer.append("<subdomain-of>\n" + " <var>request.to.host</var>\n"
                    + "<value>" + encodeXMLString(domain)
                    + "</value>\n</subdomain-of>\n");

            buffer.append("<subdomain-of>\n"
                    + " <var>request.from.host</var>\n" + "<value>"
                    + encodeXMLString(domain) + "</value>\n</subdomain-of>\n");

            buffer.append("</or>\n</mapping>\n");
            buffer.append("</context-mapping>\n</context-mappings>\n");

            cmap = buffer.toString();
        }
        else if ((contextCriteria != null)
                && (contextCriteria.trim().length() > 0))
        {
            // the user has directly specified the mapping in XML format
            cmap = contextCriteria.trim();
        }

        return cmap;
    }

    // lifted from the Ace Operator project :-)
    private String encodeXMLString(java.lang.String input)
    {
        char[] str = input.toCharArray();
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < str.length; i++)
        {
            switch (str[i])
            {
            case '<':
                buffer.append("&lt;");
                break;

            case '>':
                buffer.append("&gt;");
                break;

            case '&':
                buffer.append("&amp;");
                break;

            case '\'':
                buffer.append("&apos;");
                break;

            case '\"':
                buffer.append("&quot;");
                break;

            default:
                int intv = str[i];
                if (intv > 127)
                {
                    buffer.append("&#x" + Integer.toHexString(intv) + ";");
                }
                else
                {
                    buffer.append(str[i]);
                }
            }
        }

        return buffer.toString();
    }

    public String getServerContextPath()
    {
        return serverContextPath;
    }

    public void setServerContextPath(String serverContextPath)
    {
        this.serverContextPath = serverContextPath;
    }

    public String getContextNameOverride()
    {
        return contextNameOverride;
    }

    public void setContextNameOverride(String contextNameOverride)
    {
        this.contextNameOverride = contextNameOverride;
    }

    public String getDomainName()
    {
        return domainName;
    }

    public void setDomainName(String domainName)
    {
        this.domainName = domainName;
    }

    public String getSelectedConnector()
    {
        return selectedConnector;
    }

    public void setSelectedConnector(String selectedConnector)
    {
        this.selectedConnector = selectedConnector;
    }

    public String getContextCriteria()
    {
        return contextCriteria;
    }

    public void setContextCriteria(String contextCriteria)
    {
        this.contextCriteria = contextCriteria;
    }

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

    public void setUploadSprPath(String uploadSprPath)
    {
        this.uploadSprPath = uploadSprPath;
    }

    public String getUploadSprPath()
    {
        return uploadSprPath;
    }

}
TOP

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

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.