Table of Contents

1 A servlet to capture POST bodies into files

I needed something that could capture POST bodies so that I could use real POST bodies in unit tests (specifically: I needed some examples of Sonar Webhooks for my sonar-collector utility).

The solution was this servlet:

  1. The servlet installs into an apache karaf instance
  2. All POST bodies are written to a temp file, and the file name is written to the log as an INFO message. Example log message:

    [INFO] Saved body of POST to http://localhost:8181/post-body-capture to file: /tmp/postbodycaptureservlet628424040798817132.json
    

Note: The temp files aren't cleaned up by the servlet or when uninstalling the servlet, so leaving this servlet running can create a lot of clutter in the temp directory.

Note also, that this servlet is written to be installed as a karaf feature in Apache Karaf and connect to the Pax Web Whiteboard Extender.

But outside of OSGi, Pax Web and Karaf, this is just a plain servlet with no special dependencies, and can presumably be used everywhere such a servlet can be used. I haven't tried this myself, however.

1.2 How to build and install

This is how to build and install the servlet:

  1. Clone and build the servlet

    mkdir -p ~/git
    cd ~/git
    git clone https://github.com/steinarb/post-body-capture.git
    cd ~/git/post-body-capture
    mvn clean install
    
  2. SSH into karaf

    ssh -p 8101 karaf@localhost
    
  3. In karaf, install the feature that pulls in the servlet with dependencies

    feature:repo-add mvn:no.priv.bang.debug-utils/post-body-capture/LATEST/xml/features
    feature:install post-body-capture
    
  4. After this, the servlet will be listening for POST requests, at the local path "/post-body-capture" on the karaf web server, e.g. http://localhost:8181/post-body-capture for a karaf server started locally, with default settings

1.3 To uninstall the servlet from karaf

This is how to uninstall the servlet from karaf:

  1. SSH into karaf

    ssh -p 8101 karaf@localhost
    
  2. At the karaf console prompt, give the following command:

    feature:uninstall post-body-capture
    

This will disconnect the servlet from the Pax Web Whiteboard Extender and uninstall the servlet, and all dependencies (e.g. Pax Web) that has been pulled in by this feature.

However, if a different feature has pulled in the same dependencies, the dependencies won't be uninstalled until everything that has required them is uninstalled.

1.4 License

This software project is licensed under Apache License v. 2.

See the LICENSE file for details.

Created: 2017-11-25 lør. 19:56

Emacs 25.2.1 (Org mode 8.3beta)

Validate