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.panels.request.views.html;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.beans.PropertyChangeEvent;
19  import java.beans.PropertyChangeListener;
20  import java.io.File;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  
24  import javax.swing.BorderFactory;
25  import javax.swing.JComponent;
26  import javax.swing.JLabel;
27  import javax.swing.JPanel;
28  import javax.swing.JScrollPane;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel.HttpResponseDocument;
32  import com.eviware.soapui.impl.wsdl.monitor.JProxyServletWsdlMonitorMessageExchange;
33  import com.eviware.soapui.impl.wsdl.support.MessageExchangeModelItem;
34  import com.eviware.soapui.support.UISupport;
35  import com.eviware.soapui.support.components.BrowserComponent;
36  import com.eviware.soapui.support.components.JXToolBar;
37  import com.eviware.soapui.support.editor.inspectors.attachments.ContentTypeHandler;
38  import com.eviware.soapui.support.editor.views.AbstractXmlEditorView;
39  import com.eviware.soapui.support.editor.xml.XmlEditor;
40  
41  @SuppressWarnings( "unchecked" )
42  public class HttpHtmlMessageExchangeResponseView extends AbstractXmlEditorView<HttpResponseDocument> implements
43  		PropertyChangeListener
44  {
45  	private final MessageExchangeModelItem messageExchangeModelItem;
46  	private JPanel contentPanel;
47  	private JPanel panel;
48  	private JLabel statusLabel;
49  	private BrowserComponent browser;
50  
51  	public HttpHtmlMessageExchangeResponseView( XmlEditor editor, MessageExchangeModelItem messageExchangeModelItem )
52  	{
53  		super( "HTML", editor, HttpHtmlResponseViewFactory.VIEW_ID );
54  		this.messageExchangeModelItem = messageExchangeModelItem;
55  
56  		messageExchangeModelItem.addPropertyChangeListener( this );
57  	}
58  
59  	public JComponent getComponent()
60  	{
61  		if( panel == null )
62  		{
63  			panel = new JPanel( new BorderLayout() );
64  
65  			panel.add( buildToolbar(), BorderLayout.NORTH );
66  			panel.add( buildContent(), BorderLayout.CENTER );
67  			panel.add( buildStatus(), BorderLayout.SOUTH );
68  		}
69  
70  		return panel;
71  	}
72  
73  	@Override
74  	public void release()
75  	{
76  		super.release();
77  
78  		if( browser != null )
79  			browser.release();
80  
81  		messageExchangeModelItem.removePropertyChangeListener( this );
82  	}
83  
84  	private Component buildStatus()
85  	{
86  		statusLabel = new JLabel();
87  		statusLabel.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
88  		return statusLabel;
89  	}
90  
91  	private Component buildContent()
92  	{
93  		contentPanel = new JPanel( new BorderLayout() );
94  
95  		if( SoapUI.isJXBrowserDisabled() )
96  		{
97  			contentPanel.add( new JLabel( "Browser Component is disabled" ) );
98  		}
99  		else
100 		{
101 			browser = new BrowserComponent( false, false );
102 			Component component = browser.getComponent();
103 			component.setMinimumSize( new Dimension( 100, 100 ) );
104 			contentPanel.add( new JScrollPane( component ) );
105 
106 			setEditorContent( messageExchangeModelItem );
107 		}
108 		return contentPanel;
109 	}
110 
111 	protected void setEditorContent( JProxyServletWsdlMonitorMessageExchange jproxyServletWsdlMonitorMessageExchange )
112 	{
113 
114 		if( jproxyServletWsdlMonitorMessageExchange != null )
115 		{
116 			String contentType = jproxyServletWsdlMonitorMessageExchange.getResponseContentType();
117 			if( contentType.contains( "html" ) || contentType.contains( "text" ) )
118 			{
119 				try
120 				{
121 
122 					String content = jproxyServletWsdlMonitorMessageExchange.getResponseContent();
123 					browser.setContent( content, jproxyServletWsdlMonitorMessageExchange.getEndpoint() );
124 				}
125 				catch( Exception e )
126 				{
127 					e.printStackTrace();
128 				}
129 			}
130 			else if( isSupportedContentType( contentType ) )
131 			{
132 				try
133 				{
134 					String ext = ContentTypeHandler.getExtensionForContentType( contentType );
135 					File temp = File.createTempFile( "response", "." + ext );
136 					FileOutputStream fileOutputStream = new FileOutputStream( temp );
137 					writeHttpBody( jproxyServletWsdlMonitorMessageExchange.getRawResponseData(), fileOutputStream );
138 					fileOutputStream.close();
139 					browser.navigate( temp.toURI().toURL().toString(), null );
140 					temp.deleteOnExit();
141 				}
142 				catch( Exception e )
143 				{
144 					e.printStackTrace();
145 				}
146 			}
147 			else
148 			{
149 				browser.setContent( "unsupported content-type [" + contentType + "]" );
150 			}
151 		}
152 		else
153 		{
154 			browser.setContent( "-missing content-" );
155 		}
156 	}
157 
158 	private boolean isSupportedContentType( String contentType )
159 	{
160 		return contentType.toLowerCase().contains( "image" );
161 	}
162 
163 	protected void setEditorContent( MessageExchangeModelItem messageExchangeModelItem2 )
164 	{
165 
166 		if( messageExchangeModelItem2 != null && messageExchangeModelItem2.getMessageExchange() != null )
167 		{
168 			String contentType = messageExchangeModelItem2.getMessageExchange().getResponseHeaders().get( "Content-Type",
169 					"" );
170 			if( contentType.contains( "html" ) || contentType.contains( "text" ) )
171 			{
172 				try
173 				{
174 
175 					String content = messageExchangeModelItem2.getMessageExchange().getResponseContent();
176 					browser.setContent( content );
177 				}
178 				catch( Exception e )
179 				{
180 					e.printStackTrace();
181 				}
182 			}
183 			else if( !contentType.contains( "xml" ) )
184 			{
185 				try
186 				{
187 					String ext = ContentTypeHandler.getExtensionForContentType( contentType );
188 					File temp = File.createTempFile( "response", "." + ext );
189 					FileOutputStream fileOutputStream = new FileOutputStream( temp );
190 					writeHttpBody( messageExchangeModelItem2.getMessageExchange().getRawResponseData(), fileOutputStream );
191 					fileOutputStream.close();
192 					browser.navigate( temp.toURI().toURL().toString(), null );
193 					temp.deleteOnExit();
194 				}
195 				catch( Exception e )
196 				{
197 					e.printStackTrace();
198 				}
199 			}
200 		}
201 		else
202 		{
203 			browser.setContent( "<missing content>" );
204 		}
205 	}
206 
207 	private void writeHttpBody( byte[] rawResponse, FileOutputStream out ) throws IOException
208 	{
209 		int index = 0;
210 		byte[] divider = "\r\n\r\n".getBytes();
211 		for( ; index < ( rawResponse.length - divider.length ); index++ )
212 		{
213 			int i;
214 			for( i = 0; i < divider.length; i++ )
215 			{
216 				if( rawResponse[index + i] != divider[i] )
217 					break;
218 			}
219 
220 			if( i == divider.length )
221 			{
222 				out.write( rawResponse, index + divider.length, rawResponse.length - ( index + divider.length ) );
223 				return;
224 			}
225 		}
226 
227 		out.write( rawResponse );
228 	}
229 
230 	private Component buildToolbar()
231 	{
232 		JXToolBar toolbar = UISupport.createToolbar();
233 
234 		return toolbar;
235 	}
236 
237 	public void propertyChange( PropertyChangeEvent evt )
238 	{
239 		// System.out.println( evt.getPropertyName() );
240 		// System.out.println( evt.getNewValue() );
241 		// System.out.println( evt.getSource() );
242 		if( evt.getPropertyName().equals( "messageExchange" ) )
243 		{
244 			if( browser != null && evt.getNewValue() != null )
245 				setEditorContent( ( ( JProxyServletWsdlMonitorMessageExchange )evt.getNewValue() ) );
246 		}
247 	}
248 
249 	@Override
250 	public void setXml( String xml )
251 	{
252 	}
253 
254 	public boolean saveDocument( boolean validate )
255 	{
256 		return false;
257 	}
258 
259 	public void setEditable( boolean enabled )
260 	{
261 	}
262 
263 }