View Javadoc

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