1 package fr.ifremer.quadrige2.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 fr.ifremer.quadrige2.core.exception.Quadrige2BusinessException;
27 import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
28 import fr.ifremer.quadrige2.core.config.Quadrige2Configuration;
29 import fr.ifremer.quadrige2.core.dao.technical.http.HttpHelper;
30 import fr.ifremer.quadrige2.core.security.AuthenticationInfo;
31 import fr.ifremer.quadrige2.core.service.RestServiceSupport;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.http.client.CredentialsProvider;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.entity.ContentType;
37 import org.apache.http.entity.mime.MultipartEntityBuilder;
38 import org.apache.http.entity.mime.content.StringBody;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.HttpClients;
41 import org.apache.http.protocol.HTTP;
42 import org.nuiton.i18n.I18n;
43 import org.nuiton.jaxx.application.type.ApplicationProgressionModel;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.context.annotation.Lazy;
46 import org.springframework.stereotype.Service;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.nio.charset.Charset;
51
52
53
54
55
56
57 @Service("extractionRestClientService")
58 @Lazy
59 public class ExtractionRestClientServiceImpl extends RestServiceSupport implements ExtractionRestClientService {
60
61 private final static Log log = LogFactory.getLog(ExtractionRestClientServiceImpl.class);
62
63 private static final String URL_EXPORT_UPLOAD = "/service/extraction/upload";
64 private static final String URL_EXPORT_UPLOAD_POST_PARAM_FILE = "file";
65 private static final String URL_EXPORT_UPLOAD_POST_PARAM_FILENAME = "filename";
66 private static final String URL_EXPORT_UPLOAD_POST_PARAM_TYPE = "type";
67
68
69
70
71
72
73
74
75
76 @Autowired
77 public ExtractionRestClientServiceImpl(Quadrige2Configuration config) {
78 super(config);
79 }
80
81
82 @Override
83 public void uploadExtractionFile(AuthenticationInfo authenticationInfo, File extractionFile, String extractionType,
84 ApplicationProgressionModel progressionModel) throws Quadrige2TechnicalException {
85
86 if (!extractionFile.exists()) {
87 throw new Quadrige2BusinessException(I18n.t("quadrige2.error.file.not.exists", extractionFile.getAbsolutePath()));
88 }
89
90 if (log.isDebugEnabled()) {
91 log.debug(String.format("file to transfer to server: %s", extractionFile.getAbsolutePath()));
92 }
93
94
95 CredentialsProvider credsProvider = getCredentialsProvider(authenticationInfo);
96
97 try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(getRequestConfig())
98 .setDefaultCredentialsProvider(credsProvider)
99 .build()) {
100
101 HttpPost uploadHttpPost = new HttpPost(getPathUri(URL_EXPORT_UPLOAD));
102
103 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
104
105
106 builder.addPart(URL_EXPORT_UPLOAD_POST_PARAM_TYPE, new StringBody(extractionType, ContentType.TEXT_PLAIN));
107
108 builder.addPart(URL_EXPORT_UPLOAD_POST_PARAM_FILE, new ProgressFileBody(extractionFile, progressionModel));
109
110 builder.addPart(URL_EXPORT_UPLOAD_POST_PARAM_FILENAME,
111 new StringBody(extractionFile.getName(), ContentType.create("text/plain", Charset.defaultCharset())));
112
113 uploadHttpPost.setEntity(builder.build());
114 uploadHttpPost.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
115
116
117 HttpHelper.executeRequest(httpClient, uploadHttpPost);
118
119 } catch (Quadrige2TechnicalException e) {
120 log.error(I18n.t("quadrige2.error.remote.upload.failed", e.getMessage()), e);
121 throw e;
122 } catch (IOException e) {
123 log.error(I18n.t("quadrige2.error.remote.upload.failed", e.getMessage()), e);
124 throw new Quadrige2TechnicalException(e);
125 }
126
127 }
128
129 }