View Javadoc
1   package fr.ifremer.quadrige2.core.service;
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 com.google.gson.Gson;
27  import fr.ifremer.quadrige2.core.dao.technical.gson.Gsons;
28  import fr.ifremer.quadrige2.core.dao.technical.http.HttpHelper;
29  import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
30  import fr.ifremer.quadrige2.core.config.Quadrige2Configuration;
31  import fr.ifremer.quadrige2.core.security.AuthenticationInfo;
32  import org.apache.http.auth.AuthScope;
33  import org.apache.http.auth.UsernamePasswordCredentials;
34  import org.apache.http.client.CredentialsProvider;
35  import org.apache.http.client.config.RequestConfig;
36  import org.apache.http.entity.mime.content.FileBody;
37  import org.apache.http.impl.client.BasicCredentialsProvider;
38  import org.apache.http.util.Args;
39  import org.nuiton.jaxx.application.type.ApplicationProgressionModel;
40  
41  import java.io.*;
42  import java.net.URI;
43  import java.net.URISyntaxException;
44  import java.net.URL;
45  
46  import static org.nuiton.i18n.I18n.t;
47  
48  /**
49   * Support for REST services
50   * <p/>
51   * Created by Ludovic on 08/02/2016.
52   */
53  public class RestServiceSupport {
54  
55  	protected final Quadrige2Configuration config;
56  
57  	private Gson gson = null;
58  
59  	private RequestConfig requestConfig = null;
60  
61  	/**
62  	 * <p>
63  	 * Constructor for RestServiceSupport.
64  	 * </p>
65  	 * 
66  	 * @param config
67  	 *            a {@link fr.ifremer.quadrige2.core.config.Quadrige2Configuration} object.
68  	 */
69  	public RestServiceSupport(Quadrige2Configuration config) {
70  		this.config = config;
71  	}
72  
73  	/**
74  	 * <p>
75  	 * Getter for the field <code>gson</code>.
76  	 * </p>
77  	 * 
78  	 * @return a {@link com.google.gson.Gson} object.
79  	 */
80  	protected Gson getGson() {
81  		if (gson == null) {
82  			gson = Gsons
83  					.newBuilder(Quadrige2Configuration.getInstance().getDbTimezone())
84  					.create();
85  		}
86  		return gson;
87  	}
88  
89  	/**
90  	 * <p>
91  	 * Getter for the field <code>requestConfig</code>.
92  	 * </p>
93  	 * 
94  	 * @return a {@link org.apache.http.client.config.RequestConfig} object.
95  	 */
96  	protected RequestConfig getRequestConfig() {
97  		if (requestConfig == null) {
98  			Integer timeOut = config.getSynchronizationSiteTimeout();
99  
100 			// build request config for timeout
101 			requestConfig = RequestConfig.custom().setSocketTimeout(timeOut).setConnectTimeout(timeOut).build();
102 		}
103 
104 		return requestConfig;
105 	}
106 
107 	/**
108 	 * <p>
109 	 * getCredentialsProvider.
110 	 * </p>
111 	 * 
112 	 * @param authenticationInfo
113 	 *            a {@link fr.ifremer.quadrige2.core.security.AuthenticationInfo} object.
114 	 * @return a {@link org.apache.http.client.CredentialsProvider} object.
115 	 */
116 	protected CredentialsProvider getCredentialsProvider(AuthenticationInfo authenticationInfo) {
117 		// For anonymous connection
118 		if (authenticationInfo == null) {
119 			return null;
120 		}
121 
122 		URL url = getRemoteUrl();
123 		// build credentials information
124 		CredentialsProvider result = new BasicCredentialsProvider();
125 		result.setCredentials(new AuthScope(url.getHost(), url.getPort()),
126 				new UsernamePasswordCredentials(authenticationInfo.getLogin(), authenticationInfo.getPassword()));
127 		return result;
128 	}
129 
130 	/**
131 	 * <p>
132 	 * getRemoteUrl.
133 	 * </p>
134 	 * 
135 	 * @return a {@link java.net.URL} object.
136 	 */
137 	protected URL getRemoteUrl() {
138 		return config.getSynchronizationSiteUrl();
139 	}
140 
141 	/**
142 	 * <p>
143 	 * getPathUri.
144 	 * </p>
145 	 * 
146 	 * @param subPath
147 	 *            a {@link java.lang.String} object.
148 	 * @return a {@link java.net.URI} object.
149 	 */
150 	protected URI getPathUri(String subPath) {
151 		try {
152 			return HttpHelper.getAppendedPath(config.getSynchronizationSiteUrl().toURI(), subPath);
153 		} catch (URISyntaxException e) {
154 			throw new Quadrige2TechnicalException(t("quadrige2.error.remote.synchronizationSiteUrl", e.getMessage()), e);
155 		}
156 	}
157 
158 	protected class ProgressFileBody extends FileBody {
159 
160 		private final ApplicationProgressionModel progressionModel;
161 
162 		public ProgressFileBody(File file, ApplicationProgressionModel progressionModel) {
163 			super(file);
164 			this.progressionModel = progressionModel;
165 		}
166 
167 		@Override
168 		public long getContentLength() {
169 			long size = super.getContentLength();
170 			progressionModel.setTotal((int) size);
171 			return size;
172 		}
173 
174 		@Override
175 		public void writeTo(OutputStream out) throws IOException {
176 			Args.notNull(out, "Output stream");
177 			try (InputStream in = new BufferedInputStream(new FileInputStream(getFile()))) {
178 				final byte[] tmp = new byte[4096];
179 				int l;
180 				long count = 0;
181 				while ((l = in.read(tmp)) != -1) {
182 					out.write(tmp, 0, l);
183 					count += l;
184 					progressionModel.setCurrent((int) count);
185 				}
186 				out.flush();
187 			}
188 		}
189 	}
190 }