/driver-sync/src/main/com/mongodb/client/internal/CryptBinding.java

https://github.com/jyemin/mongo-java-driver · Java · 156 lines · 114 code · 27 blank · 15 comment · 0 complexity · 895a276f7bc69a5bab1b43f8ec5fbed0 MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.client.internal;
  17. import com.mongodb.ReadPreference;
  18. import com.mongodb.RequestContext;
  19. import com.mongodb.ServerAddress;
  20. import com.mongodb.ServerApi;
  21. import com.mongodb.connection.ServerDescription;
  22. import com.mongodb.internal.binding.ClusterAwareReadWriteBinding;
  23. import com.mongodb.internal.binding.ConnectionSource;
  24. import com.mongodb.internal.binding.ReadWriteBinding;
  25. import com.mongodb.internal.connection.Cluster;
  26. import com.mongodb.internal.connection.Connection;
  27. import com.mongodb.internal.session.SessionContext;
  28. import com.mongodb.lang.Nullable;
  29. class CryptBinding implements ClusterAwareReadWriteBinding {
  30. private final ClusterAwareReadWriteBinding wrapped;
  31. private final Crypt crypt;
  32. CryptBinding(final ClusterAwareReadWriteBinding wrapped, final Crypt crypt) {
  33. this.crypt = crypt;
  34. this.wrapped = wrapped;
  35. }
  36. @Override
  37. public ReadPreference getReadPreference() {
  38. return wrapped.getReadPreference();
  39. }
  40. @Override
  41. public ConnectionSource getReadConnectionSource() {
  42. return new CryptConnectionSource(wrapped.getReadConnectionSource());
  43. }
  44. @Override
  45. public ConnectionSource getReadConnectionSource(final int minWireVersion, final ReadPreference fallbackReadPreference) {
  46. return new CryptConnectionSource(wrapped.getReadConnectionSource(minWireVersion, fallbackReadPreference));
  47. }
  48. @Override
  49. public ConnectionSource getWriteConnectionSource() {
  50. return new CryptConnectionSource(wrapped.getWriteConnectionSource());
  51. }
  52. @Override
  53. public ConnectionSource getConnectionSource(final ServerAddress serverAddress) {
  54. return new CryptConnectionSource(wrapped.getConnectionSource(serverAddress));
  55. }
  56. @Override
  57. public SessionContext getSessionContext() {
  58. return wrapped.getSessionContext();
  59. }
  60. @Override
  61. @Nullable
  62. public ServerApi getServerApi() {
  63. return wrapped.getServerApi();
  64. }
  65. @Override
  66. public RequestContext getRequestContext() {
  67. return wrapped.getRequestContext();
  68. }
  69. @Override
  70. public int getCount() {
  71. return wrapped.getCount();
  72. }
  73. @Override
  74. public ReadWriteBinding retain() {
  75. wrapped.retain();
  76. return this;
  77. }
  78. @Override
  79. public int release() {
  80. return wrapped.release();
  81. }
  82. @Override
  83. public Cluster getCluster() {
  84. return wrapped.getCluster();
  85. }
  86. private class CryptConnectionSource implements ConnectionSource {
  87. private final ConnectionSource wrapped;
  88. CryptConnectionSource(final ConnectionSource wrapped) {
  89. this.wrapped = wrapped;
  90. }
  91. @Override
  92. public ServerDescription getServerDescription() {
  93. return wrapped.getServerDescription();
  94. }
  95. @Override
  96. public SessionContext getSessionContext() {
  97. return wrapped.getSessionContext();
  98. }
  99. @Override
  100. public ServerApi getServerApi() {
  101. return wrapped.getServerApi();
  102. }
  103. @Override
  104. public RequestContext getRequestContext() {
  105. return wrapped.getRequestContext();
  106. }
  107. @Override
  108. public ReadPreference getReadPreference() {
  109. return wrapped.getReadPreference();
  110. }
  111. @Override
  112. public Connection getConnection() {
  113. return new CryptConnection(wrapped.getConnection(), crypt);
  114. }
  115. @Override
  116. public int getCount() {
  117. return wrapped.getCount();
  118. }
  119. @Override
  120. public ConnectionSource retain() {
  121. wrapped.retain();
  122. return this;
  123. }
  124. @Override
  125. public int release() {
  126. return wrapped.release();
  127. }
  128. }
  129. }