View Javadoc

1   /*
2    * $Id: ServletRequestToOutputHandler.java 19250 2010-08-30 16:53:14Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transformer.types.DataTypeFactory;
18  import org.mule.util.IOUtils;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  /**
27   * Adds support for converting a {@link javax.servlet.http.HttpServletRequest} into an {@link org.mule.api.transport.OutputHandler}
28   */
29  public class ServletRequestToOutputHandler extends AbstractTransformer implements DiscoverableTransformer
30  {
31      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
32  
33      public ServletRequestToOutputHandler()
34      {
35          registerSourceType(DataTypeFactory.create(HttpServletRequest.class));
36          setReturnDataType(DataTypeFactory.create(OutputHandler.class));
37      }
38  
39      @Override
40      public Object doTransform(final Object src, String encoding) throws TransformerException
41      {
42              return new OutputHandler()
43              {
44                  public void write(MuleEvent event, OutputStream out) throws IOException
45                  {
46                      InputStream is = ((HttpServletRequest) src).getInputStream();
47                      try
48                      {
49                          IOUtils.copyLarge(is, out);
50                      }
51                      finally
52                      {
53                          is.close();
54                      }
55                  }
56              };
57          }
58  
59      /**
60       * If 2 or more discoverable transformers are equal, this value can be used to select the correct one
61       *
62       * @return the priority weighting for this transformer. This is a value between
63       *         {@link #MIN_PRIORITY_WEIGHTING} and {@link #MAX_PRIORITY_WEIGHTING}.
64       */
65      public int getPriorityWeighting()
66      {
67          return priorityWeighting;
68      }
69  
70      /**
71       * If 2 or more discoverable transformers are equal, this value can be used to select the correct one
72       *
73       * @param weighting the priority weighting for this transformer. This is a value between
74       *                  {@link #MIN_PRIORITY_WEIGHTING} and {@link #MAX_PRIORITY_WEIGHTING}.
75       */
76      public void setPriorityWeighting(int weighting)
77      {
78          priorityWeighting = weighting;
79      }
80  }