Search in sources :

Example 71 with HongsException

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

the class Common method file.

public static String file(String path) throws HongsException {
    try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
        int bn;
        char[] bs;
        StringBuilder sb = new StringBuilder();
        while (true) {
            bs = new char[1024];
            if ((bn = br.read(bs)) < 0) {
                break;
            }
            sb.append(bs, 0, bn);
        }
        return sb.toString();
    } catch (FileNotFoundException ex) {
        throw new HongsException.Common("Can not find " + path, ex);
    } catch (IOException ex) {
        throw new HongsException.Common("Can not read " + path, ex);
    }
}
Also used : HongsException(app.hongs.HongsException) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 72 with HongsException

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

the class Common method callAction.

@Cmdlet("call-action")
public static void callAction(String[] args) throws HongsException {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "request:s", "cookies:s");
    args = (String[]) opts.get("");
    String act = args[0];
    String req = text((String) opts.get("request"));
    Map<String, String> cok = data((String) opts.get("cookies"));
    String hst = System.getProperty("server.host", "localhost");
    String pot = System.getProperty("server.port", "8080");
    String url = "http://" + hst + ":" + pot + Core.BASE_HREF + "/" + act + Cnst.ACT_EXT;
    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setConnectTimeout(0);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept", "application/json,text/html,*/*;q=0.8");
        conn.setRequestProperty("X-Requested-With", "HongsCORE/0.4");
        // 放入 cookie
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> et : cok.entrySet()) {
            sb.append(URLEncoder.encode(et.getKey(), "UTF-8"));
            sb.append("=");
            sb.append(URLEncoder.encode(et.getValue(), "UTF-8"));
            sb.append(";");
        }
        conn.setRequestProperty("Cookie", sb.toString());
        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(req);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.print(line);
        }
    } catch (UnsupportedEncodingException ex) {
        throw new HongsException.Common(ex);
    } catch (MalformedURLException ex) {
        throw new HongsException.Common(ex);
    } catch (IOException ex) {
        throw new HongsException.Common(ex);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) HongsException(app.hongs.HongsException) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 73 with HongsException

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

the class DBAction method acting.

@Override
public void acting(ActionHelper helper, ActionRunner runner) throws HongsException {
    String act = runner.getHandle();
    String ent = runner.getEntity();
    String mod = runner.getModule();
    try {
        // 下划线开头的为内部资源, 不直接对外开放
        if (ent.startsWith("_")) {
            throw new HongsException(0x1100, "Unsupported Request!");
        }
        // 判断是否禁用了当前动作, 忽略表单不存在
        Map fa = FormSet.getInstance(mod).getForm(ent);
        Set ca = Synt.toSet(Dict.getDepth(fa, "@", "callable"));
        if (ca != null && !ca.contains(act)) {
            throw new HongsException(0x1100, "Unsupported Request.");
        }
    } catch (HongsException | HongsExpedient ex) {
        int ec = ex.getErrno();
        if (ec != 0x10e8 && ec != 0x10ea) {
            throw ex;
        }
    }
}
Also used : Set(java.util.Set) FormSet(app.hongs.action.FormSet) HongsException(app.hongs.HongsException) HongsExpedient(app.hongs.HongsExpedient) Map(java.util.Map)

Example 74 with HongsException

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

the class DBConfig method parseBySource.

/**
 * 根据输入流解析配置
 *
 * @param ds
 * @return 配置对象
 * @throws app.hongs.HongsException
 */
public static DBConfig parseBySource(InputSource ds) throws HongsException {
    Document doc;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder dbn = dbf.newDocumentBuilder();
        doc = dbn.parse(ds);
    } catch (ParserConfigurationException ex) {
        throw new app.hongs.HongsException(0x1063, ex);
    } catch (SAXException ex) {
        throw new app.hongs.HongsException(0x1063, ex);
    } catch (IOException ex) {
        throw new app.hongs.HongsException(0x1069, ex);
    }
    return new DBConfig(doc);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) HongsException(app.hongs.HongsException) SAXException(org.xml.sax.SAXException)

Example 75 with HongsException

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

the class DBFields method imports.

@Override
protected final void imports() throws HongsException {
    fields = new LinkedHashMap();
    Loop rs = db.query("SELECT * FROM `" + tn + "`", 0, 1);
    try {
        ResultSetMetaData md = rs.getMetaData();
        for (int i = 1; i <= md.getColumnCount(); i++) {
            Map field = new HashMap();
            field.put("type", md.getColumnType(i));
            field.put("size", md.getPrecision(i));
            field.put("scale", md.getScale(i));
            field.put("unsigned", !md.isSigned(i));
            field.put("required", md.isNullable(i) == ResultSetMetaData.columnNoNulls);
            field.put("autoIncrement", md.isAutoIncrement(i));
            field.put("caseSensitive", md.isCaseSensitive(i));
            // 用处不大的的属性:
            /*
        field.put("currency",       md.isCurrency(i));
        field.put("readOnly",       md.isReadOnly(i));
        field.put("writable",       md.isWritable(i));
        field.put("searchable",     md.isSearchable(i));
        field.put("tableName",      md.getTableName(i));
        field.put("schemaName",     md.getSchemaName(i));
        field.put("catalogName",    md.getCatalogName(i));
        field.put("label",          md.getColumnLable(i));
        field.put("typeName",       md.getColumnTypeName(i));
        field.put("className",      md.getColumnClassName(i));
        field.put("displaySize",    md.getColumnDisplaySize(i));
        */
            this.fields.put(md.getColumnName(i), field);
        }
    } catch (SQLException ex) {
        throw new HongsException(0x1071, ex);
    } finally {
        rs.close();
    }
}
Also used : Loop(app.hongs.db.link.Loop) ResultSetMetaData(java.sql.ResultSetMetaData) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) HongsException(app.hongs.HongsException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

HongsException (app.hongs.HongsException)89 Map (java.util.Map)42 HashMap (java.util.HashMap)34 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)15 LinkedHashMap (java.util.LinkedHashMap)15 Set (java.util.Set)15 List (java.util.List)13 File (java.io.File)11 SQLException (java.sql.SQLException)10 FileNotFoundException (java.io.FileNotFoundException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 PreparedStatement (java.sql.PreparedStatement)8 Iterator (java.util.Iterator)8 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 HongsExpedient (app.hongs.HongsExpedient)7 FormSet (app.hongs.action.FormSet)7 Table (app.hongs.db.Table)7