View Javadoc

1   /*
2    * $Id: MuleHttpServletResponse.java 21025 2011-01-14 21:24:20Z dandiep $
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.servlet;
11  
12  import org.mule.api.MuleEvent;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.PropertyScope;
15  import org.mule.transport.http.HttpConnector;
16  import org.mule.transport.http.HttpConstants;
17  
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  import java.util.Locale;
24  
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.Cookie;
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * THIS CLASS IS UNSUPPORTED AND THE IMPLEMENTATION DOES NOT CONFORM TO THE SERVLET SPECIFICATION!
31   */
32  public class MuleHttpServletResponse implements HttpServletResponse
33  {
34  //    private static String[] DAYS =
35  //    { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
36  //    private static String[] MONTHS =
37  //    { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan"};
38  
39      private MuleEvent event;
40      private MuleMessage message;
41  
42      public MuleHttpServletResponse(MuleEvent event)
43      {
44          super();
45          this.event = event;
46          this.message = event.getMessage();
47      }
48      
49      public String getCharacterEncoding()
50      {
51          return event.getEncoding();
52      }
53  
54      public String getContentType()
55      {
56          return message.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE);
57      }
58  
59      public ServletOutputStream getOutputStream() throws IOException
60      {
61          throw new UnsupportedOperationException();
62      }
63  
64      public PrintWriter getWriter() throws IOException
65      {
66          throw new UnsupportedOperationException();
67      }
68  
69      public void setCharacterEncoding(String charset)
70      {
71          message.setEncoding(charset);
72      }
73  
74      public void setContentLength(int len)
75      {
76          throw new UnsupportedOperationException();
77      }
78  
79      public void setContentType(String type)
80      {
81          message.setProperty(HttpConstants.HEADER_CONTENT_TYPE, type, PropertyScope.OUTBOUND);
82      }
83  
84      public void setBufferSize(int size)
85      {
86      }
87  
88      public int getBufferSize()
89      {
90          return 0;
91      }
92  
93      public void flushBuffer() throws IOException
94      {
95      }
96  
97      public void resetBuffer()
98      {
99      }
100 
101     public boolean isCommitted()
102     {
103         return false;
104     }
105 
106     public void reset()
107     {
108     }
109 
110     public void setLocale(Locale loc)
111     {
112     }
113 
114     public Locale getLocale()
115     {
116         return null;
117     }
118 
119     public void addCookie(Cookie cookie)
120     {
121         org.apache.commons.httpclient.Cookie internal = toHttpClientCookie(cookie);
122         
123         org.apache.commons.httpclient.Cookie[] internalCookies = message.getOutboundProperty(HttpConnector.HTTP_COOKIES_PROPERTY);
124         if (internalCookies == null)
125         {
126             internalCookies = new org.apache.commons.httpclient.Cookie[] { internal };
127         }
128         else
129         {
130             List<org.apache.commons.httpclient.Cookie> list = new ArrayList<org.apache.commons.httpclient.Cookie>(Arrays.asList(internalCookies));
131             list.add(internal);
132             internalCookies = list.toArray(new org.apache.commons.httpclient.Cookie[list.size()]);
133         }
134         message.setInvocationProperty(HttpConnector.HTTP_COOKIES_PROPERTY, internalCookies);
135     }
136 
137     private org.apache.commons.httpclient.Cookie toHttpClientCookie(Cookie cookie)
138     {
139         org.apache.commons.httpclient.Cookie internal = new org.apache.commons.httpclient.Cookie();
140         
141         internal.setName(cookie.getName());
142         internal.setValue(cookie.getValue());
143         internal.setComment(cookie.getComment());
144         internal.setDomain(cookie.getDomain());
145 //        internal.setExpiryDate(toExpiry(cookie.getMaxAge()));
146         internal.setPath(cookie.getPath());
147         internal.setVersion(cookie.getVersion());
148         
149         return internal;
150     }
151 
152     public boolean containsHeader(String name)
153     {
154         return false;
155     }
156 
157     public String encodeURL(String url)
158     {
159         return null;
160     }
161 
162     public String encodeRedirectURL(String url)
163     {
164         return null;
165     }
166 
167     public String encodeUrl(String url)
168     {
169         return null;
170     }
171 
172     public String encodeRedirectUrl(String url)
173     {
174         return null;
175     }
176 
177     public void sendError(int sc, String msg) throws IOException
178     {
179     }
180 
181     public void sendError(int sc) throws IOException
182     {
183     }
184 
185     public void sendRedirect(String location) throws IOException
186     {
187     }
188 
189     public void setDateHeader(String name, long date)
190     {
191         
192     }
193 
194     public void addDateHeader(String name, long date)
195     {
196         setDateHeader(name, date);
197     }
198 
199 //    /**
200 //     * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'"for
201 //     * cookies
202 //     */
203 //    public static void formatDate(StringBuffer buf, Calendar calendar, boolean cookie)
204 //    {
205 //        // "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
206 //        // "EEE, dd-MMM-yy HH:mm:ss 'GMT'", cookie
207 //
208 //        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
209 //        int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
210 //        int month = calendar.get(Calendar.MONTH);
211 //        int year = calendar.get(Calendar.YEAR);
212 //        int century = year / 100;
213 //        year = year % 100;
214 //
215 //        int epoch = (int) ((calendar.getTimeInMillis() / 1000) % (60 * 60 * 24));
216 //        int seconds = epoch % 60;
217 //        epoch = epoch / 60;
218 //        int minutes = epoch % 60;
219 //        int hours = epoch / 60;
220 //
221 //        buf.append(DAYS[day_of_week]);
222 //        buf.append(',');
223 //        buf.append(' ');
224 //        StringUtil.append2digits(buf, day_of_month);
225 //
226 //        if (cookie)
227 //        {
228 //            buf.append('-');
229 //            buf.append(MONTHS[month]);
230 //            buf.append('-');
231 //            StringUtil.append2digits(buf, century);
232 //            StringUtil.append2digits(buf, year);
233 //        }
234 //        else
235 //        {
236 //            buf.append(' ');
237 //            buf.append(MONTHS[month]);
238 //            buf.append(' ');
239 //            StringUtil.append2digits(buf, century);
240 //            StringUtil.append2digits(buf, year);
241 //        }
242 //        buf.append(' ');
243 //        StringUtil.append2digits(buf, hours);
244 //        buf.append(':');
245 //        StringUtil.append2digits(buf, minutes);
246 //        buf.append(':');
247 //        StringUtil.append2digits(buf, seconds);
248 //        buf.append(" GMT");
249 //    }
250 
251     public void setHeader(String name, String value)
252     {
253         message.setOutboundProperty(name, value);
254     }
255 
256     public void addHeader(String name, String value)
257     {
258         message.setOutboundProperty(name, value);
259     }
260 
261     public void setIntHeader(String name, int value)
262     {
263         message.setOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, value);
264     }
265 
266     public void addIntHeader(String name, int value)
267     {
268         message.setOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, value);
269     }
270 
271     public void setStatus(int sc)
272     {
273         message.setOutboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, sc);
274     }
275 
276     public void setStatus(int sc, String sm)
277     {
278     }
279 
280 }