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.List;
9   import org.miloss.fgsms.agentcore.MessageProcessor;
10  import org.miloss.fgsms.common.Utility;
11  import org.apache.log4j.Level;
12  import org.miloss.fgsms.common.Logger;;
13  import org.jboss.soa.esb.ConfigurationException;
14  import org.jboss.soa.esb.actions.ActionLifecycleException;
15  import org.jboss.soa.esb.actions.ActionProcessingException;
16  import org.jboss.soa.esb.helpers.ConfigTree;
17  import org.jboss.soa.esb.http.HttpHeader;
18  import org.jboss.soa.esb.http.HttpRequest;
19  import org.jboss.soa.esb.http.HttpResponse;
20  import org.jboss.soa.esb.message.Message;
21  import org.jboss.soa.esb.message.body.content.TextBody;
22  import org.miloss.fgsms.agentcore.IMessageProcessor;
23  
24  /**
25   * For Jboss ESB deployments. Use this for AFTER all processing has been
26   * completed in the deployment but before returning
27   *
28   * @author AO
29   */
30  public class JbossESBProxyAfterAction implements org.jboss.soa.esb.actions.ActionPipelineProcessor {
31  
32      private Logger log;
33  
34      public JbossESBProxyAfterAction(ConfigTree config) throws ConfigurationException {
35          log = Logger.getLogger(org.miloss.fgsms.common.Constants.LoggerName);
36          httpport = config.getAttribute("http_port", "80");
37          httpsport = config.getAttribute("https_port", "443");
38  
39      }
40      private String httpport = "80";
41      private String httpsport = "443";
42  
43      public Message process(Message msg) throws ActionProcessingException {
44          
45          log.log(Level.DEBUG, "Current VM Memory : total = " + Runtime.getRuntime().totalMemory() + " free = " + Runtime.getRuntime().freeMemory());
46  
47          for (int i = 0; i < msg.getProperties().getNames().length; i++) {
48              log.log(Level.DEBUG, "key = " + msg.getProperties().getNames()[i] + " value = " + msg.getProperties().getProperty(msg.getProperties().getNames()[i]));
49          }
50  
51          String id = "";
52          try {
53              id = (String) msg.getProperties().getProperty("fgsms.TransactionID");
54          } catch (Exception ex) {
55              log.log(Level.ERROR, "fgsms Agent for JbossESBPostProxyAgent, could not obtain the transaction id, this was unexpected." + ex.getLocalizedMessage());
56              return msg;
57          }
58          if (Utility.stringIsNullOrEmpty(id)) {
59              log.log(Level.ERROR, "fgsms Agent for JbossESBPostProxyAgent, outbound message does not have transaction id, this was unexpected.");
60              return msg;
61          }
62  
63          HttpRequest request = HttpRequest.getRequest(msg);
64          HttpResponse response = HttpResponse.getResponse(msg);
65          HashMap headers = new HashMap();
66          String relatedTransaction = "";
67          //grab the original url from the gateway, useful but not fullproof
68          String url = (String) msg.getProperties().getProperty("org.jboss.soa.esb.gateway.original.url");
69          if (Utility.stringIsNullOrEmpty(url)) {
70              if (request != null) {
71                  //this is here because there's no other way to get the information
72                  if (request.getScheme().equalsIgnoreCase("http")) {
73                      url = request.getScheme() + "://" + request.getServerName() + ":" + httpport + request.getRequestURI();
74                  } else if (request.getScheme().equalsIgnoreCase("https")) {
75                      url = request.getScheme() + "://" + request.getServerName() + ":" + httpsport + request.getRequestURI();
76                  }
77              }
78          }
79  
80          if (Utility.stringIsNullOrEmpty(url)) {
81              try {
82                  //alternate method, but should always work
83                  url = msg.getHeader().getCall().getTo().getURI().toString();
84                  if (url.startsWith("invm://")) {
85                      log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, response message is internal esb traffic " + url);
86                      return msg;
87                  }
88                  log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, response message for " + url + " via SOAP To field.");
89              } catch (Exception ex) {
90                  log.log(Level.DEBUG, "error caught determining the url of the response message", ex);
91              }
92          }
93  
94          if (Utility.stringIsNullOrEmpty(url)) {
95              log.log(Level.WARN, "untable to determine request url, message will be ignored.");
96              return msg;
97          }
98  
99  
100         Long msgsize = null;
101         try {
102             Integer i = (Integer) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size");
103             if (i != null) {
104                 msgsize = i.longValue();
105             }
106             if (msgsize != null && msgsize < 0) {
107                 msgsize = null;
108             }
109             if (msgsize != null) {
110                 log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message size " + msgsize + " via long.");
111             }
112         } catch (Exception ex) {
113         }
114         if (msgsize == null) {
115             try {
116                 msgsize = Long.parseLong((String) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size"));
117                 if (msgsize != null) {
118                     log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message size " + msgsize + " via string.");
119                 }
120             } catch (Exception ex) {
121             }
122         }
123         if (msgsize == null) {
124             try {
125                 msgsize = response.getLength();
126                 if (msgsize != null && msgsize < 0) {
127                     msgsize = null;
128                 }
129                 if (msgsize != null) {
130                     log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message size " + msgsize + " via response object.");
131                 }
132             } catch (Exception ex) {
133             }
134         }
135 
136         boolean fault = false;
137         //getFault 'should' be for comms error, not app error
138         if (msg.getFault() != null) {
139             if (!Utility.stringIsNullOrEmpty(msg.getFault().getReason())
140                     || msg.getFault().getCode() != null
141                     || msg.getFault().getCause() != null) //faulted msg
142             {
143                 fault = true;
144                 log.log(Level.WARN, "fgsms, this message to " + url + " transaction id:" + id.toString() + " has faulted.");
145             }
146         }
147 
148 //        Long dod = (Long) params.get("org.jboss.soa.esb.message.time.dod");
149         //      if (dod == null) {
150         Long dod = System.currentTimeMillis();
151         //    }
152 
153         if (msg.getAttachment().getNamedCount() > 0 || msg.getAttachment().getUnnamedCount() > 0) {
154             log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message has attachments, named: " + msg.getAttachment().getNamedCount() + " unnamed:" + msg.getAttachment().getUnnamedCount());
155             int count = (msg.getAttachment().getNamedCount() + msg.getAttachment().getUnnamedCount());
156             for (int i = 0; i < count; i++) {
157                 log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message has attachments " + i + " type: " + msg.getAttachment().itemAt(i).getClass().getCanonicalName());
158             }
159         }
160         String threadid = MessageProcessor.getSingletonObject().getTransactionThreadId(Thread.currentThread().getId());
161         if (response != null) {
162             if (MessageProcessor.getSingletonObject().isDependencyInjectionEnabled()) {
163                 List<HttpHeader> httpHeaders = response.getHttpHeaders();
164                 boolean foundthread = false;
165                 int relatedIdx = -1;
166                 if (headers != null) {
167                     for (int i = 0; i < httpHeaders.size(); i++) {
168                         if (httpHeaders.get(i).getName().equalsIgnoreCase(org.miloss.fgsms.common.Constants.transactionthreadKey)) {
169                             foundthread = true;
170                         }
171                         if (httpHeaders.get(i).getName().equalsIgnoreCase(org.miloss.fgsms.common.Constants.relatedtransactionKey)) {
172                             relatedTransaction = httpHeaders.get(i).getValue();
173                             relatedIdx = -1;
174                         }
175                     }
176                     if (relatedIdx > -1) {
177                         httpHeaders.remove(relatedIdx);
178                     }
179                 }
180                 
181 
182 
183                 //response.addHeader(new HttpHeader(org.miloss.fgsms.common.Constants.relatedtransactionKey, (String) msg.getProperties().getProperty(org.miloss.fgsms.common.Constants.relatedtransactionKey)));
184                 response.addHeader(new HttpHeader(org.miloss.fgsms.common.Constants.relatedtransactionKey, id));
185                 if (!foundthread) {
186                     response.addHeader(new HttpHeader(org.miloss.fgsms.common.Constants.transactionthreadKey, threadid));
187                 }
188             }
189             for (int i = 0; i < response.getHttpHeaders().size(); i++) {
190                 headers.put(response.getHttpHeaders().get(i).getName(), response.getHttpHeaders().get(i).getValue());
191             }
192 
193         }
194         String body = "";
195         if (MessageProcessor.getSingletonObject().shouldAgentRecordResponseContent(url)) {
196             try {
197                 if (msg.getBody() instanceof TextBody) {
198                     TextBody t = (TextBody) msg.getBody();
199                     if (t != null) {
200                         //  log.log(Level.WARN, "*******getText()**********");
201                         //   log.log(Level.WARN, t.getText());
202                         //   log.log(Level.WARN, "*******get().toString()***********");
203                         //   log.log(Level.WARN, t.get().toString());
204                         // body = t.getText();
205                         body += t.get().toString();
206                     }
207                 }
208                 if (msg.getBody() instanceof org.jboss.internal.soa.esb.message.format.xml.BodyImpl) {
209                     org.jboss.internal.soa.esb.message.format.xml.BodyImpl t = (org.jboss.internal.soa.esb.message.format.xml.BodyImpl) msg.getBody();
210                     //body = new String(t.getContents());
211                     //  body = t.toString();// new String(t.getContents());
212                     log.log(Level.DEBUG, "body get is of type " + t.get().getClass().getCanonicalName());
213                     body += t.get().toString();
214                 }
215                 if (msg.getBody() instanceof org.jboss.internal.soa.esb.message.format.serialized.BodyImpl) {
216                     org.jboss.internal.soa.esb.message.format.serialized.BodyImpl t = (org.jboss.internal.soa.esb.message.format.serialized.BodyImpl) msg.getBody();
217                     // body = t.toString();//new String(t.getContents());
218                     //body = new String(t.getContents());
219                     log.log(Level.DEBUG, "body get is of type " + t.get().getClass().getCanonicalName());
220                     body += t.get().toString();
221                 }
222                 log.log(Level.DEBUG, "outbound messsage for " + url + " has a body of type " + msg.getBody().getClass().getName());
223             } catch (Exception ex) {
224                 log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message for " + url + " has a non-text body", ex);
225 
226             }
227         }
228 
229         if (msgsize == null) {
230             MessageProcessor.getSingletonObject().processMessageOutput(id, body, body.length(), fault, Long.valueOf(dod), headers, relatedTransaction);
231         } else {
232             MessageProcessor.getSingletonObject().processMessageOutput(id, body, msgsize.intValue(), fault, Long.valueOf(dod), headers);
233         }
234         return msg;
235     }
236 
237     @Override
238     public void processException(Message msg, Throwable thrwbl) {
239 
240         log.log(Level.DEBUG, "Current VM Memory : total = " + Runtime.getRuntime().totalMemory() + " free = " + Runtime.getRuntime().freeMemory());
241 
242 
243         for (int i = 0; i < msg.getProperties().getNames().length; i++) {
244             log.log(Level.DEBUG, "key = " + msg.getProperties().getNames()[i] + " value = " + msg.getProperties().getProperty(msg.getProperties().getNames()[i]));
245         }
246 
247         String id = "";
248         try {
249             id = (String) msg.getProperties().getProperty("fgsms.TransactionID");
250         } catch (Exception ex) {
251             log.log(Level.ERROR, "fgsms Agent for JbossESBPostProxyAgent, could not obtain the transaction id, this was unexpected." + ex.getLocalizedMessage());
252             return;// msg;
253         }
254         if (Utility.stringIsNullOrEmpty(id)) {
255             log.log(Level.ERROR, "fgsms Agent for JbossESBPostProxyAgent, outbound message does not have transaction id, this was unexpected.");
256             return;// msg;
257         }
258 
259         HttpRequest request = HttpRequest.getRequest(msg);
260         HttpResponse response = HttpResponse.getResponse(msg);
261         HashMap headers = new HashMap();
262 
263         //grab the original url from the gateway, useful but not fullproof
264         String url = (String) msg.getProperties().getProperty("org.jboss.soa.esb.gateway.original.url");
265         if (Utility.stringIsNullOrEmpty(url)) {
266             if (request != null) {
267 
268                 if (request.getScheme().equalsIgnoreCase("http")) {
269                     url = request.getScheme() + "://" + request.getServerName() + ":" + httpport + request.getRequestURI();
270                 } else if (request.getScheme().equalsIgnoreCase("https")) {
271                     url = request.getScheme() + "://" + request.getServerName() + ":" + httpsport + request.getRequestURI();
272                 }
273             }
274 
275         }
276 
277         if (Utility.stringIsNullOrEmpty(url)) {
278             try {
279                 //alternate method, but should always work
280                 url = msg.getHeader().getCall().getTo().getURI().toString();
281                 if (url.startsWith("invm://")) {
282                     log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, inbound message is internal esb traffic " + url);
283                     return;
284                 }
285                 log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, inbound message for " + url + " via SOAP To field.");
286             } catch (Exception ex) {
287 //                log.log(Level.WARN, "fgsms Agent for JbossESBPostProxyAgent, unable to determine the URL or TO field from the message, this message will be ignored");
288                 //               return msg;
289             }
290         }
291 
292         if (Utility.stringIsNullOrEmpty(url)) {
293             log.log(Level.WARN, "untable to determine request url, message will be ignored.");
294             return;
295         }
296 
297 
298         Long msgsize = null;
299         try {
300             Integer i = (Integer) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size");
301             if (i != null) {
302                 msgsize = i.longValue();
303             }
304             if (msgsize != null) {
305                 log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message size " + msgsize + " via getPropertyInt.");
306             }
307         } catch (Exception ex) {
308             log.log(Level.DEBUG, "error caught determining message size", ex);
309         }
310         if (msgsize == null) {
311             try {
312                 msgsize = Long.parseLong((String) msg.getProperties().getProperty("org.jboss.soa.esb.message.byte.size"));
313                 if (msgsize != null) {
314                     log.log(Level.INFO, "fgsms Agent for JbossESBPostProxyAgent, outbound message size " + msgsize + " via getPropertyLong.");
315                 }
316             } catch (Exception ex) {
317                 log.log(Level.DEBUG, "error caught determining message size", ex);
318             }
319         }
320         if (msgsize == null) {
321             try {
322                 msgsize = response.getLength();
323                 if (msgsize != null) {
324                     log.log(Level.INFO, "fgsms Agent for JbossESBPostProxyAgent, outbound message size " + msgsize + " via response object getLength.");
325                 }
326             } catch (Exception ex) {
327                 log.log(Level.DEBUG, "error caught determining message size", ex);
328             }
329         }
330 
331         log.log(Level.DEBUG, "fgsms, this message to " + url + " transaction id:" + id.toString() + " has faulted.");
332 
333         Long dod = System.currentTimeMillis();
334 
335         if (msg.getAttachment().getNamedCount() > 0 || msg.getAttachment().getUnnamedCount() > 0) {
336             log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message has attachments, named: " + msg.getAttachment().getNamedCount() + " unnamed:" + msg.getAttachment().getUnnamedCount());
337             int count = (msg.getAttachment().getNamedCount() + msg.getAttachment().getUnnamedCount());
338             for (int i = 0; i < count; i++) {
339                 log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message has attachments " + i + " type: " + msg.getAttachment().itemAt(i).getClass().getCanonicalName());
340             }
341         }
342 
343 
344         if (response != null) {
345             if (!response.getHttpHeaders().isEmpty()) {
346                 for (int i = 0; i < response.getHttpHeaders().size(); i++) {
347                     headers.put(response.getHttpHeaders().get(i).getName(), response.getHttpHeaders().get(i).getValue());
348                 }
349             }
350             String threadid = MessageProcessor.getSingletonObject().getTransactionThreadId(Thread.currentThread().getId());
351             if (MessageProcessor.getSingletonObject().isDependencyInjectionEnabled()) {
352                 //response.addHeader(new HttpHeader(org.miloss.fgsms.common.Constants.relatedtransactionKey, (String) msg.getProperties().getProperty(org.miloss.fgsms.common.Constants.relatedtransactionKey)));
353                 response.addHeader(new HttpHeader(org.miloss.fgsms.common.Constants.relatedtransactionKey, id));
354                 response.addHeader(new HttpHeader(org.miloss.fgsms.common.Constants.transactionthreadKey, threadid));
355             }
356             for (int i = 0; i < response.getHttpHeaders().size(); i++) {
357                 headers.put(response.getHttpHeaders().get(i).getName(), response.getHttpHeaders().get(i).getValue());
358             }
359         }
360 
361         String body = "";
362         if (MessageProcessor.getSingletonObject().shouldAgentRecordResponseContent(url)) {
363             try {
364                 body = thrwbl.getMessage();
365                 body += thrwbl.toString();
366             } catch (Exception ex) {
367                 log.log(Level.DEBUG, "fgsms Agent for JbossESBPostProxyAgent, outbound message for " + url + " has a non-text body", ex);
368             }
369         }
370         IMessageProcessor mp = MessageProcessor.getSingletonObject();
371         if (msgsize == null) {
372             MessageProcessor.getSingletonObject().processMessageOutput(id, body, body.length(), true, (dod), headers);
373         } else {
374             MessageProcessor.getSingletonObject().processMessageOutput(id, body, msgsize.intValue(), true, (dod), headers);
375         }
376         // return msg;
377     }
378 
379     public void processSuccess(Message msg) {
380     }
381 
382     public void initialise() throws ActionLifecycleException {
383     }
384 
385     public void destroy() throws ActionLifecycleException {
386     }
387 }