Search in sources :

Example 6 with HongsExemption

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

the class DBConfig method imports.

@Override
protected void imports() throws HongsException {
    InputStream is;
    String fn;
    DBConfig cp;
    try {
        fn = Core.CONF_PATH + "/" + name + Cnst.DB_EXT + ".xml";
        is = new FileInputStream(fn);
    } catch (FileNotFoundException ex) {
        fn = name.contains(".") || name.contains("/") ? name + Cnst.DB_EXT + ".xml" : Cnst.CONF_PACK + "/" + name + Cnst.DB_EXT + ".xml";
        is = this.getClass().getClassLoader().getResourceAsStream(fn);
        if (is == null) {
            throw new HongsExemption(826, "Can not find the config file '" + name + Cnst.DB_EXT + ".xml'.");
        }
    }
    try {
        cp = parseByStream(is);
    } finally {
        try {
            is.close();
        } catch (IOException ex) {
            throw new HongsException(ex);
        }
    }
    this.link = cp.link;
    this.source = cp.source;
    this.origin = cp.origin;
    this.dbClass = cp.dbClass;
    this.tableClass = cp.tableClass;
    this.modelClass = cp.modelClass;
    this.tablePrefix = cp.tablePrefix;
    this.tableSuffix = cp.tableSuffix;
    this.tableConfigs = cp.tableConfigs;
}
Also used : HongsException(io.github.ihongs.HongsException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) HongsExemption(io.github.ihongs.HongsExemption) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 7 with HongsExemption

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

the class VarsFilter method doFilter.

