1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.net;
17
18 import org.apache.commons.lang.StringUtils;
19
20
21
22
23
24
25 public class JNRPERequest extends JNRPEProtocolPacket {
26
27
28
29
30 public JNRPERequest() {
31 setPacketType(PacketType.QUERY);
32 }
33
34
35
36
37
38
39
40
41
42 public JNRPERequest(final String commandName, final String... arguments) {
43 init(commandName, arguments);
44 updateCRC();
45 }
46
47
48
49
50
51
52
53
54
55
56 JNRPERequest(final String commandName, final String sArguments) {
57 init(commandName, sArguments);
58 }
59
60
61
62
63
64
65
66
67
68
69
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
88 init(commandName, StringUtils.join(ary, '!'));
89 } else {
90 init(commandName, (String) null);
91 }
92 }
93
94
95
96
97
98
99
100
101
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
122 }
123
124
125
126
127
128
129 public final String getCommand() {
130
131 String[] partsAry = split(getPacketString());
132
133 return partsAry[0];
134 }
135
136
137
138
139
140
141 public final String[] getArguments() {
142
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
153
154
155
156
157
158
159 private String[] split(final String sCommandLine) {
160 return it.jnrpe.utils.StringUtils.split(sCommandLine, '!', false);
161 }
162 }