View Javadoc

1   /*
2    * $Id: ServletRequestToOutputHandler.java 12162 2008-06-26 00:00:33Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.http.transformers;
11  
12  import org.mule.api.MuleEvent;
13  import org.mule.api.transformer.DiscoverableTransformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.api.transport.OutputHandler;
16  import org.mule.transformer.AbstractTransformer;
17  import org.mule.util.IOUtils;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  /**
26   * Adds support for converting a {@link javax.servlet.http.HttpServletRequest} into an {@link org.mule.api.transport.OutputHandler}
27   */
28  public class ServletRequestToOutputHandler extends AbstractTransformer implements DiscoverableTransformer
29  {
30      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
31  
32      public ServletRequestToOutputHandler()
33      {
34          registerSourceType(HttpServletRequest.class);
35          setReturnClass(OutputHandler.class);
36      }
37  
38      //@Override
39      public Object doTransform(final Object src, String encoding) throws TransformerException
40      {
41              return new OutputHandler()
42              {
43                  public void write(MuleEvent event, OutputStream out) throws IOException
44                  {
45                      InputStream is = ((HttpServletRequest) src).getInputStream();
46                      try
47                      {
48                          IOUtils.copyLarge(is, out);
49                      }
50                      finally
51                      {
52                          is.close();
53                      }
54                  }
55              };
56          }
57  
58      /**
59       * If 2 or more discoverable transformers are equal, this value can be used to select the correct one
60       *
61       * @return the priority weighting for this transformer. This is a value between
62       *         {@link #MIN_PRIORITY_WEIGHTING} and {@link #MAX_PRIORITY_WEIGHTING}.
63       */
64      public int getPriorityWeighting()
65      {
66          return priorityWeighting;
67      }
68  
69      /**
70       * If 2 or more discoverable transformers are equal, this value can be used to select the correct one
71       *
72       * @param weighting the priority weighting for this transformer. This is a value between
73       *                  {@link #MIN_PRIORITY_WEIGHTING} and {@link #MAX_PRIORITY_WEIGHTING}.
74       */
75      public void setPriorityWeighting(int weighting)
76      {
77          priorityWeighting = weighting;
78      }
79  }