1 package com.ibm.webdav.fileSystem; 2 3 17 import java.io.*; 18 import java.net.*; 19 import java.util.*; 20 21 import javax.xml.parsers.*; 22 23 import org.w3c.dom.*; 24 25 import com.ibm.webdav.*; 26 import com.ibm.webdav.impl.*; 27 28 29 35 public class NamespaceManager implements com.ibm.webdav.impl.NamespaceManager { 36 37 private ResourceImpl resource = null; 38 private OutputStream openedOutputStream = null; 39 40 private static final int bufferSize = 8192; 41 44 public NamespaceManager() { 45 } 46 49 public NamespaceManager(ResourceImpl resource) { 50 initialize(resource); 51 } 52 56 public void closeContentsOutputStream() throws WebDAVException { 57 if (openedOutputStream == null) { 59 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "No output stream is opened"); 60 } 61 try { 62 openedOutputStream.close(); 63 } catch (WebDAVException exc) { 64 throw exc; 65 } catch (java.io.IOException exc) { 66 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error"); 67 } 68 openedOutputStream = null; 69 } 70 75 public void createCollection(String localName) throws WebDAVException { 76 String parentName = getParentName(); 78 File parent = new File(parentName); 79 if (!parent.canWrite()) { 80 throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Insufficient permissions"); 81 } 82 File dir = new File(localName); 83 if (!dir.mkdir()) { 84 throw new WebDAVException(WebDAVStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE, "Could not create collection"); 85 } 86 } 87 93 public void createLockNullResource() throws WebDAVException { 94 Document document = resource.loadProperties(); 95 resource.saveProperties(document); 96 } 97 100 public void createBinding(URL destURL) throws WebDAVException { 101 } 103 106 public void move(String path) throws WebDAVException { 107 } 109 114 public void delete() throws WebDAVException { 115 String fileName = ResourceFactory.getRealPath(resource.getURL()); 116 File file = new File(fileName); 117 118 if (!file.canWrite()) { 120 throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Insufficient permissions"); 121 } 122 123 if (!file.delete()) { 125 throw new WebDAVException(WebDAVStatus.SC_CONFLICT, "Unable to delete resource, may be in use"); 126 } 127 } 128 133 private InputStream directoryToXMLStream(File file) throws WebDAVException { 134 Document document = null; 135 136 try { 137 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 138 } catch(Exception e) { 139 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getMessage()); 140 } 141 144 Element collection = document.createElement("D:collection"); 145 collection.setAttribute("xmlns:D", "DAV:"); 146 document.appendChild(collection); 147 String [] fileNames = file.list(); 148 for (int i = 0; i < fileNames.length; i++) { 149 String fileName = fileNames[i]; 150 String propertiesFileName = null; 151 if (fileName.endsWith(PropertiesManager.propertiesSuffix)) { 152 propertiesFileName = fileName; 153 fileName = propertiesFileName.substring(0, propertiesFileName.length() - PropertiesManager.propertiesSuffix.length()); 154 } 155 if (propertiesFileName == null || (propertiesFileName != null && !new File(fileName).exists())) { 157 Element uri = document.createElement("D:member"); 158 uri.appendChild(document.createTextNode(fileName)); 159 collection.appendChild(uri); 160 } 161 } 162 ByteArrayOutputStream os = new ByteArrayOutputStream(); 163 try { 164 PrintWriter pout = new PrintWriter(new OutputStreamWriter(os, Resource.defaultCharEncoding), false); 165 pout.print(XMLUtility.printNode(document.getDocumentElement())); 166 } catch (Exception exc) { 168 } 169 resource.getResponseContext().contentType("text/xml"); 172 return new ByteArrayInputStream(os.toByteArray()); 173 } 174 179 public boolean exists() throws WebDAVException { 180 File file = new File(ResourceFactory.getRealPath(resource.getURL())); 181 return file.exists(); 182 } 183 190 public InputStream getContentsInputStream() throws WebDAVException { 191 File file = new File(ResourceFactory.getRealPath(resource.getURL())); 192 InputStream is = null; 193 194 try { 195 if (file.isDirectory()) { 197 resource.getResponseContext().contentType("text/xml"); 198 is = directoryToXMLStream(file); 199 } else { 200 is = new BufferedInputStream(new FileInputStream(file), bufferSize); 201 String contentType = guessAtContentTypeForName( file.getName() ); 203 resource.getResponseContext().contentType(contentType); 204 if (!contentType.startsWith("text/")) { 206 resource.getResponseContext().contentLength(file.length()); 207 } 208 String cdstring = new SimpleRFC1123DateFormat().format(new Date(file.lastModified())); 209 resource.getResponseContext().lastModified(cdstring); 210 } 211 } catch (FileNotFoundException exc) { 212 if (file.exists()) { 213 throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions"); 214 } else { 215 throw new WebDAVException(WebDAVStatus.SC_NOT_FOUND, "Resource not found"); 216 } 217 } catch (SecurityException exc) { 218 throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions"); 219 } 220 return is; 221 } 222 227 public OutputStream getContentsOutputStream() throws WebDAVException { 228 String fileName = ResourceFactory.getRealPath(resource.getURL()); 229 File file = new File(fileName); 230 231 if (file.exists() && !file.canWrite()) { 233 throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Insufficient permissions"); 234 } 235 236 if (fileName.endsWith(PropertiesManager.propertiesSuffix)) { 238 throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Can't write to WebDAV properties files"); 239 } 240 241 try { 243 openedOutputStream = new BufferedOutputStream(new FileOutputStream(file), bufferSize); 244 } catch (java.io.IOException exc) { 247 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error"); 248 } 249 return openedOutputStream; 250 } 251 256 public Vector getMembers() throws WebDAVException { 257 Vector members = new Vector(); 258 String parentName = ResourceFactory.getRealPath(resource.getURL()); 259 if (!parentName.endsWith(File.separator)) { 260 parentName = parentName + File.separator; 261 } 262 263 File parent = new File(parentName); 264 if (!parent.isDirectory()) { 265 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Resource is not a collection"); 266 } 267 268 String [] fileNames = parent.list(); 269 if (fileNames == null) { 270 return members; 273 } 274 275 for (int i = 0; i < fileNames.length; i++) { 276 String fileName = fileNames[i]; 277 String propertiesFileName = null; 278 if (fileName.endsWith(PropertiesManager.propertiesSuffix)) { 279 propertiesFileName = fileName; 280 fileName = propertiesFileName.substring(0, propertiesFileName.length() - PropertiesManager.propertiesSuffix.length()); 281 } 282 String childName = parentName + fileName; 284 File file = new File(childName); 285 if (propertiesFileName == null || !file.exists()) { 286 ResourceImpl member = null; 287 if (file.isDirectory()) { 288 member = new CollectionImpl(((CollectionImpl) resource).getChildURL(fileName), childName, null); 289 } else { 290 member = new ResourceImpl(((CollectionImpl) resource).getChildURL(fileName), childName); 291 } 292 member.setRequestContext(resource.getContext()); 293 members.addElement(member); 294 } 295 } 296 return members; 297 } 298 303 public String getParentName() throws WebDAVException { 304 String fileName = ResourceFactory.getRealPath(resource.getURL()) ; 305 int delimiterPosition = 0; 306 if (fileName.endsWith(File.separator)) { 307 delimiterPosition = fileName.substring(0, fileName.length() - 1).lastIndexOf(File.separator); 308 } else { 309 delimiterPosition = fileName.lastIndexOf(File.separator); 310 } 311 return fileName.substring(0, delimiterPosition + 1); 312 } 313 318 public static String guessAtContentTypeForName(String fn) { 319 int i = fn.lastIndexOf('.'); 320 if (i == -1) 321 return "text/plain"; 322 String ext = fn.substring(i + 1); 323 String mtype = ResourceFactory.properties.getProperty(ext, "text/plain").trim(); 324 if (mtype == null) { 328 mtype = "text/plain"; 329 } 330 if (fn.endsWith(".xml")) { 331 mtype = "text/xml"; 332 } 333 return mtype; 334 } 335 338 public void initialize(ResourceImpl resource) { 339 this.resource = resource; 340 } 341 345 public boolean isCollection() throws WebDAVException { 346 File file = new File(ResourceFactory.getRealPath(resource.getURL())); 347 if (!file.exists()) { 348 throw new ClientException( 404, "File Not Found" ); 349 } 350 return file.isDirectory(); 351 } 352 357 public boolean isLockNull() throws WebDAVException { 358 File file = new File(ResourceFactory.getRealPath(resource.getURL())); 359 String propertiesFileName = ResourceFactory.getRealPath(resource.getURL()); 360 if (propertiesFileName.endsWith(File.separator)) { 361 propertiesFileName = propertiesFileName.substring(0, propertiesFileName.length() - 1); 362 } 363 propertiesFileName = propertiesFileName + PropertiesManager.propertiesSuffix; 364 File propertiesFile = new File(propertiesFileName); 365 return !file.exists() && propertiesFile.exists(); 366 } 367 374 public byte[] performWith(String args) throws WebDAVException { 375 StringWriter buf = new StringWriter(); 377 378 try { 379 Runtime runtime = Runtime.getRuntime(); 380 Process p = runtime.exec(ResourceFactory.getRealPath(resource.getURL()) + " " + args); 381 BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream())); 382 int ch = -1; 383 while ((ch = inputStream.read()) != -1) { 384 buf.write(ch); 385 } 386 } catch (Exception exc) { 387 System.err.println("MsxlxServlet exception: " + exc); 388 } 389 390 return buf.toString().getBytes(); 391 } 392 395 public boolean isVersionable() throws WebDAVException { 396 return false; 398 } 399 402 public List getAllowedMethods() throws WebDAVException { 403 return null; 405 } 406 409 public void setOrdering(Document orderPatch) { 410 412 } 413 416 public void createBinding(String bindName, URL source) throws WebDAVException { 417 419 } 420 423 public String getContentType() throws WebDAVException { 424 return null; 426 } 427 430 public void closeContentsOutputStream(String sContentType) throws WebDAVException { 431 433 } 434 } 435 | Popular Tags |