]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.jcr/src/org/argeo/jcr/fs/BinaryChannel.java
Improve error feedback in JCR FS
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / BinaryChannel.java
index ff4b4a1b3b1055be35e80791f80971140a62bfa2..94eb70482b6751ba3b7d62b98b138acab37a891f 100644 (file)
@@ -12,6 +12,8 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
 
+import javax.activation.FileTypeMap;
+import javax.activation.MimetypesFileTypeMap;
 import javax.jcr.Binary;
 import javax.jcr.Node;
 import javax.jcr.Property;
@@ -19,7 +21,6 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 
-import org.apache.commons.io.IOUtils;
 import org.argeo.jcr.JcrUtils;
 
 public class BinaryChannel implements SeekableByteChannel {
@@ -32,6 +33,16 @@ public class BinaryChannel implements SeekableByteChannel {
        // private ByteBuffer toWrite;
        private FileChannel fc = null;
 
+       private static FileTypeMap fileTypeMap;
+
+       static {
+               try {
+                       fileTypeMap = new MimetypesFileTypeMap("/etc/mime.types");
+               } catch (IOException e) {
+                       fileTypeMap = FileTypeMap.getDefaultFileTypeMap();
+               }
+       }
+
        public BinaryChannel(Node file) throws RepositoryException, IOException {
                this.file = file;
                // int capacity = 1024 * 1024;
@@ -45,6 +56,13 @@ public class BinaryChannel implements SeekableByteChannel {
                                try (InputStream in = new ByteArrayInputStream(new byte[0])) {
                                        this.binary = data.getSession().getValueFactory().createBinary(in);
                                }
+                               data.setProperty(Property.JCR_DATA, this.binary);
+
+                               // MIME type
+                               String mime = fileTypeMap.getContentType(file.getName());
+                               data.setProperty(Property.JCR_MIMETYPE, mime);
+
+                               data.getSession().save();
                        }
                } else {
                        throw new IllegalArgumentException(
@@ -60,7 +78,7 @@ public class BinaryChannel implements SeekableByteChannel {
        @Override
        public synchronized void close() throws IOException {
                if (isModified()) {
-                       Binary newBinary=null;
+                       Binary newBinary = null;
                        try {
                                Session session = file.getSession();
                                // byte[] arr = new byte[(int) position];
@@ -73,10 +91,13 @@ public class BinaryChannel implements SeekableByteChannel {
                                session.save();
                                open = false;
                        } catch (RepositoryException e) {
-                               throw new JcrFsException("Cannot close " + file, e);
-                       }finally{
+                               throw new IOException("Cannot close " + file, e);
+                       } finally {
                                JcrUtils.closeQuietly(newBinary);
-                               IOUtils.closeQuietly(fc);
+                               // IOUtils.closeQuietly(fc);
+                               if (fc != null) {
+                                       fc.close();
+                               }
                        }
                } else {
                        clearReadState();
@@ -92,10 +113,10 @@ public class BinaryChannel implements SeekableByteChannel {
 
                        try {
                                int read;
-//                             int capacity = dst.capacity();
+                               // int capacity = dst.capacity();
                                byte[] arr = dst.array();
                                read = binary.read(arr, position);
-                               //dst.put(arr, 0, read);
+                               // dst.put(arr, 0, read);
 
                                // try {
                                // byte[] arr = dst.array();
@@ -110,7 +131,7 @@ public class BinaryChannel implements SeekableByteChannel {
                                        position = position + read;
                                return read;
                        } catch (RepositoryException e) {
-                               throw new JcrFsException("Cannot read into buffer", e);
+                               throw new IOException("Cannot read into buffer", e);
                        }
                }
        }
@@ -156,7 +177,7 @@ public class BinaryChannel implements SeekableByteChannel {
                        try {
                                return binary.getSize();
                        } catch (RepositoryException e) {
-                               throw new JcrFsException("Cannot get size", e);
+                               throw new IOException("Cannot get size", e);
                        }
                }
        }
@@ -174,24 +195,25 @@ public class BinaryChannel implements SeekableByteChannel {
                try {
                        if (fc == null) {
                                Path tempPath = Files.createTempFile(getClass().getSimpleName(), null);
-                               fc = FileChannel.open(tempPath, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE, StandardOpenOption.SPARSE);
+                               fc = FileChannel.open(tempPath, StandardOpenOption.WRITE, StandardOpenOption.READ,
+                                               StandardOpenOption.DELETE_ON_CLOSE, StandardOpenOption.SPARSE);
                                ReadableByteChannel readChannel = Channels.newChannel(binary.getStream());
                                fc.transferFrom(readChannel, 0, binary.getSize());
                                clearReadState();
                        }
                        return fc;
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot get temp file channel", e);
+                       throw new IOException("Cannot get temp file channel", e);
                }
        }
 
        private boolean isModified() {
                return fc != null;
        }
-       
-       private void clearReadState(){
+
+       private void clearReadState() {
                position = -1;
                JcrUtils.closeQuietly(binary);
-               binary=null;
+               binary = null;
        }
 }