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
		//Security.insertProviderAt(new BouncyCastleProvider(),1);
105
		//System.setProperty("https.protocols", httpsProtocols);
106
	}
107

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

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

    
116
				}
117

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

    
121
				}
122

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

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

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

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

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

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

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

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

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

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

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

    
190
}
(5-5/5)