Search code examples
scalaplayframeworkmultipartform-datalagom

Multipart Form Errors with Lagom


Most of our Lagom entrypoints don't use multipart form requests, but one does. Since Lagom doesn't currently support multipart requests natively, the general suggestion I have seen is to call the underlying Play API, using the PlayServiceCall mechanism.

We have done that, and it works--most of the time. But we experience intermittent errors, especially when submitting large files. These are always cases of java.util.zip.ZipException (of various kinds), looking as if not an entire file has been received for processing.

Here's how the entrypoint looks in the code; in particular, the Play wrapping mechanism:

def upload = PlayServiceCall[NotUsed, UUID] {
  wrapCall => Action.async(multipartFormData) {
    request => wrapCall(ServiceCall { _ =>
      val upload = request.body.file("upload")
      val input = new FileInputStream(upload.get.ref.file)
      val filename = upload.get.filename
      // ...
      // other code to actually process the file
      // ...
    })(request).run
  }
}

Here are just two examples of exceptions we're seeing:

Caused by: java.util.zip.ZipException: invalid code lengths set
  at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
  at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
  at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:214)
  at java.io.FilterInputStream.read(FilterInputStream.java:107)
  etc.

Caused by: java.util.zip.ZipException: invalid distance too far back
  at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
  at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
  at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:214)
  at java.io.FilterInputStream.read(FilterInputStream.java:107)
  etc.

We use Lagom 1.3.8, in Scala. Any suggestions, please?


Solution

  • Try using the new service gateway based on Akka HTTP.

    You can enable this by adding the following to your build.sbt:

    lagomServiceGatewayImpl in ThisBuild := "akka-http"
    

    The new service gateway is still disabled by default in Lagom 1.3.8, but Lagom users that have experienced this problem have reported that it is resolved by enabling the akka-http gateway. This will become the default implementation in Lagom 1.4.0.