1 package fr.ifremer.quadrige3.core.service.extraction;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
46
47
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
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
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 }