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.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" ), getInitParameter( "settingsPassword" ) ) );
50  
51  			logger.info( "Loading project" );
52  			project = new WsdlProject( getInitParameter( "projectFile" ), getInitParameter( "projectPassword" ) );
53  
54  			logger.info( "Starting MockService" );
55  			mockService = project.getMockServiceByName( getInitParameter( "mockService" ) );
56  			mockRunner = mockService.start();
57  		}
58  		catch( Exception ex )
59  		{
60  			logger.log( Level.SEVERE, null, ex );
61  		}
62  	}
63  
64  	@Override
65  	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
66  			IOException
67  	{
68  		try
69  		{
70  			mockRunner.dispatchRequest( request, response );
71  		}
72  		catch( DispatchException ex )
73  		{
74  			logger.log( Level.SEVERE, null, ex );
75  		}
76  	}
77  
78  	/***
79  	 * Returns a short description of the servlet.
80  	 */
81  	public String getServletInfo()
82  	{
83  		return mockService.getName();
84  	}
85  
86  	// </editor-fold>
87  
88  	protected SoapUICore createSoapUICore( String settingsFile, String soapUISettingsPassword )
89  	{
90  		return new DefaultSoapUICore( null, settingsFile, soapUISettingsPassword );
91  	}
92  }