@Override
public void doFilter(Core core, ActionHelper hlpr, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse rsp = hlpr.getResponse();
    HttpServletRequest req = hlpr.getRequest();
    /**
     * 跳过内部动作代理, 如 AutoFilter
     */
    if (null != req.getAttribute(Cnst.ACTION_ATTR)) {
        chain.doFilter(req, rsp);
        return;
    }
    String act = ActionDriver.getRecentPath(req);
    if (null != act && !patter.matches(act)) {
        chain.doFilter(req, rsp);
        return;
    }
    /**
     * 上传文件时可能会发生异常
     */
    Map rd;
    try {
        rd = hlpr.getRequestData();
    } catch (Throwable e) {
        if (e instanceof HongsCause) {
            hlpr.fault((HongsCause) e);
        } else {
            hlpr.fault(e.getMessage());
        }
        return;
    }
    if (rn_limit != 0) {
        int rn = Synt.declare(rd.get(Cnst.RN_KEY), Cnst.RN_DEF);
        if (rn < 1 || rn > rn_limit) {
            rsp.setStatus(400);
            hlpr.fault(Cnst.RN_KEY + " must be 1 to " + rn_limit);
            return;
        }
    }
    if (illegals != null && !illegals.isEmpty()) {
        Set ls = new HashSet(illegals);
        Set ks = rd.keySet();
        ls.retainAll(ks);
        if (!ls.isEmpty()) {
            rsp.setStatus(400);
            hlpr.fault("Illegal parameters: " + Syno.concat(",", ls));
            return;
        }
    }
    if (sr_limit != 0 || sr_level != 0) {
        try {
            srCheck(rd, sr_limit, sr_level, 0, 1);
        } catch (HongsException | HongsExemption ex) {
            hlpr.fault(ex);
            return;
        }
    }
    chain.doFilter(req, rsp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Set(java.util.Set) HashSet(java.util.HashSet) HongsCause(io.github.ihongs.HongsCause) HongsException(io.github.ihongs.HongsException) HttpServletResponse(javax.servlet.http.HttpServletResponse) HongsExemption(io.github.ihongs.HongsExemption) Map(java.util.Map) HashSet(java.util.HashSet)

Example 8 with HongsExemption

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

the class ActsAction method service.

/**
 * 服务方法
 * Servlet Mapping: *.act<br/>
 * 注意: 不支持请求URI的路径中含有"."(句点), 且必须区分大小写;
 * 其目的是为了防止产生多种形式的请求路径, 影响动作过滤, 产生安全隐患.
 *
 * @param req
 * @param rsp
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException {
    String act = ActionDriver.getRecentPath(req);
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.got(ActionHelper.class);
    Core.THREAD_CORE.set(core);
    if (act == null || act.length() == 0) {
        helper.fault(new HongsException(404, "Action URI can not be empty."));
        return;
    }
    // 去掉根和扩展名
    int pos = act.lastIndexOf('.');
    if (pos != -1) {
        act = act.substring(1, pos);
    } else {
        act = act.substring(1);
    }
    // 获取并执行动作
    try {
        new ActionRunner(helper, act).doAction();
    } catch (HongsException e) {
        helper.fault(e);
    } catch (HongsExemption e) {
        helper.fault(e);
    } catch (RuntimeException e) {
        helper.fault(new HongsException(500, e));
    }
}
Also used : ActionRunner(io.github.ihongs.action.ActionRunner) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) HongsExemption(io.github.ihongs.HongsExemption) Core(io.github.ihongs.Core)

Example 9 with HongsExemption

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

the class AuthAction method service.

/**
 * 服务方法
 * 判断配置和消息有没有生成, 如果没有则生成; 消息按客户语言存放
 * @param req
 * @param rsp
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    /*
    // 2020/05/14  通过配置和用户的修改时间来判断是否能有变化
    // 受是否登录、不同用户等影响, 权限经常变化, 必须禁止缓存
    rsp.setHeader("Expires", "0");
    rsp.addHeader("Pragma" , "no-cache");
    rsp.setHeader("Cache-Control", "no-cache");
    */
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.got(ActionHelper.class);
    String name = req.getPathInfo();
    if (name == null || name.length() == 0) {
        helper.error(400, "Path info required");
        return;
    }
    int p = name.lastIndexOf('.');
    if (p < 0) {
        helper.error(400, "File type required");
        return;
    }
    String type = name.substring(1 + p);
    name = name.substring(1, p);
    if (!"js".equals(type) && !"json".equals(type)) {
        helper.error(400, "Wrong file type: " + type);
        return;
    }
    String s;
    try {
        NaviMap sitemap = NaviMap.getInstance(name);
        Set<String> roleset = sitemap.getRoleSet();
        Set<String> authset;
        // 没有设置 rsname 的不公开
        if (null == sitemap.session) {
            helper.error(403, "Auth data for '" + name + "' is not open to the public");
            return;
        }
        // HTTP 304 缓存策略
        if (roleset instanceof CoreSerial.Mtimes) {
            CoreSerial.Mtimes rolemod = (CoreSerial.Mtimes) roleset;
            long l = Math.max(sitemap.dataModified(), rolemod.dataModified());
            long m = helper.getRequest().getDateHeader("If-Modified-Since");
            if (l != 0) {
                // HTTP 时间精确到秒
                l = l / 1000;
                m = m / 1000;
                if (m >= l) {
                    helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                } else {
                    helper.getResponse().setHeader("Cache-Control", "no-cache");
                    helper.getResponse().setDateHeader("Last-Modified", l * 1000);
                }
            }
        }
        Map<String, Boolean> datamap = new HashMap();
        if (null == roleset)
            authset = new HashSet();
        else
            authset = sitemap.getRoleAuths(roleset.toArray(new String[] {}));
        for (String act : sitemap.actions) {
            datamap.put(act, authset.contains(act));
        }
        s = Dawn.toString(datamap);
    } catch (IllegalArgumentException ex) {
        helper.error(500, ex.getMessage());
        return;
    } catch (HongsException | HongsExemption ex) {
        helper.error(404, ex.getMessage());
        return;
    }
    // 输出权限信息
    if ("json".equals(type)) {
        helper.write("application/json", s);
    } else {
        String c = req.getParameter("callback");
        if (c != null && !c.isEmpty()) {
            if (!c.matches("^[a-zA-Z_\\$][a-zA-Z0-9_]*$")) {
                helper.error(400, "Illegal callback function name!");
                return;
            }
            helper.write("text/javascript", c + "(" + s + ");");
        } else {
            c = "self.HsAUTH=Object.assign(self.HsAUTH||{}";
            helper.write("text/javascript", c + "," + s + ");");
        }
    }
}
Also used : HashMap(java.util.HashMap) HongsExemption(io.github.ihongs.HongsExemption) NaviMap(io.github.ihongs.action.NaviMap) CoreSerial(io.github.ihongs.CoreSerial) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) Core(io.github.ihongs.Core) HashSet(java.util.HashSet)

Example 10 with HongsExemption

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

the class ActionHelper method error.

/**
 * 错误通知
 * @param sc 400,500 等
 * @param msg
 */
public void error(int sc, String msg) {
    try {
        this.response.sendError(sc, msg);
        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)

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