📄 abstractnetworkplacemount.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.vfs.webdav;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs.FileObject;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.security.AuthenticationScheme;
import com.sslexplorer.security.Constants;
import com.sslexplorer.security.SessionInfo;
import com.sslexplorer.vfs.NetworkPlace;
import com.sslexplorer.vfs.utils.URI;
import com.sslexplorer.vfs.utils.URI.MalformedURIException;
/**
* An abstract implementation of a {@link DAVMount} that is based upon a
* configured <i>Network Place</i>.
* <p>
* The URI provided in the network place is used as the root for the mount.
* <p>
* The {@link DAVResource} instances returned by this mount use the SSL-Explorer
* extensions to <i>Commons VFS</i> as the underlying file system.
*
* @author Brett Smith <a href="mailto: brett@3sp.com"><brett@3sp.com></a>
* @version $Revision: 1.10 $
* @see NetworkPlace
*/
public abstract class AbstractNetworkPlaceMount implements DAVMount {
final static Log log = LogFactory.getLog(AbstractNetworkPlaceMount.class);
// Private instance variables
private DAVStore store;
private NetworkPlace networkPlace;
private boolean readOnly;
private boolean tryCurrentUser, tryGuest;
/**
* Constructor.
*
* @param networkPlace network place
* @param store store
*/
public AbstractNetworkPlaceMount(NetworkPlace networkPlace, DAVStore store) {
this.store = store;
this.networkPlace = networkPlace;
this.readOnly = networkPlace.isReadOnly();
try {
tryCurrentUser = CoreServlet.getServlet().getPropertyDatabase().getPropertyBoolean(0, null,
"fileBrowsing.auth.tryCurrentUser");
tryGuest = CoreServlet.getServlet().getPropertyDatabase().getPropertyBoolean(0, null, "fileBrowsing.auth.tryGuest");
} catch (Exception e) {
}
}
/**
* Set whether this mount is read-only. By default this is determined by the
* network place resource object.
*
* @param readOnly read only
*/
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
/**
* Get whether this mount is read-only. By default this is determined by the
* network place resource object.
*
* @return read only
*/
public boolean isReadOnly() {
return readOnly;
}
/**
* Create
*
* @param path path
* @param transaction transaction
* @return resource
* @throws IOException on any error
* @throws DAVAuthenticationRequiredException if resources requires
* authentication
*/
protected abstract FileObject createVFSFileObject(String path, DAVTransaction transaction) throws IOException,
DAVAuthenticationRequiredException;
/*
* (non-Javadoc)
*
* @see com.sslexplorer.vfs.webdav.DAVMount#getStore()
*/
public DAVStore getStore() {
return store;
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.vfs.webdav.DAVMount#getResource(java.lang.String,
* com.sslexplorer.vfs.webdav.DAVTransaction)
*/
public DAVResource getResource(String path, DAVTransaction transaction) throws IOException {
DAVResource resource = transaction.getCachedResource(DAVUtilities.concatenatePaths(getMountString(), path));
if (resource == null) {
resource = new NetworkPlaceDAVResource(this, createAuthenticatedVFSFileObject(path, transaction), transaction, path);
transaction.putCachedResource(resource);
}
return resource;
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.vfs.webdav.DAVMount#getMountString()
*/
public String getMountString() {
return this.getStore().getName() + "/" + this.getNetworkPlace().getResourceName();
}
/**
* Get the network place that backs this mount.
*
* @return network place
*/
public NetworkPlace getNetworkPlace() {
return networkPlace;
}
/**
* Get the root VFS URI for the current network place. By default this
* assumes the path contains a full URI. It is up to the individual mount
* implementations to overide this method and return a correct URI if they
* support paths other than URIs (<i>file</i> for example supports local
* file paths)
*
* @param transaction
* @return uri
* @throws MalformedURIException
*/
public URI getRootVFSURI(DAVTransaction transaction, String charset) throws MalformedURIException {
URI uri = DAVUtilities.processAndEncodeURI(getNetworkPlace().getUri(), transaction.getSessionInfo(), charset);
if (uri.getUserinfo() != null) {
uri.setUserinfo(Util.urlEncode(uri.getUserinfo()));
}
/*
* if(uri.getPath() != null && !uri.getPath().equals("")) {
* uri.setPath(DAVUtilities.encodePath(uri.getPath(), charset)); }
*/
return uri;
}
public URI getRootVFSURI(DAVTransaction transaction) throws MalformedURIException {
return getRootVFSURI(transaction, "UTF-8");
}
/**
* Create a file object. The actual creation is delegated to
* {@link #createVFSFileObject}, this methos keeps try that method is an
* authenticated object is returned (i.e.
* {@link DAVAuthenticationRequiredException} stops getting thrown.
*
* @param path path
* @param transaction transaction
* @return file object
* @throws IOException on any error
*/
public FileObject createAuthenticatedVFSFileObject(String path, DAVTransaction transaction) throws IOException {
// 0 = Current
// 1 = URI
// 2 = HTTP authentication response
// 3 = Current users credentials
// 4 = Guest
// 5 = Prompt
int type = 0;
DAVAuthenticationRequiredException dave = null;
while (true) {
// If no credentials are currently set, try those in the cache
// first
if (type == 0) {
DAVCredentials creds = transaction.getDAVCredentialsCashe().getDAVCredentials(getStore().getName(),
getMountString());
if (creds == null) {
type++;
} else {
transaction.setCurrentCredentials(creds);
}
}
// User info from URI
if (type == 1) {
URI uri = getRootVFSURI(transaction, store.getEncoding());
String userInfo = uri.getUserinfo();
if (userInfo == null || userInfo.equals("")) {
type++;
} else {
String username = null;
char[] pw = null;
userInfo = Util.urlDecode(userInfo);
int idx = userInfo.indexOf(":");
username = userInfo;
if (idx != -1) {
username = userInfo.substring(0, idx);
pw = userInfo.substring(idx + 1).toCharArray();
}
transaction.setCurrentCredentials(new DAVCredentials(username, pw));
}
}
// HTTP authentication response
if (type == 2) {
DAVCredentials creds = transaction.getCredentials();
if (creds == null) {
type++;
} else {
transaction.setCurrentCredentials(creds);
}
}
// Current user creds
if (type == 3) {
if (!tryCurrentUser) {
type++;
} else {
char[] pw = CoreServlet.getServlet().getLogonController().getPasswordFromCredentials(
(AuthenticationScheme) transaction.getRequest().getSession().getAttribute(Constants.AUTH_SESSION));
if (pw == null) {
type++;
} else {
DAVCredentials creds = new DAVCredentials(transaction.getSessionInfo().getUser().getPrincipalName(), pw);
transaction.setCurrentCredentials(creds);
}
}
}
// Guest creds
if (type == 4) {
if (!tryGuest) {
type++;
} else {
String guestAccount = getStore().getGuestUsername(transaction);
if (guestAccount == null) {
type++;
} else {
DAVCredentials creds = new DAVCredentials(guestAccount, getStore().getGuestPassword(transaction));
transaction.setCurrentCredentials(creds);
}
}
}
// Throw exception. Servlet will then request HTTP
// authentication
if (type > 4 && dave != null) {
throw dave;
}
try {
FileObject file = createVFSFileObject(path, transaction);
if (file == null) {
throw new IOException("Could not create file object.");
}
// Cache authentication
if (transaction.getCurrentCredentials() != null) {
transaction.getDAVCredentialsCashe().addDAVCredentials(getStore().getName(), getMountString(),
transaction.getCurrentCredentials());
}
return file;
} catch (DAVAuthenticationRequiredException dare) {
dave = dare;
type++;
}
}
}
class NetworkPlaceDAVResource extends FileObjectDAVResource {
NetworkPlaceDAVResource(DAVMount mount, FileObject root, DAVTransaction transaction, String relativePath)
throws IOException {
super(mount, root, transaction, null, relativePath);
}
public String getDisplayName() {
if (isMount()) {
return getNetworkPlace().getResourceName() + "/";
}
return super.getDisplayName();
}
public boolean isMount() {
return getRelativePath().equals("");
}
public String toString() {
return "Rel. URI = " + getRelativeURI() + ", Rel. Path = " + getRelativePath();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -