View Javadoc
1   package fr.ifremer.quadrige3.core.service.extraction;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.ImmutableMap;
27  import fr.ifremer.quadrige3.core.ProgressionCoreModel;
28  import fr.ifremer.quadrige3.core.exception.QuadrigeBusinessException;
29  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
30  import fr.ifremer.quadrige3.core.security.AuthenticationInfo;
31  import fr.ifremer.quadrige3.core.service.http.HttpService;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.http.client.methods.HttpPost;
35  import org.apache.http.entity.ContentType;
36  import org.apache.http.entity.mime.content.StringBody;
37  import org.nuiton.i18n.I18n;
38  import org.springframework.context.annotation.Lazy;
39  import org.springframework.stereotype.Service;
40  
41  import javax.annotation.Resource;
42  import java.io.File;
43  
44  /**
45   * Rest client service for extraction
46   * <p>
47   * Created by Ludovic on 08/02/2016.
48   */
49  @Service("extractionRestClientService")
50  @Lazy
51  public class ExtractionRestClientServiceImpl implements ExtractionRestClientService {
52  
53      private final static Log log = LogFactory.getLog(ExtractionRestClientServiceImpl.class);
54  
55      private static final String URL_UPLOAD_EXTRACTION = "/service/extraction/upload";
56      private static final String PART_UPLOAD_EXTRACTION_TYPE = "type";
57  
58      @Resource(name = "httpService")
59      private HttpService httpService;
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void uploadExtractionFile(AuthenticationInfo authenticationInfo, File extractionFile, String extractionType,
66                                       ProgressionCoreModel progressionModel) throws QuadrigeTechnicalException {
67  
68          if (!extractionFile.exists()) {
69              throw new QuadrigeBusinessException(I18n.t("quadrige3.error.file.not.exists", extractionFile.getAbsolutePath()));
70          }
71  
72          if (log.isDebugEnabled()) {
73              log.debug(String.format("file to transfer to server: %s", extractionFile.getAbsolutePath()));
74          }
75  
76          try {
77  
78              HttpPost uploadHttpPost = new HttpPost(httpService.getPathUri(URL_UPLOAD_EXTRACTION));
79  
80              // execute request
81              httpService.executeUploadFileRequest(authenticationInfo, uploadHttpPost, progressionModel, extractionFile,
82                      ImmutableMap.of(PART_UPLOAD_EXTRACTION_TYPE, new StringBody(extractionType, ContentType.TEXT_PLAIN)));
83  
84          } catch (QuadrigeTechnicalException e) {
85              log.error(I18n.t("quadrige3.error.remote.upload.failed", e.getMessage()), e);
86              throw e;
87          }
88  
89      }
90  
91  }