/** * Sets the value of this variable only when no value was set. * * @return {@code null} if the specified value was set. * An existing value if failed to set. */ public T setIfAbsent(Channel channel, T value) { if (value == null) { return get(channel); } else { if (channel == null) { throw new NullPointerException("channel"); } T mapping = map.putIfAbsent(channel, value); if (removeOnClose && mapping == null) { channel.getCloseFuture().addListener(remover); } return mapping; } }
private static NodeInfo getNodeInfo(Channel channel) { return ChannelNodeInfo.nodeInfo.get(channel); }
@Override protected short getNodeId(ChannelHandlerContext ctx, SocketAddress address) { final Channel channel = ctx.getChannel(); //final SocketAddress address = channel.getRemoteAddress(); NodeInfo node = ChannelNodeInfo.nodeInfo.get(channel); assert node != null; return node.getNodeId(); } }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!(e.getMessage() instanceof OdetteFtpExchangeBuffer)) { super.writeRequested(ctx, e); return; } OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); OdetteFtpExchangeBuffer message = (OdetteFtpExchangeBuffer) e.getMessage(); // set last command sent in session attribute session.setLastCommandSent(message.getIdentifier()); super.writeRequested(ctx, e); }
/** * Cleanup temporary files * * @param channel */ protected void cleanup(Channel channel) { // Make sure we dispose everything on exit on session close SMTPSession smtpSession = (SMTPSession) attributes.get(channel); if (smtpSession != null) { LifecycleUtil.dispose(smtpSession.getState().get(SMTPConstants.MAIL)); LifecycleUtil.dispose(smtpSession.getState().get(SMTPConstants.DATA_MIMEMESSAGE_STREAMSOURCE)); } super.cleanup(channel); }
@Override public boolean add(Channel channel) { if (channel instanceof ServerChannel) return super.add(channel); else { final NodeInfo node = ChannelNodeInfo.nodeInfo.get(channel); if (node == null) { LOG.warn("Received connection from an unknown address {}.", channel.getRemoteAddress()); throw new RuntimeException("Unknown node for address " + channel.getRemoteAddress()); } final short nodeId = node.getNodeId(); if (channels.containsKey(nodeId)) { LOG.warn("Received connection from address {} of node {}, but this node is already connected.", channel.getRemoteAddress(), nodeId); throw new RuntimeException("Node " + nodeId + " already connected."); } final boolean added = super.add(channel); if (added) channels.put(nodeId, channel); return added; } }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); Oftplet oftplet = getSessionOftplet(session); oftplet.destroy(); super.channelDisconnected(ctx, e); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); Oftplet oftplet = getSessionOftplet(session); oftplet.onExceptionCaught(e.getCause()); } }
@Override public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { // check if the client did nothing for too long if (e.getState().equals(IdleState.ALL_IDLE)) { ImapSession session = (ImapSession) attributes.get(ctx.getChannel()); InetSocketAddress address = (InetSocketAddress) ctx.getChannel().getRemoteAddress(); LOGGER.info("Logout client {} ({}) because it idled for too long...", address.getHostName(), address.getAddress().getHostAddress()); // logout the client session.logout(); // close the channel ctx.getChannel().close(); } super.channelIdle(ctx, e); }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); /* Get handler implementation for the correct protocol version. */ OdetteFtpVersion version = session.getVersion(); ProtocolHandler handler = getProtocolHandlerByVersion(version); /* * Delegate the session opened processing to the correct handler * implementation version. */ handler.sessionConnected(session); super.channelConnected(ctx, e); }
/** * This method is called whenever the communication data flow is idle and * the session timeout limit is over. */ @Override public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); // session is terminated by timeout OdetteFtpVersion version = session.getVersion(); ProtocolHandler handler = getProtocolHandlerByVersion(version); // TODO perhaps we may specify the timeout period in release message handler.abort(session, EndSessionReason.TIME_OUT, null); super.channelIdle(ctx, e); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { try (Closeable closeable = ManageSieveMDCContext.from(ctx, attributes)) { String request = (String) e.getMessage(); Session manageSieveSession = attributes.get(ctx.getChannel()); String responseString = manageSieveProcessor.handleRequest(manageSieveSession, request); ((ChannelManageSieveResponseWriter) ctx.getAttachment()).write(responseString); if (manageSieveSession.getState() == Session.State.SSL_NEGOCIATION) { turnSSLon(ctx.getChannel()); manageSieveSession.setSslEnabled(true); manageSieveSession.setState(Session.State.UNAUTHENTICATED); } } }
ImapSession session = (ImapSession) attributes.get(channel);
final ImapSession imapSession = (ImapSession) attributes.get(ctx.getChannel()); if (imapSession != null) { imapSession.logout();
public static Closeable from(ChannelHandlerContext ctx, ChannelLocal<Object> attributes) { return MDCBuilder.create() .addContext(from(attributes.get(ctx.getChannel()))) .addContext(MDCBuilder.PROTOCOL, "IMAP") .addContext(MDCBuilder.IP, retrieveIp(ctx)) .addContext(MDCBuilder.HOST, retrieveHost(ctx)) .build(); }
try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) { imapCommandsMetric.increment(); ImapSession session = (ImapSession) attributes.get(ctx.getChannel()); ImapResponseComposer response = (ImapResponseComposer) ctx.getAttachment(); ImapMessage message = (ImapMessage) e.getMessage();
public static Closeable from(ChannelHandlerContext ctx, ChannelLocal<Session> attributes) { return MDCBuilder.create() .addContext(from(attributes.get(ctx.getChannel()))) .addContext(MDCBuilder.PROTOCOL, "MANAGE-SIEVE") .addContext(MDCBuilder.IP, retrieveIp(ctx)) .addContext(MDCBuilder.HOST, retrieveHost(ctx)) .addContext(MDCBuilder.SESSION_ID, ctx.getChannel().getId()) .build(); }
OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel()); OdetteFtpVersion version = session.getVersion(); CommandFormat commandFormat = CommandFormatHelper.resolveByVersion(version, identifier);
OdetteFtpSession session = ChannelContext.SESSION.get(ctx.getChannel());