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.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>" ).
77  				append( 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>" ).
81  					append( 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++ ).
93  						append( ":</b><pre><font size=-1>" ).
94  						append( cert.toString() ).append( "</font></pre></td></tr>");
95  					
96  				}
97  				
98  				buf.append( "</table><table cellpadding=1 cellspacing=1 border=0>" );
99  			}
100 			
101 			if( sslInfo.getPeerPrincipal() != null )
102 				buf.append( "<tr><td><b>PeerPrincipal:</b></td><td align=left valign=top>" ).
103 					append( sslInfo.getPeerPrincipal().toString() ).append( "</td></tr>");
104 			
105 			Certificate[] peerCertificates = sslInfo.getPeerCertificates();
106 			if( peerCertificates != null)
107 			{
108 				buf.append( "</table><table cellpadding=0 cellspacing=0 border=0>" );
109 				
110 				int cnt = 1;
111 				for( Certificate cert : peerCertificates )
112 				{
113 					buf.append( "<tr><td colspan=2><b>Peer Certificate " ).append( cnt++ ).
114 						append( ":</b><pre><font size=-1>" ).
115 						append( cert.toString() ).append( "</font></pre></td></tr>");
116 				}
117 				
118 				buf.append( "</table><table cellpadding=0 cellspacing=0 border=0>" );
119 			}
120 			
121 			buf.append(  "</table></body></html>" );
122 			sslInfoPane.setText( buf.toString() );
123 			
124 			sslInfoTabTitle += " (" + sslInfo.getPeerCertificates().length + " certs)";
125 		}
126 		
127 		setTitle( sslInfoTabTitle );
128 	}
129 
130 	public void propertyChange( PropertyChangeEvent evt )
131 	{
132 		HttpResponse response = request.getResponse();
133 		updateSSLInfo( response == null ? null : response.getSSLInfo() );
134 		setEnabled( response != null && response.getSSLInfo() != null );
135 	}
136 
137 	@Override
138 	public boolean isEnabledFor( EditorView<XmlDocument> view )
139 	{
140 		return true;
141 	}
142 }