1 package com.ibm.webdav.fileSystem; 2 3 17 import java.io.*; 18 import java.util.*; 19 20 import javax.xml.parsers.*; 21 22 import org.w3c.dom.*; 23 24 import com.ibm.webdav.*; 25 import com.ibm.webdav.impl.*; 26 27 28 32 public class PropertiesManager extends CachedPropertiesManager { 33 34 public static String propertiesSuffix = ".wdp"; 36 protected Document cachedPropertiesDocument = null; 37 protected long cacheTimeStamp = 0; 38 private static final int bufferSize = 8192; 39 public PropertiesManager() 40 { 41 } 42 44 public PropertiesManager(ResourceImpl resource, 45 com.ibm.webdav.impl.NamespaceManager namespaceManager) 46 { 47 super(resource, namespaceManager); 48 } 49 54 public void deleteProperties() throws WebDAVException { 55 File propertiesFile = new File(getPropertiesFileName()); 56 propertiesFile.delete(); 57 cachedPropertiesDocument = null; 58 } 59 64 private String getPropertiesFileName() throws WebDAVException { 65 String fileName = ResourceFactory.getRealPath(resource.getURL()); 66 if (fileName.endsWith(File.separator)) { 68 fileName = fileName.substring(0, fileName.length() - 1); 69 } 70 71 return fileName + PropertiesManager.propertiesSuffix; 73 } 74 public Vector loadPropertyDefinitions() throws WebDAVException { 75 return null; 76 } 77 78 83 public Document loadProperties() throws WebDAVException { 84 File propertiesFile = new File(getPropertiesFileName()); 85 try { 86 if (propertiesFile.exists() && !propertiesFile.canRead()) { 88 throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions"); 89 } 90 91 if (cachedPropertiesDocument != null && propertiesFile.lastModified() <= cacheTimeStamp) { 92 return cachedPropertiesDocument; 93 } 94 cacheTimeStamp = propertiesFile.lastModified(); 95 96 Reader reader = new InputStreamReader(new FileInputStream(propertiesFile), Resource.defaultCharEncoding); 98 Reader is = new BufferedReader(reader); 99 WebDAVErrorHandler errorHandler = new WebDAVErrorHandler(resource.getURL().toString()); 100 101 105 DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 106 docbuilder.setErrorHandler(errorHandler); 107 cachedPropertiesDocument = docbuilder.parse(new org.xml.sax.InputSource (is)); 108 if (errorHandler.getErrorCount() > 0) { 109 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Syntax error in properties file"); 110 } 111 112 Element properties = (Element) cachedPropertiesDocument.getDocumentElement(); 115 if (properties == null || !properties.getTagName().equals("properties")) { 116 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Bad properties file"); 117 } 118 } catch (FileNotFoundException exc) { 119 if (propertiesFile.exists()) { 120 throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions"); 121 } else { 122 try { 124 cachedPropertiesDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 125 } catch(Exception e) { 126 throw new WebDAVException(WebDAVStatus.SC_PROCESSING,e.getMessage()); 127 } 128 Element properties = cachedPropertiesDocument.createElement("properties"); 130 properties.setAttribute("xmlns:D", "DAV:"); 131 cachedPropertiesDocument.appendChild(properties); 132 } 133 } catch (SecurityException exc) { 134 throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions"); 135 } catch (Exception exc) { 136 exc.printStackTrace(); 137 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Can't read properties file"); 138 } 139 return cachedPropertiesDocument; 140 } 141 148 public void removeLiveProperties(Document propertiesDocument) { 149 Element properties = propertiesDocument.getDocumentElement(); 150 Element p = null; 151 p = (Element)((Element) properties).getElementsByTagNameNS("DAV:", "getcontentlength").item(0); 152 if (p != null) 153 properties.removeChild(p); 154 p = (Element)((Element) properties).getElementsByTagNameNS("DAV:", "resourcetype").item(0); 155 if (p != null) 156 properties.removeChild(p); 157 p = (Element)((Element) properties).getElementsByTagNameNS("DAV:", "getlastmodified").item(0); 158 if (p != null) 159 properties.removeChild(p); 160 p = (Element)((Element) properties).getElementsByTagNameNS("DAV:", "getcontenttype").item(0); 164 if (p != null) 165 properties.removeChild(p); 166 } 167 172 public void saveProperties(Document propertiesDocument) throws WebDAVException { 173 File propertiesFile = new File(getPropertiesFileName()); 174 try { 175 if (propertiesFile.exists() && !propertiesFile.canWrite()) { 177 throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions"); 178 } 179 180 Writer writer = new OutputStreamWriter(new FileOutputStream(propertiesFile), Resource.defaultCharEncoding); 182 PrintWriter pout = new PrintWriter(writer, false); 183 pout.print(XMLUtility.printNode(propertiesDocument.getDocumentElement())); 184 pout.close(); 186 } catch (Exception exc) { 187 exc.printStackTrace(); 188 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "Can't write properties file"); 189 } 190 } 191 197 public void updateLiveProperties(Document document) throws WebDAVException { 198 Element properties = document.getDocumentElement(); 199 200 Element getContentLength = document.createElement("D:getcontentlength"); 202 long length = 0; 203 File file = new File(ResourceFactory.getRealPath(resource.getURL())); 204 if (file.exists()) { 205 length = file.length(); 206 } 207 getContentLength.appendChild(document.createTextNode(new Long (length).toString())); 211 properties.appendChild(getContentLength); 212 Element resourceType = document.createElement("D:resourcetype"); 213 if (file.exists() && file.isDirectory()) { 214 resourceType.appendChild(document.createElement("D:collection")); 215 } 216 properties.appendChild(resourceType); 217 Element lastModifiedDate = document.createElement("D:getlastmodified"); 218 String cdstring = new SimpleRFC1123DateFormat().format(new Date(file.lastModified())); 221 lastModifiedDate.appendChild(document.createTextNode(cdstring)); 222 properties.appendChild(lastModifiedDate); 223 224 Element contentType = document.createElement("D:getcontenttype"); 225 String mimetype = com.ibm.webdav.fileSystem.NamespaceManager.guessAtContentTypeForName(ResourceFactory.getRealPath(resource.getURL())); 226 contentType.appendChild(document.createTextNode(mimetype)); 227 properties.appendChild(contentType); 228 } 229 } 230 | Popular Tags |