View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2009 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.tools;
14  
15  import java.io.IOException;
16  import java.util.logging.Level;
17  import java.util.logging.Logger;
18  
19  import javax.servlet.ServletException;
20  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import com.eviware.soapui.DefaultSoapUICore;
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.SoapUICore;
27  import com.eviware.soapui.impl.wsdl.WsdlProject;
28  import com.eviware.soapui.impl.wsdl.mock.DispatchException;
29  import com.eviware.soapui.impl.wsdl.mock.WsdlMockRunner;
30  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
31  
32  /***
33   * @author ole
34   */
35  public class SoapUIMockServlet extends HttpServlet
36  {
37  	private WsdlMockRunner mockRunner;
38  	private WsdlMockService mockService;
39  	private WsdlProject project;
40  	private static Logger logger = Logger.getLogger( SoapUIMockServlet.class.getName() );
41  
42  	@Override
43  	public void init() throws ServletException
44  	{
45  		super.init();
46  		try
47  		{
48  			logger.info( "Initializing soapUI Core" );
49  			SoapUI.setSoapUICore( createSoapUICore( getInitParameter( "settingsFile" ),
50  					getInitParameter( "settingsPassword" ) ) );
51  
52  			logger.info( "Loading project" );
53  			project = new WsdlProject( getInitParameter( "projectFile" ), getInitParameter( "projectPassword" ) );
54  
55  			logger.info( "Starting MockService" );
56  			mockService = project.getMockServiceByName( getInitParameter( "mockService" ) );
57  			mockRunner = mockService.start();
58  		}
59  		catch( Exception ex )
60  		{
61  			logger.log( Level.SEVERE, null, ex );
62  		}
63  	}
64  
65  	@Override
66  	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
67  			IOException
68  	{
69  		try
70  		{
71  			mockRunner.dispatchRequest( request, response );
72  		}
73  		catch( DispatchException ex )
74  		{
75  			logger.log( Level.SEVERE, null, ex );
76  		}
77  	}
78  
79  	/***
80  	 * Returns a short description of the servlet.
81  	 */
82  	public String getServletInfo()
83  	{
84  		return mockService.getName();
85  	}
86  
87  	// </editor-fold>
88  
89  	protected SoapUICore createSoapUICore( String settingsFile, String soapUISettingsPassword )
90  	{
91  		return new DefaultSoapUICore( null, settingsFile, soapUISettingsPassword );
92  	}
93  }