svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.downloader / org.gvsig.downloader.lib / org.gvsig.downloader.lib.impl / src / main / java / org / gvsig / downloader / lib / impl / DownloaderManagerImpl.java @ 47843
History | View | Annotate | Download (9.65 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2023 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.downloader.lib.impl; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
import java.net.ConnectException; |
29 |
import java.net.URL; |
30 |
import java.net.UnknownHostException; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Collection; |
33 |
import java.util.HashMap; |
34 |
import java.util.Iterator; |
35 |
import java.util.List; |
36 |
import java.util.Map; |
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
import org.apache.http.entity.ContentType; |
39 |
import org.gvsig.compat.net.ICancellable; |
40 |
import org.gvsig.compat.se.net.downloader.se.SEDownloader; |
41 |
import org.gvsig.downloader.DownloaderAuthenticationConfig; |
42 |
import org.gvsig.downloader.DownloaderAuthenticationFactory; |
43 |
import org.gvsig.downloader.DownloaderCredentials; |
44 |
import org.gvsig.downloader.DownloaderManager; |
45 |
import static org.gvsig.downloader.DownloaderManager.urlStartsWith; |
46 |
import org.gvsig.json.Json; |
47 |
import org.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
|
51 |
|
52 |
@SuppressWarnings("UseSpecificCatch") |
53 |
public class DownloaderManagerImpl |
54 |
extends SEDownloader
|
55 |
implements DownloaderManager {
|
56 |
|
57 |
private static final Logger LOG = LoggerFactory.getLogger(DownloaderManagerImpl.class); |
58 |
|
59 |
private final List<DownloaderCredentials> credentials; |
60 |
private final Map<String,DownloaderAuthenticationFactory> authenticationTypes; |
61 |
private final List<DownloaderAuthenticationConfig> authenticationConfigurationServices; |
62 |
|
63 |
public DownloaderManagerImpl() {
|
64 |
super();
|
65 |
this.credentials = new ArrayList<>(); |
66 |
this.authenticationTypes = new HashMap<>(); |
67 |
this.authenticationConfigurationServices = new ArrayList<>(); |
68 |
} |
69 |
|
70 |
@Override
|
71 |
protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String data, File target, Object groupID, int maxbytes) { |
72 |
SEAuthDownloaderTask t = new SEAuthDownloaderTask(
|
73 |
downloader, |
74 |
url, |
75 |
data, |
76 |
target, |
77 |
groupID); |
78 |
t.setMaxbytes(maxbytes); |
79 |
return t;
|
80 |
} |
81 |
|
82 |
@Override
|
83 |
protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String method, ContentType contenttype, String data, File target, Object groupID, int maxbytes) { |
84 |
SEAuthDownloaderTask t = new SEAuthDownloaderTask(
|
85 |
downloader, |
86 |
url, |
87 |
method, |
88 |
contenttype, |
89 |
data, |
90 |
target, |
91 |
groupID); |
92 |
t.setMaxbytes(maxbytes); |
93 |
return t;
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void addOrReplaceCredentials(DownloaderCredentials credentials) { |
98 |
if( credentials == null ) { |
99 |
LOG.warn("Invalid credentials (null).");
|
100 |
return;
|
101 |
} |
102 |
|
103 |
for (int i = 0; i < this.credentials.size(); i++) { |
104 |
DownloaderCredentials x = this.credentials.get(i);
|
105 |
if(DownloaderManager.areSameURLs(credentials.getServiceUrl(), x.getServiceUrl())){
|
106 |
this.credentials.set(i, credentials);
|
107 |
return;
|
108 |
} |
109 |
} |
110 |
this.credentials.add(credentials);
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void removeCredentials(DownloaderCredentials credentials) { |
115 |
if( credentials == null ) { |
116 |
return;
|
117 |
} |
118 |
Iterator<DownloaderCredentials> it = this.credentials.iterator(); |
119 |
while (it.hasNext()) {
|
120 |
DownloaderCredentials next = it.next(); |
121 |
if(DownloaderManager.areSameURLs(credentials.getServiceUrl(), next.getServiceUrl())){
|
122 |
it.remove(); |
123 |
} |
124 |
} |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public void removeCredentials(String serviceUrl) { |
129 |
if( StringUtils.isBlank(serviceUrl) ) {
|
130 |
return;
|
131 |
} |
132 |
Iterator<DownloaderCredentials> it = this.credentials.iterator(); |
133 |
while (it.hasNext()) {
|
134 |
DownloaderCredentials next = it.next(); |
135 |
if(DownloaderManager.areSameURLs(serviceUrl, next.getServiceUrl())){
|
136 |
it.remove(); |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public DownloaderCredentials getCredentials(URL url) { |
143 |
return this.getCredentials(url.toExternalForm()); |
144 |
} |
145 |
|
146 |
@Override
|
147 |
public DownloaderCredentials getCredentials(String url) { |
148 |
for (DownloaderCredentials theCredentials : this.credentials) { |
149 |
if(urlStartsWith(url, theCredentials.getServiceUrl())){
|
150 |
return theCredentials;
|
151 |
} |
152 |
} |
153 |
DownloaderAuthenticationConfig config = this.getAuthenticationConfigurationService(url);
|
154 |
if(config == null){ |
155 |
return null; |
156 |
} |
157 |
for (DownloaderCredentials theCredentials : this.credentials) { |
158 |
DownloaderCredentials x = config.getCredentials(theCredentials); |
159 |
if(x != null) { |
160 |
this.addOrReplaceCredentials(x);
|
161 |
return x;
|
162 |
} |
163 |
} |
164 |
|
165 |
return null; |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public void registerAuthenticationConfigurationService(DownloaderAuthenticationConfig config ) { |
170 |
if( config == null ) { |
171 |
LOG.warn("Invalid config (null).");
|
172 |
return;
|
173 |
} |
174 |
|
175 |
for (int i = 0; i < this.authenticationConfigurationServices.size(); i++) { |
176 |
DownloaderAuthenticationConfig x = this.authenticationConfigurationServices.get(i);
|
177 |
if(DownloaderManager.areSameURLs(config.getServiceUrl(), x.getServiceUrl())){
|
178 |
this.authenticationConfigurationServices.set(i, config);
|
179 |
return;
|
180 |
} |
181 |
} |
182 |
this.authenticationConfigurationServices.add(config);
|
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void removeAuthenticationConfigurationService(DownloaderAuthenticationConfig config) { |
187 |
if( config == null ) { |
188 |
return;
|
189 |
} |
190 |
Iterator<DownloaderAuthenticationConfig> it = this.authenticationConfigurationServices.iterator(); |
191 |
while (it.hasNext()) {
|
192 |
DownloaderAuthenticationConfig next = it.next(); |
193 |
if(DownloaderManager.areSameURLs(config.getServiceUrl(), next.getServiceUrl())){
|
194 |
it.remove(); |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public void registerAuthenticationType(DownloaderAuthenticationFactory factory) { |
201 |
this.authenticationTypes.put(factory.getProviderName().toLowerCase(), factory);
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public DownloaderAuthenticationConfig getAuthenticationConfigurationService(String baseUrl) { |
206 |
for (DownloaderAuthenticationConfig theConfig : this.authenticationConfigurationServices) { |
207 |
if(urlStartsWith(baseUrl, theConfig.getServiceUrl())){
|
208 |
return theConfig;
|
209 |
} |
210 |
} |
211 |
return null; |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public Collection<DownloaderAuthenticationFactory> getAuthenticationTypes() { |
216 |
return this.authenticationTypes.values(); |
217 |
} |
218 |
|
219 |
@Override
|
220 |
public Collection<DownloaderAuthenticationConfig> getAuthenticationConfigurationServices() { |
221 |
return this.authenticationConfigurationServices; |
222 |
} |
223 |
|
224 |
@Override
|
225 |
public Collection<DownloaderCredentials> getCredentials() { |
226 |
return this.credentials; //.values(); |
227 |
} |
228 |
|
229 |
public DownloaderAuthenticationConfig requestAutenticationConfig(URL url) { // TODO: falta por implementar |
230 |
return null; |
231 |
} |
232 |
|
233 |
@Override
|
234 |
public DownloaderAuthenticationFactory getDownloaderAuthenticationFactory(String providerName) { |
235 |
return this.authenticationTypes.get(providerName.toLowerCase()); |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public void registerAuthenticationConfigurationService(String config) { |
240 |
if(StringUtils.isNotBlank(config)) {
|
241 |
DownloaderAuthenticationConfig downloaderConfig = (DownloaderAuthenticationConfig) Json.toObject(config); |
242 |
if(downloaderConfig != null){ |
243 |
this.registerAuthenticationConfigurationService(downloaderConfig);
|
244 |
} |
245 |
} |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public String getAuthenticationConfigurationServiceAsString(String baseUrl) { |
250 |
DownloaderAuthenticationConfig downloaderConfig = this.getAuthenticationConfigurationService(baseUrl);
|
251 |
if(downloaderConfig != null){ |
252 |
return downloaderConfig.toJson().toString();
|
253 |
} |
254 |
return null; |
255 |
} |
256 |
|
257 |
@Override
|
258 |
public synchronized File downloadFile(URL url, String method, ContentType contenttype, String data, String name, ICancellable cancel, int maxbytes) throws IOException, ConnectException, UnknownHostException { |
259 |
// File target = getPreviousDownloadedURL(url, data);
|
260 |
// if(target != null){
|
261 |
// return target;
|
262 |
// }
|
263 |
File target = new File(name); |
264 |
if (!target.isAbsolute()){
|
265 |
target = getUniqueTemporaryFile(name); |
266 |
} |
267 |
if(cancel == null) { |
268 |
cancel = ICancellable.DUMB; |
269 |
} |
270 |
Runnable task = this.createDownloaderTask(this, url, method, contenttype, data, target, cancel.getID(), maxbytes); |
271 |
task.run(); |
272 |
|
273 |
return target;
|
274 |
} |
275 |
|
276 |
|
277 |
} |