View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.rest.support.handlers;
14  
15  import sun.misc.BASE64Encoder;
16  
17  import com.eviware.soapui.impl.rest.support.MediaTypeHandler;
18  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
19  import com.eviware.soapui.support.StringUtils;
20  import com.eviware.soapui.support.xml.XmlUtils;
21  
22  public class DefaultMediaTypeHandler implements MediaTypeHandler
23  {
24  	public boolean canHandle( String contentType )
25  	{
26  		return true;
27  	}
28  
29  	public String createXmlRepresentation( HttpResponse response )
30  	{
31  		String contentType = response.getContentType();
32  		String content = response.getContentAsString();
33  
34  		if( StringUtils.hasContent( contentType ) && contentType.toUpperCase().endsWith( "XML" ) )
35  			return content;
36  
37  		if( XmlUtils.seemsToBeXml( content ) )
38  			return content;
39  		else if( content == null )
40  			content = "";
41  
42  		String result = "<data contentType=\"" + contentType + "\" contentLength=\"" + response.getContentLength()
43  				+ "\">";
44  
45  		for( int c = 0; c < content.length(); c++ )
46  		{
47  			if( content.charAt( c ) < 8 )
48  			{
49  				return result + new BASE64Encoder().encode( content.getBytes() ) + "</data>";
50  			}
51  		}
52  
53  		return result + "<![CDATA[" + content + "]]></data>";
54  	}
55  }