Search in sources :

Example 6 with CoreLocale

use of app.hongs.CoreLocale in project HongsCORE by ihongs.

the class NaviMap method getRoleTranslated.

public List<Map> getRoleTranslated(String name, int d, Set<String> rolez) {
    CoreLocale lang = getCurrTranslator();
    Map menu = (Map) getMenu(name).get("menus");
    return getRoleTranslated(menu, rolez, lang, d, 0);
}
Also used : CoreLocale(app.hongs.CoreLocale) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with CoreLocale

use of app.hongs.CoreLocale in project HongsCORE by ihongs.

the class FileAction method search.

@Override
@Action("search")
public void search(ActionHelper helper) throws HongsException {
    MimetypesFileTypeMap nmap = new MimetypesFileTypeMap();
    CoreLocale lang = CoreLocale.getInstance("manage");
    String type = helper.getParameter("type");
    String path = helper.getParameter("path");
    String pxth = path;
    File file;
    // 根目录
    if ("".equals(path) || "/".equals(path)) {
        if ("file".equals(type)) {
            helper.reply(Synt.mapOf("page", Synt.mapOf("ern", 1), "list", new ArrayList()));
        } else {
            helper.reply(Synt.mapOf("page", Synt.mapOf("ern", 0), "list", ROOT_LIST));
        }
        return;
    }
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.required"));
        return;
    }
    path = realPath(path);
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.is.error"));
        return;
    }
    file = new File(path);
    if (!file.exists()) {
        helper.fault(lang.translate("core.manage.file.path.is.not.exist"));
        return;
    }
    byte t = 0;
    if ("dir".equals(type)) {
        t = 1;
    } else if ("file".equals(type)) {
        t = 2;
    }
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        Set<Map> filez;
        // 排序规则, 默认按名称排序
        final boolean ds;
        String ob = helper.getParameter("ob");
        if (ob != null && ob.startsWith("-")) {
            ob = ob.substring(1);
            ds = true;
        } else {
            ds = false;
        }
        if ("type".equals(ob)) {
            filez = new TreeSet(new Comparator<Map>() {

                @Override
                public int compare(Map f1, Map f2) {
                    String t1 = (String) f1.get("type");
                    String t2 = (String) f2.get("type");
                    byte s1 = Synt.declare(TYPE_SORT.get(t1), (byte) 0);
                    byte s2 = Synt.declare(TYPE_SORT.get(t2), (byte) 0);
                    if (s1 != s2)
                        if (ds)
                            return s1 < s2 ? 1 : -1;
                        else
                            return s1 > s2 ? 1 : -1;
                    String n1 = (String) f1.get("name");
                    String n2 = (String) f2.get("name");
                    if (ds)
                        return n2.compareTo(n1);
                    else
                        return n1.compareTo(n2);
                }
            });
        } else if ("size".equals(ob)) {
            filez = new TreeSet(new Comparator<Map>() {

                @Override
                public int compare(Map f1, Map f2) {
                    long s1 = Synt.declare(f1.get("size"), 0L);
                    long s2 = Synt.declare(f2.get("size"), 0L);
                    if (s1 != s2)
                        if (ds)
                            return s1 < s2 ? 1 : -1;
                        else
                            return s1 > s2 ? 1 : -1;
                    String n1 = (String) f1.get("name");
                    String n2 = (String) f2.get("name");
                    if (ds)
                        return n2.compareTo(n1);
                    else
                        return n1.compareTo(n2);
                }
            });
        } else {
            filez = new TreeSet(new Comparator<Map>() {

                @Override
                public int compare(Map f1, Map f2) {
                    String n1 = (String) f1.get("name");
                    String n2 = (String) f2.get("name");
                    if (ds)
                        return n2.compareTo(n1);
                    else
                        return n1.compareTo(n2);
                }
            });
        }
        for (File item : files) {
            if (t == 1) {
                if (!item.isDirectory()) {
                    continue;
                }
            } else if (t == 2) {
                if (!item.isFile()) {
                    continue;
                }
            }
            String name = item.getName();
            String extn = name.replaceFirst("^.*/", "");
            String mime = nmap.getContentType(extn);
            Map xxxx = new HashMap();
            xxxx.put("path", pxth + "/" + name);
            xxxx.put("name", name);
            xxxx.put("mime", mime);
            xxxx.put("type", item.isDirectory() ? "dir" : (isTextFile(item) ? "txt" : "bin"));
            xxxx.put("size", item.isDirectory() ? item.list().length : item.length());
            xxxx.put("mtime", item.lastModified());
            filez.add(xxxx);
        }
        Map rsp = new HashMap();
        rsp.put("list", filez);
        rsp.put("page", Synt.mapOf("ern", filez.size() > 0 ? 0 : 1));
        helper.reply(rsp);
    } else if (isTextFile(file)) {
        String name = file.getName();
        String extn = name.replaceFirst("^.*/", "");
        String mime = nmap.getContentType(extn);
        Map xxxx = new HashMap();
        xxxx.put("path", pxth);
        xxxx.put("name", name);
        xxxx.put("mime", mime);
        xxxx.put("type", "txt");
        xxxx.put("size", file.length());
        xxxx.put("text", readFile(file));
        xxxx.put("mtime", file.lastModified());
        helper.reply("", xxxx);
    } else {
        helper.fault(lang.translate("core.manage.file.unsupported"));
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) CoreLocale(app.hongs.CoreLocale) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Comparator(java.util.Comparator) TreeSet(java.util.TreeSet) File(java.io.File) HashMap(java.util.HashMap) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Map(java.util.Map) IAction(app.hongs.dh.IAction) Action(app.hongs.action.anno.Action)

