public boolean accept(File file) { if (!file.isFile()) { return false; } String type = getContentTypeFromName(file.getName()); String primary = type.substring(0, type.indexOf('/')); return accept.contains(type) || accept.contains(primary + "/*"); } });
String primary = media.substring(0, media.indexOf('/') + 1); String subtype = media.substring(plus + 1); return getFileSuffixFromType(primary + subtype); } else if (!media.startsWith("application/")) { String subtype = media.substring(media.indexOf('/') + 1); return getFileSuffixFromType("application/" + subtype);
protected String getContentTypeFromName(String name) { final String ext = getFileExtension(name); if (ext == null) { return "application/octet-stream"; } if (cachedMapping == null) { // Let's do this only once... cachedMapping = new Hashtable<String,String> (); Enumeration<?> types = contentTypes.propertyNames(); while (types.hasMoreElements()) { String type = (String) types.nextElement(); String attrs = contentTypes.getProperty(type); String[] tokens = attrs.split(";"); for (String tok : tokens) { if (tok.startsWith("file_extensions=")) { String extList = tok.substring(16); String[] exts = extList.split(","); for (String e : exts) { cachedMapping.put(e, type); } } } } } if (cachedMapping.containsKey(ext)) { return cachedMapping.get(ext); } return "application/octet-stream"; }
public FileDataStore(DataStore fallback) { super(); this.fallback = fallback; contentTypes = new Properties(); loadDefaultContentTypes(contentTypes); loadContentTypes(contentTypes); }
public void testWriteFile() throws IOException { store.writeEntry("file.txt", tmp, "text/plain", new DataWriter() { public void store(OutputStream content) throws IOException { content.write("content".getBytes()); } }); store.readEntry("file.txt", tmp, "text/plain", null, new DataReader() { public void load(URI id, String media, InputStream content, long len) throws IOException { byte[] buf = new byte[1024]; assertEquals("content", new String(buf, 0, content.read(buf))); } }); store.deleteEntry("file.txt", tmp); }
public void listEachEntry(String href, String base, String accept, DataInfo handler) throws MalformedURLException, FileNotFoundException, IOException { URI baseURI = URI.create(base); URI uri = baseURI.resolve(href); if ("file".equalsIgnoreCase(uri.getScheme())) { File file = URIUtils.toFile(uri); if (!file.canRead()) { throw XProcException.stepError(12); } if (file.isDirectory()) { for (File f : listAcceptableFiles(file, accept)) { String type; if (f.isDirectory()) { type = null; } else { type = getContentTypeFromName(f.getName()); } handler.list(f.toURI(), type, f.lastModified()); } } else { throw new FileNotFoundException(file.getAbsolutePath() + " is not a directory"); } } else { fallback.listEachEntry(href, base, accept, handler); } }
public void testWriteDirectory() throws IOException { URI file = store.writeEntry("dir/", tmp, "text/plain", new DataWriter() { public void store(OutputStream content) throws IOException { content.write("content".getBytes()); } }); store.infoEntry(file.getPath(), tmp, "text/plain", new DataInfo() { public void list(URI id, String media, long lastModified) throws IOException { assertEquals("text/plain", media); } }); store.readEntry(file.getPath(), tmp, "text/plain", null, new DataReader() { public void load(URI id, String media, InputStream content, long len) throws IOException { byte[] buf = new byte[1024]; assertEquals("content", new String(buf, 0, content.read(buf))); } }); store.deleteEntry(file.getPath(), tmp); store.deleteEntry("dir", tmp); }
public void setUp() throws Exception { store = new FileDataStore(new FallbackDataStore()); }
public void testListAcceptableFiles() throws IOException { URI dir = store.createList("dir", tmp); final File text = File.createTempFile("test", ".txt", new File(dir)); final File xml = File.createTempFile("test", ".xml", new File(dir)); final File json = File.createTempFile("test", ".json", new File(dir)); final File zip = File.createTempFile("test", ".zip", new File(dir)); assertEquals(4, store.listAcceptableFiles(new File(dir), "*/*").length); assertEquals(Collections.singletonList(text), Arrays.asList(store.listAcceptableFiles(new File(dir), "text/plain"))); assertEquals(Collections.singletonList(xml), Arrays.asList(store.listAcceptableFiles(new File(dir), "application/xml"))); assertEquals(Collections.singletonList(json), Arrays.asList(store.listAcceptableFiles(new File(dir), "application/json"))); assertEquals(Collections.singletonList(zip), Arrays.asList(store.listAcceptableFiles(new File(dir), "application/zip"))); zip.delete(); json.delete(); xml.delete(); text.delete(); new File(dir).delete(); }
public void testListEachEntry() throws IOException { URI dir = store.createList("dir", tmp); final File text = File.createTempFile("test", ".txt", new File(dir)); final File xml = File.createTempFile("test", ".xml", new File(dir)); store.listEachEntry("dir", tmp, "text/plain", new DataInfo() { public void list(URI id, String media, long lastModified) throws IOException { assertEquals("text/plain", media); assertEquals(text.getAbsolutePath(), new File(id).getAbsolutePath()); } }); store.listEachEntry("dir", tmp, "application/xml", new DataInfo() { public void list(URI id, String media, long lastModified) throws IOException { assertEquals("application/xml", media); assertEquals(xml.getAbsolutePath(), new File(id).getAbsolutePath()); } }); store.deleteEntry(text.getPath(), tmp); store.deleteEntry(xml.getPath(), tmp); store.deleteEntry(dir.getPath(), tmp); }
public void testInfoEntry() throws IOException { File file = File.createTempFile("test", ".txt"); FileWriter writer = new FileWriter(file); try { writer.write("read content"); } finally { writer.close(); } String uri = file.toURI().toASCIIString(); store.readEntry(uri, uri, "*/*", null, new DataReader() { public void load(URI id, String media, InputStream content, long len) throws IOException { assertEquals("text/plain", media); } }); store.deleteEntry(file.getPath(), tmp); }
public void testDeleteEntry() throws IOException { URI file = store.writeEntry("dir/", tmp, "text/plain", new DataWriter() { public void store(OutputStream content) throws IOException { content.write("content".getBytes()); } }); assertTrue(new File(file).exists()); store.deleteEntry(file.getPath(), tmp); assertFalse(new File(file).exists()); }
public void testCreateList() throws IOException { URI dir = store.createList("dir", tmp); assertTrue(new File(dir).isDirectory()); }
public void listEachEntry(String href, String base, String accept, DataInfo handler) throws MalformedURLException, FileNotFoundException, IOException { URI baseURI = URI.create(base); URI uri = baseURI.resolve(href); if ("file".equalsIgnoreCase(uri.getScheme())) { File file = URIUtils.toFile(uri); if (!file.canRead()) { throw XProcException.stepError(12); } if (file.isDirectory()) { for (File f : listAcceptableFiles(file, accept)) { String type; if (f.isDirectory()) { type = null; } else { type = getContentTypeFromName(f.getName()); } handler.list(f.toURI(), type, f.lastModified()); } } else { throw new FileNotFoundException(file.getAbsolutePath() + " is not a directory"); } } else { fallback.listEachEntry(href, base, accept, handler); } }
public FileDataStore(DataStore fallback) { super(); this.fallback = fallback; contentTypes = new Properties(); loadDefaultContentTypes(contentTypes); loadContentTypes(contentTypes); }
public synchronized DataStore getDataStore() { if (dataStore == null) { DataStore fallback = new URLDataStore(new FallbackDataStore()); if (!getSafeMode()) { fallback = new FileDataStore(fallback); } dataStore = new HttpClientDataStore(getHttpClient(), fallback); } return dataStore; }
public void testReadEntry() throws IOException { File file = File.createTempFile("test", ".txt"); FileWriter writer = new FileWriter(file); try { writer.write("read content"); } finally { writer.close(); } String uri = file.toURI().toASCIIString(); store.readEntry(uri, uri, "*/*", null, new DataReader() { public void load(URI id, String media, InputStream content, long len) throws IOException { byte[] buf = new byte[1024]; assertEquals("read content", new String(buf, 0, content.read(buf))); } }); store.deleteEntry(file.getPath(), tmp); }
public boolean accept(File file) { if (!file.isFile()) { return false; } String type = getContentTypeFromName(file.getName()); String primary = type.substring(0, type.indexOf('/')); return accept.contains(type) || accept.contains(primary + "/*"); } });
String primary = media.substring(0, media.indexOf('/') + 1); String subtype = media.substring(plus + 1); return getFileSuffixFromType(primary + subtype); } else if (!media.startsWith("application/")) { String subtype = media.substring(media.indexOf('/') + 1); return getFileSuffixFromType("application/" + subtype);
public synchronized DataStore getDataStore() { if (dataStore == null) { DataStore fallback = new URLDataStore(new FallbackDataStore()); if (!getSafeMode()) { fallback = new FileDataStore(fallback); } dataStore = new HttpClientDataStore(getHttpClient(), fallback); } return dataStore; }