Java Source Code: net.sourceforge.ganttproject.document.HttpDocument


   1: /*
   2:  * Created on 18.08.2003
   3:  *
   4:  */
   5: package net.sourceforge.ganttproject.document;
   6: 
   7: import java.io.IOException;
   8: import java.io.InputStream;
   9: import java.io.OutputStream;
  10: import java.net.URI;
  11: import java.net.URISyntaxException;
  12: import java.security.AccessControlException;
  13: 
  14: import org.apache.commons.httpclient.HttpException;
  15: import org.apache.commons.httpclient.HttpURL;
  16: import org.apache.commons.httpclient.URIException;
  17: import org.apache.webdav.lib.WebdavResource;
  18: 
  19: /**
  20:  * This class implements the interface Document for file access on HTTP-servers
  21:  * and WebDAV-enabled-servers.
  22:  * 
  23:  * @author Michael Haeusler (michael at akatose.de)
  24:  */
  25:	  public class HttpDocument extends AbstractURLDocument {
  26:
  27:    private String url;
  28:
  29:    private String lastError;
  30:
  31:    private HttpURL httpURL;
  32:
  33:    private WebdavResource webdavResource;
  34:
  35:    private boolean locked = false;
  36:
  37:    private boolean malformedURL = false;
  38:
  39:    private static int lockDAVMinutes = 240;
  40:
  41:	      public HttpDocument(String url, String user, String pass) {
  42:        this.url = url;
  43:	          try {
  44:            httpURL = new HttpURL(url);
  45:            if (user == null || user.length() == 0)
  46:                user = httpURL.getUser();
  47:            if (pass == null || pass.length() == 0)
  48:                pass = httpURL.getPassword();
  49:            if (user != null)
  50:                httpURL.setUserinfo(user, pass);
  51:        } catch (URIException e) {
  52:            lastError = e.getMessage();
  53:            malformedURL = true;
  54:        }
  55:    }
  56:
  57:	      private WebdavResource getWebdavResource() {
  58:        if (null == webdavResource)
  59:	              try {
  60:                webdavResource = new WebdavResource(httpURL,
  61:                        WebdavResource.NOACTION, 0);
  62:                webdavResource.setFollowRedirects(true);
  63:            } catch (HttpException e) {
  64:                lastError = e.getMessage() + "(" + e.getReasonCode() + ")";
  65:            } catch (IOException e) {
  66:                lastError = e.getMessage();
  67:            }
  68:        return webdavResource;
  69:    }
  70:
  71:	      public String getDescription() {
  72:        String description = httpURL.toString();
  73:        return (description != null) ? description : url;
  74:    }
  75:
  76:	      public boolean canRead() {
  77:        WebdavResource res = getWebdavResource();
  78:        return (null == res) ? false : (res.exists() && !res.isCollection());
  79:    }
  80:
  81:	      public boolean canWrite() {
  82:        WebdavResource res = getWebdavResource();
  83:        if (null == res)
  84:            return false;
  85:
  86:	          try {
  87:            res.setProperties(0);
  88:        } catch (HttpException e) {
  89:            if (404 != e.getReasonCode())
  90:                return false;
  91:        } catch (IOException e) {
  92:            return false;
  93:        }
  94:
  95:	          if (res.exists()) {
  96:            return (!res.isCollection());
  97:        } else {
  98:	              try {
  99:                HttpURL parentURL = new HttpURL(httpURL.toString());
 100:                String user = httpURL.getUser();
 101:                String pass = httpURL.getPassword();
 102:                if (user != null)
 103:                    parentURL.setUserinfo(user, pass);
 104:                String currentHierPath = httpURL.getCurrentHierPath();
 105:                if (!currentHierPath.endsWith("/"))
 106:                    currentHierPath = currentHierPath + "/";
 107:                parentURL.setPath(currentHierPath);
 108:                WebdavResource parentRes = new WebdavResource(parentURL);
 109:                if (!parentRes.isCollection())
 110:                    return false;
 111:                return true;
 112:            } catch (Exception e) {
 113:                return false;
 114:            }
 115:        }
 116:    }
 117:
 118:	      public boolean isValidForMRU() {
 119:        return (!malformedURL);
 120:    }
 121:
 122:    /*
 123:     * (non-Javadoc)
 124:     * 
 125:     * @see net.sourceforge.ganttproject.document.Document#acquireLock(java.lang.String)
 126:     */
 127:	      public boolean acquireLock() {
 128:        if (locked)
 129:            return true;
 130:        if (null == getWebdavResource())
 131:            return false;
 132:	          try {
 133:            String userName = " (GanttProject)";
 134:	              try {
 135:                userName = " (" + System.getProperty("user.name")
 136:                        + "@GanttProject)";
 137:            } catch (AccessControlException e) {
 138:                // This can happen when running in a sandbox (Java WebStart)
 139:                System.err.println(e + ": " + e.getMessage());
 140:            }
 141:            int lockSeconds = (lockDAVMinutes > 0) ? lockDAVMinutes * 60 : 15;
 142:            locked = getWebdavResource().lockMethod(
 143:                    httpURL.getUser() + userName, lockSeconds);
 144:            return locked;
 145:        } catch (HttpException e) {
 146:            System.err.println(e.getMessage() + e.getReasonCode());
 147:            e.printStackTrace();
 148:        } catch (IOException e) {
 149:            System.err.println(e);
 150:            e.printStackTrace();
 151:        }
 152:        return false;
 153:    }
 154:
 155:    /*
 156:     * (non-Javadoc)
 157:     * 
 158:     * @see net.sourceforge.ganttproject.document.Document#releaseLock()
 159:     */
 160:	      public void releaseLock() {
 161:        if (null == getWebdavResource())
 162:            return;
 163:	          try {
 164:            locked = false;
 165:            getWebdavResource().unlockMethod();
 166:        } catch (HttpException e) {
 167:            System.err.println(e.getMessage() + e.getReasonCode());
 168:            e.printStackTrace();
 169:        } catch (IOException e) {
 170:            System.err.println(e);
 171:            e.printStackTrace();
 172:        }
 173:    }
 174:
 175:	      public InputStream getInputStream() throws IOException {
 176:        if (null == getWebdavResource())
 177:            throw new IOException(lastError);
 178:	          try {
 179:            return getWebdavResource().getMethodData();
 180:        } catch (HttpException e) {
 181:            throw new IOException(e.getMessage() + "(" + e.getReasonCode()
 182:                    + ")");
 183:        }
 184:    }
 185:
 186:	      public OutputStream getOutputStream() throws IOException {
 187:        if (null == getWebdavResource())
 188:            throw new IOException(lastError);
 189:        return new HttpDocumentOutputStream(getWebdavResource());
 190:    }
 191:
 192:	      public String getPath() {
 193:        return getDescription();
 194:    }
 195:
 196:	      public String getURLPath() {
 197:        return getPath();
 198:    }
 199:
 200:	      public String getUsername() {
 201:	          try {
 202:            return httpURL.getUser();
 203:        } catch (URIException e) {
 204:            return null;
 205:        }
 206:    }
 207:
 208:	      public String getPassword() {
 209:	          try {
 210:            return httpURL.getPassword();
 211:        } catch (URIException e) {
 212:            return null;
 213:        }
 214:    }
 215:
 216:	      public void setUserInfo(String user, String pass) {
 217:	          try {
 218:            httpURL.setUserinfo(user, pass);
 219:            webdavResource = null;
 220:        } catch (URIException e) {
 221:            lastError = e.getMessage();
 222:            malformedURL = true;
 223:        }
 224:    }
 225:
 226:	      public String getLastError() {
 227:        return lastError;
 228:    }
 229:
 230:	      public static void setLockDAVMinutes(int i) {
 231:        lockDAVMinutes = i;
 232:    }
 233:
 234:	      public void write() throws IOException {
 235:        // TODO Auto-generated method stub
 236:        
 237:    }
 238:
 239:	      public URI getURI() {
 240:	          try {
 241:            return new URI(url);
 242:        } catch (URISyntaxException e) {
 243:            return null;
 244:        }
 245:    }
 246:
 247:	      public boolean isLocal() {
 248:        return false;
 249:    }
 250:    
 251:    
 252:}