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 @ 47828
History | View | Annotate | Download (5.89 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.Collection; |
29 |
import java.util.HashMap; |
30 |
import java.util.Map; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.gvsig.compat.se.net.downloader.se.SEDownloader; |
33 |
import org.gvsig.downloader.DownloaderAuthenticationConfig; |
34 |
import org.gvsig.downloader.DownloaderAuthenticationFactory; |
35 |
import org.gvsig.downloader.DownloaderCredentials; |
36 |
import org.gvsig.downloader.DownloaderManager; |
37 |
import org.slf4j.Logger; |
38 |
import org.slf4j.LoggerFactory; |
39 |
|
40 |
|
41 |
|
42 |
@SuppressWarnings("UseSpecificCatch") |
43 |
public class DownloaderManagerImpl |
44 |
extends SEDownloader
|
45 |
implements DownloaderManager {
|
46 |
|
47 |
private static final Logger LOG = LoggerFactory.getLogger(DownloaderManagerImpl.class); |
48 |
|
49 |
private final Map<String,DownloaderCredentials> credentials; |
50 |
private final Map<String,DownloaderAuthenticationFactory> authenticationTypes; |
51 |
private final Map<String,DownloaderAuthenticationConfig> authenticationConfigurationServices; |
52 |
|
53 |
public DownloaderManagerImpl() {
|
54 |
super();
|
55 |
this.credentials = new HashMap<>(); |
56 |
this.authenticationTypes = new HashMap<>(); |
57 |
this.authenticationConfigurationServices = new HashMap<>(); |
58 |
} |
59 |
|
60 |
@Override
|
61 |
protected Runnable createDownloaderTask(SEDownloader downloader, URL url, String data, File target, Object groupID, int maxbytes) { |
62 |
SEAuthDownloaderTask t = new SEAuthDownloaderTask(
|
63 |
downloader, |
64 |
url, |
65 |
data, |
66 |
target, |
67 |
groupID); |
68 |
t.setMaxbytes(maxbytes); |
69 |
return t;
|
70 |
} |
71 |
|
72 |
private String getUrlWithoutQuery(URL url) { |
73 |
try {
|
74 |
URL x = new URL(url.getProtocol(), url.getHost(), url.getPath()); |
75 |
return x.toExternalForm();
|
76 |
} catch(Exception ex) { |
77 |
return null; |
78 |
} |
79 |
} |
80 |
|
81 |
private String getUrlWithoutQuery(String url) { |
82 |
try {
|
83 |
return this.getUrlWithoutQuery(new URL(url)); |
84 |
} catch(Exception ex) { |
85 |
return null; |
86 |
} |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void addOrReplaceCredentials(DownloaderCredentials credentials) { |
91 |
if( credentials == null ) { |
92 |
LOG.warn("Invalid credentials (null).");
|
93 |
return;
|
94 |
} |
95 |
String url = this.getUrlWithoutQuery(credentials.getServiceUrl()); |
96 |
if( url == null ) { |
97 |
LOG.warn("Invalid URL '"+credentials.getServiceUrl()+"'"); |
98 |
return;
|
99 |
} |
100 |
this.credentials.put(url, credentials);
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public void removeCredentials(DownloaderCredentials credentials) { |
105 |
if( credentials == null ) { |
106 |
return;
|
107 |
} |
108 |
String x = credentials.getServiceUrl();
|
109 |
if( x == null ) { |
110 |
return;
|
111 |
} |
112 |
this.credentials.remove(x);
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public DownloaderCredentials getCredentials(URL url) { |
117 |
return this.getCredentials(url.toExternalForm()); |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public DownloaderCredentials getCredentials(String url) { |
122 |
for (DownloaderCredentials theCredentials : this.credentials.values()) { |
123 |
if( StringUtils.startsWith(url, theCredentials.getServiceUrl()) ) {
|
124 |
return theCredentials;
|
125 |
} |
126 |
} |
127 |
return null; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void registerAuthenticationConfigurationService(DownloaderAuthenticationConfig config ) { |
132 |
authenticationConfigurationServices.put(config.getKey(), config); |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public void removeAuthenticationConfigurationService(DownloaderAuthenticationConfig config) { |
137 |
authenticationConfigurationServices.remove(config.getKey()); |
138 |
} |
139 |
|
140 |
@Override
|
141 |
public void registerAuthenticationType(DownloaderAuthenticationFactory factory) { |
142 |
this.authenticationTypes.put(factory.getProviderName().toLowerCase(), factory);
|
143 |
} |
144 |
|
145 |
@Override
|
146 |
public DownloaderAuthenticationConfig getAuthenticationConfigurationService(String baseUrl) { |
147 |
for (DownloaderAuthenticationConfig config : this.authenticationConfigurationServices.values()) { |
148 |
if( StringUtils.startsWith(baseUrl, config.getServiceUrl()) ) {
|
149 |
return config;
|
150 |
} |
151 |
} |
152 |
return null; |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public Collection<DownloaderAuthenticationFactory> getAuthenticationTypes() { |
157 |
return this.authenticationTypes.values(); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public Collection<DownloaderAuthenticationConfig> getAuthenticationConfigurationServices() { |
162 |
return this.authenticationConfigurationServices.values(); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public Collection<DownloaderCredentials> getCredentials() { |
167 |
return this.credentials.values(); |
168 |
} |
169 |
|
170 |
public DownloaderAuthenticationConfig requestAutenticationConfig(URL url) { // TODO: falta por implementar |
171 |
return null; |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public DownloaderAuthenticationFactory getDownloaderAuthenticationFactory(String providerName) { |
176 |
return this.authenticationTypes.get(providerName); |
177 |
} |
178 |
|
179 |
} |