Java Source Code: edu.washington.biostr.sig.vault.files.URIStreams


   1: /*
   2:  * Created on Jul 25, 2005
   3:  * IHBP
   4:  * @author Eider Moore
   5:  */
   6: package edu.washington.biostr.sig.vault.files;
   7: 
   8: import java.io.File;
   9: import java.io.FileInputStream;
  10: import java.io.FileOutputStream;
  11: import java.io.IOException;
  12: import java.io.InputStream;
  13: import java.io.OutputStream;
  14: import java.net.MalformedURLException;
  15: import java.net.URI;
  16: import java.net.URISyntaxException;
  17: import java.net.URL;
  18: import java.net.URLConnection;
  19: import java.security.AccessControlContext;
  20: import java.security.AccessController;
  21: import java.security.PrivilegedAction;
  22: import java.security.PrivilegedExceptionAction;
  23: 
  24: import org.eiderman.util.FileUtilities;
  25: 
  26: /**
  27:  * Translate string versions of URI's into Input and Output streams.
  28:  * This class provides the file based security that the system uses by using
  29:  * AccessControlContexts.
  30:  * Currently this just handles File's specially and defaults to URL's
  31:  * for everything else.
  32:  * @author eider
  33:  */
  34: public class URIStreams
  35:	  {
  36:   private AccessControlContext context;
  37:   
  38:   public static URIStreams getURIStreams()
  39:	     {
  40:      return new URIStreams(AccessController.getContext());
  41:   }
  42:   
  43:   public static URIStreams getURIStreams(AccessControlContext context)
  44:	     {
  45:      return new URIStreams(context);
  46:   }
  47:   
  48:   /**
  49:    * 
  50:    */
  51:   URIStreams(AccessControlContext context)
  52:	     {
  53:      if (context == null)
  54:         throw new NullPointerException("context cannot be null");
  55:      this.context = context;
  56:   }
  57:
  58:   /**
  59:    * Get an InputStream for the URI specified by uri
  60:    * @param uri
  61:    * @return the input stream for the specified uri
  62:    * @throws IOException
  63:    */
  64:   private InputStream getInputPrivate(URI uri) throws IOException
  65:	     {      
  66:      if (uri.getScheme().toLowerCase().indexOf("file") >= 0)
  67:	        {
  68:         File f= new File(uri);
  69:         return new FileInputStream(f);
  70:      }
  71:      else
  72:	        {
  73:         URL url = uri.toURL();
  74:         URLConnection conn = url.openConnection();
  75:         conn.setDoInput(true);
  76:         conn.connect();
  77:         return conn.getInputStream();
  78:      }
  79:   }
  80:
  81:   /**
  82:    * Get an OutputStream for the URI specified by uristr
  83:    * @param uri
  84:    * @return the output stream for the specified uri.
  85:    * @throws IOException
  86:    */
  87:   private OutputStream getOutputPrivate(URI uri) throws IOException
  88:	     {
  89:      if (uri.getScheme().toLowerCase().indexOf("file") >= 0)
  90:	        {
  91:         File f= new File(uri);
  92:         return new FileOutputStream(f);
  93:      }
  94:      else
  95:	        {
  96:         URL url = uri.toURL();
  97:         URLConnection conn = url.openConnection();
  98:         conn.setDoOutput(true);
  99:         conn.connect();
 100:         return conn.getOutputStream();
 101:      }
 102:   }
 103:   
 104:   /**
 105:    * Get an InputStream within the given context for the URI 
 106:    * specified by uristr
 107:    * @param uristr
 108:    * @return
 109:    * @throws URISyntaxException
 110:    * @throws IOException
 111:    */
 112:   public InputStream getInput(final URI uri) throws IOException
 113:	     {
 114:      try
 115:	        {
 116:         return AccessController.doPrivileged(
 117:               new PrivilegedExceptionAction<InputStream>()
 118:	                 {
 119:                  public InputStream run() throws Exception
 120:	                    {
 121:                     return getInputPrivate(uri);
 122:                  }
 123:               }, context);
 124:      } 
 125:      catch (Exception e)
 126:	        {
 127:         if (e.getCause() instanceof IOException)
 128:	           {
 129:            throw (IOException) e.getCause();
 130:         }
 131:         else
 132:	           {
 133:            e.printStackTrace();
 134:            return null;
 135:         }
 136:      }
 137:   }
 138:   
 139:   /**
 140:    * Determine if the uri exists.
 141:    * @param uri
 142:    * @return
 143:    */
 144:   public boolean touch(final URI uri)
 145:	     {
 146:      return AccessController.doPrivileged(
 147:            new PrivilegedAction<Boolean>()
 148:	              {
 149:               public Boolean run()
 150:	                 {
 151:                  return touchPrivate(uri);
 152:               }
 153:            }, context);      
 154:   }
 155:
 156:   private boolean touchPrivate(URI uri)
 157:	     {
 158:      if (uri.getScheme().toLowerCase().indexOf("file") >= 0)
 159:	        {
 160:         File f= new File(uri);
 161:         return f.exists();
 162:      }
 163:      else
 164:	        {
 165:         try
 166:	           {
 167:            return FileUtilities.touchURL(uri.toURL());
 168:         }
 169:         catch (MalformedURLException e)
 170:	           {
 171:            return false;
 172:         }
 173:      }
 174:   }
 175:   
 176:   /**
 177:    * Get an OutputStream within the given context for the URI 
 178:    * specified by uristr
 179:    * @param uristr
 180:    * @return
 181:    * @throws URISyntaxException
 182:    * @throws IOException
 183:    */
 184:   public OutputStream getOutput(final URI uri) throws IOException
 185:	     {
 186:      try
 187:	        {
 188:         return AccessController.doPrivileged(
 189:               new PrivilegedExceptionAction<OutputStream>()
 190:	                 {
 191:                  public OutputStream run() throws Exception
 192:	                    {
 193:                     return getOutputPrivate(uri);
 194:                  }
 195:               }, context);
 196:      } 
 197:      catch (Exception e)
 198:	        {
 199:         if (e.getCause() instanceof IOException)
 200:	           {
 201:            throw (IOException) e.getCause();
 202:         }
 203:         else
 204:	           {
 205:            e.printStackTrace();
 206:            return null;
 207:         }
 208:      }
 209:   }
 210:
 211:}