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 | 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 | 47841 | fdiaz | import org.gvsig.json.Json; |
43 | 47821 | jjdelcerro | import org.slf4j.Logger; |
44 | import org.slf4j.LoggerFactory; |
||
45 | 47092 | jjdelcerro | |
46 | |||
47 | 47821 | jjdelcerro | |
48 | 47092 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
49 | 47821 | jjdelcerro | public class DownloaderManagerImpl |
50 | extends SEDownloader
|
||
51 | implements DownloaderManager {
|
||
52 | 47092 | jjdelcerro | |
53 | 47821 | jjdelcerro | private static final Logger LOG = LoggerFactory.getLogger(DownloaderManagerImpl.class); |
54 | |||
55 | 47833 | fdiaz | private final List<DownloaderCredentials> credentials; |
56 | 47821 | jjdelcerro | private final Map<String,DownloaderAuthenticationFactory> authenticationTypes; |
57 | 47833 | fdiaz | private final List<DownloaderAuthenticationConfig> authenticationConfigurationServices; |
58 | 47821 | jjdelcerro | |
59 | public DownloaderManagerImpl() {
|
||
60 | super();
|
||
61 | 47833 | fdiaz | this.credentials = new ArrayList<>(); |
62 | 47821 | jjdelcerro | this.authenticationTypes = new HashMap<>(); |
63 | 47833 | fdiaz | this.authenticationConfigurationServices = new ArrayList<>(); |
64 | 47263 | jjdelcerro | } |
65 | 47092 | jjdelcerro | |
66 | 47824 | fdiaz | @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 | 47821 | jjdelcerro | |
78 | 47092 | jjdelcerro | @Override
|
79 | 47840 | jjdelcerro | 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 | 47821 | jjdelcerro | public void addOrReplaceCredentials(DownloaderCredentials credentials) { |
94 | 47828 | jjdelcerro | if( credentials == null ) { |
95 | LOG.warn("Invalid credentials (null).");
|
||
96 | return;
|
||
97 | } |
||
98 | 47833 | fdiaz | |
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 | 47828 | jjdelcerro | } |
106 | 47833 | fdiaz | this.credentials.add(credentials);
|
107 | 47092 | jjdelcerro | } |
108 | 47821 | jjdelcerro | |
109 | 47092 | jjdelcerro | @Override
|
110 | 47821 | jjdelcerro | public void removeCredentials(DownloaderCredentials credentials) { |
111 | 47828 | jjdelcerro | if( credentials == null ) { |
112 | return;
|
||
113 | } |
||
114 | 47833 | fdiaz | 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 | 47828 | jjdelcerro | } |
121 | 47092 | jjdelcerro | } |
122 | 47821 | jjdelcerro | |
123 | 47092 | jjdelcerro | @Override
|
124 | 47838 | fdiaz | 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 | 47828 | jjdelcerro | public DownloaderCredentials getCredentials(URL url) { |
139 | return this.getCredentials(url.toExternalForm()); |
||
140 | } |
||
141 | |||
142 | @Override
|
||
143 | public DownloaderCredentials getCredentials(String url) { |
||
144 | 47833 | fdiaz | for (DownloaderCredentials theCredentials : this.credentials) { |
145 | if(urlStartsWith(url, theCredentials.getServiceUrl())){
|
||
146 | 47828 | jjdelcerro | return theCredentials;
|
147 | } |
||
148 | } |
||
149 | 47833 | fdiaz | DownloaderAuthenticationConfig config = this.getAuthenticationConfigurationService(url);
|
150 | 47841 | fdiaz | if(config == null){ |
151 | return null; |
||
152 | } |
||
153 | 47833 | fdiaz | 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 | 47828 | jjdelcerro | return null; |
162 | } |
||
163 | 47833 | fdiaz | |
164 | 47828 | jjdelcerro | @Override
|
165 | 47821 | jjdelcerro | public void registerAuthenticationConfigurationService(DownloaderAuthenticationConfig config ) { |
166 | 47833 | fdiaz | 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 | 47092 | jjdelcerro | } |
180 | 47821 | jjdelcerro | |
181 | 47092 | jjdelcerro | @Override
|
182 | 47821 | jjdelcerro | public void removeAuthenticationConfigurationService(DownloaderAuthenticationConfig config) { |
183 | 47833 | fdiaz | 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 | 47092 | jjdelcerro | } |
194 | |||
195 | @Override
|
||
196 | 47821 | jjdelcerro | public void registerAuthenticationType(DownloaderAuthenticationFactory factory) { |
197 | this.authenticationTypes.put(factory.getProviderName().toLowerCase(), factory);
|
||
198 | 47092 | jjdelcerro | } |
199 | |||
200 | @Override
|
||
201 | 47821 | jjdelcerro | public DownloaderAuthenticationConfig getAuthenticationConfigurationService(String baseUrl) { |
202 | 47833 | fdiaz | for (DownloaderAuthenticationConfig theConfig : this.authenticationConfigurationServices) { |
203 | if(urlStartsWith(baseUrl, theConfig.getServiceUrl())){
|
||
204 | return theConfig;
|
||
205 | 47821 | jjdelcerro | } |
206 | } |
||
207 | return null; |
||
208 | 47486 | fdiaz | } |
209 | 47841 | fdiaz | |
210 | 47486 | fdiaz | @Override
|
211 | 47821 | jjdelcerro | public Collection<DownloaderAuthenticationFactory> getAuthenticationTypes() { |
212 | return this.authenticationTypes.values(); |
||
213 | 47486 | fdiaz | } |
214 | 47092 | jjdelcerro | |
215 | 47821 | jjdelcerro | @Override
|
216 | public Collection<DownloaderAuthenticationConfig> getAuthenticationConfigurationServices() { |
||
217 | 47833 | fdiaz | return this.authenticationConfigurationServices; |
218 | 47092 | jjdelcerro | } |
219 | 47821 | jjdelcerro | |
220 | 47347 | fdiaz | @Override
|
221 | 47821 | jjdelcerro | public Collection<DownloaderCredentials> getCredentials() { |
222 | 47833 | fdiaz | return this.credentials; //.values(); |
223 | 47347 | fdiaz | } |
224 | |||
225 | 47828 | jjdelcerro | public DownloaderAuthenticationConfig requestAutenticationConfig(URL url) { // TODO: falta por implementar |
226 | 47821 | jjdelcerro | return null; |
227 | 47347 | fdiaz | } |
228 | 47821 | jjdelcerro | |
229 | 47828 | jjdelcerro | @Override
|
230 | public DownloaderAuthenticationFactory getDownloaderAuthenticationFactory(String providerName) { |
||
231 | 47841 | fdiaz | return this.authenticationTypes.get(providerName.toLowerCase()); |
232 | 47347 | fdiaz | } |
233 | 47841 | fdiaz | |
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 | 47821 | jjdelcerro | |
243 | 47841 | fdiaz | 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 | 47092 | jjdelcerro | } |