1 package fr.ifremer.quadrige2.core.service;
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.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
50
51
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
63
64
65
66
67
68
69 public RestServiceSupport(Quadrige2Configuration config) {
70 this.config = config;
71 }
72
73
74
75
76
77
78
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
91
92
93
94
95
96 protected RequestConfig getRequestConfig() {
97 if (requestConfig == null) {
98 Integer timeOut = config.getSynchronizationSiteTimeout();
99
100
101 requestConfig = RequestConfig.custom().setSocketTimeout(timeOut).setConnectTimeout(timeOut).build();
102 }
103
104 return requestConfig;
105 }
106
107
108
109
110
111
112
113
114
115
116 protected CredentialsProvider getCredentialsProvider(AuthenticationInfo authenticationInfo) {
117
118 if (authenticationInfo == null) {
119 return null;
120 }
121
122 URL url = getRemoteUrl();
123
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
132
133
134
135
136
137 protected URL getRemoteUrl() {
138 return config.getSynchronizationSiteUrl();
139 }
140
141
142
143
144
145
146
147
148
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 }