View Javadoc
1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.miloss.fgsms.agents;
6   
7   import java.util.HashMap;
8   import java.util.Iterator;
9   import java.util.UUID;
10  import org.miloss.fgsms.agentcore.MessageProcessor;
11  import org.miloss.fgsms.common.Utility;
12  import org.apache.log4j.Level;
13  import org.jboss.soa.esb.couriers.CourierException;
14  import org.miloss.fgsms.common.Logger;;
15  import org.jboss.soa.esb.http.HttpRequest;
16  import org.jboss.soa.esb.message.body.content.TextBody;
17  import org.miloss.fgsms.agentcore.IMessageProcessor;
18  
19  /**
20   * This class will log a ton of internal Jboss ESB messages, most of it probably
21   * isn't useful data, therefore it is deprecated
22   * @author AO
23   * use action pipeline processor instead
24   */
25  @Deprecated 
26  public class JbossESBAgent extends org.jboss.soa.esb.filter.InputOutputFilter {
27  
28      private Logger log;
29  
30      public JbossESBAgent() {
31          log = Logger.getLogger(org.miloss.fgsms.common.Constants.LoggerName);
32      }
33  
34      /**
35       * Called as the message flows towards the transport.
36       * this is really on output!!!
37       * http://docs.jboss.org/jbossesb/docs/4.2MR3/javadoc/esb/index.html?org/jboss/soa/esb/filter/InputOutputFilter.html
38       * @param msg
39       * @param params
40       * @return
41       * @throws CourierException
42       */
43      public org.jboss.soa.esb.message.Message onInput(org.jboss.soa.esb.message.Message msg,
44              java.util.Map<java.lang.String, java.lang.Object> params)
45              throws org.jboss.soa.esb.couriers.CourierException {
46          log.log(Level.INFO, "Current VM Memory : total = " + Runtime.getRuntime().totalMemory() + " free = " + Runtime.getRuntime().freeMemory());
47  
48  
49          for (int i = 0; i < msg.getProperties().getNames().length; i++) {
50              log.log(Level.DEBUG, "key = " + msg.getProperties().getNames()[i] + " value = " + msg.getProperties().getProperty(msg.getProperties().getNames()[i]));
51          }
52          if (params != null) {
53              Iterator<String> iterator = params.keySet().iterator();
54              while (iterator.hasNext()) {
55                  String s = iterator.next();
56                  log.log(Level.DEBUG, "pkey = " + s + " pvalue = " + params.get(s).toString());
57              }
58          }
59  
60          //an output message could be a request, service or web app as a client OR it could be a response from this service, handle this
61  
62  
63          String id = "";
64          try {
65              id = (String) msg.getProperties().getProperty("fgsms.TransactionID");
66          } catch (Exception ex) {
67              log.log(Level.ERROR, "fgsms Agent for JbossESB, could not obtain the transaction id, this was unexpected." + ex.getLocalizedMessage());
68              return msg;
69          }
70          if (Utility.stringIsNullOrEmpty(id)) {
71              log.log(Level.ERROR, "fgsms Agent for JbossESB, outbound message does not have transaction id, this was unexpected.");
72              return msg;
73          }
74  
75          //grab the original url from the gateway, useful but not fullproof
76          String url = (String) msg.getProperties().getProperty("org.jboss.soa.esb.gateway.original.url");
77          if (Utility.stringIsNullOrEmpty(url)) {
78              try {
79                  //alternate method, but should always work
80                  url = msg.getHeader().getCall().getTo().getURI().toString();
81                  if (url.startsWith("invm://")) {
82                      log.log(Level.WARN, "fgsms Agent for JbossESB, outbound message is internal esb traffic, ignoring " + url);
83                      return msg;
84                  }
85                  log.log(Level.WARN, "fgsms Agent for JbossESB, outbound message for " + url + " via SOAP To field.");
86              } catch (Exception ex) {
87                  log.log(Level.ERROR, "fgsms Agent for JbossESB, unable to determine the URL or TO field from the message, this message will be ignored");
88                  return msg;
89              }
90          } else {
91              log.log(Level.INFO, "fgsms Agent for JbossESB, outbound message for " + url + " via transport.");
92          }
93          // search for endswith _reply to identify reply messages from queues
94          if (url.endsWith("_reply")) {
95          }
96  
97  
98          Integer msgsize = null;
99          try {
100             msgsize = (Integer) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size");
101             log.log(Level.INFO, "fgsms Agent for JbossESB, outbound message size " + msgsize + " via long.");
102         } catch (Exception ex) {
103         }
104         try {
105             msgsize = Integer.parseInt((String) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size"));
106             log.log(Level.INFO, "fgsms Agent for JbossESB, outbound message size " + msgsize + " via string.");
107         } catch (Exception ex) {
108         }
109 
110 
111         boolean fault = false;
112         //getFault 'should' be for comms error, not app error
113         if (msg.getFault() != null) {
114             if (!Utility.stringIsNullOrEmpty(msg.getFault().getReason())
115                     || msg.getFault().getCode() != null
116                     || msg.getFault().getCause() != null) //faulted msg
117             {
118                 fault = true;
119                 log.log(Level.WARN, "fgsms, this message to " + url + " transaction id:" + id.toString() + " has faulted.");
120             }
121         }
122 
123 //        Long dod = (Long) params.get("org.jboss.soa.esb.message.time.dod");
124         //      if (dod == null) {
125         Long dod = System.currentTimeMillis();
126         //    }
127 
128         if (msg.getAttachment().getNamedCount() > 0 || msg.getAttachment().getUnnamedCount() > 0) {
129             log.log(Level.INFO, "fgsms Agent for JbossESB, outbound message has attachments, named: " + msg.getAttachment().getNamedCount() + " unnamed:" + msg.getAttachment().getUnnamedCount());
130             int count = (msg.getAttachment().getNamedCount() + msg.getAttachment().getUnnamedCount());
131             for (int i = 0; i < count; i++) {
132                 log.log(Level.INFO, "fgsms Agent for JbossESB, outbound message has attachments " + i + " type: " + msg.getAttachment().itemAt(i).getClass().getCanonicalName());
133             }
134         }
135 
136 
137         String body = "";
138         try {
139             if (msg.getBody() instanceof TextBody) {
140                 TextBody t = (TextBody) msg.getBody();
141                 if (t != null) {
142                     body = t.getText();
143                     body += t.get().toString();
144                 }
145             }
146             if (msg.getBody() instanceof org.jboss.internal.soa.esb.message.format.xml.BodyImpl) {
147                 org.jboss.internal.soa.esb.message.format.xml.BodyImpl t = (org.jboss.internal.soa.esb.message.format.xml.BodyImpl) msg.getBody();
148                 log.log(Level.DEBUG, "body get is of type " + t.get().getClass().getCanonicalName());
149                 body = t.toString();//new String(t.getContents());
150                 body += t.get().toString();
151             }
152             if (msg.getBody() instanceof org.jboss.internal.soa.esb.message.format.serialized.BodyImpl) {
153                 org.jboss.internal.soa.esb.message.format.serialized.BodyImpl t = (org.jboss.internal.soa.esb.message.format.serialized.BodyImpl) msg.getBody();
154                 log.log(Level.DEBUG, "body get is of type " + t.get().getClass().getCanonicalName());
155                 body = t.toString();//new String(t.getContents());
156                 body += t.get().toString();
157             }
158             log.log(Level.INFO, "outbound messsage for " + url + " has a body of type " + msg.getBody().getClass().getName());
159         } catch (Exception ex) {
160             log.log(Level.WARN, "fgsms Agent for JbossESB, outbound message for " + url + " has a non-text body", ex);
161 
162         }
163 
164         IMessageProcessor mp = MessageProcessor.getSingletonObject();
165         if (msgsize == null) {
166             mp.processMessageOutput(id, body, body.length(), fault, Long.valueOf(dod),new HashMap());
167         } else {
168             mp.processMessageOutput(id, body, msgsize, fault, Long.valueOf(dod),new HashMap());
169         }
170         return msg;
171     }
172 
173     /**
174      * Called immediately after the message is received from the transport.
175      * this is really on input!!
176      * http://docs.jboss.org/jbossesb/docs/4.2MR3/javadoc/esb/index.html?org/jboss/soa/esb/filter/InputOutputFilter.html
177      * @param msg
178      * @param params
179      * @return
180      * @throws CourierException
181      */
182     public org.jboss.soa.esb.message.Message onOutput(org.jboss.soa.esb.message.Message msg,
183             java.util.Map<java.lang.String, java.lang.Object> params)
184             throws org.jboss.soa.esb.couriers.CourierException {
185         log.log(Level.INFO, "Current VM Memory : total = " + Runtime.getRuntime().totalMemory() + " free = " + Runtime.getRuntime().freeMemory());
186 //an inbound message could be a request from a client OR it could be a response from another service, handle this
187         
188         //MessageID: used to uniquely identify this message. OPTIONAL, therefore just make a new one
189         String id = UUID.randomUUID().toString();
190 
191 
192         msg.getProperties().setProperty("fgsms.TransactionID", id);
193 
194 
195         for (int i = 0; i < msg.getProperties().getNames().length; i++) {
196             log.log(Level.DEBUG, "JbossESBAgent key = " + msg.getProperties().getNames()[i] + " value = " + msg.getProperties().getProperty(msg.getProperties().getNames()[i]));
197         }
198         if (params != null) {
199             Iterator<String> iterator = params.keySet().iterator();
200             while (iterator.hasNext()) {
201                 String s = iterator.next();
202                 log.log(Level.DEBUG, "JbossESBAgent pkey = " + s + " pvalue = " + params.get(s).toString());
203             }
204         }
205 
206 
207         HttpRequest request = HttpRequest.getRequest(msg);
208 
209         //grab the original url from the gateway, useful but not fullproof
210         String url = (String) msg.getProperties().getProperty("org.jboss.soa.esb.gateway.original.url");
211 
212         
213         //if we get the rreques value here, there's no way to get the current connection's port, bummer.
214         
215         /*  if (Utility.stringIsNullOrEmpty(url)) {
216         if (request != null) {
217         
218         url = request.getScheme() + "://" + request.getServerName() + request.getRequestURI();
219         }
220         //   url = request.getProtocol() + request.getServerName() + request.getRequestURI() + request.getLocalAddr() + request.getLocalName()
221         //            + request.getMethod() + request.getPathInfo() + request.getScheme() ;
222         }*/
223 
224 
225         if (Utility.stringIsNullOrEmpty(url)) {
226             try {
227                 //alternate method, but should always work
228                 url = msg.getHeader().getCall().getTo().getURI().toString();
229                 if (url.startsWith("invm://")) {
230                     log.log(Level.INFO, "fgsms Agent for JbossESB, inbound message is internal esb traffic " + url);
231                     return msg;
232                 }
233                 log.log(Level.DEBUG, "fgsms Agent for JbossESB, inbound message for " + url + " via SOAP To field.");
234             } catch (Exception ex) {
235                 log.log(Level.WARN, "fgsms Agent for JbossESB, unable to determine the URL or TO field from the message, this message will be ignored");
236                 return msg;
237             }
238         } else {
239             log.log(Level.INFO, "fgsms Agent for JbossESB, inbound message for " + url + " via transport.");
240         }
241 
242 
243         Integer msgsize = null;
244         if (request != null) {
245             msgsize = request.getContentLength();
246             if (msgsize < 0) {
247                 msgsize = null;
248             } else {
249                 log.log(Level.DEBUG, "fgsms Agent for JbossESB, inbound message size " + msgsize + " via request object.");
250             }
251         }
252         if (msgsize == null) {
253             try {
254                 msgsize = (Integer) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size");
255                 log.log(Level.DEBUG, "fgsms Agent for JbossESB, inbound message size " + msgsize + " via long.");
256             } catch (Exception ex) {
257             }
258         }
259         if (msgsize == null) {
260             try {
261                 msgsize = Integer.parseInt((String) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size"));
262                 log.log(Level.DEBUG, "fgsms Agent for JbossESB, inbound message size " + msgsize + " via string.");
263             } catch (Exception ex) {
264             }
265         }
266 
267         if (msg.getAttachment().getNamedCount() > 0 || msg.getAttachment().getUnnamedCount() > 0) {
268             log.log(Level.DEBUG, "fgsms Agent for JbossESB, inbound message has attachments, named: " + msg.getAttachment().getNamedCount() + " unnamed:" + msg.getAttachment().getUnnamedCount());
269             int count = (msg.getAttachment().getNamedCount() + msg.getAttachment().getUnnamedCount());
270             for (int i = 0; i < count; i++) {
271                 log.log(Level.DEBUG, "fgsms Agent for JbossESB, inbound message has attachments " + i + " type: " + msg.getAttachment().itemAt(i).getClass().getCanonicalName());
272             }
273         }
274 
275 
276 
277         String action = null;
278         if (request != null) {
279             action=request.getHeaderValue("soapaction");
280             if (!Utility.stringIsNullOrEmpty(action)) {
281                 action = action.replaceAll("\"", "");
282                 action = action.replaceAll("\'", "");
283             }
284         }
285         if (Utility.stringIsNullOrEmpty(action)) {
286             action = (String) msg.getProperties().getProperty("soapaction");
287             if (!Utility.stringIsNullOrEmpty(action)) {
288                 action = action.replaceAll("\"", "");
289                 action = action.replaceAll("\'", "");
290             }
291         }
292         if (Utility.stringIsNullOrEmpty(action)) {
293 
294             //Action: used by the sender to indicate the semantics of the message. Must be unique. MANDATORY
295             if (msg != null && msg.getHeader() != null && msg.getHeader().getCall() != null
296                     && msg.getHeader().getCall().getAction() != null) {
297                 action = msg.getHeader().getCall().getAction().toString();
298                 if (!Utility.stringIsNullOrEmpty(action)) {
299                     action = action.replaceAll("\"", "");
300                     action = action.replaceAll("\'", "");
301                 }
302             }
303         }
304 
305         if (Utility.stringIsNullOrEmpty(action))
306         {
307             action = "urn:undeterminable";
308         }
309 
310         if (action==null)
311             action = "urn:undeterminable";
312          if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction3.toLowerCase().trim())) {
313             log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
314             return msg;     //prevent recursive loops
315         }
316         if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction4.toLowerCase().trim())) {
317             log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
318             return msg;     //prevent recursive loops
319         }
320 
321         if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction.toLowerCase().trim())) {
322             log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
323             return msg;     //prevent recursive loops
324         }
325         //if (action.equalsIgnoreCase("urn:org:miloss:fgsms:services:interfaces:policyConfiguration/policyConfigurationService/GetServicePolicy")) {
326         if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.PCSaction.toLowerCase().trim())) {
327             log.log(Level.DEBUG, "fgsms, skipping the request for PCS GetServicePolicy to prevent recursive looping. This is normal and no action is required.");
328             return msg;     //prevent recursive loops
329         }
330 
331         if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction2.toLowerCase().trim())) {
332             log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
333             return msg;     //prevent recursive loops
334         }
335         //if (action.equalsIgnoreCase("urn:org:miloss:fgsms:services:interfaces:policyConfiguration/policyConfigurationService/GetServicePolicy")) {
336         if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.PCSaction2.toLowerCase().trim())) {
337             log.log(Level.DEBUG, "fgsms, skipping the request for PCS GetServicePolicy to prevent recursive looping. This is normal and no action is required.");
338             return msg;     //prevent recursive loops
339         }
340 
341 
342         log.log(Level.DEBUG, "fgsms Agent for JbossESB, inbound message for " + url + " action " + action);
343 
344         String sender = "";
345 
346         //sender is optional
347         if (msg != null && msg.getHeader() != null && msg.getHeader().getCall() != null
348                 && msg.getHeader().getCall().getFrom() != null) {
349             sender = msg.getHeader().getCall().getFrom().toString();
350 
351         }
352         if (Utility.stringIsNullOrEmpty(sender) && request != null) {
353             request.getRemoteUser();
354         }
355 
356 
357         String body = "";
358         try {
359             //msg.getBody could be one of several interfaces, for right now, just stick to text bodies
360             ///org.jboss.internal.soa.esb.message.format.serialized.BodyImpl
361             //org.jboss.internal.soa.esb.message.format.xml.BodyImpl
362             if (msg.getBody() instanceof TextBody) {
363                 TextBody t = (TextBody) msg.getBody();
364                 if (t != null) {
365                     body = t.getText();
366                     body += t.get().toString();
367                 }
368             }
369             if (msg.getBody() instanceof org.jboss.internal.soa.esb.message.format.xml.BodyImpl) {
370                 org.jboss.internal.soa.esb.message.format.xml.BodyImpl t = (org.jboss.internal.soa.esb.message.format.xml.BodyImpl) msg.getBody();
371                 //body = new String(t.getContents());
372                 body = t.toString();// new String(t.getContents());
373                 log.log(Level.DEBUG, "body get is of type " + t.get().getClass().getCanonicalName());
374                 body += t.get().toString();
375             }
376             if (msg.getBody() instanceof org.jboss.internal.soa.esb.message.format.serialized.BodyImpl) {
377                 org.jboss.internal.soa.esb.message.format.serialized.BodyImpl t = (org.jboss.internal.soa.esb.message.format.serialized.BodyImpl) msg.getBody();
378                 body = t.toString();//new String(t.getContents());
379                 //body = new String(t.getContents());
380                 log.log(Level.DEBUG, "body get is of type " + t.get().getClass().getCanonicalName());
381                 body += t.get().toString();
382             }
383             // log.log(Level.WARN, "fgsms Agent for JbossESB, inbound message for " + url + " has body that I don't know how to identify of type " + msg.getBody().getClass().getName());
384         } catch (Exception ex) {
385             log.log(Level.WARN, "fgsms Agent for JbossESB, inbound message for " + url + " has body that I don't know how to identify of type " + msg.getBody().getClass().getName());
386         }
387 
388         if (Utility.stringIsNullOrEmpty(action)) {
389             action = Utility.getActionNameFromXML(body);
390             if (!Utility.stringIsNullOrEmpty(action)) {
391                 action = action.replaceAll("\"", "");
392                 action = action.replaceAll("\'", "");
393             }
394         }
395         if (Utility.stringIsNullOrEmpty(action)) {
396             if (request != null) {
397                 action = request.getMethod();
398             }
399             if (!Utility.stringIsNullOrEmpty(action)) {
400                 action = action.replaceAll("\"", "");
401                 action = action.replaceAll("\'", "");
402             }
403             //action = "urn:undeterminable";
404         }
405 
406 
407         if (Utility.stringIsNullOrEmpty(action)) {
408             action = "urn:undeterminable";
409         }
410 
411         String ip = null;
412         if (request != null) {
413             ip = request.getRemoteAddr();
414         }
415         if (msgsize == null) {
416             MessageProcessor.getSingletonObject().processMessageInput(body, body.length(), url, action, sender, id, null, ip, JbossESBAgent.class.getCanonicalName(), null, null);
417         } else {
418             MessageProcessor.getSingletonObject().processMessageInput(body, msgsize, url, action, sender, id, null, ip, JbossESBAgent.class.getCanonicalName(), null, null);
419         }
420         return msg;
421     }
422 }