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

📄 davtransaction.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * <p>Get the request object.</p>
     */
    public HttpServletRequest getRequest() {
        return req;        
    }
    
    /**
     * <p>Return the path originally requested by the client.</p>
     */
    public String getMethod() {
        return this.req.getMethod();
    }
    
    /**
     * <p>Return the path for this transaction. This will be the path
     * as the client sees it less the first element
     * 
     * @return path
     */
    public String getPath() {
        return path;
//        String path = this.req.getPathInfo();
//        if (path == null) return "";
//        if ((path.length() > 0) && (path.charAt(0) == '/')) {
//            return path.substring(1);
//        } else {
//            return path;
//        }
    }
    
    public boolean isRequiredRootRedirect() {
        return false;
    }

    /**
     * <p>Return the path originally requested by the client encoded.</p>
     */
    public String getPathEncoded() {
        return DAVUtilities.encodePath(getPath());
    }

    /**
     * <p>Return the depth requested by the client for this transaction.</p>
     */
    public int getDepth() {
        String depth = req.getHeader("Depth");
        if (depth == null) return INFINITY;
        if ("infinity".equals(depth)) return INFINITY;
        try {
            return Integer.parseInt(depth);
        } catch (NumberFormatException exception) {
            throw new DAVException(412, "Unable to parse depth", exception);
        }
    }

    /**
     * <p>Return a {@link URI} 
     */
    public URI getDestination() {
        String destination = this.req.getHeader("Destination");
        if (destination != null) try {
            return this.base.relativize(new URI(destination.replaceAll(" ", "%20")));
        } catch (URISyntaxException exception) {
            throw new DAVException(412, "Can't parse destination", exception);
        }
        return null;
    }

    /**
     * <p>Return the overwrite flag requested by the client for this
     * transaction.</p>
     */
    public boolean getOverwrite() {
        String overwrite = req.getHeader("Overwrite");
        if (overwrite == null) return true;
        if ("T".equals(overwrite)) return true;
        if ("F".equals(overwrite)) return false;
        throw new DAVException(412, "Unable to parse overwrite flag");
    }

    /**
     * <p>Check if the client requested a date-based conditional operation.</p>
     */
    public Date getIfModifiedSince() {
        String name = "If-Modified-Since";
        if (this.req.getHeader(name) == null) return null;
        return new Date(this.req.getDateHeader(name));
    }

    /* ====================================================================== */
    /* Response methods                                                       */
    /* ====================================================================== */
    
    /**
     * <p>Set the HTTP status code of the response.</p>
     */
    public void setStatus(int status) {
        this.res.setStatus(status);
    }

    /**
     * <p>Set the HTTP <code>Content-Type</code> header.</p>
     */
    public void setContentType(String type) {
        this.res.setContentType(type);
    }

    /**
     * <p>Set an HTTP header in the response.</p>
     */
    public void setHeader(String name, String value) {
        this.res.setHeader(name, value);
    }

    /**
     * <p>Set an HTTP header in the response.</p>
     * 
     * @param name name
     * @param value value
     */
    public void setDateHeader(String name, int value) {
        this.res.setDateHeader(name, value);
    }

    /* ====================================================================== */
    /* I/O methods                                                            */
    /* ====================================================================== */
    
    /**
     * <p>Read from the body of the original request.</p>
     */
    public InputStream read()
    throws IOException {
        /* We don't support ranges */
        if (req.getHeader("Content-Range") != null)
            throw new DAVException(501, "Content-Range not supported");

        if (this.req.getContentLength() >= 0) this.req.getInputStream();
        String len = this.req.getHeader("Content-Length");
        if (len != null) try {
            if (Long.parseLong(len) > 0) return this.req.getInputStream();
        } catch (NumberFormatException exception) {
            // Unparseable content length header...
        }
        
        // Do not throw an exception, this could be null without an error condition
        return null;
    }

    /**
     * <p>Write the body of the response.</p>
     */
    public OutputStream write()
    throws IOException {
        if(System.getProperty("sslexplorer.webdav.debug", "false").equals("true"))
            return new TempOutputStream(this.res.getOutputStream());
        else
            return this.res.getOutputStream();
    }

    class TempOutputStream extends OutputStream {
    	
    	OutputStream out;
    	TempOutputStream(OutputStream out) {
    		this.out = out;
    	}
        
        public void write(byte[] buf, int off, int len) throws IOException {
            out.write(buf, off, len);
        }
    	public void write(int b) throws IOException {
    		System.out.write((byte)b);
    		System.out.flush();
    		out.write((byte) b);
    	}
    }
    /**
     * <p>Write the body of the response.</p>
     */
    public PrintWriter write(String encoding)
    throws IOException {
        return new PrintWriter(new OutputStreamWriter(this.write(), encoding));
    }

    /* ====================================================================== */
    /* Lookup methods                                                         */
    /* ====================================================================== */
    
    /**
     * <p>Look up the final URI of a {@link DAVResource} as visible from the
     * HTTP client requesting this transaction.</p>
     */
    public URI lookup(DAVResource resource) {
        URI uri = resource.getRelativeURI();
        URI resolved = null;
        if(uri==null || uri.toString().equals("")) {
            resolved = this.base;
        }
        else {
            resolved = this.base.resolve(uri).normalize();;
        }
        return resolved;
    }
    
    public DAVCredentials getCurrentCredentials() {
        return currentCredentials;
    }

    public DAVCredentialsCashe getDAVCredentialsCashe(){
    	// get the credentials cashe
    	DAVCredentialsCashe credentialsCashe = (DAVCredentialsCashe) req.getSession().getAttribute("CredentialsCashe");
    	// if there is not 1 make 1 then get the cashe
    	if (credentialsCashe == null){
    		req.getSession().setAttribute("CredentialsCashe", new DAVCredentialsCashe());
    		credentialsCashe = (DAVCredentialsCashe) req.getSession().getAttribute("CredentialsCashe");
    	}
    	return credentialsCashe;
    }
    /**
     * @param store
     * @param mount
     * @return
     */
    public DAVCredentials getCredentials(String store, String mount) {
    	
    	DAVCredentials cashedDAVCredentials = getDAVCredentialsCashe().getDAVCredentials(store, mount) ;
    	
    	if (cashedDAVCredentials != null){
    		return cashedDAVCredentials ;
    	}
    	else{
            return getCredentials();
    	}
    }

	public DAVCredentials getCredentials() {
		String authorization = req.getHeader("Authorization");

		if(authorization == null) {
            return null;
		}
		
		int idx =  authorization.indexOf(' ');

		if (idx == -1 || idx == authorization.length() - 1) {
		    return null;
		}

		// Authenticate the user
		String method = authorization.substring(0, idx);

		if (!method.equalsIgnoreCase("basic")) {
		    return null;
		}

		// Extract the credentials - should be ticket:tunnel
		String encoded = authorization.substring(idx + 1);
		
		/* NOTE BPS - Changed from com.sun decoder to use maverick Base64 decoder. I hope this
		 * works as I cant test atm
		 */
		
		String credentials = new String(Base64.decode(encoded));
		idx = credentials.indexOf(':');

		if (idx == 0 || idx == -1) {
		    return null;
		}

		// Get the user credentials
		String username = credentials.substring(0, idx);
		String password = credentials.substring(idx + 1);

		return new DAVCredentials(username, password.toCharArray());
	}

    /**
     * Get the session info for this transaction. The user and other session
     * related objects may be found here.
     * 
     * @return session info
     */
    public SessionInfo getSessionInfo() {
        return sessionInfo;
    }

    /**
     * Set the credentials object to try next to access the resorces
     * 
     * @param currentCredentials credentials
     */
    public void setCurrentCredentials(DAVCredentials currentCredentials) {
        this.currentCredentials = currentCredentials;
        
    }

    /**
     * Check if the supplied resource path is valid for this transaction
     * path. This will be used to force a redirect to the required path if not.
     * 
     * @param fullResourcePath
     * @return is resource path
     */
    public boolean isResourcePath(String fullResourcePath) {
        String fullUri = DAVUtilities.stripTrailingSlash(DAVUtilities.stripLeadingSlash(fullResourcePath));
        return fullUri.equals(getPath());
    }
}

⌨️ 快捷键说明

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