Search in sources :

Example 1 with XMLWriter

use of org.eweb4j.util.xml.XMLWriter in project eweb4j-framework by laiweiwei.

the class DAOConfig method rebuildXmlFile.

private static String rebuildXmlFile(File configFile, String err) {
    String error = null;
    try {
        // 保存为备份文件
        File tf = new File(configFile.getAbsolutePath() + ".back" + CommonUtil.getNowTime("_MMddHHmmss"));
        FileUtil.copy(configFile, tf);
        log.debug("backup file->" + tf.getAbsolutePath());
        XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(configFile, DAOConfigBeanCreator.getDAOBean());
        writer.setBeanName("dataSource");
        writer.setClass("dataSource", DBInfoConfigBean.class);
        writer.write();
        StringBuilder tsb = new StringBuilder(ConfigInfoCons.REPAIR_CONFIG_FILE);
        tsb.append(err);
        error = tsb.toString();
        log.error(error);
    } catch (Exception e1) {
        e1.printStackTrace();
        StringBuilder sb2 = new StringBuilder(ConfigInfoCons.CANNOT_REPAIR_FILE);
        sb2.append(CommonUtil.getExceptionString(e1));
        error = sb2.toString();
        log.error(error, e1);
    }
    return error;
}
Also used : File(java.io.File) XMLWriter(org.eweb4j.util.xml.XMLWriter)

Example 2 with XMLWriter

use of org.eweb4j.util.xml.XMLWriter in project eweb4j-framework by laiweiwei.

the class ORMConfig method rebuildXmlFile.

private static String rebuildXmlFile(File configFile, String err) {
    String error;
    try {
        // 保存为备份文件
        File tf = new File(configFile.getAbsolutePath() + ".back" + CommonUtil.getNowTime("_MMddHHmmss"));
        FileUtil.copy(configFile, tf);
        log.debug("backup file ->" + tf.getAbsolutePath());
        XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(configFile, ORMConfigBeanCreator.getORMBean());
        writer.setBeanName("orm");
        writer.setClass("orm", ORMConfigBean.class);
        writer.write();
        StringBuilder sb = new StringBuilder(ConfigInfoCons.REPAIR_INFO);
        sb.append(err);
        error = sb.toString();
        log.error(error);
    } catch (Throwable e1) {
        e1.printStackTrace();
        StringBuilder sb3 = new StringBuilder(CommonUtil.getExceptionString(e1));
        sb3.append(ConfigInfoCons.CANNOT_REPAIR_CONFIG_FILE);
        error = sb3.toString();
        log.error(error, e1);
    }
    return error;
}
Also used : File(java.io.File) XMLWriter(org.eweb4j.util.xml.XMLWriter)

Example 3 with XMLWriter

use of org.eweb4j.util.xml.XMLWriter in project eweb4j-framework by laiweiwei.

the class ActionExecution method handleResult.

