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