View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * An implementation of {@link StreamService}.
37   *
38   * @author Christian Kohlschütter
39   */
40  public class StreamServiceImpl implements StreamService, Closeable {
41    private final AFUNIXRMISocketFactory socketFactory;
42  
43    /**
44     * Creates a new instance.
45     *
46     * @param socketFactory The socket factory to use.
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     * Checks if the given path may be accessed for reading.
83     *
84     * @param path The path to check.
85     * @return {@code true} if permitted.
86     */
87    protected boolean mayRead(File path) {
88      return true;
89    }
90  
91    /**
92     * Checks if the given path may be accessed for writing.
93     *
94     * @param path The path to check.
95     * @return {@code true} if permitted.
96     */
97    protected boolean mayWrite(File path) {
98      return true;
99    }
100 }