View Javadoc

1   /**
2    * Esta clase .
3    * <p>
4    * 
5    * .
6    * <p>
7    * 
8    *             
9    * @version $Revision: 1.1.2.1 $, Feb 1, 2007
10   * @author Gonzalo Espert 
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.SSHAdapter;
21  import net.sf.provisioner.commands.AsteriskCommand;
22  import net.sf.provisioner.responses.Response;
23  
24  import org.jdom.Document;
25  import org.jdom.Element;
26  
27  
28  /**
29   * @author Gonzalo
30   *
31   */
32  public class AsteriskRequest extends Request {
33  
34  	/* Comando del requerimiento */
35  	AsteriskCommand command = new AsteriskCommand();
36  		
37  	/**
38  	 * 
39  	 */
40  	public AsteriskRequest(net.sf.provisioner.config.ConfigRequest request, Document parameters) {
41  		
42  		_initRequest(parameters);
43  		this.ne = request.service.ne;
44  		this.command.bin = this.ne.bin;
45  		this.command.operation = request.operationType;
46  		
47  	}
48  
49  	/**
50       * .
51       * <p>
52       * 
53       * .
54       * <p>
55       * 
56       * 
57       * 
58       * @param 
59       *            
60       * @throws 
61       *            
62       *           
63       */
64  	void _initRequest(Document parameters) {
65  		
66  		String paramName;
67  		
68  		List children = parameters.getContent();  
69  	    Iterator iterator = children.iterator();
70  	    while (iterator.hasNext()) {
71  	      Element child = (Element) iterator.next();
72  	      if (child.getName().equalsIgnoreCase("operation")) {
73  	    	  List children1 = child.getChildren();
74  	    	  Iterator iterator1 = children1.iterator();
75  	    	  while (iterator1.hasNext()) {
76  	    		  Element child1 = (Element) iterator1.next();
77  	    		  if (child1.getName().equalsIgnoreCase("parameter")) {
78  	    			  	paramName = child1.getAttributeValue("name");
79  	    		      	if (paramName.equalsIgnoreCase("numero_linea")) this.command.line = child1.getAttributeValue("value");
80  	    		      	if (paramName.equalsIgnoreCase("password")) this.command.password = child1.getAttributeValue("value");
81  	    		      	if (paramName.equalsIgnoreCase("nombre_cliente")) this.command.user = child1.getAttributeValue("value");
82  	    		      	if (paramName.equalsIgnoreCase("email")) this.command.email = child1.getAttributeValue("value");
83  	    		      	if (paramName.equalsIgnoreCase("mensaje")) this.command.message = child1.getAttributeValue("value");
84  	    		  }
85  	    	  }
86  	      }
87  	    }
88  	}
89  	
90  	
91  	/* (non-Javadoc)
92  	 * @see requests.Request#sendRequest()
93  	 */
94  	@Override
95  	public Response sendRequest() throws Exception {
96  
97  		Response response = new Response();
98  		
99  		String command = null;
100 		
101 		SSHAdapter asterisk = new SSHAdapter(this.ne);
102 		
103 		/* Si se trata de una alta */
104 		if (this.command.operation.equalsIgnoreCase("create")) command = buildCreateCommand();
105 		/* Si es una baja */
106 		else if (this.command.operation.equalsIgnoreCase("delete")) command = buildDeleteCommand();
107 		
108 		/* Enviar a Asterisk */
109 		try { 
110 			
111 			/* Ejecutamos el comando */
112 			String respuesta = asterisk.ExecuteCommand(command);
113 			
114 			logger.debug(respuesta);
115 			
116 			/* Interpretamos la respuesta */
117 			response = interpretaRespuesta(respuesta);
118 			
119 		} catch (Exception e) { throw e; }
120 		
121 		return response;
122 	}
123 
124 	Response interpretaRespuesta (String respuesta) {
125 		
126 		Response posibleRespuesta = new Response();
127 		
128 		/* Matcheamos la respuesta con las configuradas en el archivo xml */
129 		Enumeration respuestas = this.ne.responses.elements();
130 		while (respuestas.hasMoreElements()) {
131 			posibleRespuesta = (Response)respuestas.nextElement();
132 			Pattern pattern = Pattern.compile(posibleRespuesta.result, Pattern.CASE_INSENSITIVE);
133 			Matcher matcher = pattern.matcher(respuesta);
134 			if (matcher.find() && this.command.operation.equalsIgnoreCase(posibleRespuesta.tipoOperacion))
135 				return posibleRespuesta;
136 		}
137 		posibleRespuesta.errorStr = "No match found in the responses file, unknown response";
138 		posibleRespuesta.successfull = false;
139 		posibleRespuesta.retry = false;
140 		return posibleRespuesta;
141 	}
142 
143 	String buildDeleteCommand() {
144 		
145 		/* Armamos el string del comando */
146 		String command = this.command.bin + this.ne.delete;
147 		command = command + " \"" + this.command.line + "\"";
148 		command = command + " \"" + this.command.message + "\"";
149 		
150 		return command;
151 	}
152 	
153 	String buildCreateCommand() {
154 		
155 		/* Armamos el string del comando */
156 		String command = this.command.bin + this.ne.create;
157 		command = command + " \"" + this.command.line + "\"";
158 		command = command + " \"" + this.command.password + "\"";
159 		command = command + " \"" + this.command.user + "\"";
160 		command = command + " \"" + this.command.email + "\"";
161 		
162 		return command;
163 	}
164 }