Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Enable multipart support in Handlers under Embedded jetty without XML configs or WebAppContext

Thought I would re-visit this recently after moving to Apache FileUpload, a simple workaround to enable Multipart support in Handlers is:
Define a MulitpartConfigElement that you want your handlers to use:

private static final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement(System.getProperty(""));

Within your Handler's doHandle, check if the request is multipart, if it is just add your config to the request:

if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data")) {
  baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);

Now calls to getPart will work fine, if someone knows a nicer way let me know!

On Thu, Jan 10, 2013 at 2:06 PM, Neil Williams <neil@xxxxxxxxxxx> wrote:
Under Jetty 8.1.8 you have to explicitly enable Multipart support using a flag in the web.xml or using the @Multipartconfig annotation.

I have a project that uses embedded Jetty with no xml configs and with no WebAppContext, it is simply a few handlers directly added to the server (simplified example below).

After looking at this ( bug report, the solutions offered to get annotations working to enable multipart support do not seem applicable to Handlers (or, at least setting the "org.eclipse.jetty.webapp.configuration" attributes on the server instance have no effect)

Is it possible to enable servlet 3.0 multipart support for handlers with this setup?


package anno;


import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.log.Log;

public class AnnoTest {
  public static void main(String[] args) throws Exception {
    Server server = new Server(9000);

    // String[] configuration = new String[] {
    // "org.eclipse.jetty.webapp.WebInfConfiguration",
    // "org.eclipse.jetty.webapp.WebXmlConfiguration",
    // "org.eclipse.jetty.webapp.MetaInfConfiguration",
    // "org.eclipse.jetty.webapp.FragmentConfiguration",
    // "",
    // "",
    // "org.eclipse.jetty.annotations.AnnotationConfiguration",
    // "org.eclipse.jetty.webapp.JettyWebXmlConfiguration" };
    // server.setAttribute("org.eclipse.jetty.webapp.configuration", configuration);

    server.setHandler(new PostHandler());

  public static class PostHandler extends ContextHandler {
    public PostHandler() {

    public void doHandle(String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {
      Part p = request.getPart("content");

Calls to /post/ will result in the following:
java.lang.IllegalStateException: No multipart config for servlet
at org.eclipse.jetty.server.Request.getPart(
at anno.AnnoTest$PostHandler.doHandle(
at org.eclipse.jetty.server.handler.ContextHandler.doScope(
at org.eclipse.jetty.server.handler.ScopedHandler.handle(
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(
at org.eclipse.jetty.server.Server.handle(
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(
at org.eclipse.jetty.server.AbstractHttpConnection.content(
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(
at org.eclipse.jetty.http.HttpParser.parseNext(
at org.eclipse.jetty.http.HttpParser.parseAvailable(
at org.eclipse.jetty.server.AsyncHttpConnection.handle(
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(
at org.eclipse.jetty.util.thread.QueuedThreadPool$
at Source)

Back to the top