Statistics
| Revision:

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 @ 47845

History | View | Annotate | Download (11.8 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 java.util.Objects;
38
import org.apache.commons.lang3.StringUtils;
39
import org.apache.http.entity.ContentType;
40
import org.gvsig.compat.net.ICancellable;
41
import org.gvsig.compat.se.net.downloader.se.SEDownloader;
42
import org.gvsig.downloader.DownloaderAuthenticationConfig;
43
import org.gvsig.downloader.DownloaderAuthenticationFactory;
44
import org.gvsig.downloader.DownloaderCredentials;
45
import org.gvsig.downloader.DownloaderManager;
46
import static org.gvsig.downloader.DownloaderManager.urlStartsWith;
47
import org.gvsig.json.Json;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51

    
52

    
53
@SuppressWarnings("UseSpecificCatch")
54
public class DownloaderManagerImpl 
55
        extends SEDownloader
56
        implements DownloaderManager {
57

    
58
    private static final Logger LOG = LoggerFactory.getLogger(DownloaderManagerImpl.class);
59

    
60
    private final List<DownloaderCredentials> credentials;
61
    private final Map<String,DownloaderAuthenticationFactory> authenticationTypes;
62
    private final List<DownloaderAuthenticationConfig> authenticationConfigurationServices;
63
    
64
    public DownloaderManagerImpl() {
65
        super();
66
        this.credentials = new ArrayList<>();
67
        this.authenticationTypes = new HashMap<>();
68
        this.authenticationConfigurationServices = new ArrayList<>();
69
    }
70

    
71
    @Override
72
    protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String data, File target, Object groupID, int maxbytes) {
73
        SEAuthDownloaderTask t = new SEAuthDownloaderTask(
74
            downloader, 
75
            url, 
76
            data, 
77
            target, 
78
            groupID);
79
        t.setMaxbytes(maxbytes);
80
        return t;
81
    }
82

    
83
    @Override
84
    protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String method, ContentType contenttype, String data, File target, Object groupID, int maxbytes) {
85
        SEAuthDownloaderTask t = new SEAuthDownloaderTask(
86
            downloader, 
87
            url, 
88
            method,
89
            contenttype,
90
            data, 
91
            target, 
92
            groupID);
93
        t.setMaxbytes(maxbytes);
94
        return t;
95
    }
96

    
97
    @Override
98
    public void addOrReplaceCredentials(DownloaderCredentials credentials) {
99
        if( credentials == null ) {
100
            LOG.warn("Invalid credentials (null).");
101
            return;
102
        }
103

    
104
        for (int i = 0; i < this.credentials.size(); i++) {
105
            DownloaderCredentials x = this.credentials.get(i);
106
            if(DownloaderManager.areSameURLs(credentials.getServiceUrl(), x.getServiceUrl())){
107
                this.credentials.set(i, credentials);
108
                return;
109
            }
110
        }
111
        this.credentials.add(credentials);
112
    }
113

    
114
    @Override
115
    public void removeCredentials(DownloaderCredentials credentials) {
116
        if( credentials == null ) {
117
            return;
118
        }
119
        Iterator<DownloaderCredentials> it = this.credentials.iterator();
120
        while (it.hasNext()) {
121
            DownloaderCredentials next = it.next();
122
            if(DownloaderManager.areSameURLs(credentials.getServiceUrl(), next.getServiceUrl())){
123
                it.remove();
124
            }
125
        }
126
    }
127

    
128
    @Override
129
    public void removeCredentials(String serviceUrl) {
130
        if( StringUtils.isBlank(serviceUrl) ) {
131
            return;
132
        }
133
        Iterator<DownloaderCredentials> it = this.credentials.iterator();
134
        while (it.hasNext()) {
135
            DownloaderCredentials next = it.next();
136
            if(DownloaderManager.areSameURLs(serviceUrl, next.getServiceUrl())){
137
                it.remove();
138
            }
139
        }
140
    }
141

    
142
    @Override
143
    public DownloaderCredentials getCredentials(URL url) {
144
        return this.getCredentials(url.toExternalForm());
145
    }
146

    
147
    @Override
148
    public DownloaderCredentials getCredentials(String url) {
149
        if (StringUtils.isBlank(url)) {
150
            return null;
151
        }
152

    
153
        for (DownloaderCredentials theCredentials : this.credentials) {
154
            if(urlStartsWith(url, theCredentials.getServiceUrl())){
155
                return theCredentials;
156
            }
157
        }
158
        DownloaderAuthenticationConfig config = this.getAuthenticationConfigurationService(url);
159
        if(config == null){
160
            return null;
161
        }
162
        for (DownloaderCredentials theCredentials : this.credentials) {
163
            DownloaderCredentials x = theCredentials.createCredentials(config);
164
            if(x != null) {
165
                this.addOrReplaceCredentials(x);
166
                return x;
167
            }
168
        }
169
        
170
        return null;
171
    }
172
    
173
    @Override
174
    public void registerAuthenticationConfigurationService(DownloaderAuthenticationConfig config ) {
175
        if( config == null ) {
176
            LOG.warn("Invalid config (null).");
177
            return;
178
        }
179

    
180
        for (int i = 0; i < this.authenticationConfigurationServices.size(); i++) {
181
            DownloaderAuthenticationConfig x = this.authenticationConfigurationServices.get(i);
182
            if(DownloaderManager.areSameURLs(config.getServiceUrl(), x.getServiceUrl())){
183
                this.authenticationConfigurationServices.set(i, config);
184
                return;
185
            }
186
        }
187
        this.authenticationConfigurationServices.add(config);
188
    }
189

    
190
    @Override
191
    public void removeAuthenticationConfigurationService(DownloaderAuthenticationConfig config) {
192
        if( config == null ) {
193
            return;
194
        }
195
        Iterator<DownloaderAuthenticationConfig> it = this.authenticationConfigurationServices.iterator();
196
        while (it.hasNext()) {
197
            DownloaderAuthenticationConfig next = it.next();
198
            if(DownloaderManager.areSameURLs(config.getServiceUrl(), next.getServiceUrl())){
199
                it.remove();
200
            }
201
        }
202
    }
203
    
204
    @Override
205
    public void registerAuthenticationType(DownloaderAuthenticationFactory factory) {
206
        this.authenticationTypes.put(factory.getProviderName().toLowerCase(), factory);
207
    }
208
    
209
    @Override
210
    public DownloaderAuthenticationConfig getAuthenticationConfigurationService(String baseUrl) {
211
        if (StringUtils.isBlank(baseUrl)) {
212
            return null;
213
        }
214

    
215
        for (DownloaderAuthenticationConfig theConfig : this.authenticationConfigurationServices) {
216
            if (urlStartsWith(baseUrl, theConfig.getServiceUrl())) {
217
                return theConfig;
218
            }
219
        }
220
        return null;
221
    }
222
        
223
    @Override
224
    public Collection<DownloaderAuthenticationFactory> getAuthenticationTypes() {
225
        return this.authenticationTypes.values();
226
    }
227

    
228
    @Override
229
    public Collection<DownloaderAuthenticationConfig> getAuthenticationConfigurationServices() {
230
        return this.authenticationConfigurationServices;
231
    }
232
    
233
    @Override
234
    public Collection<DownloaderCredentials> getCredentials() {
235
        return this.credentials; //.values();
236
    }
237

    
238
    public DownloaderAuthenticationConfig requestAutenticationConfig(URL url) { // TODO: falta por implementar
239
        return null;
240
    }
241

    
242
    @Override
243
    public DownloaderAuthenticationFactory getDownloaderAuthenticationFactory(String providerName) {
244
        return this.authenticationTypes.get(providerName.toLowerCase());
245
    }
246
    
247
    @Override
248
    public void registerAuthenticationConfigurationService(String config) {
249
        if(StringUtils.isNotBlank(config)) {
250
            DownloaderAuthenticationConfig downloaderConfig = (DownloaderAuthenticationConfig) Json.toObject(config);
251
            if(downloaderConfig != null){
252
                this.registerAuthenticationConfigurationService(downloaderConfig);
253
            }
254
        }
255
    }
256

    
257
    @Override
258
    public String getAuthenticationConfigurationServiceAsString(String baseUrl) {
259
        DownloaderAuthenticationConfig downloaderConfig = this.getAuthenticationConfigurationService(baseUrl);
260
        if(downloaderConfig != null){
261
            return downloaderConfig.toJson().toString();
262
        }
263
        return null;
264
    }
265

    
266
    @Override
267
    public synchronized File downloadFile(URL url, String method, ContentType contenttype, String data, String name, ICancellable cancel, int maxbytes) throws IOException, ConnectException, UnknownHostException {
268
        return downloadFile(url, method, contenttype, data, name, cancel, maxbytes, true);
269
    }
270
    
271
    @Override
272
    public synchronized File downloadFile(URL url, String method, ContentType contenttype, String data, String name, ICancellable cancel, int maxbytes, boolean useCache) throws IOException, ConnectException, UnknownHostException {
273
        LOG.info("Download file " + Objects.toString(url));
274
        if (useCache) {
275
            File target = getPreviousDownloadedURL(url, data);
276
            if (target != null) {
277
                LOG.info("CACHED "+url.toString() + " at '" + target.getAbsolutePath() + "'");
278
                return target;
279
            }
280
        }
281
        File target = new File(name);
282
        if (!target.isAbsolute()){
283
            target = getUniqueTemporaryFile(name);
284
        }
285
        if(cancel == null) {
286
            cancel = ICancellable.DUMB;
287
        }
288
        Runnable task = this.createDownloaderTask(this, url, method, contenttype, data, target, cancel.getID(), maxbytes);
289
        task.run();
290
        return target;
291
    }
292
    
293
    public void propagateCredentials(String sourceServiceUrl, Collection<String> targetServicesUrls) {
294
        if(StringUtils.isBlank(sourceServiceUrl)){
295
            return;
296
        }
297
        DownloaderAuthenticationConfig sourceConfig = this.getAuthenticationConfigurationService(sourceServiceUrl);
298
        if(sourceConfig == null){
299
            return;
300
        }
301
        DownloaderCredentials sourceCredentials = this.getCredentials(sourceServiceUrl);
302
        for (String targetServiceUrl : targetServicesUrls) {
303
            if(StringUtils.isBlank(targetServiceUrl)){
304
                continue;
305
            }
306
            DownloaderAuthenticationConfig targetConfig = this.getAuthenticationConfigurationService(targetServiceUrl);
307
            if(targetConfig == null) {
308
                targetConfig = sourceConfig.createAuthenticationConfig(targetServiceUrl);
309
                if(targetConfig != null) {
310
                    this.registerAuthenticationConfigurationService(targetConfig);
311
                }
312
            }
313
            if(sourceCredentials != null) {
314
                DownloaderCredentials targetCredentials = this.getCredentials(targetServiceUrl);
315
                if(targetCredentials == null){
316
                    targetCredentials = sourceCredentials.createCredentials(targetServiceUrl);
317
                    if(targetCredentials != null) {
318
                        this.addOrReplaceCredentials(targetCredentials);
319
                    }
320
                }
321
            }
322
        }
323
 
324
        
325
    }
326

    
327
    
328
}