Search in sources :

Example 1 with UploadedFile

use of com.sun.webui.jsf.model.UploadedFile in project Payara by payara.

the class WoodstockHandler method uploadFileToTempDir.

/**
 * <p>
 * This method uploads a file temp directory</p>
 * <p>
 * Input value: "file" -- Type:
 * <code>com.sun.webui.jsf.model.UploadedFile</code></p>
 * <p>
 * Output value: "uploadDir" -- Type: <code>java.lang.String</code></p>
 *
 * @param	handlerCtx	The HandlerContext.
 */
@Handler(id = "uploadFileToTempDir", input = { @HandlerInput(name = "file", type = UploadedFile.class) }, output = { @HandlerOutput(name = "origPath", type = String.class), @HandlerOutput(name = "uploadedTempFile", type = String.class) })
public static void uploadFileToTempDir(HandlerContext handlerCtx) {
    Logger logger = GuiUtil.getLogger();
    if (logger.isLoggable(Level.FINE)) {
        logger.fine(GuiUtil.getCommonMessage("log.inUploadFileToTmpDir"));
    }
    UploadedFile uploadedFile = (UploadedFile) handlerCtx.getInputValue("file");
    File tmpFile = null;
    String uploadTmpFile = "";
    if (uploadedFile != null) {
        String name = uploadedFile.getOriginalName();
        logger.info("uploadFileName=" + name);
        // see bug# 6498910, for IE, getOriginalName() returns the full path, including the drive.
        // for any other browser, it just returns the file name.
        int lastIndex = name.lastIndexOf("\\");
        if (lastIndex != -1) {
            name = name.substring(lastIndex + 1, name.length());
        }
        int index = name.indexOf(".");
        if (index <= 0) {
            logger.info("name=" + name + ",index=" + index);
            String mesg = GuiUtil.getMessage("msg.deploy.nullArchiveError");
            GuiUtil.handleError(handlerCtx, mesg);
            return;
        }
        String suffix = name.substring(index);
        String prefix = name.substring(0, index);
        handlerCtx.setOutputValue("origPath", prefix);
        try {
            // createTempFile requires min. of 3 char for prefix.
            if (prefix.length() <= 2) {
                prefix = prefix + new SecureRandom().nextInt(100000);
            }
            tmpFile = File.createTempFile(prefix, suffix);
            tmpFile.deleteOnExit();
            if (logger.isLoggable(Level.FINE)) {
                logger.fine(GuiUtil.getCommonMessage("log.writeToTmpFile"));
            }
            uploadedFile.write(tmpFile);
            if (logger.isLoggable(Level.FINE)) {
                logger.fine(GuiUtil.getCommonMessage("log.afterWriteToTmpFile"));
            }
            uploadTmpFile = tmpFile.getCanonicalPath();
        } catch (IOException ioex) {
            try {
                if (tmpFile != null) {
                    uploadTmpFile = tmpFile.getAbsolutePath();
                }
            } catch (Exception ex) {
            // Handle AbsolutePathException here
            }
        } catch (Exception ex) {
            GuiUtil.handleException(handlerCtx, ex);
        }
    }
    if (logger.isLoggable(Level.FINE)) {
        logger.fine(GuiUtil.getCommonMessage("log.successfullyUploadedTmp") + uploadTmpFile);
    }
    handlerCtx.setOutputValue("uploadedTempFile", uploadTmpFile);
}
Also used : UploadedFile(com.sun.webui.jsf.model.UploadedFile) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException) Logger(java.util.logging.Logger) UploadedFile(com.sun.webui.jsf.model.UploadedFile) File(java.io.File) IOException(java.io.IOException) Handler(com.sun.jsftemplating.annotation.Handler)

Aggregations

Handler (com.sun.jsftemplating.annotation.Handler)1 UploadedFile (com.sun.webui.jsf.model.UploadedFile)1 File (java.io.File)1 IOException (java.io.IOException)1 SecureRandom (java.security.SecureRandom)1 Logger (java.util.logging.Logger)1