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.module.ibeans.spi;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.MuleRuntimeException;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.api.transformer.TransformerException;
13  import org.mule.module.ibeans.spi.support.DataTypeConverter;
14  import org.mule.transport.NullPayload;
15  import org.mule.transport.http.HttpConnector;
16  
17  import java.io.InputStream;
18  import java.util.Set;
19  
20  import javax.activation.DataHandler;
21  import javax.activation.MimeTypeParseException;
22  
23  import org.ibeans.api.DataType;
24  import org.ibeans.api.Response;
25  import org.ibeans.api.channel.MimeTypes;
26  
27  /**
28   * An implementation of an IBeans {@link org.ibeans.api.Response} that adapts to a {@link org.mule.api.MuleMessage}
29   */
30  public class MuleResponseMessage implements Response
31  {
32      private MuleMessage message;
33      private DataType dataType;
34      private String status;
35  
36      public MuleResponseMessage(MuleMessage message) throws MimeTypeParseException
37      {
38          this.message = message;
39          //TODO should DataType ever be null?
40          if(message.getDataType()==null)
41          {
42              //s this is response
43              String mime = message.findPropertyInAnyScope(MuleProperties.CONTENT_TYPE_PROPERTY,null);
44              if (mime == null)
45              {
46                  //case insensitive
47                  mime = message.findPropertyInAnyScope("ContentType", null);
48              }
49              if(mime==null) mime = MimeTypes.ANY.getBaseType();
50  
51              dataType = org.ibeans.impl.support.datatype.DataTypeFactory.create(message.getPayload().getClass(), mime);
52          }
53          else
54          {
55              dataType = DataTypeConverter.convertMuleToIBeans(message.getDataType());
56          }
57          status = message.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY);
58      }
59  
60      public String getStatusCode()
61      {
62          //TODO this will be null for non-http
63          return status;
64      }
65  
66      public void setStatusCode(String code)
67      {
68          this.status = code;
69      }
70  
71      public String getMimeType()
72      {
73          return dataType.getMimeType();
74      }
75  
76      public DataType getDataType()
77      {
78          return dataType;
79      }
80  
81      public InputStream getPayloadAsStream()
82      {
83          if(message.getPayload() instanceof NullPayload)
84          {
85              return null;
86          }
87          try
88          {
89              return message.getPayload(InputStream.class);
90          }
91          catch (TransformerException e)
92          {
93              throw new MuleRuntimeException(e);
94          }
95      }
96  
97      public Object getPayload()
98      {
99          if(message.getPayload() instanceof NullPayload)
100         {
101             return null;
102         }
103         return message.getPayload();
104     }
105 
106     public Object getHeader(String name)
107     {
108         return message.getInboundProperty(name);
109     }
110 
111     public Set<String> getHeaderNames()
112     {
113         return message.getInboundPropertyNames();
114     }
115 
116     public DataHandler getAttachment(String name)
117     {
118         return message.getInboundAttachment(name);
119     }
120 
121     public Set<String> getAttachmentNames()
122     {
123         return message.getInboundAttachmentNames();
124     }
125 
126     public MuleMessage getMessage()
127     {
128         return message;
129     }
130 
131     public Throwable getException()
132     {
133         if(message.getExceptionPayload()!=null)
134         {
135             return message.getExceptionPayload().getRootException();
136         }
137         return null;
138     }
139 
140 
141 }
142