Example 8 with CoreLocale

use of app.hongs.CoreLocale in project HongsCORE by ihongs.

the class FileAction method create.

@Override
@Action("create")
public void create(ActionHelper helper) throws HongsException {
    CoreLocale lang = CoreLocale.getInstance("manage");
    String path = helper.getParameter("path");
    String type = helper.getParameter("type");
    String text = helper.getParameter("text");
    File file;
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.required"));
        return;
    }
    path = realPath(path);
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.is.error"));
        return;
    }
    file = new File(path);
    if (file.exists()) {
        helper.fault(lang.translate("core.manage.file.path.is.exist"));
        return;
    }
    if (isDenyFile(file)) {
        helper.fault(lang.translate("core.manage.file.interdicted"));
        return;
    }
    // 创建目录
    if ("dir".equals(type)) {
        new File(path).mkdirs();
        helper.reply("");
        return;
    }
    // 写入文件
    try {
        saveFile(file, text);
    } catch (Exception ex) {
        CoreLogger.error(ex);
        helper.fault(lang.translate("core.manage.file.create.failed"));
        return;
    }
    helper.reply("");
}
Also used : CoreLocale(app.hongs.CoreLocale) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HongsException(app.hongs.HongsException) IAction(app.hongs.dh.IAction) Action(app.hongs.action.anno.Action)

Example 9 with CoreLocale

use of app.hongs.CoreLocale in project HongsCORE by ihongs.

the class AuthFilter method doFailed.

