2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.remoting.impl;
19  
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  import javax.faces.FacesException;
23  import javax.faces.context.FacesContext;
24  import javax.faces.context.ResponseStream;
25  import javax.faces.context.ResponseWriter;
26  import javax.servlet.ServletOutputStream;
27  import javax.servlet.http.HttpServletResponse;
28  import org.apache.shale.remoting.faces.ResponseFactory;
29  
30  /***
31   * <p>Business object that includes methods to produce output, when
32   * invoked indirectly via <code>MethodBindingProcessor</code>.</p>
33   */
34  public class MethodBindingProcessorBusinessObject {
35      
36  
37      /***
38       * <p>Create binary output directy to the servlet response.</p>
39       */
40      public void directStream() {
41  
42          try {
43              FacesContext context = FacesContext.getCurrentInstance();
44              HttpServletResponse response = (HttpServletResponse)
45                context.getExternalContext().getResponse();
46              response.setContentType("application/x-binary");
47              ServletOutputStream stream = response.getOutputStream();
48              for (int i = 0; i < 10; i++) {
49                  stream.write(i);
50              }
51          } catch (IOException e) {
52              throw new FacesException(e);
53          }
54  
55      }
56  
57  
58      /***
59       * <p>Create character output directly to the servlet response.</p>
60       */
61      public void directWriter() {
62  
63          try {
64              FacesContext context = FacesContext.getCurrentInstance();
65              HttpServletResponse response = (HttpServletResponse)
66                context.getExternalContext().getResponse();
67              response.setContentType("text/x-plain");
68              PrintWriter writer = response.getWriter();
69              for (int i = 0; i < 10; i++) {
70                  writer.write('a' + i);
71              }
72          } catch (IOException e) {
73              throw new FacesException(e);
74          }
75  
76      }
77  
78  
79      /***
80       * <p>Create binary output indirecty via a factory.</p>
81       */
82      public void indirectStream() {
83  
84          try {
85              FacesContext context = FacesContext.getCurrentInstance();
86              ResponseStream stream =
87                (new ResponseFactory()).getResponseStream(context, "application/x-binary");
88              for (int i = 0; i < 10; i++) {
89                  stream.write(i);
90              }
91          } catch (IOException e) {
92              throw new FacesException(e);
93          }
94  
95      }
96  
97  
98      /***
99       * <p>Create character output indirectly a factory.</p>
100      */
101     public void indirectWriter() {
102 
103         try {
104             FacesContext context = FacesContext.getCurrentInstance();
105             ResponseWriter writer =
106               (new ResponseFactory()).getResponseWriter(context, "text/x-plain");
107             for (int i = 0; i < 10; i++) {
108                 writer.write('a' + i);
109             }
110         } catch (IOException e) {
111             throw new FacesException(e);
112         }
113 
114     }
115 
116 
117 }