View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.support.editor.inspectors.ssl;
14  
15  import java.awt.BorderLayout;
16  import java.beans.PropertyChangeEvent;
17  import java.beans.PropertyChangeListener;
18  import java.security.cert.Certificate;
19  
20  import javax.swing.JComponent;
21  import javax.swing.JEditorPane;
22  import javax.swing.JPanel;
23  import javax.swing.JScrollPane;
24  import javax.swing.text.html.HTMLEditorKit;
25  
26  import com.eviware.soapui.impl.support.AbstractHttpRequest;
27  import com.eviware.soapui.impl.wsdl.WsdlRequest;
28  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
29  import com.eviware.soapui.impl.wsdl.submit.transports.http.SSLInfo;
30  import com.eviware.soapui.support.editor.EditorView;
31  import com.eviware.soapui.support.editor.inspectors.AbstractXmlInspector;
32  import com.eviware.soapui.support.editor.xml.XmlDocument;
33  
34  public class ResponseSSLInspector extends AbstractXmlInspector implements PropertyChangeListener
35  {
36  	private JEditorPane sslInfoPane;
37  	private JPanel panel;
38  	private AbstractHttpRequest<?> request;
39  
40  	protected ResponseSSLInspector( AbstractHttpRequest<?> abstractHttpRequest )
41  	{
42  		super( "SSL Info", "SSL Certificate Information for this response", true, SSLInspectorFactory.INSPECTOR_ID );
43  		this.request = abstractHttpRequest;
44  		abstractHttpRequest.addPropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, this );
45  	}
46  
47  	public JComponent getComponent()
48  	{
49  		if( panel != null )
50  			return panel;
51  
52  		panel = new JPanel( new BorderLayout() );
53  		sslInfoPane = new JEditorPane();
54  		sslInfoPane.setEditorKit( new HTMLEditorKit() );
55  		panel.add( new JScrollPane( sslInfoPane ) );
56  
57  		return panel;
58  	}
59  
60  	@Override
61  	public void release()
62  	{
63  		super.release();
64  
65  		request.removePropertyChangeListener( WsdlRequest.RESPONSE_PROPERTY, this );
66  	}
67  
68  	private void updateSSLInfo( SSLInfo sslInfo )
69  	{
70  		String sslInfoTabTitle = "SSL Info";
71  
72  		if( sslInfo != null )
73  		{
74  			StringBuffer buf = new StringBuffer( "<html><body><table cellpadding=1 cellspacing=1 border=0>" );
75  
76  			buf.append( "<tr><td><b>CipherSuite:</b></td><td align=left valign=top>" ).append(
77  					sslInfo.getCipherSuite().toString() ).append( "</td></tr>" );
78  
79  			if( sslInfo.getLocalPrincipal() != null )
80  				buf.append( "<tr><td><b>LocalPrincipal:</b></td><td align=left valign=top>" ).append(
81  						sslInfo.getLocalPrincipal().getName() ).append( "</td></tr>" );
82  
83  			Certificate[] localCertificates = sslInfo.getLocalCertificates();
84  
85  			if( localCertificates != null )
86  			{
87  				buf.append( "</table><table cellpadding=0 cellspacing=0 border=0>" );
88  
89  				int cnt = 1;
90  				for( Certificate cert : localCertificates )
91  				{
92  					buf.append( "<tr><td><b>Local Certificate " ).append( cnt++ ).append( ":</b><pre><font size=-1>" )
93  							.append( cert.toString() ).append( "</font></pre></td></tr>" );
94  
95  				}
96  
97  				buf.append( "</table><table cellpadding=1 cellspacing=1 border=0>" );
98  			}
99  
100 			if( sslInfo.getPeerPrincipal() != null )
101 				buf.append( "<tr><td><b>PeerPrincipal:</b></td><td align=left valign=top>" ).append(
102 						sslInfo.getPeerPrincipal().toString() ).append( "</td></tr>" );
103 
104 			Certificate[] peerCertificates = sslInfo.getPeerCertificates();
105 			if( peerCertificates != null )
106 			{
107 				buf.append( "</table><table cellpadding=0 cellspacing=0 border=0>" );
108 
109 				int cnt = 1;
110 				for( Certificate cert : peerCertificates )
111 				{
112 					buf.append( "<tr><td colspan=2><b>Peer Certificate " ).append( cnt++ ).append(
113 							":</b><pre><font size=-1>" ).append( cert.toString() ).append( "</font></pre></td></tr>" );
114 				}
115 
116 				buf.append( "</table><table cellpadding=0 cellspacing=0 border=0>" );
117 			}
118 
119 			buf.append( "</table></body></html>" );
120 			sslInfoPane.setText( buf.toString() );
121 
122 			sslInfoTabTitle += " (" + sslInfo.getPeerCertificates().length + " certs)";
123 		}
124 
125 		setTitle( sslInfoTabTitle );
126 	}
127 
128 	public void propertyChange( PropertyChangeEvent evt )
129 	{
130 		HttpResponse response = request.getResponse();
131 		updateSSLInfo( response == null ? null : response.getSSLInfo() );
132 		setEnabled( response != null && response.getSSLInfo() != null );
133 	}
134 
135 	@Override
136 	public boolean isEnabledFor( EditorView<XmlDocument> view )
137 	{
138 		return true;
139 	}
140 }