All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fastchar.multipart.FastMultipartWrapper Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
package com.fastchar.multipart;


import com.fastchar.core.FastChar;
import com.fastchar.core.FastFile;
import com.fastchar.exception.FastFileException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FastMultipartWrapper extends HttpServletRequestWrapper {
    private FastMultipartRequest multipartRequest = null;

    public FastMultipartWrapper(HttpServletRequest req, String dir) throws IOException, FastFileException {
        super(req);
        this.multipartRequest = new FastMultipartRequest(req, dir);
    }

    public FastMultipartWrapper(HttpServletRequest req, String dir, int maxPostSize) throws IOException, FastFileException {
        super(req);
        checkDirectory(dir);
        this.multipartRequest = new FastMultipartRequest(req, dir, maxPostSize);
    }

    public FastMultipartWrapper(HttpServletRequest req, String dir, int maxPostSize, String encoding) throws IOException, FastFileException {
        super(req);
        checkDirectory(dir);
        this.multipartRequest = new FastMultipartRequest(req, dir, maxPostSize, encoding, new DefaultFileRenamePolicy(false));
    }

    public FastMultipartWrapper(HttpServletRequest req, String dir, int maxPostSize, String encoding, boolean nameMD5) throws IOException, FastFileException {
        super(req);
        checkDirectory(dir);
        this.multipartRequest = new FastMultipartRequest(req, dir, maxPostSize, encoding, new DefaultFileRenamePolicy(nameMD5));
    }


    private void checkDirectory(String dir) throws FastFileException {
        File file = new File(dir);
        if (!file.exists()) {
            if (!file.mkdirs()) {
                throw new FastFileException(FastChar.getLocal().getInfo("File_Error1", "'" + dir + "'"));
            }
        }
    }

    public Enumeration getParameterNames() {
        return this.multipartRequest.getParameterNames();
    }

    public String getParameter(String name) {
        return this.multipartRequest.getParameter(name);
    }

    public String[] getParameterValues(String name) {
        return this.multipartRequest.getParameterValues(name);
    }

    public Map getParameterMap() {
        Map map = new HashMap();
        Enumeration enumm = this.getParameterNames();

        while (enumm.hasMoreElements()) {
            String name = (String) enumm.nextElement();
            map.put(name, this.multipartRequest.getParameterValues(name));
        }

        return map;
    }

    public Enumeration getFileNames() {
        return this.multipartRequest.getFileNames();
    }

    public String getFilesystemName(String name) {
        return this.multipartRequest.getFilesystemName(name);
    }

    public String getOriginalFileName(String name) {
        return this.multipartRequest.getOriginalFileName(name);
    }

    public String getContentType(String name) {
        return this.multipartRequest.getContentType(name);
    }

    public FastFile getFile(String name) {
        return this.multipartRequest.getFile(name);
    }

    public FastFile[] getFiles(String name) {
        return this.multipartRequest.getFiles(name);
    }

    public void putFile(String name, FastFile fastFile) {
        this.multipartRequest.putFile(name, fastFile);
    }

    public List> getFiles() {
        return this.multipartRequest.getFiles();
    }


    class DefaultFileRenamePolicy implements FileRenamePolicy {
        private boolean md5Name;

        public DefaultFileRenamePolicy(boolean md5Name) {
            this.md5Name = md5Name;
        }

        public File rename(File f) {
            if (!FastChar.getConstant().isAttachNameSuffix()) {
                f = new File(f.getParent(), FastChar.getSecurity().MD5_Encrypt(f.getName()));
            }
            return  FastChar.getFileRename().rename(f, md5Name);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy