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

History | View | Annotate | Download (7.92 KB)

1 47092 jjdelcerro
/**
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 47821 jjdelcerro
package org.gvsig.downloader.lib.impl;
25 47092 jjdelcerro
26 47824 fdiaz
import java.io.File;
27 47821 jjdelcerro
import java.net.URL;
28 47833 fdiaz
import java.util.ArrayList;
29 47821 jjdelcerro
import java.util.Collection;
30
import java.util.HashMap;
31 47833 fdiaz
import java.util.Iterator;
32
import java.util.List;
33 47821 jjdelcerro
import java.util.Map;
34 47283 jjdelcerro
import org.apache.commons.lang3.StringUtils;
35 47840 jjdelcerro
import org.apache.http.entity.ContentType;
36 47821 jjdelcerro
import org.gvsig.compat.se.net.downloader.se.SEDownloader;
37
import org.gvsig.downloader.DownloaderAuthenticationConfig;
38
import org.gvsig.downloader.DownloaderAuthenticationFactory;
39
import org.gvsig.downloader.DownloaderCredentials;
40
import org.gvsig.downloader.DownloaderManager;
41 47833 fdiaz
import static org.gvsig.downloader.DownloaderManager.urlStartsWith;
42 47821 jjdelcerro
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44 47092 jjdelcerro
45
46 47821 jjdelcerro
47 47092 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
48 47821 jjdelcerro
public class DownloaderManagerImpl
49
        extends SEDownloader
50
        implements DownloaderManager {
51 47092 jjdelcerro
52 47821 jjdelcerro
    private static final Logger LOG = LoggerFactory.getLogger(DownloaderManagerImpl.class);
53
54 47833 fdiaz
    private final List<DownloaderCredentials> credentials;
55 47821 jjdelcerro
    private final Map<String,DownloaderAuthenticationFactory> authenticationTypes;
56 47833 fdiaz
    private final List<DownloaderAuthenticationConfig> authenticationConfigurationServices;
57 47821 jjdelcerro
58
    public DownloaderManagerImpl() {
59
        super();
60 47833 fdiaz
        this.credentials = new ArrayList<>();
61 47821 jjdelcerro
        this.authenticationTypes = new HashMap<>();
62 47833 fdiaz
        this.authenticationConfigurationServices = new ArrayList<>();
63 47263 jjdelcerro
    }
64 47092 jjdelcerro
65 47824 fdiaz
    @Override
66
    protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String data, File target, Object groupID, int maxbytes) {
67
        SEAuthDownloaderTask t = new SEAuthDownloaderTask(
68
            downloader,
69
            url,
70
            data,
71
            target,
72
            groupID);
73
        t.setMaxbytes(maxbytes);
74
        return t;
75
    }
76 47821 jjdelcerro
77 47092 jjdelcerro
    @Override
78 47840 jjdelcerro
    protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String method, ContentType contenttype, String data, File target, Object groupID, int maxbytes) {
79
        SEAuthDownloaderTask t = new SEAuthDownloaderTask(
80
            downloader,
81
            url,
82
            method,
83
            contenttype,
84
            data,
85
            target,
86
            groupID);
87
        t.setMaxbytes(maxbytes);
88
        return t;
89
    }
90
91
    @Override
92 47821 jjdelcerro
    public void addOrReplaceCredentials(DownloaderCredentials credentials) {
93 47828 jjdelcerro
        if( credentials == null ) {
94
            LOG.warn("Invalid credentials (null).");
95
            return;
96
        }
97 47833 fdiaz
98
        for (int i = 0; i < this.credentials.size(); i++) {
99
            DownloaderCredentials x = this.credentials.get(i);
100
            if(DownloaderManager.areSameURLs(credentials.getServiceUrl(), x.getServiceUrl())){
101
                this.credentials.set(i, credentials);
102
                return;
103
            }
104 47828 jjdelcerro
        }
105 47833 fdiaz
        this.credentials.add(credentials);
106 47092 jjdelcerro
    }
107 47821 jjdelcerro
108 47092 jjdelcerro
    @Override
109 47821 jjdelcerro
    public void removeCredentials(DownloaderCredentials credentials) {
110 47828 jjdelcerro
        if( credentials == null ) {
111
            return;
112
        }
113 47833 fdiaz
        Iterator<DownloaderCredentials> it = this.credentials.iterator();
114
        while (it.hasNext()) {
115
            DownloaderCredentials next = it.next();
116
            if(DownloaderManager.areSameURLs(credentials.getServiceUrl(), next.getServiceUrl())){
117
                it.remove();
118
            }
119 47828 jjdelcerro
        }
120 47092 jjdelcerro
    }
121 47821 jjdelcerro
122 47092 jjdelcerro
    @Override
123 47838 fdiaz
    public void removeCredentials(String serviceUrl) {
124
        if( StringUtils.isBlank(serviceUrl) ) {
125
            return;
126
        }
127
        Iterator<DownloaderCredentials> it = this.credentials.iterator();
128
        while (it.hasNext()) {
129
            DownloaderCredentials next = it.next();
130
            if(DownloaderManager.areSameURLs(serviceUrl, next.getServiceUrl())){
131
                it.remove();
132
            }
133
        }
134
    }
135
136
    @Override
137 47828 jjdelcerro
    public DownloaderCredentials getCredentials(URL url) {
138
        return this.getCredentials(url.toExternalForm());
139
    }
140
141
    @Override
142
    public DownloaderCredentials getCredentials(String url) {
143 47833 fdiaz
        for (DownloaderCredentials theCredentials : this.credentials) {
144
            if(urlStartsWith(url, theCredentials.getServiceUrl())){
145 47828 jjdelcerro
                return theCredentials;
146
            }
147
        }
148 47833 fdiaz
        DownloaderAuthenticationConfig config = this.getAuthenticationConfigurationService(url);
149
        for (DownloaderCredentials theCredentials : this.credentials) {
150
            DownloaderCredentials x = config.getCredentials(theCredentials);
151
            if(x != null) {
152
                this.addOrReplaceCredentials(x);
153
                return x;
154
            }
155
        }
156
157 47828 jjdelcerro
        return null;
158
    }
159 47833 fdiaz
160 47828 jjdelcerro
    @Override
161 47821 jjdelcerro
    public void registerAuthenticationConfigurationService(DownloaderAuthenticationConfig config ) {
162 47833 fdiaz
        if( config == null ) {
163
            LOG.warn("Invalid config (null).");
164
            return;
165
        }
166
167
        for (int i = 0; i < this.authenticationConfigurationServices.size(); i++) {
168
            DownloaderAuthenticationConfig x = this.authenticationConfigurationServices.get(i);
169
            if(DownloaderManager.areSameURLs(config.getServiceUrl(), x.getServiceUrl())){
170
                this.authenticationConfigurationServices.set(i, config);
171
                return;
172
            }
173
        }
174
        this.authenticationConfigurationServices.add(config);
175 47092 jjdelcerro
    }
176 47821 jjdelcerro
177 47092 jjdelcerro
    @Override
178 47821 jjdelcerro
    public void removeAuthenticationConfigurationService(DownloaderAuthenticationConfig config) {
179 47833 fdiaz
        if( config == null ) {
180
            return;
181
        }
182
        Iterator<DownloaderAuthenticationConfig> it = this.authenticationConfigurationServices.iterator();
183
        while (it.hasNext()) {
184
            DownloaderAuthenticationConfig next = it.next();
185
            if(DownloaderManager.areSameURLs(config.getServiceUrl(), next.getServiceUrl())){
186
                it.remove();
187
            }
188
        }
189 47092 jjdelcerro
    }
190
191
    @Override
192 47821 jjdelcerro
    public void registerAuthenticationType(DownloaderAuthenticationFactory factory) {
193
        this.authenticationTypes.put(factory.getProviderName().toLowerCase(), factory);
194 47092 jjdelcerro
    }
195
196
    @Override
197 47821 jjdelcerro
    public DownloaderAuthenticationConfig getAuthenticationConfigurationService(String baseUrl) {
198 47833 fdiaz
        for (DownloaderAuthenticationConfig theConfig : this.authenticationConfigurationServices) {
199
            if(urlStartsWith(baseUrl, theConfig.getServiceUrl())){
200
                return theConfig;
201 47821 jjdelcerro
            }
202
        }
203
        return null;
204 47486 fdiaz
    }
205 47821 jjdelcerro
206 47486 fdiaz
    @Override
207 47821 jjdelcerro
    public Collection<DownloaderAuthenticationFactory> getAuthenticationTypes() {
208
        return this.authenticationTypes.values();
209 47486 fdiaz
    }
210 47092 jjdelcerro
211 47821 jjdelcerro
    @Override
212
    public Collection<DownloaderAuthenticationConfig> getAuthenticationConfigurationServices() {
213 47833 fdiaz
        return this.authenticationConfigurationServices;
214 47092 jjdelcerro
    }
215 47821 jjdelcerro
216 47347 fdiaz
    @Override
217 47821 jjdelcerro
    public Collection<DownloaderCredentials> getCredentials() {
218 47833 fdiaz
        return this.credentials; //.values();
219 47347 fdiaz
    }
220
221 47828 jjdelcerro
    public DownloaderAuthenticationConfig requestAutenticationConfig(URL url) { // TODO: falta por implementar
222 47821 jjdelcerro
        return null;
223 47347 fdiaz
    }
224 47821 jjdelcerro
225 47828 jjdelcerro
    @Override
226
    public DownloaderAuthenticationFactory getDownloaderAuthenticationFactory(String providerName) {
227
        return this.authenticationTypes.get(providerName);
228 47347 fdiaz
    }
229 47821 jjdelcerro
230 47092 jjdelcerro
}