FileDocCategorySizeDatePackage
MessageTraceFactory.javaAPI DocGlassfish v2 API11920Fri May 04 22:24:42 BST 2007com.sun.enterprise.admin.wsmgmt.msg

MessageTraceFactory

public class MessageTraceFactory extends Object
Factory class to that generates a message trace object from the SOAP invocation events.

Fields Summary
private Map
_messages
private static final MessageTraceFactory
_instance
private Pattern
_msgBodyPattern
private static final String
HTTP_REQUEST
private static final Logger
_logger
private static final com.sun.enterprise.util.i18n.StringManager
_stringMgr
Constructors Summary
private MessageTraceFactory()
Constructor.

        _messages = new HashMap();
        _msgBodyPattern = Pattern.compile("<env:Body>.*</env:Body>");
    
Methods Summary
static com.sun.enterprise.admin.wsmgmt.msg.MessageTraceFactorygetInstance()
Returns the singleton instance.

return
singleton instance of this class

        return _instance;
    
private java.io.ByteArrayOutputStreamgetMsgAsBytes(com.sun.enterprise.admin.wsmgmt.SOAPMessageContext smc)
Returns the SOAP message as an output stream.


        ByteArrayOutputStream baos = null;

        if (smc!= null) {
            baos = new ByteArrayOutputStream();
            try {
                smc.getMessage().writeTo(baos);
            } catch (Exception e) {
                String msg = "Error while retrieving SOAP message";
                _logger.log(Level.FINE, msg, e);
            }
        }

        return baos;
    
com.sun.appserv.management.ext.wsmgmt.MessageTracepostProcessResponse(com.sun.enterprise.admin.wsmgmt.filter.spi.FilterContext ctx)
Post Processing of the SOAP response.

param
ctx filter context


        String messageId = ctx.getMessageId();

        com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace = 
            (com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl) 
            _messages.remove(messageId);

        // sets response time
        setResponseTime(trace, ctx.getExecutionTime());

        return trace;
    
voidprocessRequest(com.sun.enterprise.admin.wsmgmt.filter.spi.FilterContext ctx, java.lang.String appId)
Processes the SOAP request.

param
ctx filter context


        // message id
        String messageId = ctx.getMessageId();
        com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace = new
        com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl(messageId,
        MessageTrace.CLASS_NAME); 

        // sets the time stamp
        trace.setTimeStamp( System.currentTimeMillis() );

        // set application id, FQ endpoint name
        trace.setEndpointName( ctx.getFullyQualifiedName() );
        trace.setApplicationID(appId);
        trace.setCallFlowEnabled(ctx.isCallFlowEnabled());

        // set request content, size and http header 
        setRequestInfo(ctx.getMessageContext(), trace);

        // set transport type
        setTransportType(ctx, trace);

        // sets client host
        setClientHost(ctx.getMessageContext(), trace);

        _messages.put(messageId, trace);
    
voidprocessResponse(com.sun.enterprise.admin.wsmgmt.filter.spi.FilterContext ctx)
Processes the SOAP response.

param
ctx filter context


        String messageId = ctx.getMessageId();

        com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace = 
            (com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl) 
            _messages.get(messageId);

        SOAPMessageContext mctx = ctx.getMessageContext();

        // sets response content, size and message header
        setResponseInfo(mctx, trace);

        // sets caller principal
        setCallerPrincipal(trace);

        // sets fault code, fault string and fault actor
        setFaultInfo(mctx, trace);

    
private voidsetCallerPrincipal(com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace)
Sets caller principal information.


        SecurityContext sc = SecurityContext.getCurrent();
        if (sc != null) {
            Principal p = sc.getCallerPrincipal();
            if (p != null) {
                trace.setPrincipalName( p.getName() );
                _logger.fine("[MTF] Caller Principal: " + p.getName());
            }
        }
    
private voidsetClientHost(com.sun.enterprise.admin.wsmgmt.SOAPMessageContext smc, com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace)
Sets client host.


        if (smc!= null) {
            Iterator iter = smc.getPropertyNames();
            if ( iter == null) {
                return;
            }
             CoyoteRequestFacade httpReq =
                  (CoyoteRequestFacade) smc.getProperty(HTTP_REQUEST);
             String clientHost = null;
             if (httpReq != null) {
                 clientHost = httpReq.getRemoteAddr();
                  trace.setClientHost(clientHost);
            }
        }
    
private voidsetFaultInfo(com.sun.enterprise.admin.wsmgmt.SOAPMessageContext smc, com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace)
Sets faults information for a response.


        if (smc != null) {
            SOAPMessage sm = smc.getMessage();
            try {
                if (sm != null) {
                    SOAPBody sb = sm.getSOAPBody();
                    if (sb != null) {
                        SOAPFault fault = sb.getFault();

                        if ( fault != null) {
                            trace.setFaultCode( fault.getFaultCode() );
                            trace.setFaultString( fault.getFaultString() );
                            trace.setFaultActor( fault.getFaultString() );
                        }
                    }
                }
            } catch (SOAPException se) {
                String msg = "Error while reading SOAP fault information";
                _logger.log(Level.FINE, msg, se);
            }
        }
    
private voidsetRequestInfo(com.sun.enterprise.admin.wsmgmt.SOAPMessageContext ctx, com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace)
Sets SOAP message content, request size and HTTP headers.


        ByteArrayOutputStream baos = null;
        try {
            // sets the SOAP message header 
            trace.setHTTPRequestHeaders(ctx.getHTTPRequestHeaders());
            baos = getMsgAsBytes(ctx);

            // sets the request size
            trace.setRequestSize( baos.size() );
            String soapMsg = baos.toString();
            trace.setRequestContent(soapMsg);

        } catch (Exception e) {
            String msg =_stringMgr.getString(
                "MessageTraceFactory_NoMessageTrace", e.getMessage());
            _logger.log(Level.INFO, msg);
        }finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e) { 
                _logger.log(Level.FINE, "Web Service message stream could not be closed : " + e.getMessage());
            }
        }
    
private voidsetResponseInfo(com.sun.enterprise.admin.wsmgmt.SOAPMessageContext ctx, com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace)
Sets the response content, size and message header


        ByteArrayOutputStream baos = null;
        try {
            baos = getMsgAsBytes(ctx);

            if (baos == null) {
                return;
            }

            // sets the request size
            trace.setResponseSize( baos.size() );
            String soapMsg = baos.toString();
            trace.setResponseContent(soapMsg);

            // sets the SOAP message header 
            trace.setHTTPResponseHeaders(ctx.getHTTPResponseHeaders());
        } catch (Exception e) {
            String msg =_stringMgr.getString(
                "MessageTraceFactory_NoMessageTrace", e.getMessage());
            _logger.log(Level.INFO, msg);
        }finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e) {
                _logger.log(Level.FINE, "Web Service message stream could not be closed : " + e.getMessage());
            }
        }
    
private voidsetResponseTime(com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace, long t)
Sets response time for this request.


        trace.setResponseTime(t);
    
private voidsetTransportType(com.sun.enterprise.admin.wsmgmt.filter.spi.FilterContext ctx, com.sun.appserv.management.ext.wsmgmt.MessageTraceImpl trace)
Sets the transport type.


        TransportInfo ti = ctx.getTransportInfo();
        if (ti != null) {
            TransportType type = ti.getTransportType();
            trace.setTransportType( type.name() );
        }