Search in sources :

Example 6 with RenderException

use of com.jfinal.render.RenderException in project jfinal by jfinal.

the class QrCodeRender method render.

public void render() {
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/png");
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    //去掉白色边框,极度重要,否则二维码周围的白边会很宽
    hints.put(EncodeHintType.MARGIN, 0);
    if (errorCorrectionLevel != null) {
        hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
    }
    try {
        // MultiFormatWriter 可支持多种格式的条形码,在此直接使用 QRCodeWriter,通过查看源码可知少创建一个对象
        // BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        // 经测试 200 X 200 大小的二维码使用 "png" 格式只有 412B,而 "jpg" 却达到 15KB
        // format: "jpg"、"png"
        MatrixToImageWriter.writeToStream(bitMatrix, "png", response.getOutputStream());
    } catch (Exception e) {
        throw new RenderException(e);
    }
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) RenderException(com.jfinal.render.RenderException) EncodeHintType(com.google.zxing.EncodeHintType) HashMap(java.util.HashMap) BitMatrix(com.google.zxing.common.BitMatrix) RenderException(com.jfinal.render.RenderException)

Example 7 with RenderException

use of com.jfinal.render.RenderException in project jfinal by jfinal.

the class ActionHandler method handle.

/**
	 * handle
	 * 1: Action action = actionMapping.getAction(target)
	 * 2: new Invocation(...).invoke()
	 * 3: render(...)
	 */
public final void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
    if (target.indexOf('.') != -1) {
        return;
    }
    isHandled[0] = true;
    String[] urlPara = { null };
    Action action = actionMapping.getAction(target, urlPara);
    if (action == null) {
        if (log.isWarnEnabled()) {
            String qs = request.getQueryString();
            log.warn("404 Action Not Found: " + (qs == null ? target : target + "?" + qs));
        }
        renderManager.getRenderFactory().getErrorRender(404).setContext(request, response).render();
        return;
    }
    try {
        Controller controller = action.getControllerClass().newInstance();
        controller.init(request, response, urlPara[0]);
        if (devMode) {
            if (ActionReporter.isReportAfterInvocation(request)) {
                new Invocation(action, controller).invoke();
                ActionReporter.report(target, controller, action);
            } else {
                ActionReporter.report(target, controller, action);
                new Invocation(action, controller).invoke();
            }
        } else {
            new Invocation(action, controller).invoke();
        }
        Render render = controller.getRender();
        if (render instanceof ForwardActionRender) {
            String actionUrl = ((ForwardActionRender) render).getActionUrl();
            if (target.equals(actionUrl)) {
                throw new RuntimeException("The forward action url is the same as before.");
            } else {
                handle(actionUrl, request, response, isHandled);
            }
            return;
        }
        if (render == null) {
            render = renderManager.getRenderFactory().getDefaultRender(action.getViewPath() + action.getMethodName());
        }
        render.setContext(request, response, action.getViewPath()).render();
    } catch (RenderException e) {
        if (log.isErrorEnabled()) {
            String qs = request.getQueryString();
            log.error(qs == null ? target : target + "?" + qs, e);
        }
    } catch (ActionException e) {
        int errorCode = e.getErrorCode();
        String msg = null;
        if (errorCode == 404) {
            msg = "404 Not Found: ";
        } else if (errorCode == 401) {
            msg = "401 Unauthorized: ";
        } else if (errorCode == 403) {
            msg = "403 Forbidden: ";
        }
        if (msg != null) {
            if (log.isWarnEnabled()) {
                String qs = request.getQueryString();
                log.warn(msg + (qs == null ? target : target + "?" + qs));
            }
        } else {
            if (log.isErrorEnabled()) {
                String qs = request.getQueryString();
                log.error(qs == null ? target : target + "?" + qs, e);
            }
        }
        e.getErrorRender().setContext(request, response, action.getViewPath()).render();
    } catch (Exception e) {
        if (log.isErrorEnabled()) {
            String qs = request.getQueryString();
            log.error(qs == null ? target : target + "?" + qs, e);
        }
        renderManager.getRenderFactory().getErrorRender(500).setContext(request, response, action.getViewPath()).render();
    }
}
Also used : RenderException(com.jfinal.render.RenderException) Invocation(com.jfinal.aop.Invocation) Render(com.jfinal.render.Render) RenderException(com.jfinal.render.RenderException)

Example 8 with RenderException

use of com.jfinal.render.RenderException in project my_curd by qinyou.

the class PoiRender method render.

@Override
public void render() {
    response.reset();
    response.setHeader("Content-disposition", "attachment; filename=" + fileName);
    response.setContentType(CONTENT_TYPE);
    OutputStream os = null;
    try {
        os = response.getOutputStream();
        PoiKit.with(data).sheetName(sheetName).headerRow(headerRow).headers(headers).columns(columns).cellWidth(cellWidth).export().write(os);
    } catch (Exception e) {
        throw new RenderException(e);
    } finally {
        try {
            if (os != null) {
                os.flush();
                os.close();
            }
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
Also used : RenderException(com.jfinal.render.RenderException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) IOException(java.io.IOException) RenderException(com.jfinal.render.RenderException)

Aggregations

RenderException (com.jfinal.render.RenderException)8 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)2 HashMap (java.util.HashMap)2 EncodeHintType (com.google.zxing.EncodeHintType)1 BitMatrix (com.google.zxing.common.BitMatrix)1 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)1 Invocation (com.jfinal.aop.Invocation)1 Render (com.jfinal.render.Render)1 Template (freemarker.template.Template)1 BufferedImage (java.awt.image.BufferedImage)1 OutputStream (java.io.OutputStream)1 Writer (java.io.Writer)1 Map (java.util.Map)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Cookie (javax.servlet.http.Cookie)1