1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.annotations;
20
21 import javax.servlet.MultipartConfigElement;
22 import javax.servlet.Servlet;
23 import javax.servlet.annotation.MultipartConfig;
24
25 import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
26 import org.eclipse.jetty.servlet.ServletHolder;
27 import org.eclipse.jetty.webapp.Descriptor;
28 import org.eclipse.jetty.webapp.MetaData;
29 import org.eclipse.jetty.webapp.WebAppContext;
30
31 /**
32 * MultiPartConfigAnnotationHandler
33 *
34 *
35 */
36 public class MultiPartConfigAnnotationHandler extends AbstractIntrospectableAnnotationHandler
37 {
38 protected WebAppContext _context;
39
40 public MultiPartConfigAnnotationHandler(WebAppContext context)
41 {
42 //TODO verify that MultipartConfig is not inheritable
43 super(false);
44 _context = context;
45 }
46 /**
47 * @see org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler#doHandle(java.lang.Class)
48 */
49 public void doHandle(Class clazz)
50 {
51 if (!Servlet.class.isAssignableFrom(clazz))
52 return;
53
54 MultipartConfig multi = (MultipartConfig) clazz.getAnnotation(MultipartConfig.class);
55 if (multi == null)
56 return;
57
58 MetaData metaData = _context.getMetaData();
59
60 //TODO: The MultipartConfigElement needs to be set on the ServletHolder's Registration.
61 //How to identify the correct Servlet? If the Servlet has no WebServlet annotation on it, does it mean that this MultipartConfig
62 //annotation applies to all declared instances in web.xml/programmatically?
63 //Assuming TRUE for now.
64
65 ServletHolder holder = getServletHolderForClass(clazz);
66 if (holder != null)
67 {
68 Descriptor d = metaData.getOriginDescriptor(holder.getName()+".servlet.multipart-config");
69 //if a descriptor has already set the value for multipart config, do not
70 //let the annotation override it
71 if (d == null)
72 {
73 metaData.setOrigin(holder.getName()+".servlet.multipart-config",multi,clazz);
74 holder.getRegistration().setMultipartConfig(new MultipartConfigElement(multi));
75 }
76 }
77 }
78
79 private ServletHolder getServletHolderForClass (Class clazz)
80 {
81 ServletHolder holder = null;
82 ServletHolder[] holders = _context.getServletHandler().getServlets();
83 if (holders != null)
84 {
85 for (ServletHolder h : holders)
86 {
87 if (h.getClassName() != null && h.getClassName().equals(clazz.getName()))
88 {
89 holder = h;
90 }
91 }
92 }
93 return holder;
94 }
95 }