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