<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.common.exceptions.IflowException"
%><%@page import="com.fujitsu.iflow.model.util.ModelException"
%><%@page import="com.fujitsu.iflow.model.util.ModelInternalException"
%><%@page import="com.fujitsu.iflow.model.util.StringUTF8InputStream"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.UploadFile"
%><%@page import="com.fujitsu.iflow.common.ErrorMessage"
%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.OutputStream"%>
<%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1

    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {
        response.sendRedirect("Login.jsp");
        return;
    }

%><jsp:useBean id="myUpload" scope="page" class="com.fujitsu.iflow.model.util.Upload" /><%

    myUpload.initialize(pageContext);
    myUpload.upload();
    UploadFile firstFile = myUpload.getFiles().getFile(0);
    String fileName = firstFile.getFileName();
    String fileType = firstFile.getContentType();
    String fileSubType = firstFile.getContentDisp();
    String packageRootPath = application.getRealPath("/").trim();
    if (packageRootPath.endsWith(File.separator))
       packageRootPath = packageRootPath + "packages";
    else
       packageRootPath = packageRootPath + File.separator + "packages";
    File packageFolder = new File(packageRootPath = packageRootPath + File.separator + "upload");
    InputStream bais = new JspDumbInputStream(firstFile);
    if(packageFolder.exists() == false)
       packageFolder.mkdirs();
    String packagePath = packageFolder.getAbsolutePath() + File.separator + fileName;
    FileOutputStream fout = new FileOutputStream(packagePath);
    byte data[] = new byte[1024];
    int count;
    while((count = bais.read(data, 0, 1024)) != -1)
    {
       fout.write(data, 0, count);
    }

    bais.close();
    fout.flush();
    fout.close();
    session.setAttribute("PackagePath", packagePath);
    response.sendRedirect("CheckWFAppPackage.jsp");
%>

<%!

    class JspDumbInputStream extends InputStream
    {

        UploadFile attachment;
        int pointer = 0;
        int fileSize = 0;

        public
        JspDumbInputStream(UploadFile dumbFile)
        {
            attachment = dumbFile;
            pointer = 0;
            fileSize = dumbFile.getSize();
        }

        public
        int
        read()
        {
            if (pointer >= fileSize) {
                return -1;
            }
            return (attachment.getBinaryData(pointer++) & 255);
        }
    }

    public
    void
    copyInputToFile(InputStream is, String fileName)
        throws Exception
    {
        File outFile = new File(fileName);
        OutputStream os = new FileOutputStream(outFile);

        int byteBuf = is.read();
        while (byteBuf >= 0)
        {
            os.write(byteBuf);
            byteBuf = is.read();
        }

        os.flush();
        os.close();
    }

%>
