8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: RemoteFormatter.java,v 1.9 2009/08/19 21:12:50 ww203982 Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.log.handlers;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.logging.Formatter;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.log.LogConstants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.log.LogManagerUtil;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.log.ILogRecord;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.log.spi.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The formatter which prepares the xml string to be sent to a remote log
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * service. This xml string conforms to the logDTD.xml file which is given
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * below.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <?XML version="1.0">
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!--This DTD is used by Logging operation-->
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!DOCTYPE logging [
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT logRecWrite (log, logRecord*)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT log>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ATTLIST log
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * logName CDATA #REQUIRED
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * sid CDATA #REQUIRED
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * >
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT logRecord (level ,msg ,logType? , logInfo? ,parameters?)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT level (#PCDATA)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT recMsg (#PCDATA)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT logType (#PCDATA)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT logInfoMap (logInfo*)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT logInfo (key ,infoValue)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT key (#PCDATA)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT infoValue (#PCDATA)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT parameters (parameter*)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT parameter (index ,paramValue)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT index (#PCDATA)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <!ELEMENT paramValue (#PCDATA)>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * ]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class RemoteFormatter extends Formatter {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The method which does the actual formatting of the LogRecord.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param logRecord The logRecord to be formatted
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return The string formed by formatting the logRecord
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String format(java.util.logging.LogRecord logRecord) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map logInfo = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((LogManagerUtil.isAMLoggingMode()) &&
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (logRecord instanceof ILogRecord)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster logInfo = ((com.sun.identity.log.ILogRecord) logRecord)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getLogInfoMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String logName = logRecord.getLoggerName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String loggedBySid = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (logInfo != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster loggedBySid = (String)logInfo.get(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster LogConstants.LOGGED_BY_SID);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (loggedBySid == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (Debug.warningEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Debug.warning("RemoteFormatter : returning null" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " because logRecord doesn't have loggedBySid");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object [] parameters = logRecord.getParameters();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuffer xml = new StringBuffer();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("<logRecWrite><log logName=\"");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(logName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("\" sid=\"");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(loggedBySid);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("\"></log><logRecord><level>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(logRecord.getLevel().intValue());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</level><recMsg>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = formatMessage(logRecord);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((msg == null) || (msg.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster msg = LogConstants.NOTAVAIL;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster msg = com.sun.identity.shared.encode.Base64.encode(msg.getBytes());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(msg);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</recMsg>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (logInfo != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set keys = logInfo.keySet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator keysIter = keys.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("<logInfoMap>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (keysIter.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String)keysIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("<logInfo> <infoKey>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(key);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</infoKey><infoValue>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String infoValue = (String)logInfo.get(key);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key.equalsIgnoreCase(LogConstants.DATA)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster infoValue = com.sun.identity.shared.encode.Base64.encode(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster infoValue.getBytes());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(infoValue);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</infoValue></logInfo>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</logInfoMap>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((parameters != null) && (parameters.length > 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("<parameters>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i=0; i<parameters.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("<parameter><paramIndex>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(String.valueOf(i));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</paramIndex><paramValue>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append(parameters[i].toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</paramValue></parameter>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</parameters>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster xml.append("</logRecord></logRecWrite>");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (Debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Debug.message("RemoteFormatter: XML Req string = " + xml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return xml.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}