1 23 package com.sun.enterprise.admin.wsmgmt.msg; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 import java.security.Principal ; 31 import java.io.ByteArrayOutputStream ; 32 import javax.xml.soap.SOAPMessage ; 33 import javax.xml.soap.SOAPBody ; 34 import javax.xml.soap.SOAPFault ; 35 import com.sun.enterprise.security.SecurityContext; 36 import com.sun.appserv.management.ext.wsmgmt.MessageTrace; 37 import com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl; 38 import com.sun.enterprise.admin.wsmgmt.filter.spi.FilterContext; 39 import com.sun.enterprise.webservice.monitoring.TransportInfo; 40 import com.sun.enterprise.webservice.monitoring.TransportType; 41 42 import javax.xml.soap.SOAPException ; 43 44 import java.util.logging.Logger ; 45 import java.util.logging.Level ; 46 import com.sun.logging.LogDomains; 47 import com.sun.enterprise.util.i18n.StringManager; 48 import com.sun.enterprise.admin.wsmgmt.SOAPMessageContext; 49 import org.apache.coyote.tomcat5.CoyoteRequestFacade; 50 52 56 class MessageTraceFactory { 57 58 63 static MessageTraceFactory getInstance() { 64 if (_instance == null) { 65 _instance = new MessageTraceFactory(); 66 } 67 68 return _instance; 69 } 70 71 74 private MessageTraceFactory() { 75 _messages = new HashMap (); 76 _msgBodyPattern = Pattern.compile("<env:Body>.*</env:Body>"); 77 } 78 79 84 void processRequest(FilterContext ctx, String appId) { 85 86 String messageId = ctx.getMessageId(); 88 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace = new 89 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl(messageId, 90 MessageTrace.CLASS_NAME); 91 92 trace.setTimeStamp( System.currentTimeMillis() ); 94 95 trace.setEndpointName( ctx.getFullyQualifiedName() ); 97 trace.setApplicationID(appId); 98 trace.setCallFlowEnabled(ctx.isCallFlowEnabled()); 99 100 setRequestInfo(ctx.getMessageContext(), trace); 102 103 setTransportType(ctx, trace); 105 106 setClientHost(ctx.getMessageContext(), trace); 108 109 _messages.put(messageId, trace); 110 } 111 112 115 private void setClientHost(SOAPMessageContext smc, 116 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) { 117 118 if (smc!= null) { 119 Iterator iter = smc.getPropertyNames(); 120 if ( iter == null) { 121 return; 122 } 123 CoyoteRequestFacade httpReq = 124 (CoyoteRequestFacade) smc.getProperty(HTTP_REQUEST); 125 String clientHost = null; 126 if (httpReq != null) { 127 clientHost = httpReq.getRemoteAddr(); 128 trace.setClientHost(clientHost); 129 } 130 } 131 } 132 133 136 private void setRequestInfo(SOAPMessageContext ctx, 137 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) { 138 139 ByteArrayOutputStream baos = null; 140 try { 141 trace.setHTTPRequestHeaders(ctx.getHTTPRequestHeaders()); 143 baos = getMsgAsBytes(ctx); 144 145 trace.setRequestSize( baos.size() ); 147 String soapMsg = baos.toString(); 148 trace.setRequestContent(soapMsg); 149 150 } catch (Exception e) { 151 String msg =_stringMgr.getString( 152 "MessageTraceFactory_NoMessageTrace", e.getMessage()); 153 _logger.log(Level.INFO, msg); 154 }finally { 155 try { 156 if (baos != null) { 157 baos.close(); 158 } 159 } catch (Exception e) { 160 _logger.log(Level.FINE, "Web Service message stream could not be closed : " + e.getMessage()); 161 } 162 } 163 } 164 165 168 private void setTransportType(FilterContext ctx, 169 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) { 170 171 TransportInfo ti = ctx.getTransportInfo(); 172 if (ti != null) { 173 TransportType type = ti.getTransportType(); 174 trace.setTransportType( type.name() ); 175 } 176 } 177 178 183 void processResponse(FilterContext ctx) { 184 185 String messageId = ctx.getMessageId(); 186 187 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace = 188 (com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl) 189 _messages.get(messageId); 190 191 SOAPMessageContext mctx = ctx.getMessageContext(); 192 193 setResponseInfo(mctx, trace); 195 196 setCallerPrincipal(trace); 198 199 setFaultInfo(mctx, trace); 201 202 } 203 204 209 MessageTrace postProcessResponse(FilterContext ctx) { 210 211 String messageId = ctx.getMessageId(); 212 213 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace = 214 (com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl) 215 _messages.remove(messageId); 216 217 setResponseTime(trace, ctx.getExecutionTime()); 219 220 return trace; 221 } 222 223 226 private void setFaultInfo(SOAPMessageContext smc, 227 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) { 228 229 if (smc != null) { 230 SOAPMessage sm = smc.getMessage(); 231 try { 232 if (sm != null) { 233 SOAPBody sb = sm.getSOAPBody(); 234 if (sb != null) { 235 SOAPFault fault = sb.getFault(); 236 237 if ( fault != null) { 238 trace.setFaultCode( fault.getFaultCode() ); 239 trace.setFaultString( fault.getFaultString() ); 240 trace.setFaultActor( fault.getFaultString() ); 241 } 242 } 243 } 244 } catch (SOAPException se) { 245 String msg = "Error while reading SOAP fault information"; 246 _logger.log(Level.FINE, msg, se); 247 } 248 } 249 } 250 251 254 private void setCallerPrincipal( 255 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) { 256 257 SecurityContext sc = SecurityContext.getCurrent(); 258 if (sc != null) { 259 Principal p = sc.getCallerPrincipal(); 260 if (p != null) { 261 trace.setPrincipalName( p.getName() ); 262 _logger.fine("[MTF] Caller Principal: " + p.getName()); 263 } 264 } 265 } 266 267 270 private void setResponseTime( 271 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace, long t) { 272 273 trace.setResponseTime(t); 274 } 275 276 279 private ByteArrayOutputStream getMsgAsBytes(SOAPMessageContext smc) { 280 281 ByteArrayOutputStream baos = null; 282 283 if (smc!= null) { 284 baos = new ByteArrayOutputStream (); 285 try { 286 smc.getMessage().writeTo(baos); 287 } catch (Exception e) { 288 String msg = "Error while retrieving SOAP message"; 289 _logger.log(Level.FINE, msg, e); 290 } 291 } 292 293 return baos; 294 } 295 296 299 private void setResponseInfo(SOAPMessageContext ctx, 300 com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace) { 301 302 ByteArrayOutputStream baos = null; 303 try { 304 baos = getMsgAsBytes(ctx); 305 306 if (baos == null) { 307 return; 308 } 309 310 trace.setResponseSize( baos.size() ); 312 String soapMsg = baos.toString(); 313 trace.setResponseContent(soapMsg); 314 315 trace.setHTTPResponseHeaders(ctx.getHTTPResponseHeaders()); 317 } catch (Exception e) { 318 String msg =_stringMgr.getString( 319 "MessageTraceFactory_NoMessageTrace", e.getMessage()); 320 _logger.log(Level.INFO, msg); 321 }finally { 322 try { 323 if (baos != null) { 324 baos.close(); 325 } 326 } catch (Exception e) { 327 _logger.log(Level.FINE, "Web Service message stream could not be closed : " + e.getMessage()); 328 } 329 } 330 } 331 332 private Map _messages = null; 334 private static MessageTraceFactory _instance = null; 335 private Pattern _msgBodyPattern = null; 336 private static final String HTTP_REQUEST = 337 "com.sun.xml.rpc.server.http.HttpServletRequest"; 338 private static final Logger _logger = 339 Logger.getLogger(LogDomains.ADMIN_LOGGER); 340 private static final StringManager _stringMgr = 341 StringManager.getManager(MessageTraceFactory.class); 342 } 343 | Popular Tags |