private void doFailed(Core core, ActionHelper hlpr, byte type) {
    CoreLocale lang = core.get(CoreLocale.class);
    HttpServletRequest req = hlpr.getRequest();
    boolean ia = isApi(req) || isAjax(req) || isJson(req);
    String uri;
    String msg;
    if (2 == type) {
        uri = Core.BASE_HREF;
        if (uri == null || uri.length() == 0 || ia) {
            uri = null;
            msg = lang.translate("core.error.no.place");
        } else {
            msg = lang.translate("core.error.no.place.redirect");
        }
    } else if (3 == type) {
        uri = this.indexPage;
        if (uri == null || uri.length() == 0 || ia) {
            uri = null;
            msg = lang.translate("core.error.no.power");
        } else {
            msg = lang.translate("core.error.no.power.redirect");
        }
    } else {
        uri = this.loginPage;
        if (uri == null || uri.length() == 0) {
            if (0 == type) {
                msg = lang.translate("core.error.un.login");
            } else {
                msg = lang.translate("core.error.no.login");
            }
        } else {
            if (0 == type) {
                msg = lang.translate("core.error.un.login.redirect");
            } else {
                msg = lang.translate("core.error.no.login.redirect");
            }
            /**
             * 追加来源路径, 以便登录后跳回 *
             */
            String src = null;
            String qry;
            if (isAjax(req)) {
                src = req.getHeader("Referer").replaceFirst("^\\w+://[^/]+", "");
            } else if (isHtml(req)) {
                src = req.getRequestURI();
                qry = req.getQueryString();
                if (qry != null && qry.length() != 0) {
                    src += "?" + qry;
                }
            }
            if (src != null) {
                try {
                    src = URLEncoder.encode(src, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    src = "";
                }
                if (uri.contains("?")) {
                    uri += "&r=" + src;
                } else {
                    uri += "?r=" + src;
                }
            }
        }
    }
    if (ia) {
        Map rsp = new HashMap();
        rsp.put("ok", false);
        rsp.put("msg", msg);
        rsp.put("ern", "Er40" + type);
        if (type == 1 || type == 0) {
            if (uri != null && uri.length() != 0) {
                rsp.put("err", "Goto " + uri);
            }
            hlpr.getResponse().setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            if (uri != null && uri.length() != 0) {
                rsp.put("err", "Link " + uri);
            }
            hlpr.getResponse().setStatus(HttpServletResponse.SC_FORBIDDEN);
        }
        /**
         * 必须调用 responed 立即输出
         * 用以应对 forward  后输出失败的问题
         */
        hlpr.reply(rsp);
        hlpr.responed();
    } else {
        if (uri == null && uri.length() == 0) {
            uri = core.get(CoreConfig.class).getProperty("fore.Er40" + type + ".redirect");
            if (uri == null || uri.length() == 0) {
                uri = Core.BASE_HREF + "/";
            }
        }
        Map<String, String> rep = new HashMap();
        rep.put("urt", Core.BASE_HREF);
        rep.put("uri", uri);
        rep.put("msg", msg);
        String err = lang.translate("core.redirect.html", rep);
        if (type == 1) {
            hlpr.error401(err);
        } else {
            hlpr.error403(err);
        }
        // 禁止缓存
        HttpServletResponse rsp = hlpr.getResponse();
        rsp.addHeader("Cache-Control", "no-cache");
        rsp.setHeader("Pragma", "no-cache");
        rsp.setDateHeader("Expires", 0);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CoreLocale(app.hongs.CoreLocale) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) NaviMap(app.hongs.action.NaviMap) Map(java.util.Map)

Example 10 with CoreLocale

use of app.hongs.CoreLocale in project HongsCORE by ihongs.

the class FileAction method update.

@Override
@Action("update")
public void update(ActionHelper helper) throws HongsException {
    CoreLocale lang = CoreLocale.getInstance("manage");
    String path = helper.getParameter("path");
    String dist = helper.getParameter("dist");
    String text = helper.getParameter("text");
    File file;
    File dizt;
    if (dist != null && dist.equals(path)) {
        dist = null;
    }
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.required"));
        return;
    }
    path = realPath(path);
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.is.error"));
        return;
    }
    file = new File(path);
    if (!file.exists()) {
        helper.fault(lang.translate("core.manage.file.path.is.not.exist"));
        return;
    }
    if (isDenyFile(file)) {
        helper.fault(lang.translate("core.manage.file.interdicted"));
        return;
    }
    // 改名移动
    if (dist != null) {
        dist = realPath(dist);
        if (dist == null) {
            helper.fault(lang.translate("core.manage.file.path.is.error"));
            return;
        }
        dizt = new File(dist);
        if (dizt.exists()) {
            helper.fault(lang.translate("core.manage.file.dist.is.exist"));
            return;
        }
        if (isDenyFile(file)) {
            helper.fault(lang.translate("core.manage.file.interdicted"));
            return;
        }
        if (!file.renameTo(dizt)) {
            helper.fault(lang.translate("core.manage.file.rename.failed"));
            return;
        }
        if (text == null) {
            return;
        }
        file = dizt;
    }
    // 写入文件
    try {
        saveFile(file, text);
    } catch (Exception ex) {
        CoreLogger.error(ex);
        helper.fault(lang.translate("core.manage.file.update.failed"));
        return;
    }
    helper.reply("");
}
Also used : CoreLocale(app.hongs.CoreLocale) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HongsException(app.hongs.HongsException) IAction(app.hongs.dh.IAction) Action(app.hongs.action.anno.Action)

Aggregations

CoreLocale (app.hongs.CoreLocale)22 HashMap (java.util.HashMap)15 Map (java.util.Map)15 Action (app.hongs.action.anno.Action)12 CommitSuccess (app.hongs.action.anno.CommitSuccess)7 NaviMap (app.hongs.action.NaviMap)5 IAction (app.hongs.dh.IAction)4 RoleMap (app.hongs.serv.auth.RoleMap)4 File (java.io.File)4 LinkedHashMap (java.util.LinkedHashMap)4 HongsException (app.hongs.HongsException)3 ActionRunner (app.hongs.action.ActionRunner)2 Verify (app.hongs.action.anno.Verify)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Core (app.hongs.Core)1 HongsError (app.hongs.HongsError)1 HongsExpedient (app.hongs.HongsExpedient)1 FetchCase (app.hongs.db.util.FetchCase)1 Wrong (app.hongs.util.verify.Wrong)1