private void handleResult() throws Exception {
    this.exeActionLog();
    if (retn == null)
        return;
    String baseUrl = (String) this.context.getServletContext().getAttribute(MVCConfigConstant.BASE_URL_KEY);
    if (File.class.isAssignableFrom(retn.getClass())) {
        File file = (File) retn;
        this.handleDownload(file);
        return;
    } else if (File[].class.isAssignableFrom(retn.getClass())) {
        File[] files = (File[]) retn;
        String fileName = CommonUtil.getNowTime("yyyyMMddHHmmss") + "_" + "download.zip";
        HttpServletResponse resp = this.context.getResponse();
        resp.reset();
        resp.setContentType("application/zip");
        resp.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        ServletOutputStream outputStream = resp.getOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (File file : files) {
            byte[] b = new byte[1024];
            int len;
            zip.putNextEntry(new ZipEntry(file.getName()));
            FileInputStream fis = new FileInputStream(file);
            while ((len = fis.read(b)) != -1) {
                zip.write(b, 0, len);
            }
            fis.close();
        }
        zip.flush();
        zip.close();
        outputStream.flush();
        return;
    }
    if (!String.class.isAssignableFrom(retn.getClass())) {
        String mimeType = null;
        boolean hasProduces = JAXWSUtil.hasProduces(method);
        if (hasProduces) {
            String[] mimeTypes = ProducesUtil.getProducesValue(method);
            if (mimeTypes != null && mimeTypes.length > 0)
                mimeType = mimeTypes[0];
        }
        if (mimeType == null || mimeType.trim().length() == 0)
            mimeType = this.context.getRequest().getParameter(MVCConfigConstant.HTTP_HEADER_ACCEPT_PARAM);
        if (mimeType == null || mimeType.trim().length() == 0) {
            String contentType = this.context.getRequest().getContentType();
            if (contentType != null) {
                this.context.getResponse().setContentType(contentType);
                mimeType = contentType.split(";")[0];
            }
        }
        if (this.context.getWriter() == null)
            this.context.setWriter(this.context.getResponse().getWriter());
        if (MIMEType.JSON.equals(mimeType) || "json".equalsIgnoreCase(mimeType)) {
            this.context.getResponse().setContentType(MIMEType.JSON);
            this.context.getWriter().print(CommonUtil.toJson(retn));
        } else if (MIMEType.XML.equals(mimeType) || "xml".equalsIgnoreCase(mimeType)) {
            Class<?> cls = retn.getClass();
            if (Collection.class.isAssignableFrom(cls)) {
                Class<?> _cls = ClassUtil.getPojoClass(this.method);
                if (_cls != null)
                    cls = _cls;
            }
            XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(retn);
            writer.setCheckStatck(true);
            writer.setSubNameAuto(true);
            writer.setClass(cls);
            writer.setRootElementName(null);
            this.context.getResponse().setContentType(MIMEType.XML);
            this.context.getWriter().print(writer.toXml());
        } else {
            //默认都用json
            this.context.getResponse().setContentType(MIMEType.JSON);
            this.context.getWriter().print(CommonUtil.toJson(retn));
        }
        return;
    }
    List<String> produces = this.context.getActionConfigBean().getProduces();
    if (produces != null && produces.size() > 0)
        for (String produce : produces) {
            this.context.getResponse().setContentType(produce);
            break;
        }
    String re = String.valueOf(retn);
    //model driven
    for (Field f : fields) {
        Method getter = ru.getGetter(f.getName());
        if (getter == null)
            continue;
        String name = f.getName();
        if (this.context.getModel().containsKey(name))
            continue;
        this.context.getModel().put(name, getter.invoke(actionObject));
    }
    this.context.getModel().put(MVCConfigConstant.BASE_URL_KEY, this.context.getServletContext().getAttribute(MVCConfigConstant.BASE_URL_KEY));
    this.context.getModel().put(MVCConfigConstant.APPLICATION_SCOPE_KEY, new ServletContextProxy(this.context.getServletContext()).attrs());
    this.context.getModel().put(MVCConfigConstant.SESSION_SCOPE_KEY, new HttpSessionProxy(this.context.getSession()).attrs());
    this.context.getModel().put(MVCConfigConstant.COOKIE_SCOPE_KEY, new CookieProxy(this.context.getRequest().getCookies()).attrs());
    this.context.getModel().put(MVCConfigConstant.REQ_PARAM_SCOPE_KEY, this.context.getQueryParamMap());
    // 客户端重定向
    if (re.startsWith(RenderType.REDIRECT + ":")) {
        String url = re.substring((RenderType.REDIRECT + ":").length());
        String location = url;
        this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));
        return;
    } else if (re.startsWith(RenderType.ACTION + ":")) {
        String path = re.substring((RenderType.ACTION + ":").length());
        // ACTION 重定向
        handleActionRedirect(context, path, baseUrl);
        return;
    } else if (re.startsWith(RenderType.OUT + ":")) {
        String location = re.substring((RenderType.OUT + ":").length());
        this.context.getWriter().print(location);
        return;
    } else if (re.startsWith(RenderType.FORWARD + ":") || re.startsWith(RenderType.JSP + ":") || re.endsWith("." + RenderType.JSP)) {
        String[] str = re.split("@");
        re = str[0];
        String location = re;
        if (re.startsWith(RenderType.FORWARD + ":"))
            location = re.substring((RenderType.FORWARD + ":").length());
        else if (re.startsWith(RenderType.JSP + ":"))
            location = re.substring((RenderType.JSP + ":").length());
        //渲染JSP
        JSPRendererImpl render = new JSPRendererImpl();
        render.setContext(context);
        if (str.length > 1)
            render.layout(str[1]);
        render.target(location).render(context.getWriter(), context.getModel());
        return;
    } else if (re.startsWith(RenderType.FREEMARKER + ":") || re.startsWith(RenderType.FREEMARKER2 + ":") || re.endsWith("." + RenderType.FREEMARKER2)) {
        String[] str = re.split("@");
        re = str[0];
        String location = re;
        if (re.startsWith(RenderType.FREEMARKER + ":"))
            location = re.substring((RenderType.FREEMARKER + ":").length());
        else if (re.startsWith(RenderType.FREEMARKER2 + ":"))
            location = re.substring((RenderType.FREEMARKER2 + ":").length());
        //渲染Freemarker
        Renderer render = RenderFactory.create(RenderType.FREEMARKER).target(location);
        if (str.length > 1)
            render.layout(str[1]);
        render.render(context.getWriter(), context.getModel());
        //	        this.context.getWriter().flush();
        return;
    } else if (re.startsWith(RenderType.VELOCITY + ":") || re.startsWith(RenderType.VELOCITY2 + ":") || re.endsWith("." + RenderType.VELOCITY2)) {
        String[] str = re.split("@");
        re = str[0];
        String location = re;
        if (re.startsWith(RenderType.VELOCITY + ":"))
            location = re.substring((RenderType.VELOCITY + ":").length());
        else if (re.startsWith(RenderType.VELOCITY2 + ":"))
            location = re.substring((RenderType.VELOCITY2 + ":").length());
        //渲染Velocity
        Renderer render = RenderFactory.create(RenderType.VELOCITY).target(location);
        if (str.length > 1)
            render.layout(str[1]);
        render.render(context.getWriter(), context.getModel());
        //	        this.context.getWriter().flush();
        return;
    } else {
        List<ResultConfigBean> results = this.context.getActionConfigBean().getResult();
        if (results == null || results.size() == 0) {
            this.context.getWriter().print(retn);
            //				this.context.getWriter().flush();
            return;
        }
        boolean isOut = true;
        for (ResultConfigBean r : results) {
            if (!"_props_".equals(r.getName()) && !r.getName().equals(re) && !"".equals(re)) {
                continue;
            }
            isOut = false;
            String type = r.getType();
            String location = r.getLocation();
            if (RenderType.REDIRECT.equalsIgnoreCase(type)) {
                this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));
                return;
            } else if (RenderType.FORWARD.equalsIgnoreCase(type) || RenderType.JSP.equalsIgnoreCase(type)) {
                //渲染JSP
                String[] str = location.split("@");
                JSPRendererImpl render = new JSPRendererImpl();
                render.setContext(context);
                if (str.length > 1)
                    render.layout(str[1]);
                render.target(str[0]).render(context.getWriter(), context.getModel());
                return;
            } else if (RenderType.FREEMARKER.equalsIgnoreCase(type) || RenderType.FREEMARKER2.equalsIgnoreCase(type)) {
                //渲染Freemarker
                String[] str = location.split("@");
                Renderer render = RenderFactory.create(RenderType.FREEMARKER).target(str[0]);
                if (str.length > 1)
                    render.layout(str[1]);
                render.render(context.getWriter(), context.getModel());
                return;
            } else if (RenderType.VELOCITY.equalsIgnoreCase(type) || RenderType.VELOCITY2.equalsIgnoreCase(type)) {
                //渲染Velocity
                String[] str = location.split("@");
                Renderer render = RenderFactory.create(RenderType.VELOCITY).target(str[0]);
                if (str.length > 1)
                    render.layout(str[1]);
                render.render(context.getWriter(), context.getModel());
                return;
            } else if (RenderType.ACTION.equalsIgnoreCase(type)) {
                // ACTION 重定向
                handleActionRedirect(context, location, baseUrl);
                return;
            } else if (RenderType.OUT.equalsIgnoreCase(type) || location.trim().length() == 0) {
                this.context.getWriter().print(location);
                return;
            }
        }
        if (isOut) {
            this.context.getWriter().print(retn);
        //				this.context.getWriter().flush();
        }
    }
}
Also used : ServletContextProxy(org.eweb4j.mvc.ServletContextProxy) ServletOutputStream(javax.servlet.ServletOutputStream) ZipEntry(java.util.zip.ZipEntry) HttpServletResponse(javax.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) CookieProxy(org.eweb4j.mvc.CookieProxy) XMLWriter(org.eweb4j.util.xml.XMLWriter) FileInputStream(java.io.FileInputStream) Field(java.lang.reflect.Field) HttpSessionProxy(org.eweb4j.mvc.HttpSessionProxy) ZipOutputStream(java.util.zip.ZipOutputStream) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) JSPRendererImpl(org.eweb4j.mvc.view.JSPRendererImpl) Renderer(org.eweb4j.mvc.view.Renderer) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) UploadFile(org.eweb4j.mvc.upload.UploadFile) File(java.io.File)

