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).
Is it possible to enable servlet 3.0 multipart support for handlers with this setup?
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);
}
}
}
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)