Skip to main content

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

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 (https://bugs.eclipse.org/bugs/show_bug.cgi?id=395000#c0) 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?

Thanks,
Neil

package anno;

import java.io.IOException;

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 {
    Log.getRootLogger().setDebugEnabled(true);
    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.plus.webapp.EnvConfiguration",
    // "org.eclipse.jetty.plus.webapp.PlusConfiguration",
    // "org.eclipse.jetty.annotations.AnnotationConfiguration",
    // "org.eclipse.jetty.webapp.JettyWebXmlConfiguration" };
    // server.setAttribute("org.eclipse.jetty.webapp.configuration", configuration);

    server.setHandler(new PostHandler());
    server.start();
    server.join();
  }

  @MultipartConfig
  public static class PostHandler extends ContextHandler {
    public PostHandler() {
      super("/post");
    }

    @Override
    public void doHandle(String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {
      System.out.print(request.getContentType());
      Part p = request.getPart("content");
      System.out.print(p.getSize());
      baseRequest.setHandled(true);
    }
  }
}

Calls to /post/ will result in the following:
java.lang.IllegalStateException: No multipart config for servlet
at org.eclipse.jetty.server.Request.getPart(Request.java:2002)
at anno.AnnoTest$PostHandler.doHandle(AnnoTest.java:49)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1010)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:361)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:937)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:998)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:856)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Unknown Source)

Back to the top