Forum Discussion

nmrao's avatar
nmrao
Champion Level 3
9 years ago

How to access init-param(defined in web.xml) in mock script dispatcher

Created a mock service for an operation using Script Dispatcher.

.war file is created using Deploy as war. And deploying this war in Tomcat.

Would like to access an init-parameter which is defined in web.xml (present inside .war file) in the script dispatcher, how to do it?

The following is the snippet from web.xml and like to get value of param-name scripts.

 

<init-param>
   <description>set scripts directory</description>
   <param-name>scripts</param-name>
   <param-value>WEB-INF/scripts</param-value>
</init-param>


The script dispatcher shows context, mockOperation, requestContext, log and mockRequest variables are available. Looking for right right object to use and get the value "WEB-INF/scripts" in script of mock response dispatcher.

 

Tried using context object, something like below, but returning No Such Method getServletConfig()

context.getServletConfig().getInitParameter('scripts')

I have no surprise when the No such method error is thrown, because, API documentation does not such public method.

Any ideas?

3 Replies

  • Hello Nmrao,

     

    Sorry for the delays –  due to a misunderstanding I didn't see your question until yesterday. Unfortunately there's no way to get to the servlet itself in the code. The Virts (mock services) don't have any reference, direct or indirect, back to it. Frankly this scenario never occurred to us, since the web.xml is hardcoded and packaged inside soapui.jar. Are you creating a custom web.xml or something?

     

    The Groovy snippet below will read the parameter value from the embedded web.xml and store it in the scriptsDirectory variable. Of course, if you're reading the web.xml in some other way, you need to replace the SoapUI.class.getResourceAsStream(...) with a FileInputStream etc.

     

    Hope this helps!

     

    Manne Fagerlind, Ready! API/SoapUI developer

     

     

     

     

    import com.eviware.soapui.SoapUI

    def webXml = new XmlSlurper().parse(SoapUI.class.getResourceAsStream("/com/eviware/soapui/resources/mockaswar/web.xml"))
    def scriptsParameter = webXml.servlet["init-param"].find { param ->
    param['param-name'] == 'scripts'
    }
    def scriptsDirectory = scriptsParameter['param-value']

     

    • nmrao's avatar
      nmrao
      Champion Level 3

      MFagerlind,

       

      Thank you so much for the reply with the details.

       

      Let me first put some more context to it which pushed me to create this thread.

      1. While creating mock service using script dispatcher, and that script uses local file ie., read file and modifies it in script before sending the response which usesgroovyUtils.projectPath('/path/to/local/resource/Response.xml')
      2. So far good, the above mock response is good when running it from SoapUI. Then, created a .war file using Deploy as War. And the deployment will be on a remote tomcat or other container. 
      3. Now, the problem is script dispatcher is referring local file and how to include the 'Response.xml'? so as to be available in the remote machine. So had to explore the war file and added the resource file under WEB_INF/soapui assuming that above line will get the path of it. However, that did not work(despite of resource available in the same place as the soapui-project xml is present). Opened a thread, and finally able to come up with solution as well with the help soapui.org documentation.
      4. Here it would be better to consider to include the external resource file directory as an option to Deploy as War would help user, so that end user do not have to manually make changes in the .war file. Raised this in the API Customer Advisory Board as well, and haven't heard any.
      5. Another point is that, I did not like the idea of putting resource files under WEB-INF/soapui directory due to the fact that it is not meant for it and usual practise is to have a separate directory for resouces that needs to be available during run time for container such as WEB-INF/resources. This is where this actual thread is arise, how to access in that case if in different directory during the same time, found a solution to #3? This when I thought adding a init-param in web.xml manually say resources with its value as WEB-INF/resources and access this location using whatever possible way.
      6. Later recently a thought came like this. As I know, it is going to be deployed in tomcat in a hard coded fashion like below. Could use - System.getProperties("catalina.base")+"webapps/<app name>/WEB-INF/resources"+fileName   in the script dispather. But, another issue is that, this will only work when it is deployed as war, but not in SoapUI . Then another question stands before us,  "How do we know that the mock service is running in SoapUI or in Deployed fashion"?

      Not sure how this groovy script idea will still help, will have to try.

       

      Thought of there must have some way to access servlet info using context (?), but that does not seem to be the case.

       

      Thank you for you time.

      • MFagerlind's avatar
        MFagerlind
        Staff

        OK, if you're trying to read contents from the WAR file, the Groovy snippet won't work. But in that case you're already modifying the WAR (since you have a non-standard web.xml), so maybe it's ok to include the files in the WAR file too?

         

        Your suggestion to add parameters for attaching files to the WAR in the Mock as WAR feature sounds nice, but I doubt that that's going to happen anytime soon, knowing how many other features we have in the pipeline ... In other words, you need to modify the WAR file yourself, but that could easily be automated with a .sh or .bat file using the "war" command to unpack the file, copying files, and then making a new WAR with the "war" command.

         

        About reading the file: I think there's something wrong with the code snippet under 1. because GroovyUtils.getProjectPath() doesn't take a parameter. Were you trying to use Class.getResource()/getResourceAsStream()? If so, you need to put your resources under WEB-INF/classes. Assuming you put it in WEB-INF/classes/responses/Response1.xml, you would use the code this.class.getResourceAsStream("/responses/Response1.xml") to get the contents of the file.

         

        It should be pretty easy to figure out if you're running inside SoapUI or in a container by looking for certain system properties. If "soapui.home" is defined, you can safely assume that you're in SoapUI, and then you can load the file in different ways based on where you're running.

         

        If you decide to go with the solution you described in  6. don't forget to add a slash before webapps!

         

        Good luck!

         

        Manne