Search in sources :

Example 36 with HongsExemption

use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.

the class ActionHelper method setCookibute.

/**
 * 设置跟踪参数
 * 注意: 此方法总是操作真实 Cookie
 * @param name
 * @param value
 * @param life 生命周期(秒)
 * @param path 路径
 * @param host 域名
 * @param httpOnly 文档内禁读取
 * @param secuOnly 使用安全连接
 */
public void setCookibute(String name, String value, int life, String path, String host, boolean httpOnly, boolean secuOnly) {
    if (value != null) {
        try {
            value = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new HongsExemption(e);
        }
    }
    Cookie ce = new Cookie(name, value);
    if (path != null) {
        ce.setPath(path);
    }
    if (host != null) {
        ce.setDomain(host);
    }
    if (life >= 0) {
        ce.setMaxAge(life);
    }
    if (secuOnly) {
        ce.setSecure(true);
    }
    if (httpOnly) {
        ce.setHttpOnly(true);
    }
    response.addCookie(ce);
}
Also used : Cookie(javax.servlet.http.Cookie) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HongsExemption(io.github.ihongs.HongsExemption)

Example 37 with HongsExemption

use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.

the class ActionHelper method clone.

/**
 * 克隆方法
 * 用于使用 ActionRunner 时快速构建请求对象,
 * 可用以上 setXxxxxData 在克隆之后设置参数.
 * @return
 */
@Override
public ActionHelper clone() {
    ActionHelper helper;
    try {
        helper = (ActionHelper) super.clone();
    } catch (CloneNotSupportedException e) {
        throw new HongsExemption(e);
    }
    helper.responseData = null;
    return helper;
}
Also used : HongsExemption(io.github.ihongs.HongsExemption)

Example 38 with HongsExemption

use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.

the class ActionHelper method getRequestPart.

/**
 * 解析 multipart/form-map 数据, 处理上传
 * @return
 */
final Map getRequestPart() {
    CoreConfig conf = CoreConfig.getInstance();
    String x;
    Set<String> allowTypes = null;
    x = conf.getProperty("fore.upload.allow.types", null);
    if (x != null) {
        allowTypes = new HashSet(Arrays.asList(x.split(",")));
    }
    Set<String> denyTypes = null;
    x = conf.getProperty("fore.upload.deny.types", null);
    if (x != null) {
        denyTypes = new HashSet(Arrays.asList(x.split(",")));
    }
    Set<String> allowExtns = null;
    x = conf.getProperty("fore.upload.allow.extns", null);
    if (x != null) {
        allowExtns = new HashSet(Arrays.asList(x.split(",")));
    }
    Set<String> denyExtns = null;
    x = conf.getProperty("fore.upload.deny.extns", null);
    if (x != null) {
        denyExtns = new HashSet(Arrays.asList(x.split(",")));
    }
    try {
        Map rd = new HashMap();
        Map ud = new HashMap();
        for (Part part : request.getParts()) {
            long size = part.getSize();
            String name = part.getName();
            String type = part.getContentType();
            String extn = part.getSubmittedFileName();
            // 无类型的普通参数已在外部处理
            if (name == null || type == null || extn == null) {
                continue;
            }
            // 在修改的操作中表示将其置为空
            if (size == 0) {
                if (null == request.getParameter(name)) {
                    Dict.setParam(rd, null, name);
                }
                continue;
            }
            // 检查类型
            int pos = type.indexOf(',');
            if (pos != -1) {
                type = type.substring(0, pos);
            }
            if (allowTypes != null && !allowTypes.contains(type)) {
                throw new HongsExemption(400, "File type '" + type + "' is not allowed");
            }
            if (denyTypes != null && denyTypes.contains(type)) {
                throw new HongsExemption(400, "File type '" + type + "' is denied");
            }
            // 检查扩展
            pos = extn.lastIndexOf('.');
            if (pos != -1) {
                extn = extn.substring(1 + pos);
            } else {
                extn = "";
            }
            if (allowExtns != null && !allowExtns.contains(extn)) {
                throw new HongsExemption(400, "Extension '" + extn + "' is not allowed");
            }
            if (denyExtns != null && denyExtns.contains(extn)) {
                throw new HongsExemption(400, "Extension '" + extn + "' is denied");
            }
            /**
             * 临时存储文件
             * 不再需要暂存
             * 可以直接利用 Part 继续向下传递
             */
            /*
            String id = Core.getUniqueId();
            String temp = path + File.separator + id + ".tmp";
            String tenp = path + File.separator + id + ".tnp";
            subn = subn.replaceAll("[\\r\\n\\\\/]", ""); // 清理非法字符: 换行和路径分隔符
            subn = subn + "\r\n" + type + "\r\n" + size; // 拼接存储信息: 名称和类型及大小
            try (
                InputStream      xmin = part.getInputStream();
                FileOutputStream mout = new FileOutputStream(temp);
                FileOutputStream nout = new FileOutputStream(tenp);
            ) {
                byte[] nts = subn.getBytes("UTF-8");
                byte[] buf = new byte[1024];
                int    cnt ;
                while((cnt = xmin.read(buf)) != -1) {
                    mout.write(buf, 0, cnt);
                }
                nout.write(nts);
            }
            Dict.setParam( rd , id , name );
            */
            Dict.setValue(ud, part, name, null);
            Dict.setParam(rd, part, name);
        }
        // 记录在应用里以便有需要时候还可读取原始值
        setAttribute(Cnst.UPLOAD_ATTR, ud);
        return rd;
    } catch (IllegalStateException e) {
        // 上传受限, 如大小超标
        throw new HongsExemption(400, e);
    } catch (ServletException e) {
        throw new HongsExemption(1113, e);
    } catch (IOException e) {
        throw new HongsExemption(1113, e);
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig) HashMap(java.util.HashMap) HongsExemption(io.github.ihongs.HongsExemption) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) Part(javax.servlet.http.Part) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 39 with HongsExemption

