Guest User

Untitled

a guest
Oct 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. package org.apache.activemq.transport.nio;
  2.  
  3. import com.sun.xml.internal.ws.api.pipe.Engine;
  4. import org.apache.activemq.util.IOExceptionSupport;
  5. import org.apache.activemq.wireformat.WireFormat;
  6.  
  7. import javax.net.SocketFactory;
  8. import javax.net.ssl.SSLContext;
  9. import javax.net.ssl.SSLEngine;
  10. import javax.net.ssl.SSLEngineResult;
  11. import javax.net.ssl.SSLSession;
  12. import java.io.IOException;
  13. import java.net.Socket;
  14. import java.net.URI;
  15. import java.net.UnknownHostException;
  16. import java.nio.ByteBuffer;
  17. import java.sql.SQLOutput;
  18.  
  19. import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_UNWRAP;
  20. import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_TASK;
  21. import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NEED_WRAP;
  22. import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
  23. import static javax.net.ssl.SSLEngineResult.Status.BUFFER_OVERFLOW;
  24.  
  25. public class NIOSSLTransport extends NIOTransport {
  26.  
  27. protected SSLContext sslContext;
  28. protected SSLEngine sslEngine;
  29. protected SSLSession sslSession;
  30.  
  31. private ByteBuffer readBuffer;
  32. private ByteBuffer writeBuffer;
  33. private ByteBuffer plain;
  34.  
  35. public NIOSSLTransport(WireFormat wireFormat, SocketFactory socketFactory, URI remoteLocation, URI localLocation) throws UnknownHostException, IOException {
  36. super(wireFormat, socketFactory, remoteLocation, localLocation);
  37. }
  38.  
  39. public NIOSSLTransport(WireFormat wireFormat, Socket socket) throws IOException {
  40. super(wireFormat, socket);
  41. }
  42.  
  43. public void setSslContext(SSLContext sslContext) {
  44. this.sslContext = sslContext;
  45. }
  46.  
  47. @Override
  48. protected void initializeStreams() throws IOException {
  49. super.initializeStreams();
  50. try {
  51. if (sslContext == null) {
  52. sslContext = SSLContext.getDefault();
  53. }
  54. sslEngine = sslContext.createSSLEngine();
  55. sslEngine.setUseClientMode(false);
  56. sslSession = sslEngine.getSession();
  57. readBuffer = ByteBuffer.allocate(sslSession.getPacketBufferSize());
  58. writeBuffer = ByteBuffer.allocate(sslSession.getPacketBufferSize());
  59. sslEngine.beginHandshake();
  60. plain = ByteBuffer.allocate(sslSession.getApplicationBufferSize());
  61. } catch (Exception e) {
  62. throw new IOException(e);
  63. }
  64. }
  65.  
  66. boolean handshakeInProgress = false;
  67.  
  68. protected void doHandshake() throws Exception {
  69. handshakeInProgress = true;
  70. while (true) {
  71. switch (sslEngine.getHandshakeStatus()) {
  72. case NEED_UNWRAP:
  73. readBuffer = ByteBuffer.allocate(sslSession.getPacketBufferSize());
  74. int bytesRead = channel.read(readBuffer);
  75. readBuffer.flip();
  76.  
  77. SSLEngineResult result = sslEngine.unwrap(readBuffer, plain);
  78. readBuffer.clear();
  79. plain.flip();
  80. case NEED_TASK:
  81. //TODO use the pool
  82. Runnable task;
  83. while ((task = sslEngine.getDelegatedTask()) != null) {
  84. task.run();
  85. }
  86. case NEED_WRAP:
  87. result = sslEngine.wrap(ByteBuffer.allocate(0), writeBuffer);
  88. writeBuffer.flip();
  89. channel.write(writeBuffer);
  90. case FINISHED:
  91. handshakeInProgress = false;
  92. break;
  93. }
  94. }
  95. }
  96.  
  97. protected void serviceRead() {
  98. try {
  99. while (true) {
  100. if (sslEngine.getHandshakeStatus() != NOT_HANDSHAKING) {
  101. doHandshake();
  102. }
  103.  
  104. }
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. onException(e);
  108. } catch (Throwable e) {
  109. e.printStackTrace();
  110. onException(IOExceptionSupport.create(e));
  111. }
  112. }
  113. }
Add Comment
Please, Sign In to add comment