Example 4 with XMLWriter

use of org.eweb4j.util.xml.XMLWriter in project eweb4j-framework by laiweiwei.

the class EWeb4JConfig method startByAbFile.

private static synchronized String startByAbFile(String aStartXmlPath) {
    String startXmlPath = aStartXmlPath;
    String error = null;
    File file = null;
    boolean readXml = true;
    if (ConfigConstant.SUCCESS_START.equals(String.valueOf(SingleBeanCache.get(ConfigConstant.SUCCESS_START)))) {
        ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
        String reload = (cb == null ? "true" : cb.getReload());
        if ("true".equals(reload) || "1".equals(reload)) {
            // 如果开了DEBUG,清空缓存,重新读取配置文件
            SingleBeanCache.clear();
            ORMConfigBeanCache.clear();
            IOCConfigBeanCache.clear();
            ActionConfigBeanCache.clear();
            log.debug("EWeb4J clear cache");
            readXml = true;
        } else {
            // 否则,不需要读取配置文件
            readXml = false;
        }
    }
    if (readXml) {
        // 1.读取配置文件
        try {
            file = new File(startXmlPath);
            ConfigBean cb = null;
            boolean readFile = true;
            final String check = ConfigConstant.CHECK_START_FILE_EXIST;
            if (!file.exists()) {
                if (!"true".equals(check) && !"1".equals(check)) {
                    log.warn("Skip the Start Configuation file !!!");
                    cb = ConfigBeanCreator.create();
                    readFile = false;
                }
            }
            if (readFile) {
                XMLReader reader = BeanXMLUtil.getBeanXMLReader(file);
                reader.setBeanName("eweb4j");
                reader.setClass("eweb4j", ConfigBean.class);
                cb = reader.readOne();
            }
            error = _start(error, cb);
        } catch (Throwable e) {
            // 重写配置文件
            try {
                // 保存为备份文件
                FileUtil.copy(file, new File(startXmlPath + ".back" + "_" + CommonUtil.getNowTime("MMddHHmmss")));
                XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(file, ConfigBeanCreator.create());
                writer.setBeanName("eweb4j");
                writer.setClass("eweb4j", ConfigBean.class);
                writer.write();
                String info = "configuration error, now it has repaired.";
                error = CommonUtil.getNowTime() + "EWeb4JConfig : " + info + "exception:" + CommonUtil.getExceptionString(e);
                log.error(info, e);
            } catch (Throwable e1) {
                String info = "can not write any configuration";
                error = CommonUtil.getNowTime() + "EWeb4JConfig : " + info + "exception:" + CommonUtil.getExceptionString(e1);
                log.fatal(info, e);
            }
        }
        _done(error);
    }
    return error;
}
Also used : DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) File(java.io.File) XMLWriter(org.eweb4j.util.xml.XMLWriter) XMLReader(org.eweb4j.util.xml.XMLReader)