use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.

the class ActionHelper method ensue.

/**
 * 转入目标
 * @param sc 301,302 等
 * @param url
 */
public void ensue(int sc, String url) {
    url = ActionDriver.fixUrl(url);
    try {
        this.response.setStatus(/**
         * 30X *
         */
        sc);
        this.response.setHeader("Location", url);
        this.response.flushBuffer();
        this.responseData = null;
    } catch (IOException e) {
        throw new HongsExemption(1110, "Can not send to client.", e);
    }
}
Also used : HongsExemption(io.github.ihongs.HongsExemption) IOException(java.io.IOException)

Example 40 with HongsExemption

use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.

the class UploadHelper method getDigestName.

private String getDigestName(File file) {
    if (digestType == null) {
        return Core.newIdentity();
    }
    // 摘要计算
    byte[] a;
    long l = file.length();
    try (FileInputStream in = new FileInputStream(file);
        FileChannel fc = in.getChannel()) {
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, l);
        MessageDigest m;
        m = MessageDigest.getInstance(digestType);
        m.update(bb);
        a = m.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new HongsExemption(1120, e);
    } catch (IOException e) {
        throw new HongsExemption(1121, e);
    }
    // 转为 16 进制
    int i = 0;
    int j = a.length;
    StringBuilder s = new StringBuilder(2 * j);
    for (; i < j; i++) {
        byte b = a[i];
        char y = DIGITS[b & 0xf];
        char x = DIGITS[b >>> 4 & 0xf];
        s.append(x);
        s.append(y);
    }
    return s.toString();
}
Also used : FileChannel(java.nio.channels.FileChannel) HongsExemption(io.github.ihongs.HongsExemption) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MappedByteBuffer(java.nio.MappedByteBuffer) MessageDigest(java.security.MessageDigest)

Aggregations

HongsExemption (io.github.ihongs.HongsExemption)44 HongsException (io.github.ihongs.HongsException)15 IOException (java.io.IOException)12 HashMap (java.util.HashMap)10 Map (java.util.Map)9 LinkedHashMap (java.util.LinkedHashMap)7 HashSet (java.util.HashSet)6 Core (io.github.ihongs.Core)5 ActionHelper (io.github.ihongs.action.ActionHelper)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 ArrayList (java.util.ArrayList)5 File (java.io.File)4 List (java.util.List)4 CoreConfig (io.github.ihongs.CoreConfig)3 FileInputStream (java.io.FileInputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 Matcher (java.util.regex.Matcher)3