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 org.apache.commons.lang.StringUtils;
19  
20  /**
21   * This object represent a generic request packet.
22   * 
23   * @author Massimiliano Ziccardi
24   */
25  public class JNRPERequest extends JNRPEProtocolPacket {
26  
27  	/**
28  	 * Creates and empty request.
29  	 */
30  	public JNRPERequest() {
31  		setPacketType(PacketType.QUERY);
32  	}
33  
34  	/**
35  	 * Inizialize the request with the supplied command and command arguments.
36  	 * 
37  	 * @param commandName
38  	 *            The command
39  	 * @param arguments
40  	 *            The arguments
41  	 */
42  	public JNRPERequest(final String commandName, final String... arguments) {
43  		init(commandName, arguments);
44  		updateCRC();
45  	}
46  
47  	/**
48  	 * Inizialize the request with the supplied command and command arguments.
49  	 * The arguments must be separated by an exclamation mark ('!')
50  	 * 
51  	 * @param commandName
52  	 *            The command
53  	 * @param sArguments
54  	 *            The arguments
55  	 */
56  	JNRPERequest(final String commandName, final String sArguments) {
57  		init(commandName, sArguments);
58  	}
59  
60  	/**
61  	 * Initializes the object with the given command and the given arguments.
62  	 * 
63  	 * The arguments gets quoted if they contains '!' and are then joined using
64  	 * the '!' as separator.
65  	 * 
66  	 * @param commandName
67  	 *            The command
68  	 * @param arguments
69  	 *            The arguments
70  	 */
71  	private void init(final String commandName, final String... arguments) {
72  		if (arguments != null) {
73  			if (arguments.length == 1) {
74  				init(commandName, arguments[0]);
75  				return;
76  			}
77  
78  			String[] ary = new String[arguments.length];
79  
80  			for (int i = 0; i < arguments.length; i++) {
81  				if (arguments[i].indexOf('!') == -1) {
82  					ary[i] = arguments[i];
83  				} else {
84  					ary[i] = "'" + arguments[i] + "'";
85  				}
86  			}
87  			// sCommandBytes = StringUtils.join(ary, '!');
88  			init(commandName, StringUtils.join(ary, '!'));
89  		} else {
90  			init(commandName, (String) null);
91  		}
92  	}
93  
94  	/**
95  	 * Initializes the object with the given command and the given list of '!'
96  	 * separated list of arguments.
97  	 * 
98  	 * @param commandName
99  	 *            The command
100 	 * @param argumentsString
101 	 *            The arguments
102 	 */
103 	private void init(final String commandName, final String argumentsString) {
104 		String fullCommandString;
105 
106 		String tmpArgumentsString = argumentsString;
107 
108 		if (tmpArgumentsString != null && tmpArgumentsString.startsWith("!")) {
109 			tmpArgumentsString = tmpArgumentsString.substring(1);
110 		}
111 
112 		if (!StringUtils.isBlank(tmpArgumentsString)) {
113 			fullCommandString = commandName + "!" + tmpArgumentsString;
114 		} else {
115 			fullCommandString = commandName;
116 		}
117 
118 		setPacketVersion(PacketVersion.VERSION_2);
119 		super.setPacketType(PacketType.QUERY);
120 		super.setBuffer(fullCommandString);
121 		// updateCRC();
122 	}
123 
124 	/**
125 	 * Returns the query command.
126 	 * 
127 	 * @return the query command
128 	 */
129 	public final String getCommand() {
130 		// extracting command
131 		String[] partsAry = split(getPacketString());
132 
133 		return partsAry[0];
134 	}
135 
136 	/**
137 	 * Returns the command arguments.
138 	 * 
139 	 * @return the command arguments
140 	 */
141 	public final String[] getArguments() {
142 		// extracting params
143 		String[] partsAry = split(getPacketString());
144 		String[] argsAry = new String[partsAry.length - 1];
145 
146 		System.arraycopy(partsAry, 1, argsAry, 0, argsAry.length);
147 
148 		return argsAry;
149 	}
150 
151 	/**
152 	 * Utility method that splits using the '!' character and handling quoting
153 	 * by "'" and '"'.
154 	 * 
155 	 * @param sCommandLine
156 	 *            The command line string
157 	 * @return The splitted string
158 	 */
159 	private String[] split(final String sCommandLine) {
160 		return it.jnrpe.utils.StringUtils.split(sCommandLine, '!', false);
161 	}
162 }