Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/Tutorial/3.8/webservices/DatabaseLogHandler"

< Scout‎ | Tutorial‎ | 3.8
Line 41: Line 41:
 
             , new NVPair("port", StringUtility.nvl(context.get(SOAPMessageContext.WSDL_PORT), "?"))
 
             , new NVPair("port", StringUtility.nvl(context.get(SOAPMessageContext.WSDL_PORT), "?"))
 
             , new NVPair("operation", StringUtility.nvl(context.get(SOAPMessageContext.WSDL_OPERATION), "?"))
 
             , new NVPair("operation", StringUtility.nvl(context.get(SOAPMessageContext.WSDL_OPERATION), "?"))
             , new NVPair("request", soapMessage)
+
             , new NVPair("request", wellformXml(soapMessage))
 
             , new NVPair("evtDate", new Date())
 
             , new NVPair("evtDate", new Date())
 
             );
 
             );
Line 52: Line 52:
 
             "WHERE    WS_LOG_NR = :wsLogNr"
 
             "WHERE    WS_LOG_NR = :wsLogNr"
 
             , new NVPair("wsLogNr", wsLogNr)
 
             , new NVPair("wsLogNr", wsLogNr)
             , new NVPair("response", soapMessage)
+
             , new NVPair("response", wellformXml(soapMessage))
 
             );
 
             );
 
       }
 
       }
Line 60: Line 60:
 
     }
 
     }
 
   }
 
   }
}
 
  
 +
  private String wellformXml(String rawXml) {
 +
    try {
 +
      ScoutXmlDocument doc = new ScoutXmlDocument(rawXml);
 +
      doc.setPrettyPrint(true);
 +
      ByteArrayOutputStream os = new ByteArrayOutputStream();
 +
      doc.write(os);
 +
      return new String(os.toByteArray(), doc.getXmlEncoding());
 +
    }
 +
    catch (Exception e) {
 +
      LOG.warn("failed to wellform XML", e);
 +
    }
 +
    return rawXml;
 +
  }
 
</source>
 
</source>

Revision as of 12:01, 14 November 2011

@ScoutTransaction
public class DatabaseLogHandler extends LogHandler {
 
  private static final IScoutLogger LOG = ScoutLogManager.getLogger(DatabaseLogHandler.class);
 
  /**
   * Property to access the primary key of the log entry in the message context.
   */
  public static final String PROP_WS_LOG_NR = UUID.randomUUID().toString();
 
  @Override
  protected void handleLogMessage(DirectionType directionType, String soapMessage, SOAPMessageContext context) {
    try {
      /*
       * If this handler is invoked to post-process the SOAP-response, the SOAP-request was already logged and
       * the log's primary key put into the message context. In consequence, the log entry is updated with the SOAP response.
       */
      Long wsLogNr = (Long) context.get(PROP_WS_LOG_NR);
      if (wsLogNr == null) {
        // SOAP request
        wsLogNr = SQL.getSequenceNextval("GLOBAL_SEQ");
        context.put(PROP_WS_LOG_NR, wsLogNr);
 
        SQL.insert("" +
            "INSERT INTO   WS_LOG " +
            "             (WS_LOG_NR, " +
            "              EVT_DATE, " +
            "              SERVICE, " +
            "              PORT, " +
            "              OPERATION, " +
            "              REQUEST) " +
            "VALUES       (:wsLogNr, " +
            "              :evtDate, " +
            "              :service, " +
            "              :port, " +
            "              :operation, " +
            "              :request)"
            , new NVPair("wsLogNr", wsLogNr)
            , new NVPair("service", StringUtility.nvl(context.get(SOAPMessageContext.WSDL_SERVICE), "?"))
            , new NVPair("port", StringUtility.nvl(context.get(SOAPMessageContext.WSDL_PORT), "?"))
            , new NVPair("operation", StringUtility.nvl(context.get(SOAPMessageContext.WSDL_OPERATION), "?"))
            , new NVPair("request", wellformXml(soapMessage))
            , new NVPair("evtDate", new Date())
            );
      }
      else {
        // SOAP response
        SQL.update("" +
            "UPDATE   WS_LOG " +
            "SET      RESPONSE = :response " +
            "WHERE    WS_LOG_NR = :wsLogNr"
            , new NVPair("wsLogNr", wsLogNr)
            , new NVPair("response", wellformXml(soapMessage))
            );
      }
    }
    catch (ProcessingException e) {
      LOG.error("failed to persist webservice-log", e);
    }
  }
 
  private String wellformXml(String rawXml) {
    try {
      ScoutXmlDocument doc = new ScoutXmlDocument(rawXml);
      doc.setPrettyPrint(true);
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      doc.write(os);
      return new String(os.toByteArray(), doc.getXmlEncoding());
    }
    catch (Exception e) {
      LOG.warn("failed to wellform XML", e);
    }
    return rawXml;
  }

Back to the top