1 17 package org.alfresco.repo.content.filestore; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.RandomAccessFile ; 24 import java.nio.channels.Channels ; 25 import java.nio.channels.ReadableByteChannel ; 26 import java.text.MessageFormat ; 27 28 import org.alfresco.repo.content.AbstractContentReader; 29 import org.alfresco.repo.content.MimetypeMap; 30 import org.alfresco.service.cmr.repository.ContentIOException; 31 import org.alfresco.service.cmr.repository.ContentReader; 32 import org.alfresco.service.cmr.repository.ContentWriter; 33 import org.alfresco.util.TempFileProvider; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 44 public class FileContentReader extends AbstractContentReader 45 { 46 53 public static final String MSG_MISSING_CONTENT = "content.content_missing"; 54 55 private static final Log logger = LogFactory.getLog(FileContentReader.class); 56 57 private File file; 58 private boolean allowRandomAccess; 59 60 73 public static ContentReader getSafeContentReader(ContentReader existingReader, String msgTemplate, Object ... args) 74 { 75 ContentReader reader = existingReader; 76 if (existingReader == null || !existingReader.exists()) 77 { 78 String fakeContent = MessageFormat.format(msgTemplate, args); 80 81 if (logger.isDebugEnabled()) 83 { 84 logger.debug(fakeContent); 85 } 86 87 File tempFile = TempFileProvider.createTempFile("getSafeContentReader_", ".txt"); 89 ContentWriter writer = new FileContentWriter(tempFile); 90 writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 91 writer.setEncoding("UTF-8"); 92 writer.putContent(fakeContent); 93 reader = writer.getReader(); 95 } 96 if (logger.isDebugEnabled()) 98 { 99 logger.debug("Created safe content reader: \n" + 100 " existing reader: " + existingReader + "\n" + 101 " safe reader: " + reader); 102 } 103 return reader; 104 } 105 106 112 public FileContentReader(File file) 113 { 114 this(file, FileContentStore.STORE_PROTOCOL + file.getAbsolutePath()); 115 } 116 117 124 public FileContentReader(File file, String url) 125 { 126 super(url); 127 128 this.file = file; 129 allowRandomAccess = true; 130 } 131 132 void setAllowRandomAccess(boolean allow) 133 { 134 this.allowRandomAccess = allow; 135 } 136 137 140 public File getFile() 141 { 142 return file; 143 } 144 145 public boolean exists() 146 { 147 return file.exists(); 148 } 149 150 153 public long getSize() 154 { 155 if (!exists()) 156 { 157 return 0L; 158 } 159 else 160 { 161 return file.length(); 162 } 163 } 164 165 168 public long getLastModified() 169 { 170 if (!exists()) 171 { 172 return 0L; 173 } 174 else 175 { 176 return file.lastModified(); 177 } 178 } 179 180 184 @Override 185 protected ContentReader createReader() throws ContentIOException 186 { 187 FileContentReader reader = new FileContentReader(this.file, getContentUrl()); 188 reader.setAllowRandomAccess(this.allowRandomAccess); 189 return reader; 190 } 191 192 @Override 193 protected ReadableByteChannel getDirectReadableChannel() throws ContentIOException 194 { 195 try 196 { 197 if (!file.exists()) 199 { 200 throw new IOException ("File does not exist"); 201 } 202 ReadableByteChannel channel = null; 204 if (allowRandomAccess) 205 { 206 RandomAccessFile randomAccessFile = new RandomAccessFile (file, "r"); channel = randomAccessFile.getChannel(); 208 } 209 else 210 { 211 InputStream is = new FileInputStream (file); 212 channel = Channels.newChannel(is); 213 } 214 if (logger.isDebugEnabled()) 216 { 217 logger.debug("Opened write channel to file: \n" + 218 " file: " + file + "\n" + 219 " random-access: " + allowRandomAccess); 220 } 221 return channel; 222 } 223 catch (Throwable e) 224 { 225 throw new ContentIOException("Failed to open file channel: " + this, e); 226 } 227 } 228 229 232 public boolean canWrite() 233 { 234 return false; } 236 } 237 | Popular Tags |