View Javadoc

1   package net.trajano.twiff.internal.servlet;
2   
3   import java.beans.BeanInfo;
4   import java.beans.Introspector;
5   import java.beans.PropertyDescriptor;
6   import java.io.IOException;
7   import java.io.OutputStream;
8   import javax.servlet.ServletException;
9   import javax.servlet.http.HttpServlet;
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  import net.trajano.twiff.AttachmentPageLookup;
13  import net.trajano.twiff.ViewServletConfiguration;
14  
15  /***
16   * Displays byte data stored in the page beans. If there is a token specified as
17   * a parameter, it will use the data in the token.
18   * 
19   * @author Archimedes Trajano
20   */
21  public class AttachmentServlet extends HttpServlet {
22      /***
23       * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
24       *           javax.servlet.http.HttpServletResponse)
25       */
26      protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
27          ViewServletRequestAdapter requestAdapter = new ViewServletRequestAdapter(request, configuration.getTokenFieldName());
28          try {
29              final Class pageBeanClass = pageLookup.getPageBeanClass(request);
30              requestAdapter.registerComponentImplementation(pageBeanClass);
31              BeanInfo beanInfo = Introspector.getBeanInfo(pageBeanClass);
32              for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
33                  if (pageLookup.getPropertyName(request).equals(descriptor.getName())) {
34                      byte[] data = (byte[]) descriptor.getReadMethod().invoke(requestAdapter.getContainer().getComponentInstanceOfType(pageBeanClass));
35                      response.setContentLength(data.length);
36                      final OutputStream outputStream = response.getOutputStream();
37                      outputStream.write(data);
38                      break;
39                  }
40              }
41          } catch (ServletException e) {
42              throw e;
43          } catch (IOException e) {
44              throw e;
45          } catch (Exception e) {
46              throw new ServletException(e);
47          }
48      }
49  
50      /***
51       * @param configuration
52       *                   servlet configuration.
53       * @param pageLookup
54       *                   logic that performs page lookups.
55       */
56      public AttachmentServlet(final ViewServletConfiguration configuration, final AttachmentPageLookup pageLookup) {
57          this.configuration = configuration;
58          this.pageLookup = pageLookup;
59      }
60  
61      /***
62       * Object that performs page lookups.
63       */
64      private final AttachmentPageLookup pageLookup;
65  
66      /***
67       * ViewServletConfiguration file.
68       */
69      private final ViewServletConfiguration configuration;
70  }