简体   繁体   中英

Invoking filter programmatically inside servlet (Created using jetty/spark)?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM