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