1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.miloss.fgsms.agents;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.InputStream;
24 import java.io.StringWriter;
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 import java.util.*;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.xml.bind.JAXB;
31 import javax.xml.namespace.QName;
32
33 import org.apache.cxf.binding.soap.Soap11;
34 import org.apache.cxf.binding.soap.Soap12;
35 import org.miloss.fgsms.agentcore.MessageProcessor;
36 import org.miloss.fgsms.agentcore.FgsmsSoapHeaderConstants;
37 import org.miloss.fgsms.common.Utility;
38 import org.apache.cxf.Bus;
39 import org.apache.cxf.BusFactory;
40 import org.apache.cxf.binding.Binding;
41 import org.apache.cxf.binding.soap.SoapHeader;
42 import org.apache.cxf.databinding.DataBinding;
43 import org.apache.cxf.endpoint.Endpoint;
44 import org.apache.cxf.headers.Header;
45 import org.apache.cxf.jaxb.JAXBDataBinding;
46 import org.apache.cxf.message.Exchange;
47 import org.apache.cxf.message.Message;
48 import org.apache.cxf.service.Service;
49 import org.apache.cxf.service.model.BindingInfo;
50 import org.apache.cxf.service.model.BindingMessageInfo;
51 import org.apache.cxf.service.model.BindingOperationInfo;
52 import org.apache.cxf.transport.Destination;
53 import org.apache.cxf.transport.http.AbstractHTTPDestination;
54 import org.apache.log4j.Level;
55 import org.miloss.fgsms.agentcore.IMessageProcessor;
56 import org.miloss.fgsms.common.Constants;
57 import org.miloss.fgsms.common.Logger;;
58 import org.w3c.dom.Element;
59
60
61
62
63
64
65
66
67 public class CXFCommonMessageHandler {
68
69 static boolean DEBUG = false;
70 static final Logger log = Logger.getLogger(org.miloss.fgsms.common.Constants.LoggerName);
71
72
73
74
75
76
77 static void ProcessRequest(Message messageContext, boolean client, String classname) {
78 if (DEBUG) {
79 System.out.println("Current VM Memory : total = " + Runtime.getRuntime().totalMemory() + " free = " + Runtime.getRuntime().freeMemory());
80 System.out.println("Message Processor " + MessageProcessor.getSingletonObject().getPolicyCache() + "," + MessageProcessor.getSingletonObject().internalMessageMapSize() +","+ MessageProcessor.getSingletonObject().outboundQueueSize());
81 }
82 log.log(Level.DEBUG, "Current VM Memory : total = " + Runtime.getRuntime().totalMemory() + " free = " + Runtime.getRuntime().freeMemory());
83 long start = System.currentTimeMillis();
84
85
86 BindingMessageInfo bi = (BindingMessageInfo) messageContext.get("org.apache.cxf.service.model.BindingMessageInfo");
87 boolean isoneway = false;
88 if (bi != null) {
89 BindingOperationInfo bindingOperation = bi.getBindingOperation();
90 if (bindingOperation != null) {
91 BindingMessageInfo output = bindingOperation.getOutput();
92 if (output == null) {
93 isoneway = true;
94 }
95 }
96 }
97
98 if (messageContext.containsKey("org.apache.cxf.interceptor.OneWayProcessorInterceptor")) {
99 isoneway = true;
100 }
101
102
103 UUID id = UUID.randomUUID();
104 messageContext.getExchange().put(org.miloss.fgsms.common.Constants.key, id);
105
106 try {
107 IMessageProcessor mp = MessageProcessor.getSingletonObject();
108 } catch (Exception ex) {
109 log.log(Level.ERROR, "Unable to get a reference to the Message Processor singleton object", ex);
110 }
111 String transactionthreadid = "";
112 HttpServletRequest ctx = null;
113 try {
114 ctx = (HttpServletRequest) messageContext.get(AbstractHTTPDestination.HTTP_REQUEST);
115 } catch (Exception ex) {
116 }
117
118 log.log(Level.DEBUG, "fgsms message context is of type " + messageContext.getClass().getName());
119
120 String action = org.miloss.fgsms.common.Constants.Undetermineable;
121 java.util.Map headers = new HashMap();
122 try {
123 Map m = (Map) messageContext.get(Message.MIME_HEADERS);
124 headers.putAll(m);
125 } catch (Exception ex) {
126 }
127 try {
128 headers.putAll((Map) messageContext.get(Message.PROTOCOL_HEADERS));
129 } catch (Exception ex) {
130 }
131 try {
132 List l = (List) headers.get("SOAPAction");
133 if (l != null && !l.isEmpty()) {
134 action = (String) l.get(0);
135 }
136 l = (List) headers.get("Soapaction");
137 if (l != null && !l.isEmpty()) {
138 action = (String) l.get(0);
139 }
140 } catch (Exception ex) {
141 log.log(Level.WARN, "error getting outbound soap action", ex);
142 }
143
144 if (action != null) {
145 action = action.replace("\"", "");
146 action = action.replace("'", "");
147 } else {
148 action = org.miloss.fgsms.common.Constants.Undetermineable;
149 }
150 if (action==null || action.trim().length()==0)
151 action = org.miloss.fgsms.common.Constants.Undetermineable;
152
153
154 if (action.equals(org.miloss.fgsms.common.Constants.Undetermineable)) {
155 try {
156 BindingOperationInfo bindingOperationInfo = messageContext.getExchange().getBindingOperationInfo();
157 if (bindingOperationInfo != null && bindingOperationInfo.getOperationInfo() != null) {
158 action = (bindingOperationInfo.getOperationInfo().getName().toString());
159 }
160 } catch (Exception ex) {
161 }
162 }
163 boolean isrest = false;
164
165 if (Utility.stringIsNullOrEmpty(action) || action.equals(org.miloss.fgsms.common.Constants.Undetermineable)) {
166 Exchange exchange = messageContext.getExchange();
167
168 try {
169 action = (String) exchange.get("org.apache.cxf.resource.operation.name");
170 } catch (Exception ex) {
171 }
172 if (Utility.stringIsNullOrEmpty(action)) {
173 action = org.miloss.fgsms.common.Constants.Undetermineable;
174 }
175 Binding binding = exchange.getBinding();
176 BindingInfo bindingInfo = binding.getBindingInfo();
177 if (binding.getBindingInfo().getBindingId().equalsIgnoreCase("http://apache.org/cxf/binding/jaxrs")) {
178 isrest = true;
179 }
180
181
182
183
184
185
186
187
188
189
190 }
191
192 if (action.equals(org.miloss.fgsms.common.Constants.Undetermineable) && ctx != null) {
193 try {
194
195 action = ctx.getHeader("SOAPAction");
196 if (Utility.stringIsNullOrEmpty(action)) {
197 action = org.miloss.fgsms.common.Constants.Undetermineable;
198 }
199 action = action.replace("\"", "");
200 action = action.replace("'", "");
201 if (Utility.stringIsNullOrEmpty(action)) {
202 action = org.miloss.fgsms.common.Constants.Undetermineable;
203 }
204
205
206
207
208 } catch (Exception ex) {
209 log.log(Level.WARN, "fgsms error getting context object " + ex.getLocalizedMessage());
210 }
211 }
212 if (action.equals(org.miloss.fgsms.common.Constants.Undetermineable)) {
213 try {
214 action = (String) messageContext.get(messageContext.WSDL_OPERATION);
215 action = action.replace("\"", "");
216 action = action.replace("'", "");
217 if (Utility.stringIsNullOrEmpty(action)) {
218 action = org.miloss.fgsms.common.Constants.Undetermineable;
219 }
220 } catch (Exception ex) {
221 }
222 }
223 if (Utility.stringIsNullOrEmpty(action)) {
224 action = org.miloss.fgsms.common.Constants.Undetermineable;
225 }
226 action = action.replace("\"", "");
227 action = action.replace("'", "");
228
229 if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction3.toLowerCase().trim())) {
230 log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
231 return;
232 }
233 if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction4.toLowerCase().trim())) {
234 log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
235 return;
236 }
237
238 if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction.toLowerCase().trim())) {
239 log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
240 return;
241 }
242
243 if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.PCSaction.toLowerCase().trim())) {
244 log.log(Level.DEBUG, "fgsms, skipping the request for PCS GetServicePolicy to prevent recursive looping. This is normal and no action is required.");
245 return;
246 }
247
248 if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.DCSaction2.toLowerCase().trim())) {
249 log.log(Level.DEBUG, "fgsms, skipping the request for DCS AddData to prevent recursive looping. This is normal and no action is required.");
250 return;
251 }
252
253 if (action.toLowerCase().trim().equals(org.miloss.fgsms.common.Constants.PCSaction2.toLowerCase().trim())) {
254 log.log(Level.DEBUG, "fgsms, skipping the request for PCS GetServicePolicy to prevent recursive looping. This is normal and no action is required.");
255 return;
256 }
257
258 String requestURL = "";
259 if (ctx != null) {
260 try {
261 StringBuffer buff = ctx.getRequestURL();
262 requestURL = buff.toString();
263 } catch (Exception ex) {
264 log.log(Level.WARN, "fgsms error getting request url " + ex.getLocalizedMessage());
265 requestURL = "";
266 }
267
268 }
269 if (Utility.stringIsNullOrEmpty(requestURL)) {
270 try {
271
272 requestURL = (String) messageContext.get("org.apache.cxf.request.url");
273 if (Utility.stringIsNullOrEmpty(requestURL)) {
274 requestURL = (String) messageContext.get(Message.ENDPOINT_ADDRESS);
275 }
276 } catch (Exception ex) {
277 log.log(Level.WARN, "fgsms error getting request url " + ex.getLocalizedMessage());
278 requestURL = "";
279 }
280 }
281 if ((Utility.stringIsNullOrEmpty(requestURL)) && ctx != null) {
282 try {
283 StringBuffer buff = ctx.getRequestURL();
284 requestURL = buff.toString();
285 } catch (Exception ex) {
286 log.log(Level.WARN, "fgsms error getting request url " + ex.getLocalizedMessage());
287 requestURL = "";
288
289 }
290 }
291 if (!client) {
292 if (isrest) {
293
294
295 requestURL = (String) messageContext.get("http.base.path");
296 requestURL += (String) messageContext.get("org.apache.cxf.message.Message.BASE_PATH");
297 }
298
299
300 }
301
302 if (Utility.stringIsNullOrEmpty(requestURL)) {
303 requestURL = "urn:" + MessageProcessor.getSingletonObject().getHostName() + ":undeterminable";
304 }
305
306 ByteArrayOutputStream b = new ByteArrayOutputStream();
307 String messagexml = "";
308 if (MessageProcessor.getSingletonObject().shouldAgentRecordRequestContent(requestURL))
309 {
310
311 try {
312
313 boolean gotPayload = false;
314 InputStream buff = messageContext.getContent(InputStream.class);
315 if (buff == null) {
316 messagexml = "";
317 } else {
318 StringBuilder sb = new StringBuilder();
319 byte[] buffer = new byte[1024];
320 int len = buff.read(buffer);
321 while (len > 0) {
322 String data = new String(buffer, 0, len, Constants.CHARSET);
323 sb.append(data);
324 len = buff.read(buffer);
325 }
326 if (buff.markSupported()) {
327 buff.reset();
328 }
329 messagexml = sb.toString();
330 if (messagexml.length()>0)
331 gotPayload = true;
332 }
333 if (!gotPayload) {
334 try {
335 List list = messageContext.getContent(List.class);
336 if (list != null) {
337 for (int i = 0; i < list.size(); i++) {
338
339
340
341 StringWriter sw = new StringWriter();
342 JAXB.marshal(list.get(i), sw);
343 messagexml = sw.toString();
344 gotPayload = true;
345 break;
346 }
347 }
348 } catch (Exception e) {
349 log.log(Level.WARN, "fgsms, error obtaining request message.", e);
350 }
351 }
352 if (!gotPayload) {
353 log.log(Level.INFO, "Payload not obtainable");
354 }
355 } catch (Exception ex) {
356 log.log(Level.WARN, "fgsms, error obtaining request message.", ex);
357 messagexml = "fgsms was unable to obtain the request message.";
358 }
359 }
360
361 if (action.equals(org.miloss.fgsms.common.Constants.Undetermineable)) {
362 action = Utility.getActionNameFromXML(messagexml);
363 if (Utility.stringIsNullOrEmpty(action)) {
364 action = org.miloss.fgsms.common.Constants.Undetermineable;
365 }
366 action = action.replace("\"", "");
367 action = action.replace("'", "");
368 if (Utility.stringIsNullOrEmpty(action)) {
369 action = org.miloss.fgsms.common.Constants.Undetermineable;
370 }
371 }
372 if (action.equals(org.miloss.fgsms.common.Constants.Undetermineable)) {
373 action = (String) messageContext.get(messageContext.HTTP_REQUEST_METHOD);
374 if (Utility.stringIsNullOrEmpty(action)) {
375 action = org.miloss.fgsms.common.Constants.Undetermineable;
376 }
377 action = action.replace("\"", "");
378 action = action.replace("'", "");
379 if (Utility.stringIsNullOrEmpty(action)) {
380 action = org.miloss.fgsms.common.Constants.Undetermineable;
381 }
382 }
383
384 log.log(Level.DEBUG, "fgsms Agent for CXF, inbound message for " + requestURL + " action " + action);
385
386
387
388 String user = "";
389
390 try {
391 if (ctx != null) {
392 user = ctx.getRemoteUser();
393 }
394 if (ctx != null && ctx.getAuthType() != null && ctx.getUserPrincipal() != null) {
395
396 user += ctx.getUserPrincipal().getName();
397 }
398 } catch (Exception ex) {
399 log.log(Level.WARN, "fgsms error getting user principal " + ex.getLocalizedMessage());
400 }
401
402
403 String ipaddress = "";
404 if (client) {
405
406 transactionthreadid = MessageProcessor.getSingletonObject().getTransactionThreadId(Thread.currentThread().getId());
407 if (Utility.stringIsNullOrEmpty(transactionthreadid)) {
408
409 try {
410 transactionthreadid = UUID.randomUUID().toString();
411 } catch (Exception ex) {
412 log.log(Level.WARN, "error caught build transaction thread id", ex);
413 }
414 }
415
416 if (headers == null) {
417 headers = new HashMap<String, List<String>>();
418 }
419 if (MessageProcessor.getSingletonObject().isDependencyInjectionEnabled()) {
420 Map realheaders = (Map) messageContext.get(Message.PROTOCOL_HEADERS);
421 if (realheaders == null) {
422 realheaders = new HashMap();
423 }
424 ArrayList t = new ArrayList();
425 t.add(transactionthreadid);
426 realheaders.put(org.miloss.fgsms.common.Constants.transactionthreadKey, t);
427 t = new ArrayList();
428 t.add(id.toString());
429 realheaders.put(org.miloss.fgsms.common.Constants.relatedtransactionKey, t);
430
431
432 messageContext.remove(Message.PROTOCOL_HEADERS);
433 messageContext.put(Message.PROTOCOL_HEADERS, realheaders);
434 try {
435 List<Header> soapheaders = (List<Header>) messageContext.get(Header.HEADER_LIST);
436 if (soapheaders != null) {
437 DataBinding db = new JAXBDataBinding(String.class);
438 soapheaders.add(new SoapHeader(new QName(FgsmsSoapHeaderConstants.namespace, FgsmsSoapHeaderConstants.related_message_localname), id.toString(), db));
439 soapheaders.add(new SoapHeader(new QName(FgsmsSoapHeaderConstants.namespace, FgsmsSoapHeaderConstants.threadid_message_localname), transactionthreadid, db));
440 }
441 } catch (Exception ex) {
442 log.log(Level.DEBUG, null, ex);
443 }
444 }
445
446 try {
447 InetAddress addr = InetAddress.getLocalHost();
448 ipaddress = addr.getHostAddress();
449 } catch (UnknownHostException ex) {
450 log.log(Level.ERROR, "error obtaining local ip address", ex);
451 }
452 } else if (ctx != null) {
453 ipaddress = ctx.getRemoteAddr();
454 }
455 String relatedTransaction = null;
456
457 if (!client) {
458
459
460
461
462 Map realheaders = (Map) messageContext.get(Message.PROTOCOL_HEADERS);
463 if (realheaders != null) {
464 if (realheaders.containsKey(org.miloss.fgsms.common.Constants.relatedtransactionKey)) {
465 relatedTransaction = ((List<String>) realheaders.get(org.miloss.fgsms.common.Constants.relatedtransactionKey)).get(0);
466 }
467 if (realheaders.containsKey(org.miloss.fgsms.common.Constants.transactionthreadKey)) {
468 transactionthreadid = ((List<String>) realheaders.get(org.miloss.fgsms.common.Constants.transactionthreadKey)).get(0);
469 }
470 }
471 List<Header> soapheaders = (List<Header>) messageContext.get(Header.HEADER_LIST);
472 if (soapheaders != null && !soapheaders.isEmpty()) {
473
474
475 for (int k = 0; k < soapheaders.size(); k++) {
476 if (soapheaders.get(k).getName().getNamespaceURI().equalsIgnoreCase(FgsmsSoapHeaderConstants.namespace)
477 && soapheaders.get(k).getName().getLocalPart().equalsIgnoreCase(FgsmsSoapHeaderConstants.related_message_localname)) {
478 if (Utility.stringIsNullOrEmpty(relatedTransaction)) {
479 relatedTransaction = soapheaders.get(k).getObject().toString();
480 }
481 } else if (soapheaders.get(k).getName().getNamespaceURI().equalsIgnoreCase(FgsmsSoapHeaderConstants.namespace)
482 && soapheaders.get(k).getName().getLocalPart().equalsIgnoreCase(FgsmsSoapHeaderConstants.threadid_message_localname)) {
483 if (Utility.stringIsNullOrEmpty(transactionthreadid)) {
484 transactionthreadid = soapheaders.get(k).getObject().toString();
485 }
486 }
487 }
488
489 }
490
491 if (transactionthreadid == null) {
492 transactionthreadid = UUID.randomUUID().toString();
493 }
494 try {
495 MessageProcessor.getSingletonObject().setTransactionThreadId(Thread.currentThread().getId(), transactionthreadid);
496 } catch (Exception ex) {
497 }
498 }
499
500 HashMap headervalues = new HashMap();
501 if (ctx != null) {
502 try {
503 Enumeration<String> headersnames = ctx.getHeaderNames();
504
505 while (headersnames.hasMoreElements()) {
506 String s = headersnames.nextElement();
507 headervalues.put(s, ctx.getHeader(s));
508
509 }
510 } catch (Exception ex) {
511 log.log(Level.DEBUG, "fgsms error getting http headers " + ex.getMessage(),ex);
512 }
513 }
514 headervalues.putAll(headers);
515
516
517 if (ctx != null) {
518 try {
519 String query = ctx.getQueryString();
520 if (query != null && (query == null ? "null" != null : !query.equals("null"))) {
521 requestURL += "?" + query;
522 }
523 } catch (Exception ex) {
524 log.log(Level.WARN, "fgsms error getting request query string " + ex.getLocalizedMessage());
525 }
526 }
527
528 messageContext.getExchange().put(org.miloss.fgsms.common.Constants.urlKEY, requestURL);
529
530 log.log(Level.DEBUG, "fgsms Message intercepted, this is a request message to " + requestURL + " with the SOAPAction of " + action + ". Assigning message id:" + id.toString());
531
532
533 try {
534 MessageProcessor.getSingletonObject().processMessageInput(messagexml, messagexml.length(), requestURL, action, user, id.toString(), headervalues, ipaddress, classname, relatedTransaction, transactionthreadid);
535 } catch (Exception ex) {
536 log.log(Level.ERROR, "Unable to get a reference to the MessageProcessor.processMessageInput", ex);
537 }
538 if (isoneway) {
539
540 messageContext.put(org.miloss.fgsms.common.Constants.oneway, true);
541 ProcessResponse(messageContext, false, client, classname);
542 }
543 log.log(Level.DEBUG, "estimated additional latency " + (System.currentTimeMillis() - start));
544
545 }
546
547 static void ProcessResponse(Message messageContext, boolean fault, boolean client, String classname) {
548
549 if (DEBUG) {
550 System.out.println("Current VM Memory : total = " + Runtime.getRuntime().totalMemory() + " free = " + Runtime.getRuntime().freeMemory());
551 System.out.println("Message Processor " + MessageProcessor.getSingletonObject().getPolicyCache() + "," + MessageProcessor.getSingletonObject().internalMessageMapSize() +","+ MessageProcessor.getSingletonObject().outboundQueueSize());
552 }
553
554
555
556 Bus bus = BusFactory.getDefaultBus();
557 long start = System.currentTimeMillis();
558
559 UUID id = (UUID) messageContext.getExchange().get(org.miloss.fgsms.common.Constants.key);
560 boolean isoneway = messageContext.containsKey(org.miloss.fgsms.common.Constants.oneway);
561
562 String requrl = (String) messageContext.getExchange().get(org.miloss.fgsms.common.Constants.urlKEY);
563
564 boolean ok = true;
565 if (id == null) {
566 log.log(Level.DEBUG, "fgsms reply message did not have context variable " + org.miloss.fgsms.common.Constants.key + " added. This transaction will be ignored.");
567 ok = false;
568 }
569 if (Utility.stringIsNullOrEmpty(requrl)) {
570 log.log(Level.DEBUG, "fgsms reply message did not have context variable " + org.miloss.fgsms.common.Constants.urlKEY + " added. This transaction will be ignored.");
571 ok = false;
572 }
573 bus.getProperties().remove(org.miloss.fgsms.common.Constants.key);
574 bus.getProperties().remove(org.miloss.fgsms.common.Constants.urlKEY);
575
576 if (!ok) {
577 if (id != null) {
578 MessageProcessor.getSingletonObject().removeFromQueue(id);
579 }
580 return;
581 }
582
583 java.util.HashMap headervalues = new HashMap();
584 try {
585 Map m = (Map) messageContext.get(Message.MIME_HEADERS);
586 headervalues.putAll(m);
587 } catch (Exception ex) {
588 }
589 try {
590 headervalues.putAll((Map) messageContext.get(Message.PROTOCOL_HEADERS));
591 } catch (Exception ex) {
592 }
593 try{
594 HttpServletResponse get = (HttpServletResponse) messageContext.get(AbstractHTTPDestination.HTTP_RESPONSE);
595 if (get!=null) {
596 Iterator<String> iterator = get.getHeaderNames().iterator();
597 while (iterator.hasNext()) {
598 String key=iterator.next();
599 String val=get.getHeader(key);
600 if (val!=null)
601 headervalues.put(key, val);
602 }
603 }
604 }catch (Exception ex) {
605
606 }
607 String relatedTransaction = null;
608 {
609 List<Header> soapheaders = (List<Header>) messageContext.get(Header.HEADER_LIST);
610 if (soapheaders != null) {
611 for (int k = 0; k < soapheaders.size(); k++) {
612 if (soapheaders.get(k).getName().getNamespaceURI().equalsIgnoreCase(FgsmsSoapHeaderConstants.namespace)
613 && soapheaders.get(k).getName().getLocalPart().equalsIgnoreCase(FgsmsSoapHeaderConstants.related_message_localname)) {
614 if (Utility.stringIsNullOrEmpty(relatedTransaction)) {
615 if (soapheaders.get(k).getObject() instanceof String) {
616 relatedTransaction = (String) soapheaders.get(k).getObject();
617 } else if (soapheaders.get(k).getObject() instanceof Element) {
618 Element e = (Element) soapheaders.get(k).getObject();
619 relatedTransaction = e.getTextContent();
620 } else {
621 log.debug("unhandled cases for related transaction id " + soapheaders.get(k).getObject().getClass().getCanonicalName());
622 }
623 }
624 }
625 }
626 }
627 }
628
629 if (client && !isoneway) {
630
631 try {
632 Map headers = (Map) messageContext.get(Message.PROTOCOL_HEADERS);
633 if (headers != null) {
634 log.log(Level.DEBUG, "client received " + headers.size() + " transaction " + id.toString());
635 Iterator it = headers.keySet().iterator();
636 while (it.hasNext()) {
637 String s = (String) it.next();
638 log.log(Level.DEBUG, " client header " + s + " = " + headers.get(s).toString());
639 headervalues.put(s, headers.get(s).toString());
640 }
641 }
642
643 } catch (Exception ex) {
644 log.log(Level.ERROR, "Unexpected error caught when searching for fgsms soap header", ex);
645 }
646 }
647
648 if (!client && !isoneway) {
649
650 ArrayList<String> l = new ArrayList<String>();
651 l.add(id.toString());
652 ArrayList<String> threadlist = new ArrayList<String>();
653 threadlist.add(MessageProcessor.getSingletonObject().getTransactionThreadId(Thread.currentThread().getId()));
654 Map headers = (Map) messageContext.get(Message.PROTOCOL_HEADERS);
655 if (MessageProcessor.getSingletonObject().isDependencyInjectionEnabled()) {
656
657 List<Header> soapheaders = (List<Header>) messageContext.get(Header.HEADER_LIST);
658 try {
659 DataBinding b = new JAXBDataBinding(String.class);
660 if (soapheaders != null) {
661 soapheaders.add(new SoapHeader(new QName(FgsmsSoapHeaderConstants.namespace, FgsmsSoapHeaderConstants.related_message_localname), id.toString(), b));
662 soapheaders.add(new SoapHeader(new QName(FgsmsSoapHeaderConstants.namespace, FgsmsSoapHeaderConstants.threadid_message_localname), MessageProcessor.getSingletonObject().getTransactionThreadId(Thread.currentThread().getId()), b));
663 }
664 } catch (Exception ex) {
665 log.log(Level.WARN, null, ex);
666 }
667 if (headers != null) {
668 headers.put(org.miloss.fgsms.common.Constants.relatedtransactionKey, l);
669 } else {
670 headers = new HashMap<String, List<String>>();
671 headers.put(org.miloss.fgsms.common.Constants.relatedtransactionKey, l);
672 headers.put(org.miloss.fgsms.common.Constants.transactionthreadKey, threadlist);
673 messageContext.remove(Message.PROTOCOL_HEADERS);
674 messageContext.put(messageContext.PROTOCOL_HEADERS, headers);
675
676 }
677 }
678 if (headers != null) {
679 Iterator it = headers.keySet().iterator();
680 while (it.hasNext()) {
681 String s = (String) it.next();
682 log.log(Level.DEBUG, " service response header " + s + " = " + headers.get(s).toString());
683 headervalues.put(s, headers.get(s).toString());
684 }
685 }
686
687 }
688
689 String messagexml = "";
690 if (!isoneway) {
691 try {
692 boolean gotPayload = false;
693 InputStream buff = messageContext.getContent(InputStream.class);
694 if (buff != null) {
695 StringBuilder sb = new StringBuilder();
696 byte[] buffer = new byte[1024];
697 int len = buff.read(buffer);
698 if (len < 0) {
699 gotPayload = false;
700 } else {
701 while (len > 0) {
702 String data = new String(buffer, 0, len, Constants.CHARSET);
703 sb.append(data);
704 len = buff.read(buffer);
705 }
706 if (buff.markSupported()) {
707 buff.reset();
708 }
709 messagexml = sb.toString();
710 gotPayload = true;
711 }
712 }
713 if (!gotPayload) {
714 List list = messageContext.getContent(List.class);
715 if (list != null) {
716 for (int i = 0; i < list.size(); i++) {
717 StringWriter sw = new StringWriter();
718 JAXB.marshal(list.get(i), sw);
719 messagexml = sw.toString();
720 gotPayload = true;
721 break;
722 }
723 }
724 }
725 if (!gotPayload && messageContext.containsKey("org.apache.cxf.binding.soap.SoapVersion")) {
726 Object obj = messageContext.get("org.apache.cxf.binding.soap.SoapVersion");
727 if (obj instanceof Soap11) {
728 StringWriter sw = new StringWriter();
729 JAXB.marshal(obj, sw);
730 messagexml = sw.toString();
731 gotPayload = true;
732 } else if (obj instanceof Soap12) {
733 StringWriter sw = new StringWriter();
734 JAXB.marshal(obj, sw);
735 messagexml = sw.toString();
736 gotPayload = true;
737 }
738 }
739 if (!gotPayload) {
740 log.log(Level.WARN, "fgsms, couldn't get payload.");
741 }
742 } catch (Exception ex) {
743 log.log(Level.WARN, "fgsms, error obtaining request message.", ex);
744 messagexml = "fgsms was unable to obtain the request message.";
745 }
746 } else {
747
748 }
749
750 String code2 = "200";
751
752 try {
753 Object j = messageContext.get(Message.RESPONSE_CODE).toString();
754 if (j != null) {
755 code2 = j.toString();
756 if (!code2.equalsIgnoreCase("200")) {
757 fault = true;
758 }
759 headervalues.put("RESPONSE_CODE", code2);
760 }
761 } catch (Exception ex) {
762
763 }
764
765
766 log.log(Level.DEBUG, "fgsms Message intercepted, this is a response message transaction id:" + id.toString() + " fault=" + fault + " " + classname);
767
768
769 MessageProcessor.getSingletonObject().processMessageOutput(id.toString(), messagexml, messagexml.length(), fault, Long.valueOf(System.currentTimeMillis()), headervalues, relatedTransaction);
770
771 if (!client) {
772 MessageProcessor.getSingletonObject().clearTransactionThreadId(Thread.currentThread().getId());
773 }
774 log.log(Level.DEBUG, "estimated additional latency " + (System.currentTimeMillis() - start));
775 }
776 }