View Javadoc

1   /*
2    * $Id: MuleResponseWriter.java 19270 2010-09-01 10:37:41Z 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  
11  package org.mule.module.jersey;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.transport.OutputHandler;
16  import org.mule.module.jersey.JerseyResourcesComponent;
17  import org.mule.transport.http.HttpConnector;
18  
19  import com.sun.jersey.spi.container.ContainerResponse;
20  import com.sun.jersey.spi.container.ContainerResponseWriter;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  public class MuleResponseWriter implements ContainerResponseWriter
30  {
31  
32      private ByteArrayOutputStream out;
33      private OutputHandler output;
34      private final MuleMessage message;
35  
36      public MuleResponseWriter(MuleMessage request)
37      {
38          super();
39          this.message = request;
40          this.out = new ByteArrayOutputStream();
41          this.output = new OutputHandler()
42          {
43  
44              public void write(MuleEvent arg0, OutputStream realOut) throws IOException
45              {
46                  realOut.write(out.toByteArray());
47                  realOut.flush();
48              }
49  
50          };
51      }
52  
53      public OutputStream writeStatusAndHeaders(long x, ContainerResponse response) throws IOException
54      {
55          Map<String, String> customHeaders = new HashMap<String, String>();
56          for (Map.Entry<String, List<Object>> e : response.getHttpHeaders().entrySet())
57          {
58                // TODO: is this correct?
59                message.setOutboundProperty(e.getKey(), getHeaderValue(e.getValue()));
60          }
61  
62          message.setInvocationProperty(JerseyResourcesComponent.JERSEY_RESPONSE, response);
63          message.setOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, response.getStatus());
64  
65          return out;
66      }
67  
68      private String getHeaderValue(List<Object> values)
69      {
70          StringBuilder sb = new StringBuilder();
71          boolean first = true;
72          for (Object o : values)
73          {
74              if (!first)
75              {
76                  sb.append(", ");
77              }
78              else
79              {
80                  first = false;
81              }
82  
83              sb.append(ContainerResponse.getHeaderValue(o));
84          }
85          return sb.toString();
86      }
87  
88      public void finish() throws IOException
89      {
90      }
91  
92      public OutputHandler getResponse()
93      {
94          return output;
95      }
96  }