| 1 | |
package org.webslinger.commons.vfs; |
| 2 | |
|
| 3 | |
import org.apache.commons.vfs.FileContent; |
| 4 | |
import org.apache.commons.vfs.FileContentInfo; |
| 5 | |
import org.apache.commons.vfs.FileContentInfoFactory; |
| 6 | |
import org.apache.commons.vfs.FileSystemException; |
| 7 | |
|
| 8 | |
import org.webslinger.util.MimeTypeLookup; |
| 9 | |
import org.webslinger.lang.ConcurrentCache; |
| 10 | |
|
| 11 | |
public final class MimeTimeFileContentInfoFactory implements FileContentInfoFactory { |
| 12 | 0 | private static final ConcurrentCache<String, FileContentInfo> infos = new ConcurrentCache<String, FileContentInfo>(MimeTimeFileContentInfoFactory.class, "infos", null, ConcurrentCache.SOFT) { |
| 13 | |
protected FileContentInfo createValue(final String contentType) { |
| 14 | 0 | return new FileContentInfo() { |
| 15 | |
public String getContentType() { |
| 16 | 0 | return contentType; |
| 17 | |
} |
| 18 | |
|
| 19 | |
public String getContentEncoding() { |
| 20 | 0 | return null; |
| 21 | |
} |
| 22 | |
}; |
| 23 | |
} |
| 24 | |
}; |
| 25 | 0 | private static final FileContentInfo NULL_CONTENT_INFO = new FileContentInfo() { |
| 26 | |
public String getContentType() { |
| 27 | 0 | return null; |
| 28 | |
} |
| 29 | |
|
| 30 | |
public String getContentEncoding() { |
| 31 | 0 | return null; |
| 32 | |
} |
| 33 | |
}; |
| 34 | |
|
| 35 | |
private final MimeTypeLookup mimeTypes; |
| 36 | |
|
| 37 | 0 | public MimeTimeFileContentInfoFactory(MimeTypeLookup mimeTypes) { |
| 38 | 0 | this.mimeTypes = mimeTypes; |
| 39 | 0 | } |
| 40 | |
|
| 41 | |
public MimeTypeLookup getMimeTypes() { |
| 42 | 0 | return mimeTypes; |
| 43 | |
} |
| 44 | |
|
| 45 | |
public FileContentInfo create(FileContent fileContent) throws FileSystemException { |
| 46 | 0 | String ext = fileContent.getFile().getName().getExtension(); |
| 47 | 0 | if (ext.length() == 0) ext = null; |
| 48 | 0 | return getFileContentInfo(ext); |
| 49 | |
} |
| 50 | |
|
| 51 | |
protected FileContentInfo getFileContentInfo(String ext) throws FileSystemException { |
| 52 | |
try { |
| 53 | 0 | if (ext == null) return NULL_CONTENT_INFO; |
| 54 | 0 | String mimeType = mimeTypes.getMimeType(ext); |
| 55 | 0 | if (mimeType == null) return NULL_CONTENT_INFO; |
| 56 | 0 | return infos.get(mimeType); |
| 57 | 0 | } catch (Exception e) { |
| 58 | 0 | throw VFSUtil.makeFileSystemException(e); |
| 59 | |
} |
| 60 | |
} |
| 61 | |
} |
| 62 | |
|