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  /*
14   *  soapUI, copyright (C) 2004-2008 eviware.com 
15   *
16   *  soapUI is free software; you can redistribute it and/or modify it under the 
17   *  terms of version 2.1 of the GNU Lesser General Public License as published by 
18   *  the Free Software Foundation.
19   *
20   *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
21   *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
22   *  See the GNU Lesser General Public License for more details at gnu.org.
23   */
24  
25  package com.eviware.soapui.impl.rest.panels.request.inspectors.representations;
26  
27  import com.eviware.soapui.impl.rest.RestRepresentation;
28  import com.eviware.soapui.impl.rest.RestRequest;
29  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
30  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
31  import com.eviware.soapui.model.iface.Submit;
32  import com.eviware.soapui.model.iface.SubmitContext;
33  import com.eviware.soapui.model.iface.SubmitListener;
34  import com.eviware.soapui.support.StringUtils;
35  import com.eviware.soapui.support.UISupport;
36  import com.eviware.soapui.support.components.JXToolBar;
37  
38  import javax.swing.*;
39  import java.awt.event.ItemEvent;
40  import java.awt.event.ItemListener;
41  
42  public class RestRequestRepresentationsInspector extends AbstractRestRepresentationsInspector implements SubmitListener
43  {
44     private JCheckBox enableRecordingCheckBox;
45     public static final String RECORD_REQUEST_REPRESENTATIONS = "RecordRequestRepresentations";
46  
47     protected RestRequestRepresentationsInspector( RestRequest request )
48     {
49        super( request, "Representations", "Request Representations", new RestRepresentation.Type[]
50                {RestRepresentation.Type.REQUEST} );
51  
52        request.addSubmitListener( this );
53     }
54  
55     protected JXToolBar buildToolbar()
56     {
57        JXToolBar toolbar = super.buildToolbar();
58  
59        toolbar.addSeparator();
60  
61        enableRecordingCheckBox = new JCheckBox( "Auto-Create" );
62        enableRecordingCheckBox.setToolTipText( "Automatically create Representations from sent Requests" );
63        enableRecordingCheckBox.setOpaque( false );
64        UISupport.setFixedSize( enableRecordingCheckBox, 100, 20 );
65        toolbar.addFixed( enableRecordingCheckBox );
66        XmlBeansSettingsImpl settings = getRequest().getSettings();
67        if( settings.isSet( RECORD_REQUEST_REPRESENTATIONS ) )
68        {
69           enableRecordingCheckBox.setSelected( settings.getBoolean( RECORD_REQUEST_REPRESENTATIONS ) );
70        }
71        else
72        {
73           enableRecordingCheckBox.setSelected( getRequest().getResource() == null ||
74                   getRequest().getResource().getService().isGenerated() );
75        }
76  
77        enableRecordingCheckBox.addItemListener( new ItemListener()
78        {
79           public void itemStateChanged( ItemEvent e )
80           {
81              getRequest().getSettings().setBoolean( RECORD_REQUEST_REPRESENTATIONS, enableRecordingCheckBox.isSelected() );
82           }
83        } );
84  
85        return toolbar;
86     }
87  
88     public boolean beforeSubmit( Submit submit, SubmitContext context )
89     {
90        return true;
91     }
92  
93     public void afterSubmit( Submit submit, SubmitContext context )
94     {
95        HttpResponse response = (HttpResponse) submit.getResponse();
96        if( response != null && enableRecordingCheckBox.isSelected() )
97        {
98           extractRepresentation( response );
99        }
100    }
101 
102    @SuppressWarnings( "unchecked" )
103    protected void extractRepresentation( HttpResponse response )
104    {
105       String responseContentType = response.getRequestHeaders().get( "Content-Type" );
106       if( StringUtils.isNullOrEmpty( responseContentType ) )
107          return;
108 
109       RestRepresentation[] representations = getRequest().getRepresentations( RestRepresentation.Type.REQUEST, null );
110       int c = 0;
111 
112       for( ; c < representations.length; c++ )
113       {
114          String repMediaType = representations[c].getMediaType();
115 
116          if( responseContentType.equals( repMediaType ) )
117          {
118             break;
119          }
120       }
121 
122       if( c == representations.length )
123       {
124          RestRepresentation representation = getRequest().addNewRepresentation( RestRepresentation.Type.REQUEST );
125          representation.setMediaType( responseContentType );
126       }
127    }
128 
129    public void release()
130    {
131       super.release();
132       getRequest().removeSubmitListener( this );
133    }
134 }