cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sean O'Callaghan" <SEA...@progress.com>
Subject RE: svn commit: r736332 - in /cxf/trunk/rt/core: pom.xml src/main/java/org/apache/cxf/interceptor/PrettyLoggingOutInterceptor.java
Date Thu, 22 Jan 2009 10:51:57 GMT
Thanks for that David.

Seán.


-----Original Message-----
From: David Bosschaert [mailto:david.bosschaert@gmail.com] 
Sent: 21 January 2009 20:28
To: dev@cxf.apache.org
Subject: Re: svn commit: r736332 - in /cxf/trunk/rt/core: pom.xml src/main/java/org/apache/cxf/interceptor/PrettyLoggingOutInterceptor.java

You should be able to do something similar with an Dom Level 3
LSSerializer that comes with the JRE. This also has a pretty print
function.

The code needed would roughly look something like this:
  DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
  DOMImplementationLS domLS = (DOMImplementationLS)
registry.getDOMImplementation("LS");
  LSParser lsParser =
domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

  LSInput lsInput = domLS.createLSInput();
  lsInput.setButeStream(cos.getInputStream());
  org.w3c.dom.Document doc = lsParser.parse(lsInput);

  LSSerializer lsSerializer = domLS.createLSSerializer();
  DOMConfiguration config = lsSerializer.getDomConfig();
  config.setParameter("format-pretty-print", true);
        	
  lsSerializer.writeToString(doc.getDocumentElement());

Cheers,

David

2009/1/21 Daniel Kulp <dkulp@apache.org>:
>
> Is there a way to get this done without introducing the JDOM dependency?
> Benson just spent a lot of work to remove the jdom dependency throughout the
> code and this just adds it back in.
>
> Dan
>
>
> On Wednesday 21 January 2009 11:34:18 am seanoc@apache.org wrote:
>> Author: seanoc
>> Date: Wed Jan 21 08:34:17 2009
>> New Revision: 736332
>>
>> URL: http://svn.apache.org/viewvc?rev=736332&view=rev
>> Log:
>> CXF-1327 Logging Interceptor with pretty formatting
>>
>> Added:
>>
>> cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOut
>>Interceptor.java Modified:
>>     cxf/trunk/rt/core/pom.xml
>>
>> Modified: cxf/trunk/rt/core/pom.xml
>> URL:
>> http://svn.apache.org/viewvc/cxf/trunk/rt/core/pom.xml?rev=736332&r1=736331
>>&r2=736332&view=diff
>> ===========================================================================
>>=== --- cxf/trunk/rt/core/pom.xml (original)
>> +++ cxf/trunk/rt/core/pom.xml Wed Jan 21 08:34:17 2009
>> @@ -77,6 +77,12 @@
>>           <artifactId>FastInfoset</artifactId>
>>           <version>1.2.2</version>
>>          </dependency>
>> +
>> +       <dependency>
>> +         <groupId>jdom</groupId>
>> +         <artifactId>jdom</artifactId>
>> +         <version>1.0</version>
>> +        </dependency>
>>
>>      </dependencies>
>>
>>
>> Added:
>> cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOut
>>Interceptor.java URL:
>> http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf
>>/interceptor/PrettyLoggingOutInterceptor.java?rev=736332&view=auto
>> ===========================================================================
>>=== ---
>> cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOut
>>Interceptor.java (added) +++
>> cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/PrettyLoggingOut
>>Interceptor.java Wed Jan 21 08:34:17 2009 @@ -0,0 +1,88 @@
>> +/**
>> + * Licensed to the Apache Software Foundation (ASF) under one
>> + * or more contributor license agreements. See the NOTICE file
>> + * distributed with this work for additional information
>> + * regarding copyright ownership. The ASF licenses this file
>> + * to you under the Apache License, Version 2.0 (the
>> + * "License"); you may not use this file except in compliance
>> + * with the License. You may obtain a copy of the License at
>> + *
>> + * http://www.apache.org/licenses/LICENSE-2.0
>> + *
>> + * Unless required by applicable law or agreed to in writing,
>> + * software distributed under the License is distributed on an
>> + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
>> + * KIND, either express or implied. See the License for the
>> + * specific language governing permissions and limitations
>> + * under the License.
>> + */
>> +
>> +package org.apache.cxf.interceptor;
>> +
>> +import java.io.OutputStream;
>> +import java.io.StringWriter;
>> +import java.util.logging.Level;
>> +import java.util.logging.Logger;
>> +
>> +import org.apache.cxf.common.logging.LogUtils;
>> +import org.apache.cxf.io.CacheAndWriteOutputStream;
>> +import org.apache.cxf.io.CachedOutputStream;
>> +import org.apache.cxf.io.CachedOutputStreamCallback;
>> +import org.apache.cxf.message.Message;
>> +import org.apache.cxf.phase.AbstractPhaseInterceptor;
>> +import org.apache.cxf.phase.Phase;
>> +import org.jdom.Document;
>> +import org.jdom.input.SAXBuilder;
>> +import org.jdom.output.Format;
>> +import org.jdom.output.XMLOutputter;
>> +
>> +/**
>> + *
>> + */
>> +public class PrettyLoggingOutInterceptor extends AbstractPhaseInterceptor
>> { +
>> +    private static final Logger LOG =
>> LogUtils.getL7dLogger(PrettyLoggingOutInterceptor.class); +
>> +    private SAXBuilder saxBuilder = new SAXBuilder();
>> +    private XMLOutputter xmlOutputter = new XMLOutputter();
>> +
>> +    public PrettyLoggingOutInterceptor() {
>> +        super(Phase.PRE_STREAM);
>> +        addBefore(StaxOutInterceptor.class.getName());
>> +    }
>> +
>> +    public void handleMessage(Message message) throws Fault {
>> +        final OutputStream os = message.getContent(OutputStream.class);
>> +        if (os == null) {
>> +            return;
>> +        }
>> +        if (!LOG.isLoggable(Level.ALL)) {
>> +            return;
>> +        }
>> +
>> +//     Write the output while caching it for the log message
>> +        final CacheAndWriteOutputStream newOut = new
>> CacheAndWriteOutputStream(os); +
>> message.setContent(OutputStream.class, newOut);
>> +        newOut.registerCallback(new LoggingCallback());
>> +    }
>> +
>> +    class LoggingCallback implements CachedOutputStreamCallback {
>> +
>> +        public void onFlush(CachedOutputStream cos) {
>> +
>> +        }
>> +
>> +        public void onClose(CachedOutputStream cos) {
>> +
>> +            try {
>> +                Document jdoCument =
>> saxBuilder.build(cos.getInputStream()); +
>> xmlOutputter.setFormat(Format.getPrettyFormat()); +
>> StringWriter writer = new StringWriter();
>> +                xmlOutputter.output(jdoCument, writer);
>> +                LOG.info(writer.getBuffer().toString());
>> +            } catch (Exception e) {
>> +                LOG.severe("fatal parsing the SOAP message " +
>> e.getMessage()); +            }
>> +        }
>> +    }
>> +}
>> \ No newline at end of file
>
>
>
> --
> Daniel Kulp
> dkulp@apache.org
> http://dankulp.com/blog
>

Mime
View raw message