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.inspectors.representations;
14  
15  import com.eviware.soapui.impl.rest.RestRepresentation;
16  import com.eviware.soapui.impl.rest.RestRequest;
17  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
18  import com.eviware.soapui.impl.support.HttpUtils;
19  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
20  import com.eviware.soapui.model.iface.Submit;
21  import com.eviware.soapui.model.iface.SubmitContext;
22  import com.eviware.soapui.model.iface.SubmitListener;
23  import com.eviware.soapui.support.UISupport;
24  import com.eviware.soapui.support.components.JXToolBar;
25  
26  import javax.swing.*;
27  import java.awt.event.ItemEvent;
28  import java.awt.event.ItemListener;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.List;
32  
33  public class RestResponseRepresentationsInspector extends AbstractRestRepresentationsInspector implements SubmitListener
34  {
35     private JCheckBox enableRecordingCheckBox;
36     public static final String RECORD_RESPONSE_REPRESENTATIONS = "RecordResponseRepresentations";
37  
38     protected RestResponseRepresentationsInspector( RestRequest request )
39     {
40        super( request, "Representations", "Response Representations", new RestRepresentation.Type[]
41                {RestRepresentation.Type.RESPONSE, RestRepresentation.Type.FAULT} );
42  
43        request.addSubmitListener( this );
44     }
45  
46     protected JXToolBar buildToolbar()
47     {
48        JXToolBar toolbar = super.buildToolbar();
49  
50        toolbar.addSeparator();
51  
52        enableRecordingCheckBox = new JCheckBox( "Auto-Create" );
53        enableRecordingCheckBox.setToolTipText( "Automatically create Representations from received Responses" );
54        enableRecordingCheckBox.setOpaque( false );
55        UISupport.setFixedSize( enableRecordingCheckBox, 150, 20 );
56        toolbar.addFixed( enableRecordingCheckBox );
57        XmlBeansSettingsImpl settings = getRequest().getSettings();
58        if( settings.isSet( RECORD_RESPONSE_REPRESENTATIONS ) )
59        {
60           enableRecordingCheckBox.setSelected( settings.getBoolean( RECORD_RESPONSE_REPRESENTATIONS ) );
61        }
62        else
63        {
64           enableRecordingCheckBox.setSelected( getRequest().getResource() == null ||
65                   getRequest().getResource().getService().isGenerated() );
66        }
67  
68        enableRecordingCheckBox.addItemListener( new ItemListener()
69        {
70           public void itemStateChanged( ItemEvent e )
71           {
72              getRequest().getSettings().setBoolean( RECORD_RESPONSE_REPRESENTATIONS, enableRecordingCheckBox.isSelected() );
73           }
74        } );
75  
76        return toolbar;
77     }
78  
79     public boolean beforeSubmit( Submit submit, SubmitContext context )
80     {
81        return true;
82     }
83  
84     public void afterSubmit( Submit submit, SubmitContext context )
85     {
86        HttpResponse response = getRequest().getResponse();
87        if( response != null && enableRecordingCheckBox.isSelected() )
88        {
89           if( HttpUtils.isErrorStatus( response.getStatusCode() ) )
90           {
91              extractRepresentation( response, RestRepresentation.Type.FAULT );
92           }
93           else
94           {
95              extractRepresentation( response, RestRepresentation.Type.RESPONSE );
96           }
97        }
98     }
99  
100    @SuppressWarnings( "unchecked" )
101    protected void extractRepresentation( HttpResponse response, RestRepresentation.Type type )
102    {
103       RestRepresentation[] representations = getRequest().getRepresentations( type, null );
104       int c = 0;
105       for( ; c < representations.length; c++ )
106       {
107          if( representations[c].getMediaType().equals( response.getContentType() ) )
108          {
109             List status = representations[c].getStatus();
110             if( status == null || !status.contains( response.getStatusCode() ) )
111             {
112                status = status == null ? new ArrayList<Integer>() : new ArrayList<Integer>( status );
113                status.add( response.getStatusCode() );
114                representations[c].setStatus( status );
115             }
116             break;
117          }
118       }
119 
120       if( c == representations.length )
121       {
122          RestRepresentation representation = getRequest().addNewRepresentation( type );
123          representation.setMediaType( response.getContentType() );
124          representation.setStatus( Arrays.asList( response.getStatusCode() ) );
125       }
126    }
127 
128    public void release()
129    {
130       super.release();
131       getRequest().removeSubmitListener( this );
132    }
133 }