View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
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 it.jnrpe.net;
17  
18  import io.netty.channel.ChannelFuture;
19  import io.netty.channel.ChannelFutureListener;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.ChannelInboundHandlerAdapter;
22  import io.netty.util.ReferenceCountUtil;
23  import it.jnrpe.ReturnValue;
24  import it.jnrpe.Status;
25  import it.jnrpe.commands.CommandInvoker;
26  import it.jnrpe.events.EventsUtil;
27  import it.jnrpe.events.IJNRPEEventListener;
28  import it.jnrpe.events.LogEvent;
29  
30  import java.util.Collection;
31  
32  import org.apache.commons.lang.StringUtils;
33  
34  /**
35   * Receives and handles connections to the JNRPE server.
36   * 
37   * @author Massimiliano Ziccardi
38   */
39  public class JNRPEServerHandler extends ChannelInboundHandlerAdapter {
40  
41  	/**
42  	 * The command invoker.
43  	 */
44  	private final CommandInvoker commandInvoker;
45  
46  	/**
47  	 * The list of listener that will receive JNRPE events.
48  	 */
49  	private final Collection<IJNRPEEventListener> listeners;
50  
51  	/**
52  	 * Constructor.
53  	 * 
54  	 * @param invoker
55  	 *            the command invoker
56  	 * @param eventListeners
57  	 *            The list of listeners
58  	 */
59  	public JNRPEServerHandler(final CommandInvoker invoker,
60  			final Collection<IJNRPEEventListener> eventListeners) {
61  		this.commandInvoker = invoker;
62  		this.listeners = eventListeners;
63  	}
64  
65  	@Override
66  	public final void channelRead(final ChannelHandlerContext ctx,
67  			final Object msg) {
68  		try {
69  			JNRPERequest req = (JNRPERequest) msg;
70  
71  			ReturnValue ret = commandInvoker.invoke(req.getCommand(),
72  					req.getArguments());
73  
74  			if (ret == null) {
75  				String args = StringUtils.join(req.getArguments(), ',');
76  
77  				ret = new ReturnValue(Status.UNKNOWN, "Command ["
78  						+ req.getCommand() + "] with args [" + args
79  						+ "] returned null");
80  			}
81  
82  			JNRPEResponse res = new JNRPEResponse();
83  			res.setPacketVersion(PacketVersion.VERSION_2);
84  
85  			res.setResultCode(ret.getStatus().intValue());
86  			res.setMessage(ret.getMessage());
87  
88  			ChannelFuture f = ctx.writeAndFlush(res);
89  			f.addListener(ChannelFutureListener.CLOSE);
90  		} finally {
91  			ReferenceCountUtil.release(msg);
92  		}
93  	}
94  
95  	@Override
96  	public final void exceptionCaught(final ChannelHandlerContext ctx,
97  			final Throwable cause) {
98  		EventsUtil.sendEvent(listeners, this, LogEvent.ERROR,
99  				cause.getMessage(), cause);
100 		ctx.close();
101 	}
102 }