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().getService().isGenerated() );
65        }
66  
67        enableRecordingCheckBox.addItemListener( new ItemListener()
68        {
69           public void itemStateChanged( ItemEvent e )
70           {
71              getRequest().getSettings().setBoolean( RECORD_RESPONSE_REPRESENTATIONS, enableRecordingCheckBox.isSelected() );
72           }
73        } );
74  
75        return toolbar;
76     }
77  
78     public boolean beforeSubmit( Submit submit, SubmitContext context )
79     {
80        return true;
81     }
82  
83     public void afterSubmit( Submit submit, SubmitContext context )
84     {
85        HttpResponse response = getRequest().getResponse();
86        if( response != null && enableRecordingCheckBox.isSelected() )
87        {
88           if( HttpUtils.isErrorStatus( response.getStatusCode() ) )
89           {
90              extractRepresentation( response, RestRepresentation.Type.FAULT );
91           }
92           else
93           {
94              extractRepresentation( response, RestRepresentation.Type.RESPONSE );
95           }
96        }
97     }
98  
99     @SuppressWarnings( "unchecked" )
100    protected void extractRepresentation( HttpResponse response, RestRepresentation.Type type )
101    {
102       RestRepresentation[] representations = getRequest().getRepresentations( type, null );
103       int c = 0;
104       for( ; c < representations.length; c++ )
105       {
106          if( representations[c].getMediaType().equals( response.getContentType() ) )
107          {
108             List status = representations[c].getStatus();
109             if( status == null || !status.contains( response.getStatusCode() ) )
110             {
111                status = status == null ? new ArrayList<Integer>() : new ArrayList<Integer>( status );
112                status.add( response.getStatusCode() );
113                representations[c].setStatus( status );
114             }
115             break;
116          }
117       }
118 
119       if( c == representations.length )
120       {
121          RestRepresentation representation = getRequest().addNewRepresentation( type );
122          representation.setMediaType( response.getContentType() );
123          representation.setStatus( Arrays.asList( response.getStatusCode() ) );
124       }
125    }
126 
127    public void release()
128    {
129       super.release();
130       getRequest().removeSubmitListener( this );
131    }
132 }