Search in sources :

Example 1 with FileUploadBean

use of org.alfresco.web.bean.FileUploadBean in project acs-community-packaging by Alfresco.

the class ImportDialog method getFileName.

/**
 * @return Returns the name of the file
 */
public String getFileName() {
    // try and retrieve the file and filename from the file upload bean
    // representing the file we previously uploaded.
    FacesContext ctx = FacesContext.getCurrentInstance();
    FileUploadBean fileBean = (FileUploadBean) ctx.getExternalContext().getSessionMap().get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
    if (fileBean != null) {
        this.fileName = fileBean.getFileName();
        this.file = fileBean.getFile();
    }
    return this.fileName;
}
Also used : FacesContext(javax.faces.context.FacesContext) FileUploadBean(org.alfresco.web.bean.FileUploadBean)

Example 2 with FileUploadBean

use of org.alfresco.web.bean.FileUploadBean in project acs-community-packaging by Alfresco.

the class AddContentDialog method setFileName.

/**
 * @param fileName The name of the file
 */
public void setFileName(String fileName) {
    this.fileName = fileName;
    // we also need to keep the file upload bean in sync
    FacesContext ctx = FacesContext.getCurrentInstance();
    FileUploadBean fileBean = (FileUploadBean) ctx.getExternalContext().getSessionMap().get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
    if (fileBean != null) {
        fileBean.setFileName(this.fileName);
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) FileUploadBean(org.alfresco.web.bean.FileUploadBean)

Example 3 with FileUploadBean

use of org.alfresco.web.bean.FileUploadBean in project acs-community-packaging by Alfresco.

the class UploadFileServlet method service.

/**
 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
@SuppressWarnings("unchecked")
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String uploadId = null;
    String returnPage = null;
    final RequestContext requestContext = new ServletRequestContext(request);
    boolean isMultipart = ServletFileUpload.isMultipartContent(requestContext);
    try {
        AuthenticationStatus status = servletAuthenticate(request, response);
        if (status == AuthenticationStatus.Failure) {
            return;
        }
        if (!isMultipart) {
            throw new AlfrescoRuntimeException("This servlet can only be used to handle file upload requests, make" + "sure you have set the enctype attribute on your form to multipart/form-data");
        }
        if (logger.isDebugEnabled())
            logger.debug("Uploading servlet servicing...");
        FacesContext context = FacesContext.getCurrentInstance();
        Map<Object, Object> session = context.getExternalContext().getSessionMap();
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        // ensure that the encoding is handled correctly
        upload.setHeaderEncoding("UTF-8");
        List<FileItem> fileItems = upload.parseRequest(request);
        FileUploadBean bean = new FileUploadBean();
        for (FileItem item : fileItems) {
            if (item.isFormField()) {
                if (item.getFieldName().equalsIgnoreCase("return-page")) {
                    returnPage = item.getString();
                } else if (item.getFieldName().equalsIgnoreCase("upload-id")) {
                    uploadId = item.getString();
                }
            } else {
                String filename = item.getName();
                if (filename != null && filename.length() != 0) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Processing uploaded file: " + filename);
                    }
                    // ADB-41: Ignore non-existent files i.e. 0 byte streams.
                    if (allowZeroByteFiles() == true || item.getSize() > 0) {
                        // workaround a bug in IE where the full path is returned
                        // IE is only available for Windows so only check for the Windows path separator
                        filename = FilenameUtils.getName(filename);
                        final File tempFile = TempFileProvider.createTempFile("alfresco", ".upload");
                        item.write(tempFile);
                        bean.setFile(tempFile);
                        bean.setFileName(filename);
                        bean.setFilePath(tempFile.getAbsolutePath());
                        if (logger.isDebugEnabled()) {
                            logger.debug("Temp file: " + tempFile.getAbsolutePath() + " size " + tempFile.length() + " bytes created from upload filename: " + filename);
                        }
                    } else {
                        if (logger.isWarnEnabled())
                            logger.warn("Ignored file '" + filename + "' as there was no content, this is either " + "caused by uploading an empty file or a file path that does not exist on the client.");
                    }
                }
            }
        }
        session.put(FileUploadBean.getKey(uploadId), bean);
        if (bean.getFile() == null && uploadId != null && logger.isWarnEnabled()) {
            logger.warn("no file uploaded for upload id: " + uploadId);
        }
        if (returnPage == null || returnPage.length() == 0) {
            throw new AlfrescoRuntimeException("return-page parameter has not been supplied");
        }
        JSONObject json;
        try {
            json = new JSONObject(returnPage);
            if (json.has("id") && json.has("args")) {
                // finally redirect
                if (logger.isDebugEnabled()) {
                    logger.debug("Sending back javascript response " + returnPage);
                }
                response.setContentType(MimetypeMap.MIMETYPE_HTML);
                response.setCharacterEncoding("utf-8");
                // work-around for WebKit protection against embedded javascript on POST body response
                response.setHeader("X-XSS-Protection", "0");
                final PrintWriter out = response.getWriter();
                out.println("<html><body><script type=\"text/javascript\">");
                out.println("window.parent.upload_complete_helper(");
                out.println("'" + json.getString("id") + "'");
                out.println(", ");
                out.println(json.getJSONObject("args"));
                out.println(");");
                out.println("</script></body></html>");
                out.close();
            }
        } catch (JSONException e) {
            // finally redirect
            if (logger.isDebugEnabled())
                logger.debug("redirecting to: " + returnPage);
            response.sendRedirect(returnPage);
        }
        if (logger.isDebugEnabled())
            logger.debug("upload complete");
    } catch (Throwable error) {
        handleUploadException(request, response, error, returnPage);
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) JSONException(org.json.JSONException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) JSONObject(org.json.JSONObject) FileUploadBean(org.alfresco.web.bean.FileUploadBean) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONObject(org.json.JSONObject) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) RequestContext(org.apache.commons.fileupload.RequestContext) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 4 with FileUploadBean

use of org.alfresco.web.bean.FileUploadBean in project acs-community-packaging by Alfresco.

the class AddContentDialog method getFileName.

/**
 * @return Returns the name of the file
 */
public String getFileName() {
    // try and retrieve the file and filename from the file upload bean
    // representing the file we previously uploaded.
    FacesContext ctx = FacesContext.getCurrentInstance();
    FileUploadBean fileBean = (FileUploadBean) ctx.getExternalContext().getSessionMap().get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
    if (fileBean != null) {
        this.file = fileBean.getFile();
        this.fileName = fileBean.getFileName();
    }
    return this.fileName;
}
Also used : FacesContext(javax.faces.context.FacesContext) FileUploadBean(org.alfresco.web.bean.FileUploadBean)

Example 5 with FileUploadBean

use of org.alfresco.web.bean.FileUploadBean in project acs-community-packaging by Alfresco.

the class CheckinCheckoutDialog method setFileName.

/**
 * @param fileName The name of the file
 */
public void setFileName(String fileName) {
    property.setFileName(fileName);
    // we also need to keep the file upload bean in sync
    FacesContext ctx = FacesContext.getCurrentInstance();
    FileUploadBean fileBean = (FileUploadBean) ctx.getExternalContext().getSessionMap().get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
    if (fileBean != null) {
        fileBean.setFileName(property.getFileName());
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) FileUploadBean(org.alfresco.web.bean.FileUploadBean)

Aggregations

FileUploadBean (org.alfresco.web.bean.FileUploadBean)8 FacesContext (javax.faces.context.FacesContext)7 File (java.io.File)2 FileItem (org.apache.commons.fileupload.FileItem)2 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 PortletException (javax.portlet.PortletException)1 PortletSession (javax.portlet.PortletSession)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 SessionUser (org.alfresco.repo.SessionUser)1 AuthenticationException (org.alfresco.repo.security.authentication.AuthenticationException)1 AuthenticationService (org.alfresco.service.cmr.security.AuthenticationService)1 LoginBean (org.alfresco.web.bean.LoginBean)1 User (org.alfresco.web.bean.repository.User)1 RequestContext (org.apache.commons.fileupload.RequestContext)1 PortletFileUpload (org.apache.commons.fileupload.portlet.PortletFileUpload)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)1 JSONException (org.json.JSONException)1