2

I'm trying to create a lightweight, self-contained webservice using the spark microframework ( http://www.sparkjava.com/readme.html ). I need to work with multi-part forms (I want to receive both a file, and some key-value data).

Jetty (on which Spark depends) provides a MultiPartFilter filter which facilitates working with multipart data, but I don't understand how to hook into that in my code.

I need to do this programmatically because this service will not be deployed as part of a giant java installation, but to support a python application.

The code I have is along these lines:

public class Transcoder {

    static Base64 base64 = new Base64();

    public static void main(String[] args) {

        org.apache.log4j.BasicConfigurator.configure();

        post(new Route("/convert") {
            @Override
            public Object handle(Request request, Response response) /*throws Exception, Docx4JException*/{

                //I want to do something like this:
                new_request = new MultiPartFilter().process_my_request(request);
                /* work with altered request*/
        });

    }

}

Is this possible?

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • I cannot fathom why you'd want to invoke a filter programmatically. Wouldn't it make more sense to refactor the functionality into a non-filter class that could be invoked by both the filter, and your non-web-app code? – Dave Newton Jan 30 '12 at 13:22
  • @DaveNewton: As mentioned in the question, I didn't create the filter. I'd rather use the code as-is, rather than understand its internals, and refactor into a non-filter. – Marcin Jan 30 '12 at 13:33
  • I don't know how possible that is; they don't return a response, they depend on having a filter chain, etc. It would be easier to refactor the functional bits than to set up a request processing chain. – Dave Newton Jan 30 '12 at 14:02

1 Answers1

1

I don't have the sources for Jetty (or Spark) loaded up, but I was just looking in the Spring sources and found an Interface called MultipartResolver, which has a method resolveMultipart which looks like it would do what you want/need. I would not be surprised to find classes implementing a similar named interface in Jetty:

public interface MultipartResolver {
 /* Parse the given HTTP request into multipart files and parameters,
 * and wrap the request inside a
 * {@link org.springframework.web.multipart.MultipartHttpServletRequest} object
 * that provides access to file descriptors and makes contained
 * parameters accessible via the standard ServletRequest methods.
 * ....
 */
 MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;

Note that Commons-FileUpload package also provides a nice set of utitilies to perform the same type of process that you want, without needing to re-work the Jetty Filter.

Eric B.
  • 23,425
  • 50
  • 169
  • 316
  • Thank you! Just the information that I need to get into java, do the job, then get out again ;) – Marcin Jan 30 '12 at 15:09