1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.http; |
12 | |
|
13 | |
import org.mule.api.MessagingException; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.construct.FlowConstruct; |
16 | |
import org.mule.api.endpoint.InboundEndpoint; |
17 | |
import org.mule.api.lifecycle.CreateException; |
18 | |
import org.mule.api.transport.Connector; |
19 | |
import org.mule.transport.http.i18n.HttpMessages; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.net.Socket; |
23 | |
import java.security.cert.Certificate; |
24 | |
|
25 | |
import javax.net.ssl.HandshakeCompletedEvent; |
26 | |
import javax.net.ssl.HandshakeCompletedListener; |
27 | |
import javax.net.ssl.SSLPeerUnverifiedException; |
28 | |
import javax.net.ssl.SSLSocket; |
29 | |
import javax.resource.spi.work.Work; |
30 | |
|
31 | |
import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch; |
32 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
33 | |
|
34 | |
public class HttpsMessageReceiver extends HttpMessageReceiver |
35 | |
{ |
36 | |
|
37 | |
public HttpsMessageReceiver(Connector connector, FlowConstruct flow, InboundEndpoint endpoint) |
38 | |
throws CreateException |
39 | |
{ |
40 | 0 | super(connector, flow, endpoint); |
41 | 0 | } |
42 | |
|
43 | |
@Override |
44 | |
protected Work createWork(Socket socket) throws IOException |
45 | |
{ |
46 | 0 | return new HttpsWorker(socket); |
47 | |
} |
48 | |
|
49 | |
private class HttpsWorker extends HttpWorker implements HandshakeCompletedListener |
50 | |
{ |
51 | |
private Certificate[] peerCertificateChain; |
52 | |
private Certificate[] localCertificateChain; |
53 | 0 | private final CountDownLatch latch = new CountDownLatch(1); |
54 | |
|
55 | |
public HttpsWorker(Socket socket) throws IOException |
56 | 0 | { |
57 | 0 | super(socket); |
58 | 0 | ((SSLSocket) socket).addHandshakeCompletedListener(this); |
59 | 0 | } |
60 | |
|
61 | |
@Override |
62 | |
protected void preRouteMessage(MuleMessage message) throws MessagingException |
63 | |
{ |
64 | |
try |
65 | |
{ |
66 | 0 | long timeout = ((HttpsConnector) getConnector()).getSslHandshakeTimeout(); |
67 | 0 | boolean handshakeComplete = latch.await(timeout, TimeUnit.MILLISECONDS); |
68 | 0 | if (!handshakeComplete) |
69 | |
{ |
70 | 0 | throw new MessagingException(HttpMessages.sslHandshakeDidNotComplete(), message); |
71 | |
} |
72 | |
} |
73 | 0 | catch (InterruptedException e) |
74 | |
{ |
75 | 0 | throw new MessagingException(HttpMessages.sslHandshakeDidNotComplete(), |
76 | |
message, e); |
77 | 0 | } |
78 | |
|
79 | 0 | super.preRouteMessage(message); |
80 | |
|
81 | 0 | if (peerCertificateChain != null) |
82 | |
{ |
83 | 0 | message.setOutboundProperty(HttpsConnector.PEER_CERTIFICATES, peerCertificateChain); |
84 | |
} |
85 | 0 | if (localCertificateChain != null) |
86 | |
{ |
87 | 0 | message.setOutboundProperty(HttpsConnector.LOCAL_CERTIFICATES, localCertificateChain); |
88 | |
} |
89 | 0 | } |
90 | |
|
91 | |
public void handshakeCompleted(HandshakeCompletedEvent event) |
92 | |
{ |
93 | |
try |
94 | |
{ |
95 | 0 | localCertificateChain = event.getLocalCertificates(); |
96 | |
try |
97 | |
{ |
98 | 0 | peerCertificateChain = event.getPeerCertificates(); |
99 | |
} |
100 | 0 | catch (SSLPeerUnverifiedException e) |
101 | |
{ |
102 | 0 | logger.debug("Cannot get peer certificate chain: "+ e.getMessage()); |
103 | 0 | } |
104 | |
} |
105 | |
finally |
106 | |
{ |
107 | 0 | latch.countDown(); |
108 | 0 | } |
109 | 0 | } |
110 | |
} |
111 | |
|
112 | |
} |