Java Source Code: org.mindswap.swoop.utils.DavUtil


   1: /*
   2: GNU Lesser General Public License
   3: 
   4: Copyright (C) 2005 MINDSWAP Research Group, University of Maryland College Park
   5: 
   6: This library is free software; you can redistribute it and/or
   7: modify it under the terms of the GNU Lesser General Public
   8: License as published by the Free Software Foundation; either
   9: version 2.1 of the License, or (at your option) any later version.
  10: 
  11: This library is distributed in the hope that it will be useful,
  12: but WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: Lesser General Public License for more details.
  15: 
  16: You should have received a copy of the GNU Lesser General Public
  17: License along with this library; if not, write to the Free Software
  18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19: */
  20: 
  21: package org.mindswap.swoop.utils;
  22: import javax.swing.*;
  23: import java.awt.GridLayout;
  24: import java.io.*;
  25: import java.net.*;
  26: import java.util.*;
  27: 
  28: import org.apache.commons.httpclient.HttpURL;
  29: import org.apache.commons.httpclient.HttpException;
  30: import org.apache.commons.httpclient.URIException;
  31: 
  32: import org.apache.webdav.lib.WebdavResource;
  33: import org.apache.webdav.lib.WebdavFile;
  34: 
  35: 
  36:	  public class DavUtil {
  37:	      private static class StaticAuthenticator extends Authenticator {
  38:    private String user;
  39:    private String pass;
  40:	      public StaticAuthenticator(String username, String password) {
  41:        user = username;
  42:        pass = password;
  43:    }
  44:	      protected PasswordAuthentication getPasswordAuthentication() {
  45:        return new PasswordAuthentication(user, pass.toCharArray());
  46:    }
  47:    }
  48:
  49:	      private static class VisualAuthenticator extends Authenticator {
  50:	      protected PasswordAuthentication getPasswordAuthentication() {
  51:        JTextField url = new JTextField();
  52:        JTextField user = new JTextField();
  53:        JTextField password = new JPasswordField();
  54:        JPanel panel = new JPanel(new GridLayout(2,2));
  55:        panel.add( new JLabel("URL") );
  56:        panel.add( url );
  57:        panel.add( new JLabel("User") );
  58:        panel.add( user );
  59:        panel.add( new JLabel("Password") );
  60:        panel.add( password );
  61:        int option = JOptionPane.showConfirmDialog
  62:        (
  63:         null,
  64:         new Object[]
  65:	              {
  66:            "Host: "+getRequestingHost(),
  67:            "Realm: "+getRequestingPrompt(),
  68:            panel
  69:            },
  70:         "Authorization Required",
  71:         JOptionPane.OK_CANCEL_OPTION,
  72:         JOptionPane.PLAIN_MESSAGE
  73:         );
  74:        if ( option == JOptionPane.OK_OPTION )
  75:        return new PasswordAuthentication(user.getText(),password.getText().toCharArray());
  76:        else
  77:        return null;
  78:    }
  79:    }
  80:
  81:	      public static WebdavResource resourceIfExists(HttpURL httpURL) {
  82:	      try {
  83:        return new  WebdavResource(httpURL);
  84:    } catch (Exception e) {}
  85:    return null;
  86:    }
  87:
  88:	      public static boolean saveString(String data, String url) throws HttpException, URIException, IOException {
  89:    return saveString(data, url, null, null);
  90:    }
  91:
  92:	      public static boolean saveString(String data, String url, String username, String password) throws HttpException, URIException, IOException {
  93:    HttpURL httpURL = new HttpURL(url);
  94:    String parentPath = null;
  95:    HttpURL parentURL = null;
  96:    WebdavResource parentResource = null;
  97:    WebdavFile parentFile = new WebdavFile(httpURL);
  98:    Vector pathStack = new Vector();
  99:    
 100:	      if ((username.length() == 0) || (password.length() == 0)) {
 101:        username = null;
 102:        password = null;
 103:    }
 104:           
 105:    // Find existing parent resource
 106:	      while (parentResource == null) {
 107:        parentPath = parentFile.getParent();
 108:        System.out.println("Testing Parent: "+parentPath);
 109:        if (parentPath == null) 
 110:        return false;
 111:        parentURL = new HttpURL(httpURL, parentPath);
 112:	          if ((password != null) && (username != null)) {
 113:        parentURL.setUserinfo(username, password);
 114:        }
 115:        parentResource = resourceIfExists(parentURL);
 116:        
 117:        parentFile = new WebdavFile(parentURL);
 118:        pathStack.add(0, parentPath);
 119:    }
 120:    
 121:    // Pop parent resource off the stack
 122:    pathStack.remove(0);
 123:
 124:    // Make parent directories
 125:	      for (Iterator iterator = pathStack.iterator(); iterator.hasNext();) {
 126:        String path = (String) iterator.next();
 127:        System.out.println("Making directory: "+path);
 128:	          if (!parentResource.mkcolMethod(path)) {
 129:        System.out.println("Couldn't make directory.");
 130:        parentResource.close();
 131:        return false;
 132:        }
 133:    }
 134:    
 135:    // Put file in place
 136:	      if (!parentResource.putMethod(httpURL.getPath(), data)) {
 137:        System.out.println("Couldn't save file.");
 138:        parentResource.close();
 139:        return false;
 140:    }
 141:    
 142:    parentResource.close();
 143:    return true;
 144:    }
 145:}