PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/dxp/apps/vldap/vldap-server/src/main/java/com/liferay/vldap/server/internal/DispatchIoHandler.java

http://github.com/liferay/liferay-portal
Java | 231 lines | 171 code | 43 blank | 17 comment | 33 complexity | f7f95d58f80899d88a03752d86e7532b MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * The contents of this file are subject to the terms of the Liferay Enterprise
  5. * Subscription License ("License"). You may not use this file except in
  6. * compliance with the License. You can obtain a copy of the License by
  7. * contacting Liferay, Inc. See the License for the specific language governing
  8. * permissions and limitations under the License, including but not limited to
  9. * distribution rights of the Software.
  10. *
  11. *
  12. *
  13. */
  14. package com.liferay.vldap.server.internal;
  15. import com.liferay.petra.lang.CentralizedThreadLocal;
  16. import com.liferay.portal.kernel.cache.thread.local.Lifecycle;
  17. import com.liferay.portal.kernel.cache.thread.local.ThreadLocalCacheManager;
  18. import com.liferay.portal.kernel.exception.PortalException;
  19. import com.liferay.portal.kernel.log.Log;
  20. import com.liferay.portal.kernel.log.LogFactoryUtil;
  21. import com.liferay.vldap.server.internal.constants.VLDAPConstants;
  22. import com.liferay.vldap.server.internal.handler.AbandonLdapHandler;
  23. import com.liferay.vldap.server.internal.handler.BindLdapHandler;
  24. import com.liferay.vldap.server.internal.handler.CompareLdapHandler;
  25. import com.liferay.vldap.server.internal.handler.ExtendedLdapHandler;
  26. import com.liferay.vldap.server.internal.handler.LdapHandler;
  27. import com.liferay.vldap.server.internal.handler.SearchLdapHandler;
  28. import com.liferay.vldap.server.internal.handler.UnbindLdapHandler;
  29. import com.liferay.vldap.server.internal.handler.UnwillingToPerformLdapHandler;
  30. import com.liferay.vldap.server.internal.handler.util.LdapHandlerContext;
  31. import com.liferay.vldap.server.internal.handler.util.LdapHandlerThreadLocal;
  32. import com.liferay.vldap.server.internal.handler.util.LiferayLdapMessageContainer;
  33. import com.liferay.vldap.server.internal.util.PortletPropsValues;
  34. import java.util.List;
  35. import java.util.Map;
  36. import org.apache.directory.api.ldap.codec.api.LdapDecoder;
  37. import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
  38. import org.apache.directory.api.ldap.model.message.Request;
  39. import org.apache.directory.api.ldap.model.message.Response;
  40. import org.apache.mina.core.service.IoHandler;
  41. import org.apache.mina.core.session.IdleStatus;
  42. import org.apache.mina.core.session.IoSession;
  43. import org.apache.mina.filter.FilterEvent;
  44. /**
  45. * @author Jonathan Potter
  46. * @author Brian Wing Shun Chan
  47. */
  48. public class DispatchIoHandler implements IoHandler {
  49. @Override
  50. public void event(IoSession ioSession, FilterEvent filterEvent) {
  51. }
  52. @Override
  53. public void exceptionCaught(IoSession ioSession, Throwable throwable) {
  54. if (_log.isDebugEnabled()) {
  55. _log.debug(throwable, throwable);
  56. }
  57. }
  58. @Override
  59. public void inputClosed(IoSession ioSession) throws Exception {
  60. }
  61. @Override
  62. public void messageReceived(IoSession ioSession, Object message) {
  63. Request request = (Request)message;
  64. LdapHandler ldapHandler = getLdapHandler(request);
  65. if (ldapHandler == null) {
  66. if (_log.isWarnEnabled()) {
  67. _log.warn(request.getType() + " is not supported");
  68. }
  69. ldapHandler = _unwillingToPerformLdapHandler;
  70. }
  71. try {
  72. List<Response> responses = ldapHandler.messageReceived(
  73. request, ioSession, getLdapHandlerContext(ioSession));
  74. writeResponses(responses, ioSession);
  75. }
  76. catch (PortalException portalException) {
  77. _log.error(portalException, portalException);
  78. }
  79. finally {
  80. ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
  81. CentralizedThreadLocal.clearShortLivedThreadLocals();
  82. }
  83. }
  84. @Override
  85. public void messageSent(IoSession ioSession, Object message) {
  86. }
  87. @Override
  88. public void sessionClosed(IoSession ioSession) {
  89. LdapHandlerThreadLocal.clearSocketAddress();
  90. }
  91. @Override
  92. public void sessionCreated(IoSession ioSession) {
  93. }
  94. @Override
  95. public void sessionIdle(IoSession ioSession, IdleStatus idleStatus) {
  96. }
  97. @Override
  98. public void sessionOpened(IoSession ioSession) {
  99. try {
  100. LdapHandlerThreadLocal.setSocketAddress(
  101. ioSession.getRemoteAddress());
  102. if (!LdapHandlerThreadLocal.isHostAllowed(
  103. PortletPropsValues.HOSTS_ALLOWED)) {
  104. if (_log.isWarnEnabled()) {
  105. _log.warn(
  106. "Access denied for " + ioSession.getRemoteAddress());
  107. }
  108. ioSession.close(true);
  109. return;
  110. }
  111. ioSession.setAttribute(
  112. LdapDecoder.MESSAGE_CONTAINER_ATTR,
  113. new LiferayLdapMessageContainer());
  114. }
  115. catch (Exception exception) {
  116. _log.error(exception, exception);
  117. }
  118. }
  119. protected LdapHandler getLdapHandler(Request request) {
  120. MessageTypeEnum messageTypeEnum = request.getType();
  121. if (messageTypeEnum == MessageTypeEnum.ABANDON_REQUEST) {
  122. return _abandonLdapHandler;
  123. }
  124. else if (messageTypeEnum == MessageTypeEnum.BIND_REQUEST) {
  125. return _bindLdapHandler;
  126. }
  127. else if (messageTypeEnum == MessageTypeEnum.COMPARE_REQUEST) {
  128. return _compareLdapHandler;
  129. }
  130. else if (messageTypeEnum == MessageTypeEnum.EXTENDED_REQUEST) {
  131. return _extendedLdapHandler;
  132. }
  133. else if (messageTypeEnum == MessageTypeEnum.SEARCH_REQUEST) {
  134. return _searchLdapHandler;
  135. }
  136. else if (messageTypeEnum == MessageTypeEnum.UNBIND_REQUEST) {
  137. return _unbindLdapHandler;
  138. }
  139. return null;
  140. }
  141. protected LdapHandlerContext getLdapHandlerContext(IoSession ioSession) {
  142. LdapHandlerContext ldapHandlerContext =
  143. (LdapHandlerContext)ioSession.getAttribute(
  144. LdapHandlerContext.class.getName());
  145. if (ldapHandlerContext == null) {
  146. synchronized (ioSession) {
  147. if (ldapHandlerContext == null) {
  148. ldapHandlerContext = new LdapHandlerContext();
  149. ioSession.setAttribute(
  150. LdapHandlerContext.class.getName(), ldapHandlerContext);
  151. }
  152. }
  153. }
  154. return ldapHandlerContext;
  155. }
  156. protected void setSessionAttributes(
  157. Response response, IoSession ioSession) {
  158. Map<Object, Object> sessionAttributes =
  159. (Map<Object, Object>)response.get(
  160. VLDAPConstants.SESSION_ATTRIBUTES);
  161. if (sessionAttributes == null) {
  162. return;
  163. }
  164. for (Map.Entry<Object, Object> entry : sessionAttributes.entrySet()) {
  165. ioSession.setAttribute(entry.getKey(), entry.getValue());
  166. }
  167. }
  168. protected void writeResponses(
  169. List<Response> responses, IoSession ioSession) {
  170. if (responses == null) {
  171. return;
  172. }
  173. for (Response response : responses) {
  174. setSessionAttributes(response, ioSession);
  175. ioSession.write(response);
  176. }
  177. }
  178. private static final Log _log = LogFactoryUtil.getLog(
  179. DispatchIoHandler.class);
  180. private final LdapHandler _abandonLdapHandler = new AbandonLdapHandler();
  181. private final LdapHandler _bindLdapHandler = new BindLdapHandler();
  182. private final LdapHandler _compareLdapHandler = new CompareLdapHandler();
  183. private final LdapHandler _extendedLdapHandler = new ExtendedLdapHandler();
  184. private final LdapHandler _searchLdapHandler = new SearchLdapHandler();
  185. private final LdapHandler _unbindLdapHandler = new UnbindLdapHandler();
  186. private final LdapHandler _unwillingToPerformLdapHandler =
  187. new UnwillingToPerformLdapHandler();
  188. }