1
2
3
4
5
6
7
8
9
10
11
12 package net.sf.provisioner.requests;
13
14 import java.util.Enumeration;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import net.sf.provisioner.adapters.TelnetAdapter;
21 import net.sf.provisioner.commands.GenericCommand;
22 import net.sf.provisioner.responses.Response;
23
24 import org.jdom.Document;
25 import org.jdom.Element;
26
27
28
29
30
31
32
33
34 public class GenericRequest extends Request {
35
36
37 GenericCommand command = new GenericCommand();
38
39
40
41
42 public GenericRequest(net.sf.provisioner.config.ConfigRequest request, Document parameters) {
43
44 this.ne = request.service.ne;
45
46 _initRequest(parameters);
47
48 this.ne.loginPrompt = "ogin:";
49 this.ne.port = "23";
50 this.ne.sendInitialCRLF = false;
51
52 this.command.bin = this.ne.bin;
53 this.command.operation = request.operationType;
54 }
55
56
57
58
59 @Override
60 public Response sendRequest() throws Exception {
61
62 Response response = new Response();
63
64 String operation = new String();
65
66 TelnetAdapter destino = new TelnetAdapter(this.ne);
67
68 if (this.command.operation.equalsIgnoreCase("alta")) operation = this.ne.create;
69 if (this.command.operation.equalsIgnoreCase("baja")) operation = this.ne.delete;
70
71
72 try {
73
74 String comando = this.command.bin;
75 comando = comando + operation;
76 if (!this.command.parameter1.isEmpty()) {
77 comando = comando + " " + this.command.parameter1;
78 if (!this.command.parameter2.isEmpty()) {
79 comando = comando + " " + this.command.parameter2;
80 if (!this.command.parameter3.isEmpty()) {
81 comando = comando + " " + this.command.parameter3;
82 if (!this.command.parameter4.isEmpty()) {
83 comando = comando + " " + this.command.parameter4;
84 if (!this.command.parameter5.isEmpty()) comando = comando + " " + this.command.parameter5;
85 }
86 }
87 }
88 }
89
90
91 String respuesta = destino.ExecuteCommand(comando);
92
93 logger.debug(respuesta);
94
95
96 response = interpretaRespuesta(respuesta);
97
98 } catch (Exception e) { throw e; }
99
100 return response;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 void _initRequest(Document parameters) {
120
121 String paramName;
122
123 List children = parameters.getContent();
124 Iterator iterator = children.iterator();
125 while (iterator.hasNext()) {
126 Element child = (Element) iterator.next();
127 if (child.getName().equalsIgnoreCase("operation")) {
128 List children1 = child.getChildren();
129 Iterator iterator1 = children1.iterator();
130 while (iterator1.hasNext()) {
131 Element child1 = (Element) iterator1.next();
132 if (child1.getName().equalsIgnoreCase("parameter")) {
133 paramName = child1.getAttributeValue("name");
134 if (paramName.equalsIgnoreCase("account")) this.command.account = child1.getAttributeValue("value");
135 if (paramName.equalsIgnoreCase("u_name")) this.command.user = child1.getAttributeValue("value");
136 if (paramName.equalsIgnoreCase("u_password")) this.command.password = child1.getAttributeValue("value");
137 if (paramName.equalsIgnoreCase("u_accounttype")) this.command.accountType = child1.getAttributeValue("value");
138 if (paramName.equalsIgnoreCase(this.ne.parameter1)) this.command.parameter1 = child1.getAttributeValue("value");
139 if (paramName.equalsIgnoreCase(this.ne.parameter2)) this.command.parameter2 = child1.getAttributeValue("value");
140 if (paramName.equalsIgnoreCase(this.ne.parameter3)) this.command.parameter3 = child1.getAttributeValue("value");
141 if (paramName.equalsIgnoreCase(this.ne.parameter4)) this.command.parameter4 = child1.getAttributeValue("value");
142 if (paramName.equalsIgnoreCase(this.ne.parameter5)) this.command.parameter5 = child1.getAttributeValue("value");
143 }
144 }
145 }
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 Response interpretaRespuesta (String respuesta) {
165
166 Response posibleRespuesta = new Response();
167
168
169 Enumeration respuestas = this.ne.responses.elements();
170 while (respuestas.hasMoreElements()) {
171 posibleRespuesta = (Response)respuestas.nextElement();
172 Pattern pattern = Pattern.compile(posibleRespuesta.result, Pattern.CASE_INSENSITIVE);
173 Matcher matcher = pattern.matcher(respuesta);
174 if (matcher.find() && this.command.operation.equalsIgnoreCase(posibleRespuesta.tipoOperacion))
175 return posibleRespuesta;
176 }
177 posibleRespuesta.errorStr = "No match in responses configuration file, unknown response";
178 posibleRespuesta.successfull = false;
179 posibleRespuesta.retry = false;
180 return posibleRespuesta;
181 }
182 }