Search in sources :

Example 56 with Action

use of io.github.ihongs.action.anno.Action in project HongsCORE by ihongs.

the class FileAction method search.

@Override
@Action("search")
public void search(ActionHelper helper) throws HongsException {
    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("list", new ArrayList(), "page", Synt.mapOf("count", 0, "pages", 0, "state", 1)));
        } else {
            helper.reply(Synt.mapOf("list", ROOT_LIST, "page", Synt.mapOf("count", ROOT_LIST.size(), "pages", 1, "state", 0)));
        }
        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();
            // 跳过隐藏和备份的文件
            if (name.startsWith(".") || name.endsWith("~")) {
                continue;
            }
            Map xxxx = new HashMap();
            xxxx.put("name", name);
            xxxx.put("path", pxth + "/" + name);
            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("pages", 1, "count", filez.size(), "state", filez.size() > 0 ? 0 : 1));
        helper.reply(rsp);
    } else if (isTextFile(file)) {
        String name = file.getName();
        Map rsp = new HashMap();
        Map inf = new HashMap();
        rsp.put("info", inf);
        inf.put("name", name);
        inf.put("path", pxth);
        inf.put("type", "txt");
        inf.put("text", readFile(file));
        inf.put("size", file.length());
        inf.put("mtime", file.lastModified());
        helper.reply(rsp);
    } else {
        helper.fault(lang.translate("core.manage.file.unsupported"));
    }
}
Also used : CoreLocale(io.github.ihongs.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) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action) IAction(io.github.ihongs.dh.IAction)

Example 57 with Action

use of io.github.ihongs.action.anno.Action 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");
    String real;
    File file;
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.required"));
        return;
    }
    real = realPath(path);
    if (real == null) {
        helper.fault(lang.translate("core.manage.file.path.is.error"));
        return;
    }
    file = new File(real);
    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)) {
        file.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(io.github.ihongs.CoreLocale) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HongsException(io.github.ihongs.HongsException) Action(io.github.ihongs.action.anno.Action) IAction(io.github.ihongs.dh.IAction)

Example 58 with Action

use of io.github.ihongs.action.anno.Action in project HongsCORE by ihongs.

the class MoreAction method more.

@Action("__main__")
public void more(ActionHelper helper) {
    helper.reply("");
    HttpServletRequest req = helper.getRequest();
    HttpServletResponse rsp = helper.getResponse();
    Map re0 = helper.getRequestData();
    Map rs0 = helper.getResponseData();
    Core core = Core.getInstance();
    Wrap wrap = new Wrap(helper);
    String act = null;
    try {
        act = Core.ACTION_NAME.get();
        core.put(ActionHelper.class.getName(), wrap);
        more(wrap, null, req, rsp, re0, rs0, null, 0);
    } finally {
        Core.ACTION_NAME.set(act);
        core.put(ActionHelper.class.getName(), helper);
    }
    helper.reply(rs0);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionHelper(io.github.ihongs.action.ActionHelper) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Map(java.util.Map) Core(io.github.ihongs.Core) Action(io.github.ihongs.action.anno.Action)

Example 59 with Action

use of io.github.ihongs.action.anno.Action in project HongsCORE by ihongs.

the class MoreAction method eval.

@Action("eval")
public void eval(ActionHelper helper) throws HongsException {
    CoreConfig cnf = CoreConfig.getInstance();
    HttpServletRequest req = helper.getRequest();
    HttpServletResponse rsp = helper.getResponse();
    // 许可及IP白名单
    boolean sw = cnf.getProperty("core.eval.more.enable", false);
    String ia = cnf.getProperty("core.eval.more.allows");
    String ip = ActionDriver.getClientAddr(req);
    Set ias = Synt.toTerms(ia);
    if (ias == null || ias.isEmpty()) {
        ias = new HashSet();
        ias.add("::1");
        ias.add("127.0.0.1");
        ias.add("0:0:0:0:0:0:0:1");
    }
    if (!sw) {
        throw new HongsException(400, "Illegal request!");
    }
    if (!ias.contains(ip)) {
        throw new HongsException(400, "Illegal request.");
    }
    Map map = helper.getRequestData();
    String act = Core.ACTION_NAME.get();
    String uri = (String) map.get("act");
    // 从参数提取参数
    helper.setRequestData(data(map.get("request")));
    helper.setContextData(data(map.get("context")));
    helper.setSessionData(data(map.get("session")));
    helper.setCookiesData(data(map.get("cookies")));
    try {
        eval(helper, uri, req, rsp);
    } finally {
        Core.ACTION_NAME.set(act);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashSet(java.util.HashSet) Set(java.util.Set) CoreConfig(io.github.ihongs.CoreConfig) HongsException(io.github.ihongs.HongsException) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Action(io.github.ihongs.action.anno.Action)

Example 60 with Action

use of io.github.ihongs.action.anno.Action in project HongsCORE by ihongs.

the class CoreRoster method addServ.

private static void addServ(Map<String, Mathod> acts, Map<String, Method> cmds, String... pkgs) {
    for (String pkgn : pkgs) {
        pkgn = pkgn.trim();
        if (pkgn.length() == 0) {
            continue;
        }
        Set<String> clss = getClss(pkgn);
        for (String clsn : clss) {
            Class clso = getClso(clsn);
            // 从注解提取动作名
            Action acto = (Action) clso.getAnnotation(Action.class);
            if (acto != null) {
                addActs(acts, acto, clsn, clso);
                continue;
            }
            Cmdlet cmdo = (Cmdlet) clso.getAnnotation(Cmdlet.class);
            if (cmdo != null) {
                addCmds(cmds, cmdo, clsn, clso);
            }
        }
    }
}
Also used : Action(io.github.ihongs.action.anno.Action) Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet)

Aggregations

Action (io.github.ihongs.action.anno.Action)64 Map (java.util.Map)53 HashMap (java.util.HashMap)26 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)22 Preset (io.github.ihongs.action.anno.Preset)15 HongsException (io.github.ihongs.HongsException)14 CoreLocale (io.github.ihongs.CoreLocale)12 NaviMap (io.github.ihongs.action.NaviMap)12 Verify (io.github.ihongs.action.anno.Verify)10 IAction (io.github.ihongs.dh.IAction)10 CoreConfig (io.github.ihongs.CoreConfig)9 Select (io.github.ihongs.action.anno.Select)8 Set (java.util.Set)8 List (java.util.List)7 File (java.io.File)6 HashSet (java.util.HashSet)6 FetchCase (io.github.ihongs.db.util.FetchCase)5 JAction (io.github.ihongs.dh.JAction)4 UserAction (io.github.ihongs.serv.master.UserAction)3 ArrayList (java.util.ArrayList)3