1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.support.handlers;
14
15 import com.eviware.soapui.impl.rest.support.MediaTypeHandler;
16 import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
17 import com.eviware.soapui.support.StringUtils;
18 import com.eviware.soapui.support.xml.XmlUtils;
19 import org.w3c.dom.Document;
20 import org.w3c.tidy.Tidy;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25
26 public class HtmlMediaTypeHandler implements MediaTypeHandler
27 {
28 public boolean canHandle( String contentType )
29 {
30 return contentType != null && contentType.equals( "text/html" );
31 }
32
33 public String createXmlRepresentation( HttpResponse response )
34 {
35 String content = response == null ? null : response.getContentAsString();
36 if( !StringUtils.hasContent( content ) )
37 return null;
38
39 try
40 {
41 Tidy tidy = new Tidy();
42 tidy.setXmlOut( true );
43 tidy.setShowWarnings( false );
44 tidy.setErrout( new PrintWriter( new StringWriter() ) );
45
46 tidy.setNumEntities( true );
47 tidy.setQuoteNbsp( true );
48
49 Document document = tidy.parseDOM( new ByteArrayInputStream( content.getBytes() ), null );
50 StringWriter writer = new StringWriter();
51 XmlUtils.serializePretty( document, writer );
52 return writer.toString();
53 }
54 catch( Throwable e )
55 {
56 e.printStackTrace();
57 }
58 return null;
59 }
60 }