View Javadoc
1   package fr.ifremer.quadrige2.core.service.extraction;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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 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   * Rest client service for extraction
54   * 
55   * Created by Ludovic on 08/02/2016.
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  	 * <p>
70  	 * Constructor for ExtractionRestClientServiceImpl.
71  	 * </p>
72  	 * 
73  	 * @param config
74  	 *            a {@link fr.ifremer.quadrige2.core.config.Quadrige2Configuration} object.
75  	 */
76  	@Autowired
77  	public ExtractionRestClientServiceImpl(Quadrige2Configuration config) {
78  		super(config);
79  	}
80  
81  	/** {@inheritDoc} */
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  		// get credentials provider
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 			// builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
105 			// add type
106 			builder.addPart(URL_EXPORT_UPLOAD_POST_PARAM_TYPE, new StringBody(extractionType, ContentType.TEXT_PLAIN));
107 			// add file
108 			builder.addPart(URL_EXPORT_UPLOAD_POST_PARAM_FILE, new ProgressFileBody(extractionFile, progressionModel));
109 			// add file name
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 			// execute request
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 }