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  
21  package org.waarp.http.protocol;
22  
23  import org.waarp.common.command.exception.CommandAbstractException;
24  import org.waarp.common.database.exception.WaarpDatabaseException;
25  import org.waarp.common.logging.WaarpLogger;
26  import org.waarp.common.logging.WaarpLoggerFactory;
27  import org.waarp.common.utility.WaarpSystemUtil;
28  import org.waarp.http.protocol.servlet.HttpAuthent;
29  import org.waarp.openr66.context.R66BusinessInterface;
30  import org.waarp.openr66.context.R66FiniteDualStates;
31  import org.waarp.openr66.context.task.exception.OpenR66RunnerErrorException;
32  import org.waarp.openr66.database.data.DbTaskRunner;
33  import org.waarp.openr66.protocol.configuration.Configuration;
34  
35  import java.io.File;
36  
37  /**
38   * Http Resumable session
39   */
40  public class HttpDeleteSession extends HttpSessionAbstract {
41    private static final WaarpLogger logger =
42        WaarpLoggerFactory.getLogger(HttpDeleteSession.class);
43    private final String identifier;
44    private final File file;
45  
46    /**
47     * Constructor for reading from database only
48     *
49     * @param identifier
50     * @param authent
51     *
52     * @throws OpenR66RunnerErrorException
53     */
54    public HttpDeleteSession(final String identifier, final HttpAuthent authent)
55        throws WaarpDatabaseException {
56      super(authent);
57      this.identifier = identifier;
58      final DbTaskRunner runner;
59      if (!WaarpSystemUtil.isJunit()) {
60        checkAuthentR66Business(this, session, authent);
61        runner = getDbTaskRunner(authent.getUserId(), identifier);
62        try {
63          session.getDir().changeDirectory(runner.getRule().getRecvPath());
64        } catch (final CommandAbstractException e) {
65          // Nothing: ignore
66        }
67        final String baseDir = runner.getRule().getRecvPath();
68        final String path = session.getDir().getFullPath();
69        final String filepath_in = runner.getFilename();
70        final String finalPath =
71            filepath_in.replace(baseDir, path).replace("//", "/");
72        logger.info("From {} to {} using {} gives {}", baseDir, path, filepath_in,
73                    finalPath);
74        file = new File(finalPath);
75      } else {
76        runner = null;
77        file = null;
78      }
79    }
80  
81    /**
82     * Reload the DbTaskRunner for this user and rulename
83     *
84     * @param user
85     * @param identifier
86     *
87     * @return the DbTaskRunner, potentially new
88     *
89     * @throws IllegalArgumentException
90     */
91    private DbTaskRunner getDbTaskRunner(final String user,
92                                         final String identifier)
93        throws IllegalArgumentException, WaarpDatabaseException {
94      final long specialId = Long.parseLong(identifier);
95      final String requested = Configuration.configuration.getHostId();
96      DbTaskRunner runner = null;
97  
98      // Try to reload it
99      try {
100       logger.debug("{} {} <-> {}", specialId, user, requested);
101       runner = new DbTaskRunner(specialId, user, requested);
102     } catch (final WaarpDatabaseException e) {
103       logger.debug("{} {} {}", specialId, user, requested);
104       runner = new DbTaskRunner(specialId, requested, user);
105     }
106     runner.setSender(true);
107     try {
108       session.setRunner(runner);
109     } catch (final OpenR66RunnerErrorException e) {
110       logger.debug(e);
111     }
112     session.setReady(true);
113     return runner;
114   }
115 
116   /**
117    * @return the File
118    */
119   public final File getFile() {
120     return file;
121   }
122 
123   /**
124    * @return the identifier
125    */
126   public final String getIdentifier() {
127     return identifier;
128   }
129 
130   @Override
131   public final void error(final Exception e,
132                           final R66BusinessInterface business)
133       throws IllegalArgumentException {
134     logger.error(e.getMessage());
135     if (business != null) {
136       business.checkAtError(session);
137     }
138     session.newState(R66FiniteDualStates.ERROR);
139     throw new IllegalArgumentException(e);
140   }
141 
142   @Override
143   public String toString() {
144     return "DS: {" + session.toString() + ", " + identifier + ", " +
145            file.getAbsolutePath() + "}";
146   }
147 }