View Javadoc

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