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