1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.servlet.transformers; |
8 | |
|
9 | |
import org.mule.api.transformer.TransformerException; |
10 | |
import org.mule.transformer.AbstractDiscoverableTransformer; |
11 | |
import org.mule.transformer.types.DataTypeFactory; |
12 | |
|
13 | |
import java.io.IOException; |
14 | |
|
15 | |
import javax.servlet.http.HttpServletRequest; |
16 | |
|
17 | |
import org.apache.commons.io.IOUtils; |
18 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class HttpRequestToByteArray extends AbstractDiscoverableTransformer |
25 | |
{ |
26 | |
public HttpRequestToByteArray() |
27 | 0 | { |
28 | 0 | registerSourceType(DataTypeFactory.create(HttpServletRequest.class)); |
29 | 0 | setReturnDataType(DataTypeFactory.BYTE_ARRAY); |
30 | 0 | } |
31 | |
|
32 | |
@Override |
33 | |
protected Object doTransform(Object src, String outputEncoding) throws TransformerException |
34 | |
{ |
35 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); |
36 | |
try |
37 | |
{ |
38 | 0 | IOUtils.copy(((HttpServletRequest) src).getInputStream(), baos); |
39 | |
} |
40 | 0 | catch (IOException e) |
41 | |
{ |
42 | 0 | throw new TransformerException(this, e); |
43 | 0 | } |
44 | 0 | return baos.toByteArray(); |
45 | |
} |
46 | |
} |