Java Source Code: org.mindswap.utils.DavUtil


   1: package org.mindswap.utils;
   2: 
   3: import java.awt.GridLayout;
   4: 
   5: import org.apache.commons.httpclient.HttpURL;
   6: import org.apache.commons.httpclient.HttpException;
   7: import org.apache.commons.httpclient.URIException;
   8: 
   9: import org.apache.webdav.lib.WebdavResource;
  10: import org.apache.webdav.lib.WebdavFile;
  11: 
  12: import java.net.Authenticator;
  13: import java.net.PasswordAuthentication;
  14: 
  15: import javax.swing.JTextField;
  16: import javax.swing.JPanel;
  17: import javax.swing.JLabel;
  18: import javax.swing.JPasswordField;
  19: import javax.swing.JOptionPane;
  20: 
  21: import java.io.IOException;
  22: import java.io.File;
  23: 
  24: import java.util.Vector;
  25: import java.util.Iterator;
  26: 
  27: public class DavUtil
  28:	  {
  29:    // Authenticator.setDefault(new VisualAuthenticator());
  30:    private static final boolean DEBUG = false;
  31:
  32:    private static String mUser = "";
  33:    private static String mPassword = "";
  34:
  35:	      private static class StaticAuthenticator extends Authenticator {
  36:        private String user;
  37:        private String pass;
  38:
  39:	          public StaticAuthenticator(String username, String password) {
  40:            user = username;
  41:            pass = password;
  42:        }
  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(null, new Object[] {
  62:                                                       "Host: "+getRequestingHost(),
  63:                                                       "Realm: "+getRequestingPrompt(),
  64:                                                       panel
  65:            },
  66:            "Authorization Required",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
  67:
  68:        if ( option == JOptionPane.OK_OPTION )
  69:            return new PasswordAuthentication(user.getText(),password.getText().toCharArray());
  70:        else return null;
  71:    }
  72:  }
  73:
  74:  public static WebdavResource resourceIfExists(HttpURL httpURL)
  75:	    {
  76:	        try {
  77:          return new WebdavResource(httpURL);
  78:      } catch (Exception e) {}
  79:      return null;
  80:  }
  81:
  82:	    public static boolean saveString(String data, String url) throws HttpException, URIException, IOException {
  83:      return saveString(data, url, "", "");
  84:  }
  85:
  86:  public static boolean saveString(String data, String url, String username, String password) throws HttpException, URIException, IOException
  87:	    {
  88:      HttpURL httpURL = new HttpURL(url);
  89:      String parentPath = null;
  90:      HttpURL parentURL = null;
  91:      WebdavResource parentResource = null;
  92:      WebdavFile parentFile = new WebdavFile(httpURL);
  93:      Vector pathStack = new Vector();
  94:
  95:      if ((username.length() == 0) || (password.length() == 0))
  96:	        {
  97:          username = null;
  98:          password = null;
  99:      }
 100:
 101:      // Find existing parent resource
 102:	        while (parentResource == null) {
 103:          parentPath = parentFile.getParent();
 104:          if (DEBUG)
 105:              System.err.println("Testing Parent: "+parentPath);
 106:          if (parentPath == null)
 107:              return false;
 108:          parentURL = new HttpURL(httpURL, parentPath);
 109:	            if ((password != null) && (username != null)) {
 110:              parentURL.setUserinfo(username, password);
 111:          }
 112:          parentResource = resourceIfExists(parentURL);
 113:
 114:          parentFile = new WebdavFile(parentURL);
 115:          pathStack.add(0, parentPath);
 116:      }
 117:
 118:      // Pop parent resource off the stack
 119:      pathStack.remove(0);
 120:
 121:      // Make parent directories
 122:	        for (Iterator iterator = pathStack.iterator(); iterator.hasNext();) {
 123:          String path = (String) iterator.next();
 124:          if (DEBUG)
 125:              System.err.println("Making directory: "+path);
 126:	            if (!parentResource.mkcolMethod(path)) {
 127:              if (DEBUG)
 128:                  System.err.println("Couldn't make directory.");
 129:              parentResource.close();
 130:              return false;
 131:          }
 132:      }
 133:      // Put file in place
 134:	        if (!parentResource.putMethod(httpURL.getPath(), data)) {
 135:          if (DEBUG)
 136:              System.out.println("Couldn't save file.");
 137:          parentResource.close();
 138:          return false;
 139:      }
 140:
 141:      parentResource.close();
 142:      return true;
 143:  }
 144:
 145:	    public static boolean saveFile(File data, String url) throws HttpException, URIException, IOException {
 146:      return saveFile(data, url, mUser, mPassword);
 147:  }  // saveFile
 148:
 149:  public static boolean saveFile(File data, String url, String username, String password) throws HttpException, URIException, IOException
 150:	    {
 151:      if (username.equals("") && password.equals(""))
 152:	        {
 153:          if (getUserInfo(url))
 154:	            {
 155:              username = mUser;
 156:              password = mPassword;
 157:          } else {
 158:              mUser = mPassword = "";
 159:          }
 160:      } // if
 161:
 162:      HttpURL httpURL = new HttpURL(url);
 163:      String parentPath = null;
 164:      HttpURL parentURL = null;
 165:      WebdavResource parentResource = null;
 166:      WebdavFile parentFile = new WebdavFile(httpURL);
 167:      Vector pathStack = new Vector();
 168:
 169:	        if ( (username.length() == 0) || (password.length() == 0)) {
 170:          username = null;
 171:          password = null;
 172:      } // if
 173:
 174:     // Find existing parent resource
 175:	        while (parentResource == null) {
 176:          parentPath = parentFile.getParent();
 177:          if (DEBUG)
 178:              System.err.println("Testing Parent: " + parentPath);
 179:	            if (parentPath == null) {
 180:              mUser = mPassword = "";
 181:              // i think this is a security exception
 182:              return false;
 183:          }
 184:          parentURL = new HttpURL(httpURL, parentPath);
 185:	            if ( (password != null) && (username != null)) {
 186:              parentURL.setUserinfo(username, password);
 187:          } // if
 188:          parentResource = resourceIfExists(parentURL);
 189:          parentFile = new WebdavFile(parentURL);
 190:          pathStack.add(0, parentPath);
 191:      } // while
 192:
 193:      // Pop parent resource off the stack
 194:      pathStack.remove(0);
 195:
 196:      // Make parent directories
 197:	        for (Iterator iterator = pathStack.iterator(); iterator.hasNext(); ) {
 198:          String path = (String) iterator.next();
 199:          if (DEBUG)
 200:              System.err.println("Making directory: " + path);
 201:	            if (!parentResource.mkcolMethod(path)) {
 202:              if (DEBUG)
 203:                  System.err.println("Couldn't make directory.");
 204:              parentResource.close();
 205:              System.err.println("this fail, it is cleared...");
 206:              mUser = mPassword = "";
 207:              return false;
 208:          } // if
 209:      } // for
 210:
 211:      // Put file in place
 212:	        if (!parentResource.putMethod(httpURL.getPath(), new java.io.BufferedInputStream(new java.io.FileInputStream(data)))) {
 213:          if (DEBUG)
 214:              System.err.println("Couldn't save file.");
 215:          parentResource.close();
 216:          System.err.println("failed should be clear");
 217:          mUser = mPassword = "";
 218:          return false;
 219:      } // if
 220:
 221:      parentResource.close();
 222:      return true;
 223:  } // saveFile
 224:
 225:  // return 1 if exists, 0 if not, -1 if action cancel due to no user information
 226:  public static int doesExist(String s)
 227:	    {
 228:	      try {
 229:        boolean check = true;
 230:        if (mUser.equals("") && mPassword.equals(""))
 231:            check = getUserInfo(s);
 232:
 233:	          if (check) {
 234:            HttpURL url = new HttpURL(s);
 235:            url.setUserinfo(mUser,mPassword);
 236:            if (resourceIfExists(url) != null)
 237:                return 1;
 238:            else return 0;
 239:        } else return -1;
 240:    } // try
 241:    catch (URIException ex)
 242:	      {
 243:      return 0;
 244:    } // catch
 245:  } // doesExist
 246:
 247:  private static boolean getUserInfo(String url)
 248:	    {
 249:    JTextField userText = new JTextField();
 250:    JTextField passwordText = new JPasswordField();
 251:    JPanel panel = new JPanel(new GridLayout(2,2));
 252:    panel.add( new JLabel("User") );
 253:    panel.add( userText );
 254:    panel.add( new JLabel("Password") );
 255:    panel.add( passwordText );
 256:    int option = JOptionPane.showConfirmDialog
 257:      (
 258:      null,
 259:      new Object[]
 260:	        {
 261:      "Submit Images to image server...",
 262:      "Host: "+url.substring(0,url.indexOf("/",url.indexOf("http://")+8)),
 263:      panel
 264:    },
 265:      "Remote file save, Authorization Required",
 266:      JOptionPane.OK_CANCEL_OPTION,
 267:      JOptionPane.PLAIN_MESSAGE
 268:      );
 269:    if ( option == JOptionPane.OK_OPTION )
 270:	      {
 271:      mUser = userText.getText();
 272:      mPassword = passwordText.getText();
 273:      return true;
 274:    } // if
 275:	      else {
 276:        mPassword = mUser = "";
 277:        return false;
 278:    }
 279:  } // getUserInfo
 280:
 281:} // DavUtil