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.json;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.beans.PropertyChangeEvent;
18  import java.beans.PropertyChangeListener;
19  
20  import javax.swing.JComponent;
21  import javax.swing.JPanel;
22  import javax.swing.JScrollPane;
23  
24  import net.sf.json.JSON;
25  import net.sf.json.JSONSerializer;
26  
27  import com.eviware.soapui.impl.rest.support.handlers.JsonMediaTypeHandler;
28  import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
29  import com.eviware.soapui.impl.support.http.HttpRequestInterface;
30  import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel.HttpResponseDocument;
31  import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel.HttpResponseMessageEditor;
32  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.components.JXToolBar;
35  import com.eviware.soapui.support.editor.views.AbstractXmlEditorView;
36  import com.eviware.soapui.support.xml.JXEditTextArea;
37  
38  @SuppressWarnings( "unchecked" )
39  public class JsonResponseView extends AbstractXmlEditorView<HttpResponseDocument> implements PropertyChangeListener
40  {
41  	private final HttpRequestInterface<?> httpRequest;
42  	private JPanel contentPanel;
43  	private JXEditTextArea contentEditor;
44  	private boolean updatingRequest;
45  	private JPanel panel;
46  
47  	public JsonResponseView( HttpResponseMessageEditor httpRequestMessageEditor, HttpRequestInterface<?> httpRequest )
48  	{
49  		super( "JSON", httpRequestMessageEditor, JsonResponseViewFactory.VIEW_ID );
50  		this.httpRequest = httpRequest;
51  
52  		httpRequest.addPropertyChangeListener( this );
53  	}
54  
55  	public JComponent getComponent()
56  	{
57  		if( panel == null )
58  		{
59  			panel = new JPanel( new BorderLayout() );
60  
61  			panel.add( buildToolbar(), BorderLayout.NORTH );
62  			panel.add( buildContent(), BorderLayout.CENTER );
63  			panel.add( buildStatus(), BorderLayout.SOUTH );
64  		}
65  
66  		return panel;
67  	}
68  
69  	@Override
70  	public void release()
71  	{
72  		super.release();
73  
74  		httpRequest.removePropertyChangeListener( this );
75  	}
76  
77  	private Component buildStatus()
78  	{
79  		return new JPanel();
80  	}
81  
82  	private Component buildContent()
83  	{
84  		contentPanel = new JPanel( new BorderLayout() );
85  
86  		contentEditor = JXEditTextArea.createJavaScriptEditor();
87  		HttpResponse response = httpRequest.getResponse();
88  		if( response != null )
89  			setEditorContent( response );
90  
91  		contentPanel.add( new JScrollPane( contentEditor ) );
92  		contentEditor.setEditable( false );
93  
94  		return contentPanel;
95  	}
96  
97  	protected void setEditorContent( HttpResponse httpResponse )
98  	{
99  		if( httpResponse == null )
100 		{
101 			contentEditor.setText( "" );
102 		}
103 		else
104 		{
105 			String content = "<Not JSON content>";
106 
107 			if( JsonMediaTypeHandler.couldBeJsonContent( httpResponse.getContentType() ) )
108 			{
109 				try
110 				{
111 					JSON json = JSONSerializer.toJSON( httpResponse.getContentAsString() );
112 					if( json.isEmpty() )
113 						content = "<Empty JSON content>";
114 					else
115 						content = json.toString( 3 );
116 				}
117 				catch( Throwable e )
118 				{
119 					e.printStackTrace();
120 				}
121 
122 				contentEditor.setText( content );
123 			}
124 			else
125 			{
126 				contentEditor.setText( "<Not JSON content>" );
127 			}
128 		}
129 	}
130 
131 	private Component buildToolbar()
132 	{
133 		JXToolBar toolbar = UISupport.createToolbar();
134 
135 		return toolbar;
136 	}
137 
138 	public void propertyChange( PropertyChangeEvent evt )
139 	{
140 		if( evt.getPropertyName().equals( AbstractHttpRequestInterface.RESPONSE_PROPERTY ) && !updatingRequest )
141 		{
142 			setEditorContent( ( ( HttpResponse )evt.getNewValue() ) );
143 		}
144 	}
145 
146 	@Override
147 	public void setXml( String xml )
148 	{
149 	}
150 
151 	public boolean saveDocument( boolean validate )
152 	{
153 		return false;
154 	}
155 
156 	public void setEditable( boolean enabled )
157 	{
158 	}
159 }