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

📄 davservlet.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        try {
            User user = CoreServlet.getServlet().getLogonController().getUser(request);
            if (user != null) {
                webdavSession.setAttribute(DAVSession.ATTR_USER, user);
            }
        } catch (InvalidTicketException e1) {
        }
        if (resourcePath != null) {
            webdavSession.setAttribute(DAVSession.ATTR_RESOURCE_PATH, resourcePath);
        }
        return webdavSession;
    }

    public static DAVProcessor getDAVProcessor(HttpServletRequest req) throws DAVBundleActionMessageException, Exception {
        SessionInfo session = CoreServlet.getServlet().getLogonController().getSessionInfo(req);
        DAVProcessor processor = (DAVProcessor) req.getSession().getAttribute(PROCESSOR_ATTR);
        if (processor == null) {
            DAVRepository repository = new DAVRepository();
            processor = new DAVProcessor(repository, session);
            processors.add(processor);
            req.getSession().setAttribute(PROCESSOR_ATTR, processor);
            req.getSession().setAttribute(SESSION_INVALIDATE_LISTENER_ATTR, new SessionInvalidateListener(processor));
            processor.getRepository().refreshNetworkMounts(session);
            if (log.isInfoEnabled())
            	log.info("Initialized repository");
        }
        return processor;
    }

    public static DAVProcessor getDAVProcessor(SessionInfo session) throws DAVBundleActionMessageException, Exception {
        DAVProcessor processor = (DAVProcessor) session.getHttpSession().getAttribute(PROCESSOR_ATTR);
        if (processor == null) {
            DAVRepository repository = new DAVRepository();
            processor = new DAVProcessor(repository, session);
            processors.add(processor);
            session.getHttpSession().setAttribute(PROCESSOR_ATTR, processor);
            session.getHttpSession().setAttribute(SESSION_INVALIDATE_LISTENER_ATTR, new SessionInvalidateListener(processor));
            processor.getRepository().refreshNetworkMounts(session);
            if (log.isInfoEnabled())
            	log.info("Initialized repository");
        }
        return processor;
    }

    public static DAVResource getDAVResource(HttpServletRequest request, HttpServletResponse response, String path)
                    throws DAVBundleActionMessageException, Exception {
        DAVResource res = null;
        try {
            DAVProcessor processor = getDAVProcessor(request);
            DAVTransaction transaction = new DAVTransaction(request, response);
            res = processor.getRepository().getResource(path, transaction);
            res.start(transaction);
            res.verifyAccess();
        } catch (DAVAuthenticationRequiredException e) {
            DAVServlet.sendAuthorizationError(request, response, e.getMount().getStore().getName() + "-"
                            + e.getMount().getMountString());
            throw e;
        }
        return res;
    }

    /**
     * <p>
     * Execute the current request.
     * </p>
     */
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        log.info("Processing " + req.getMethod() + " " + req.getPathInfo());
        
//        if (isWebDAVMethod(req)) {

            DAVTransaction transaction = new DAVTransaction(req, res);

            if (transaction.verifyAuthorization()) {
                DAVServlet.updateDAVSession(req, transaction.getPath());

                DAVProcessor processor = null;
                try {
                    processor = DAVServlet.getDAVProcessor(req);
                } catch (DAVBundleActionMessageException e) {
                    ActionMessages mesgs = (ActionMessages) request.getAttribute(Globals.ERROR_KEY);
                    if (mesgs == null) {
                        mesgs = new ActionMessages();
                        request.setAttribute(Globals.ERROR_KEY, mesgs);
                    }
                    mesgs.add(Globals.MESSAGE_KEY, e.getBundleActionMessage());
                    return;
                } catch (Exception e1) {
                    throw new IOException(e1.getMessage());
                }

                /**
                 * LDP - JB I have removed the authentication code here and
                 * instead placed in DAVTransaction. This allows the transaction
                 * to correctly obtain sessionInfo through WEBDav.
                 */
                try {
                    /* Mark our presence */
                    res.setHeader("Server", this.context.getServerInfo() + ' ' + SIGNATURE);
                    res.setHeader("MS-Author-Via", "DAV");
                    res.setHeader("DAV", "1");
                    
                    /**
                     * LDP to BPS - This breaks IE downloading! For some bizarre reason IE will 
                     * throw an error when these headers are present even though the file has been 
                     * downloaded correctly
                     */
                    //Util.noCache(res);

                    processor.process(transaction);

                } catch(DAVRedirection redir) {
                    String redirPath = "/fs" + redir.getLocation().getFullPath();
                    req.getRequestDispatcher(redirPath).forward(req, res);
                }
                catch (DAVAuthenticationRequiredException e) {
                    sendAuthorizationError(req, res, e.getMount().getStore().getName() + "-" + e.getMount().getMountString());
                } catch (DAVException ex) {
                    res.sendError(ex.getStatus(), ex.getMessage());
                } catch (Throwable t) {
                    log.error("Network Places Request Failed.", t);
                    res.sendError(DAVStatus.SC_INTERNAL_SERVER_ERROR, t.getMessage() == null ? "<null>" : t.getMessage());
                }
            }
//        } else {
//            req.getSession().setAttribute(Constants.EXCEPTION,
//                new Exception(req.getMethod() + " is not valid for path " + req.getPathInfo()));
//            req.getRequestDispatcher("/showException.do").forward(req, res);
//        }
    }

    private boolean isWebDAVMethod(HttpServletRequest request) {
        return

        /*
         * BPS - WE *MUST* support GET at least for get file resources as it is
         * used by application shortcuts HTML templates and other stuff GETing a
         * directory listing iis also very *useful*
         */

        // !request.getMethod().equalsIgnoreCase("get") &&
        !request.getMethod().equalsIgnoreCase("head") && !request.getMethod().equalsIgnoreCase("post");
    }

    public static void sendAuthorizationError(HttpServletRequest request, HttpServletResponse response, String realm)
                    throws IOException {
    	if (log.isInfoEnabled())
    		log.info("Sending auth request for realm " + realm);
        response.setHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }

    /**
     * <p>
     * Receive notification of an event occurred in a specific
     * {@link DAVRepository}.
     * </p>
     */
    public void notify(DAVResource resource, int event) {
        String message = "Unknown event";
        switch (event) {
            case DAVListener.COLLECTION_CREATED:
                message = "Collection created";
                break;
            case DAVListener.COLLECTION_REMOVED:
                message = "Collection removed";
                break;
            case DAVListener.RESOURCE_CREATED:
                message = "Resource created";
                break;
            case DAVListener.RESOURCE_REMOVED:
                message = "Resource removed";
                break;
            case DAVListener.RESOURCE_MODIFIED:
                message = "Resource modified";
                break;
        }
        if (log.isDebugEnabled())
        	log.debug(message + ": \"" + resource.getRelativePath() + "\"");
    }

    static class SessionInvalidateListener implements HttpSessionBindingListener {

        private DAVProcessor processor;

        /**
         * 
         */
        SessionInvalidateListener(DAVProcessor processor) {
            this.processor = processor;
        }

        /*
         * (non-Javadoc)
         * 
         * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
         */
        public void valueBound(HttpSessionBindingEvent arg0) {
        }

        /*
         * (non-Javadoc)
         * 
         * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
         */
        public void valueUnbound(HttpSessionBindingEvent arg0) {
            processors.remove(processor);
            // processor.getRepository().removeListener(DAVServlet.this);
        }

    }
}

⌨️ 快捷键说明

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