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 |
/**
|
---|---|
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.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
|
45 |
|
46 |
|
47 |
@SuppressWarnings("UseSpecificCatch") |
48 |
public class DownloaderManagerImpl |
49 |
extends SEDownloader
|
50 |
implements DownloaderManager {
|
51 |
|
52 |
private static final Logger LOG = LoggerFactory.getLogger(DownloaderManagerImpl.class); |
53 |
|
54 |
private final List<DownloaderCredentials> credentials; |
55 |
private final Map<String,DownloaderAuthenticationFactory> authenticationTypes; |
56 |
private final List<DownloaderAuthenticationConfig> authenticationConfigurationServices; |
57 |
|
58 |
public DownloaderManagerImpl() {
|
59 |
super();
|
60 |
this.credentials = new ArrayList<>(); |
61 |
this.authenticationTypes = new HashMap<>(); |
62 |
this.authenticationConfigurationServices = new ArrayList<>(); |
63 |
} |
64 |
|
65 |
@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 |
|
77 |
@Override
|
78 |
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 |
public void addOrReplaceCredentials(DownloaderCredentials credentials) { |
93 |
if( credentials == null ) { |
94 |
LOG.warn("Invalid credentials (null).");
|
95 |
return;
|
96 |
} |
97 |
|
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 |
} |
105 |
this.credentials.add(credentials);
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void removeCredentials(DownloaderCredentials credentials) { |
110 |
if( credentials == null ) { |
111 |
return;
|
112 |
} |
113 |
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 |
} |
120 |
} |
121 |
|
122 |
@Override
|
123 |
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 |
public DownloaderCredentials getCredentials(URL url) { |
138 |
return this.getCredentials(url.toExternalForm()); |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public DownloaderCredentials getCredentials(String url) { |
143 |
for (DownloaderCredentials theCredentials : this.credentials) { |
144 |
if(urlStartsWith(url, theCredentials.getServiceUrl())){
|
145 |
return theCredentials;
|
146 |
} |
147 |
} |
148 |
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 |
return null; |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public void registerAuthenticationConfigurationService(DownloaderAuthenticationConfig config ) { |
162 |
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 |
} |
176 |
|
177 |
@Override
|
178 |
public void removeAuthenticationConfigurationService(DownloaderAuthenticationConfig config) { |
179 |
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 |
} |
190 |
|
191 |
@Override
|
192 |
public void registerAuthenticationType(DownloaderAuthenticationFactory factory) { |
193 |
this.authenticationTypes.put(factory.getProviderName().toLowerCase(), factory);
|
194 |
} |
195 |
|
196 |
@Override
|
197 |
public DownloaderAuthenticationConfig getAuthenticationConfigurationService(String baseUrl) { |
198 |
for (DownloaderAuthenticationConfig theConfig : this.authenticationConfigurationServices) { |
199 |
if(urlStartsWith(baseUrl, theConfig.getServiceUrl())){
|
200 |
return theConfig;
|
201 |
} |
202 |
} |
203 |
return null; |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public Collection<DownloaderAuthenticationFactory> getAuthenticationTypes() { |
208 |
return this.authenticationTypes.values(); |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public Collection<DownloaderAuthenticationConfig> getAuthenticationConfigurationServices() { |
213 |
return this.authenticationConfigurationServices; |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public Collection<DownloaderCredentials> getCredentials() { |
218 |
return this.credentials; //.values(); |
219 |
} |
220 |
|
221 |
public DownloaderAuthenticationConfig requestAutenticationConfig(URL url) { // TODO: falta por implementar |
222 |
return null; |
223 |
} |
224 |
|
225 |
@Override
|
226 |
public DownloaderAuthenticationFactory getDownloaderAuthenticationFactory(String providerName) { |
227 |
return this.authenticationTypes.get(providerName); |
228 |
} |
229 |
|
230 |
} |