Example 5 with XMLWriter

use of org.eweb4j.util.xml.XMLWriter in project eweb4j-framework by laiweiwei.

the class ActionConfig method rebuildXmlFile.

private static String rebuildXmlFile(File configFile, String err) {
    String error;
    try {
        // 保存为备份文件
        File tf = new File(configFile.getAbsolutePath() + ".back" + CommonUtil.getNowTime("_MMddHHmmss"));
        FileUtil.copy(configFile, tf);
        log.debug("backup file->" + tf.getAbsolutePath());
        XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(configFile, MVCConfigBeanCreator.getActionBean());
        writer.setBeanName("action");
        writer.setClass("action", ActionConfigBean.class);
        writer.write();
        StringBuilder tsb = new StringBuilder(ConfigErrCons.CONFIG_INFO_READ_ERROR);
        tsb.append(err);
        error = tsb.toString();
        // log-----
        log.error(error);
    } catch (Throwable e1) {
        StringBuilder sb2 = new StringBuilder(ConfigErrCons.CANNOT_REPAIR_CONFIG_FILE);
        error = sb2.toString();
        log.error(error, e1);
    }
    return error;
}
Also used : File(java.io.File) XMLWriter(org.eweb4j.util.xml.XMLWriter)

Aggregations

XMLWriter (org.eweb4j.util.xml.XMLWriter)11 File (java.io.File)10 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ConfigBean (org.eweb4j.config.bean.ConfigBean)1 CookieProxy (org.eweb4j.mvc.CookieProxy)1 HttpSessionProxy (org.eweb4j.mvc.HttpSessionProxy)1 ServletContextProxy (org.eweb4j.mvc.ServletContextProxy)1 ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)1 UploadFile (org.eweb4j.mvc.upload.UploadFile)1 JSPRendererImpl (org.eweb4j.mvc.view.JSPRendererImpl)1