Project

General

Profile

1
package eu.dnetlib.data.download;
2

    
3
import eu.dnetlib.data.download.rmi.DownloadItem;
4
import eu.dnetlib.data.download.rmi.DownloadService;
5
import eu.dnetlib.data.download.rmi.DownloadServiceException;
6
import eu.dnetlib.data.download.rmi.DownloadServiceFeeder;
7
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
8
import eu.dnetlib.enabling.tools.AbstractBaseService;
9
import eu.dnetlib.enabling.tools.blackboard.NotificationHandler;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.factory.annotation.Required;
13
import org.springframework.beans.factory.annotation.Value;
14

    
15
import javax.annotation.Resource;
16
import javax.net.ssl.*;
17
import javax.xml.ws.wsaddressing.W3CEndpointReference;
18
import java.security.KeyManagementException;
19
import java.security.NoSuchAlgorithmException;
20
import java.security.cert.CertificateException;
21
import java.util.ArrayList;
22
import java.util.List;
23

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

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

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

    
36
	/**
37
	 * The Constant log.
38
	 */
39
	private static final Log log = LogFactory.getLog(DownloadServiceImpl.class);
40

    
41
	/**
42
	 * The download plugin enumerator.
43
	 */
44
	@Resource
45
	DownloadPluginEnumeratorImpl downloadPluginEnumerator;
46

    
47
	@Resource
48
	private DownloadServiceFeeder downloadfeeder;
49

    
50
	/**
51
	 * The notification handler.
52
	 */
53
	private NotificationHandler notificationHandler;
54

    
55
	@Value("${services.download.http.sslcheck}")
56
	private boolean checkCerts;
57

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

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

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

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

    
96
				@Override
97
				public void run() {
98
					disableSslVerification();
99
				}
100
			}).start();
101
		}
102
	}
103

    
104
	private void disableSslVerification() {
105
		try {
106
			// Create a trust manager that does not validate certificate chains
107
			TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
108

    
109
				@Override
110
				public void checkClientTrusted(final java.security.cert.X509Certificate[] x509Certificates, final String s) throws CertificateException {
111

    
112
				}
113

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

    
117
				}
118

    
119
				@Override
120
				public java.security.cert.X509Certificate[] getAcceptedIssuers() {
121
					return null;
122
				}
123
			} };
124

    
125
			// Install the all-trusting trust manager
126
			SSLContext sc = SSLContext.getInstance("SSL");
127
			sc.init(null, trustAllCerts, new java.security.SecureRandom());
128
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
129

    
130
			// Create all-trusting host name verifier
131
			HostnameVerifier allHostsValid = new HostnameVerifier() {
132

    
133
				@Override
134
				public boolean verify(final String hostname, final SSLSession session) {
135
					return true;
136
				}
137
			};
138

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

    
149
	/**
150
	 * @return the downloadfeeder
151
	 */
152
	public DownloadServiceFeeder getDownloadfeeder() {
153
		return downloadfeeder;
154
	}
155

    
156
	/**
157
	 * @param downloadfeeder the downloadfeeder to set
158
	 */
159
	public void setDownloadfeeder(final DownloadServiceFeeder downloadfeeder) {
160
		this.downloadfeeder = downloadfeeder;
161
	}
162

    
163
	/**
164
	 * @return the notificationHandler
165
	 */
166
	public NotificationHandler getNotificationHandler() {
167
		return notificationHandler;
168
	}
169

    
170
	/**
171
	 * @param notificationHandler the notificationHandler to set
172
	 */
173
	@Required
174
	public void setNotificationHandler(final NotificationHandler notificationHandler) {
175
		this.notificationHandler = notificationHandler;
176
	}
177

    
178
	@Override
179
	public List<String> listPlugins() throws DownloadServiceException {
180

    
181
		List<String> result = new ArrayList<String>();
182
		result.addAll(downloadPluginEnumerator.getAll().keySet());
183
		return result;
184
	}
185

    
186
}
(5-5/5)