⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsjsploader.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    }
    
    /**
     * Basic top-page processing method for this I_CmsResourceLoader,
     * this method is called by <code>initlaunch()</code> if a JSP is requested and
     * the original request was from the launcher manager.
     *
     * @param cms The initialized CmsObject which provides user permissions
     * @param file The requested OpenCms VFS resource
     * @param req The original servlet request
     * @param res The original servlet response
     * 
     * @throws ServletException might be thrown in the process of including the JSP 
     * @throws IOException might be thrown in the process of including the JSP 
     * 
     * @see I_CmsResourceLoader
     * @see #initlaunch(CmsObject cms, CmsFile file, String startTemplateClass, A_OpenCms openCms)
     */
    public void load(CmsObject cms, CmsFile file, HttpServletRequest req, HttpServletResponse res) 
    throws ServletException, IOException {       

        long timer1 = 0;
        if (DEBUG > 0) {
            timer1 = System.currentTimeMillis();        
            System.err.println("========== JspLoader loading: " + file.getAbsolutePath());
            System.err.println("JspLoader.load()  cms uri is: " + cms.getRequestContext().getUri());
        }

        boolean streaming = false;            
        boolean bypass = false;
        
        // check if export mode is active, if so "streaming" must be deactivated
        boolean exportmode = (cms.getMode() == CmsObject.C_MODUS_EXPORT);
        
        try {
            // Read caching property from requested VFS resource                                     
            String stream = cms.readProperty(file.getAbsolutePath(), I_CmsResourceLoader.C_LOADER_STREAMPROPERTY);                    
            if (stream != null) {
                if ("yes".equalsIgnoreCase(stream) || "true".equalsIgnoreCase(stream)) {
                    // streaming not allowed in export mode
                    streaming = !exportmode;                
                } else if ("bypass".equalsIgnoreCase(stream) || "bypasscache".equalsIgnoreCase(stream)) {
                    // bypass not allowed in export mode
                    bypass = !exportmode;
                }
            }
        } catch (CmsException e) {
            throw new ServletException("FlexJspLoader: Error while loading stream properties for " + file.getAbsolutePath() + "\n" + e, e);
        } 
        
        if (DEBUG > 1) System.err.println("========== JspLoader stream=" + streaming + " bypass=" + bypass);
          
        CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME);
                
        CmsFlexRequest f_req; 
        CmsFlexResponse f_res;
        
        if (controller != null) {
            // re-use currently wrapped request / response
            f_req = controller.getCurrentRequest();
            f_res = controller.getCurrentResponse();
        } else {
            // create new request / response wrappers
            controller = new CmsFlexController(cms, file, m_cache, req, res);
            req.setAttribute(CmsFlexController.ATTRIBUTE_NAME, controller);
            f_req = new CmsFlexRequest(req, controller);
            f_res = new CmsFlexResponse(res, controller, streaming, true);
            controller.pushRequest(f_req);
            controller.pushResponse(f_res);
        }
        
        if (bypass) {
            // Bypass Flex cache for this page (this solves some compatibility issues in BEA Weblogic)        
            if (DEBUG > 1) System.err.println("JspLoader.load() bypassing cache for file " + file.getAbsolutePath());
            // Update the JSP first if neccessary            
            String target = updateJsp(cms, file, f_req, controller, new HashSet(11));
            // Dispatch to external JSP
            req.getRequestDispatcher(target).forward(f_req, res);              
            if (DEBUG > 1) System.err.println("JspLoader.load() cache was bypassed!");
        } else {
            // Flex cache not bypassed            
            try {
                f_req.getRequestDispatcher(file.getAbsolutePath()).include(f_req, f_res);
            } catch (java.net.SocketException e) {        
                // Uncritical, might happen if client (browser) does not wait until end of page delivery
                if (DEBUG > 1) System.err.println("JspLoader.load() ignoring SocketException " + e);
            }            
            if (! streaming && ! f_res.isSuspended()) {
                try {      
                    if (! res.isCommitted() || m_errorPagesAreNotCommited) {
                        // If a JSP errorpage was triggered the response will be already committed here
                        byte[] result = f_res.getWriterBytes();
                        
                        // Encoding project:  
                        // The byte array will internally be encoded in the OpenCms 
                        // default encoding. In case another encoding is set 
                        // in the 'content-encoding' property of the file, 
                        // we need to re-encode the output here. 
                        result = Encoder.changeEncoding(result, A_OpenCms.getDefaultEncoding(), cms.getRequestContext().getEncoding());   
                                                                                                                              
                        // Check for export request links 
                        if (exportmode) {
                            exportSetLinkHeader(cms, f_res);
                        }
                              
                        // Process headers and write output                                          
                        res.setContentLength(result.length);
                        CmsFlexResponse.processHeaders(f_res.getHeaders(), res);                        
                        res.getOutputStream().write(result);
                        res.getOutputStream().flush();
                    } else if (DEBUG > 1) {
                        System.err.println("JspLoader.load() resource is already commited!");
                    }
                } catch (IllegalStateException e) {
                    // Uncritical, might happen if JSP error page was used
                    if (DEBUG > 1) System.err.println("JspLoader.load() ignoring IllegalStateException " + e);
                } catch (java.net.SocketException e) {        
                    // Uncritical, might happen if client (browser) does not wait until end of page delivery
                    if (DEBUG > 1) System.err.println("JspLoader.load() ignoring SocketException " + e);
                }       
            }
        }
        
        if (DEBUG > 0) {
            long timer2 = System.currentTimeMillis() - timer1;
            System.err.println("========== JspLoader time delivering JSP for " + file.getAbsolutePath() + ": " + timer2 + "ms");
        }        
    }
    
    /**
     * Method to enable JSPs to be used as sub-elements in XMLTemplates.
     *
     * @param cms The initialized CmsObject which provides user permissions
     * @param file The requested OpenCms VFS resource
     * 
     * @throws CmsException In case the Loader can not process the requested resource
     * 
     * @see CmsJspTemplate
     */
    public byte[] loadTemplate(CmsObject cms, CmsFile file) 
    throws CmsException {

        byte[] result = null;
        
        long timer1 = 0;
        if (DEBUG > 0) {
            timer1 = System.currentTimeMillis();        
            System.err.println("========== JspLoader (Template) loading: " + file.getAbsolutePath());
        }       

        if (cms.getRequestContext().getRequest() instanceof CmsExportRequest) {
            if (DEBUG > 1) System.err.println("FlexJspLoader.loadTemplate(): Export requested for " + file.getAbsolutePath());
            // export the JSP
            result = exportJsp(cms, file);
        } else {
            HttpServletRequest req = (HttpServletRequest)cms.getRequestContext().getRequest().getOriginalRequest();
            HttpServletResponse res = (HttpServletResponse)cms.getRequestContext().getResponse().getOriginalResponse();             
                        
            CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME);
                    
            CmsFlexRequest f_req; 
            CmsFlexResponse f_res;
        
            if (controller != null) {
                // re-use currently wrapped request / response
                f_req = controller.getCurrentRequest();
                f_res = controller.getCurrentResponse();
            } else {
                // create new request / response wrappers
                controller = new CmsFlexController(cms, file, m_cache, req, res);
                req.setAttribute(CmsFlexController.ATTRIBUTE_NAME, controller);
                f_req = new CmsFlexRequest(req, controller);
                f_res = new CmsFlexResponse(res, controller, false, false);
                controller.pushRequest(f_req);
                controller.pushResponse(f_res);
            }
            
            try {
                f_req.getRequestDispatcher(file.getAbsolutePath()).include(f_req, f_res);
            } catch (java.net.SocketException e) {        
                // Uncritical, might happen if client (browser) does not wait until end of page delivery
                if (DEBUG > 1) System.err.println("JspLoader.loadTemplate() ignoring SocketException " + e);
            } catch (Exception e) {            
                System.err.println("Error in CmsJspLoader.loadTemplate() while loading: " + e.toString());
                if (DEBUG > 0) System.err.println(com.opencms.util.Utils.getStackTrace(e));
                throw new CmsException("Error in CmsJspLoader.loadTemplate() while loading " + file.getAbsolutePath() + "\n" + e, CmsException.C_LAUNCH_ERROR, e);
            } 
    
            if (! f_res.isSuspended()) {
                try {      
                    if ((res == null) || (! res.isCommitted())) {
                        // If a JSP errorpage was triggered the response will be already committed here
                        result = f_res.getWriterBytes();                                                
                        // Encoding project:
                        // The byte array will internally be encoded in the OpenCms
                        // default encoding. In case another encoding is set
                        // in the 'content-encoding' property of the file,
                        // we need to re-encode the output here
                        result = Encoder.changeEncoding(result, A_OpenCms.getDefaultEncoding(), cms.getRequestContext().getEncoding());                                              
                    }
                } catch (IllegalStateException e) {
                    // Uncritical, might happen if JSP error page was used
                    if (DEBUG > 1) System.err.println("JspLoader.loadTemplate() ignoring IllegalStateException " + e);
                } catch (Exception e) {
                    System.err.println("Error in CmsJspLoader.loadTemplate() while writing buffer to final stream: " + e.toString());
                    if (DEBUG > 0) System.err.println(com.opencms.util.Utils.getStackTrace(e));
                    throw new CmsException("Error in CmsJspLoader.loadTemplate() while writing buffer to final stream for " + file.getAbsolutePath() + "\n" + e, CmsException.C_LAUNCH_ERROR, e);
                }        
            }
        }
        
        if (DEBUG > 0) {
            long timer2 = System.currentTimeMillis() - timer1;
            System.err.println("========== JspLoader (Template) time delivering JSP for " + file.getAbsolutePath() + ": " + timer2 + "ms");
        }        
        
        return result;
    }
    
    /**
     * Translates the JSP file name for a OpenCms VFS resourcn 
     * to the name used in the "real" file system.<p>
     * 
     * The name given must be a absolute URI in the OpenCms VFS,
     * e.g. CmsFile.getAbsolutePath()
     *
     * @param name The file to calculate the JSP name for
     * @return The JSP name for the file
     */    
    public static String getJspName(String name) {
        return name + C_JSP_EXTENSION;
    }
    
    /**
     * Returns the uri for a given JSP in the "real" file system, 
     * i.e. the path in the file
     * system relative to the web application directory.
     *
     * @param name The name of the JSP file 
     * @param online Flag to check if this is request is online or not
     * @return The full uri to the JSP
     */
    public static String getJspUri(String name, boolean online) {
        return m_jspWebAppRepository + (online?"/online":"/offline") + getJspName(name);  
    }
    
    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -