1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.demo.rmi.fd;
19
20 import java.io.Closeable;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.nio.file.AccessDeniedException;
26
27 import org.newsclub.net.unix.demo.rmi.services.StreamService;
28 import org.newsclub.net.unix.rmi.AFUNIXRMISocketFactory;
29 import org.newsclub.net.unix.rmi.RemoteCloseableImpl;
30 import org.newsclub.net.unix.rmi.RemoteFileInput;
31 import org.newsclub.net.unix.rmi.RemoteFileOutput;
32
33 import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
34
35
36
37
38
39
40 public class StreamServiceImpl implements StreamService, Closeable {
41 private final AFUNIXRMISocketFactory socketFactory;
42
43
44
45
46
47
48 @SuppressFBWarnings("EI_EXPOSE_REP")
49 public StreamServiceImpl(AFUNIXRMISocketFactory socketFactory) {
50 this.socketFactory = socketFactory;
51 }
52
53 @Override
54 public void close() throws IOException {
55 }
56
57 @Override
58 public RemoteCloseableImpl<RemoteFileInput> openForReading(File path) throws IOException {
59 boolean permitted = mayRead(path);
60 System.out.println("Reading from " + path + "; permitted=" + permitted);
61 if (!permitted) {
62 throw new AccessDeniedException("Not permitted");
63 }
64 FileInputStream fin = new FileInputStream(path);
65 return new RemoteCloseableImpl<RemoteFileInput>(socketFactory, new RemoteFileInput(
66 socketFactory, fin));
67 }
68
69 @Override
70 public RemoteCloseableImpl<RemoteFileOutput> openForWriting(File path) throws IOException {
71 boolean permitted = mayWrite(path);
72 System.out.println("Writing to " + path + "; permitted=" + permitted);
73 if (!permitted) {
74 throw new AccessDeniedException("Not permitted");
75 }
76 FileOutputStream fout = new FileOutputStream(path);
77 return new RemoteCloseableImpl<RemoteFileOutput>(socketFactory, new RemoteFileOutput(
78 socketFactory, fout));
79 }
80
81
82
83
84
85
86
87 protected boolean mayRead(File path) {
88 return true;
89 }
90
91
92
93
94
95
96
97 protected boolean mayWrite(File path) {
98 return true;
99 }
100 }