View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.file;
21  
22  import org.waarp.common.command.exception.CommandAbstractException;
23  import org.waarp.common.command.exception.Reply502Exception;
24  import org.waarp.common.command.exception.Reply530Exception;
25  import org.waarp.common.exception.NoRestartException;
26  
27  import java.io.IOException;
28  
29  /**
30   *
31   */
32  public abstract class AbstractFile implements FileInterface {
33    /**
34     * Is this Document ready to be accessed
35     */
36    protected boolean isReady;
37  
38    @Override
39    public void clear() throws CommandAbstractException {
40      closeFile();
41      isReady = false;
42    }
43  
44    @Override
45    public final void checkIdentify() throws Reply530Exception {
46      if (!getSession().getAuth().isIdentified()) {
47        throw new Reply530Exception("User not authentified");
48      }
49    }
50  
51    @Override
52    public DataBlock getMarker() throws CommandAbstractException {
53      throw new Reply502Exception("No marker implemented");
54    }
55  
56    @Override
57    public boolean restartMarker(final Restart restart)
58        throws CommandAbstractException {
59      try {
60        final long newposition = restart.getPosition();
61        try {
62          setPosition(newposition);
63        } catch (final IOException e) {
64          throw new Reply502Exception("Cannot set the marker position");
65        }
66        return true;
67      } catch (final NoRestartException ignored) {
68        // nothing
69      }
70      return false;
71    }
72  
73    @Override
74    public final boolean retrieve() throws CommandAbstractException {
75      checkIdentify();
76      if (isReady) {
77        restartMarker(getSession().getRestart());
78        return canRead();
79      }
80      return false;
81    }
82  
83    @Override
84    public final boolean store() throws CommandAbstractException {
85      checkIdentify();
86      if (isReady) {
87        restartMarker(getSession().getRestart());
88        return canWrite();
89      }
90      return false;
91    }
92  
93  }