Project

General

Profile

1
package eu.dnetlib.data.download;
2

    
3
import java.security.KeyManagementException;
4
import java.security.NoSuchAlgorithmException;
5
import java.security.Security;
6
import java.security.cert.CertificateException;
7
import java.util.ArrayList;
8
import java.util.List;
9
import javax.annotation.Resource;
10
import javax.net.ssl.*;
11
import javax.xml.ws.wsaddressing.W3CEndpointReference;
12

    
13
import eu.dnetlib.data.download.rmi.DownloadItem;
14
import eu.dnetlib.data.download.rmi.DownloadService;
15
import eu.dnetlib.data.download.rmi.DownloadServiceException;
16
import eu.dnetlib.data.download.rmi.DownloadServiceFeeder;
17
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
18
import eu.dnetlib.enabling.tools.AbstractBaseService;
19
import eu.dnetlib.enabling.tools.blackboard.NotificationHandler;
20
import org.apache.commons.logging.Log;
21
import org.apache.commons.logging.LogFactory;
22
import org.springframework.beans.factory.annotation.Required;
23
import org.springframework.beans.factory.annotation.Value;
24

    
25
/**
26
 * The Class DownloadServiceImpl.
27
 */
28
public class DownloadServiceImpl extends AbstractBaseService implements DownloadService {
29

    
30
	/**
31
	 * The Constant END_QUEUE.
32
	 */
33
	public static final DownloadItem END_QUEUE = new DownloadItem();
34

    
35
	public static final String END_QUEUE_STRING = "END_DOWNLOAD";
36

    
37
	/**
38
	 * The Constant log.
39
	 */
40
	private static final Log log = LogFactory.getLog(DownloadServiceImpl.class);
41
	private static int DEFAULT_CONNECT_TIMEOUT_MS = 5000;
42
	private static int DEFAULT_READ_TIMEOUT_MS = 5000;
43
	/**
44
	 * The download plugin enumerator.
45
	 */
46
	@Resource
47
	private DownloadPluginEnumeratorImpl downloadPluginEnumerator;
48
	@Resource
49
	private DownloadServiceFeeder downloadfeeder;
50
	/**
51
	 * The notification handler.
52
	 */
53
	private NotificationHandler notificationHandler;
54
	@Value("${services.download.http.sslcheck}")
55
	private boolean checkCerts;
56
	@Value("${services.download.https.protocols}")
57
	private String httpsProtocols;
58

    
59
	/*
60
	 * (non-Javadoc)
61
	 *
62
	 * @see eu.dnetlib.data.download.rmi.DownloadService#downloadFromResultSet(javax.xml.ws.wsaddressing.W3CEndpointReference,
63
	 * java.lang.String, java.lang.String, java.lang.String, java.lang.String)
64
	 */
65
	@Override
66
	public void downloadFromResultSet(final W3CEndpointReference resultSet,
67
			final String plugin,
68
			final String objectStoreID,
69
			final String protocol,
70
			final String mimeType) throws DownloadServiceException {
71

    
72
		log.info(String.format("download using plugin '%s' , protocol '%s' into ObjectStore '%s'", plugin, protocol, objectStoreID));
73
		try {
74
			downloadfeeder.download(resultSet.toString(), plugin, objectStoreID, protocol, mimeType, 5, null, null, DEFAULT_CONNECT_TIMEOUT_MS,
75
					DEFAULT_READ_TIMEOUT_MS, 0);
76
		} catch (ObjectStoreServiceException e) {
77
			log.error(e);
78
		}
79
		log.info(String.format("download completed using plugin '%s' , protocol '%s' into ObjectStore '%s'", plugin, protocol, objectStoreID));
80
	}
81

    
82
	/**
83
	 * {@inheritDoc}
84
	 *
85
	 * @see eu.dnetlib.enabling.tools.AbstractBaseService#notify(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
86
	 */
87
	@Override
88
	public void notify(final String subscriptionId, final String topic, final String isId, final String message) {
89
		getNotificationHandler().notified(subscriptionId, topic, isId, message);
90
	}
91

    
92
	@Override
93
	public void start() {
94
		if (!checkCerts) {
95
			log.info("disabling SSL check ...");
96
			new Thread(new Runnable() {
97

    
98
				@Override
99
				public void run() {
100
					disableSslVerification();
101
				}
102
			}).start();
103
		}
104
		//TODO: we should remove references to BouncyCastle. We are not expecting issues like #2520 with Java8
105
		//Security.insertProviderAt(new BouncyCastleProvider(),1);
106
		//System.setProperty("https.protocols", httpsProtocols);
107
	}
108

    
109
	private void disableSslVerification() {
110
		try {
111
			// Create a trust manager that does not validate certificate chains
112
			TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
113

    
114
				@Override
115
				public void checkClientTrusted(final java.security.cert.X509Certificate[] x509Certificates, final String s) throws CertificateException {
116

    
117
				}
118

    
119
				@Override
120
				public void checkServerTrusted(final java.security.cert.X509Certificate[] x509Certificates, final String s) throws CertificateException {
121

    
122
				}
123

    
124
				@Override
125
				public java.security.cert.X509Certificate[] getAcceptedIssuers() {
126
					return null;
127
				}
128
			} };
129

    
130
			// Install the all-trusting trust manager
131
			SSLContext sc = SSLContext.getInstance("SSL");
132
			sc.init(null, trustAllCerts, new java.security.SecureRandom());
133
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
134

    
135
			// Create all-trusting host name verifier
136
			HostnameVerifier allHostsValid = new HostnameVerifier() {
137

    
138
				@Override
139
				public boolean verify(final String hostname, final SSLSession session) {
140
					return true;
141
				}
142
			};
143

    
144
			// Install the all-trusting host verifier
145
			HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
146
			log.info("disabled SSL check");
147
		} catch (NoSuchAlgorithmException e) {
148
			log.error(e.getMessage(), e);
149
		} catch (KeyManagementException e) {
150
			log.error(e.getMessage(), e);
151
		}
152
	}
153

    
154
	/**
155
	 * @return the downloadfeeder
156
	 */
157
	public DownloadServiceFeeder getDownloadfeeder() {
158
		return downloadfeeder;
159
	}
160

    
161
	/**
162
	 * @param downloadfeeder the downloadfeeder to set
163
	 */
164
	public void setDownloadfeeder(final DownloadServiceFeeder downloadfeeder) {
165
		this.downloadfeeder = downloadfeeder;
166
	}
167

    
168
	/**
169
	 * @return the notificationHandler
170
	 */
171
	public NotificationHandler getNotificationHandler() {
172
		return notificationHandler;
173
	}
174

    
175
	/**
176
	 * @param notificationHandler the notificationHandler to set
177
	 */
178
	@Required
179
	public void setNotificationHandler(final NotificationHandler notificationHandler) {
180
		this.notificationHandler = notificationHandler;
181
	}
182

    
183
	@Override
184
	public List<String> listPlugins() throws DownloadServiceException {
185

    
186
		List<String> result = new ArrayList<String>();
187
		result.addAll(downloadPluginEnumerator.getAll().keySet());
188
		return result;
189
	}
190

    
191
}
(5-5/5)