Search in sources :

Example 11 with HongsError

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

the class ActionHelper method responed.

// ** 跳转及错误 **/
/**
 * 返回数据
 * 将以 JSON/JSONP 格式输出, 编码 UTF-8
 */
public void responed() {
    if (null == this.responseData) {
        return;
    }
    // 动作调用了 forward 之后
    // 这时再调用 getWriter 会抛 STREAM 异常
    // 因容器调用 getOutputStream 来输出内容
    // 所以必须将 OutputStream 包装成 Writer
    Writer out;
    try {
        out = getOutputWriter();
    } catch (IllegalStateException e) {
        out = new PrintWriter(getOutputStream());
    }
    // 检查是否有 JSONP 的回调函数
    String fun;
    fun = getParameter(Cnst.CB_KEY);
    if (fun == null || fun.isEmpty()) {
        fun = getParameter(CoreConfig.getInstance().getProperty("core.act.call", "callback"));
    }
    // 此情况下需将回调包裹上 HTML.
    try {
        if (fun != null && fun.length() != 0) {
            if (fun.startsWith("top.") || fun.startsWith("parent.") || fun.startsWith("opener.")) {
                if (!this.response.isCommitted()) {
                    this.response.setCharacterEncoding("UTF-8");
                    this.response.setContentType("text/html");
                }
                out.append("<script type=\"text/javascript\">");
                out.append(fun);
                out.append("(");
                Data.append(out, this.responseData);
                out.append(");");
                out.append("</script>");
            } else {
                if (!this.response.isCommitted()) {
                    this.response.setCharacterEncoding("UTF-8");
                    this.response.setContentType("text/javascript");
                }
                out.append(fun);
                out.append("(");
                Data.append(out, this.responseData);
                out.append(");");
            }
        } else {
            if (!this.response.isCommitted()) {
                this.response.setCharacterEncoding("UTF-8");
                this.response.setContentType("application/json");
            }
            Data.append(out, this.responseData);
        }
    } catch (IOException e) {
        throw new HongsError(0x32, "Can not send to client.", e);
    }
    this.responseData = null;
}
Also used : HongsError(app.hongs.HongsError) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 12 with HongsError

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

the class ActionHelper method updateHelper.

/**
 * 供 ActionDriver 重设助手
 * @param req
 * @param rsp
 */
public final void updateHelper(HttpServletRequest req, HttpServletResponse rsp) {
    this.request = req;
    this.response = rsp;
    try {
        if (null != this.request) {
            this.request.setCharacterEncoding("UTF-8");
        }
        if (null != this.response) {
            this.response.setCharacterEncoding("UTF-8");
        }
    } catch (UnsupportedEncodingException ex) {
        throw new HongsError(0x31, "Can not set encoding.", ex);
    }
    this.outputStream = null;
    this.outputWriter = null;
}
Also used : HongsError(app.hongs.HongsError) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

HongsError (app.hongs.HongsError)12 Core (app.hongs.Core)5 HongsExpedient (app.hongs.HongsExpedient)4 ActionHelper (app.hongs.action.ActionHelper)4 HongsException (app.hongs.HongsException)3 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Method (java.lang.reflect.Method)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 List (java.util.List)2 CoreLocale (app.hongs.CoreLocale)1 ActionRunner (app.hongs.action.ActionRunner)1 NaviMap (app.hongs.action.NaviMap)1 Action (app.hongs.action.anno.Action)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1