View Javadoc

1   /*
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3    *
4    * Copyright (c) 2008-2010 Oracle and/or its affiliates. All rights reserved.
5    *
6    * The contents of this file are subject to the terms of either the GNU
7    * General Public License Version 2 only ("GPL") or the Common Development
8    * and Distribution License("CDDL") (collectively, the "License").  You
9    * may not use this file except in compliance with the License.  You can
10   * obtain a copy of the License at
11   * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12   * or packager/legal/LICENSE.txt.  See the License for the specific
13   * language governing permissions and limitations under the License.
14   *
15   * When distributing the software, include this License Header Notice in each
16   * file and include the License file at packager/legal/LICENSE.txt.
17   *
18   * GPL Classpath Exception:
19   * Oracle designates this particular file as subject to the "Classpath"
20   * exception as provided by Oracle in the GPL Version 2 section of the License
21   * file that accompanied this code.
22   *
23   * Modifications:
24   * If applicable, add the following below the License Header, with the fields
25   * enclosed by brackets [] replaced by your own identifying information:
26   * "Portions Copyright [year] [name of copyright owner]"
27   *
28   * Contributor(s):
29   * If you wish your version of this file to be governed by only the CDDL or
30   * only the GPL Version 2, indicate your decision by adding "[Contributor]
31   * elects to include this software in this distribution under the [CDDL or GPL
32   * Version 2] license."  If you don't indicate a single choice of license, a
33   * recipient has the option to distribute your version of this file under
34   * either the CDDL, the GPL Version 2 or to extend the choice of license to
35   * its licensees as provided above.  However, if you add GPL Version 2 code
36   * and therefore, elected the GPL Version 2 license, then the option applies
37   * only if the new code is made subject to such option by the copyright
38   * holder.
39   */
40  
41  package com.sun.grizzly.comet.handlers;
42  
43  import com.sun.grizzly.comet.CometEvent;
44  import com.sun.grizzly.comet.CometHandler;
45  import java.io.IOException;
46  import java.io.PrintWriter;
47  
48  /**
49   * Simple {@link CometHandler} that write (using a {@link PrintWriter})
50   * all messages ({@link CometEvent#attachment} it receive. This {@link CometHandler}
51   * just reflect everything, without filtering, the result of a {@link CometContext#notify).
52   * 
53   * @author Jeanfrancois Arcand
54   */
55  public class ReflectorCometHandler implements CometHandler<PrintWriter> {
56  
57      // Simple PrintWrite
58      protected PrintWriter printWriter;
59  
60      // Message send when the connection terminate or gets interrupted.
61      private String startingMessage = "";
62      
63      // Message send when the connection terminate or gets interrupted.
64      private String endingMessage = "";
65          
66      // Is http-streamin mode used 
67      private boolean useStreaming = true;
68  
69      
70      /**
71       * Create a reflector. 
72       */
73      public ReflectorCometHandler(){
74      }    
75      
76      /**
77       * Create a reflector. 
78       * @param useStreaming should the connection be resumed on the first push, 
79       *                     or never resumed from the {@link ReflectorCometHandler#onEvent}. Default is true. 
80       */
81      public ReflectorCometHandler(boolean useStreaming){
82          this.useStreaming = useStreaming;
83      }
84      
85      /**
86       * Create a reflector. 
87       * @param useStreaming should the connection be resumed on the first push, 
88       *                     or never resumed from the {@link ReflectorCometHandler#onEvent}. Default is true. 
89       * @param endingMessage Message send when the connection is about to be suspended
90       *                      ({@link ReflectorCometHandler#onInitialize})
91       *                      or gets interrupted ({@link ReflectorCometHandler#onInterrupt}) 
92       * @param endingMessage Message send when the connection terminate ({@link ReflectorCometHandler#onTerminate})
93       *                      or gets interrupted ({@link ReflectorCometHandler#onInterrupt})
94       */
95      public ReflectorCometHandler(boolean useStreaming, String startingMessage,String endingMessage){
96          this.useStreaming = useStreaming;
97          this.startingMessage = startingMessage;
98          this.endingMessage = endingMessage;
99      }
100     
101     /**
102      * Attach a {@link PrintWriter} that will be used to write the returned value of
103      * {@link CometEvent#attachment}
104      * 
105      * @param printWriter {@link PrintWriter} that will be used to write the returned value of
106      * {@link CometEvent#attachment}
107      */
108     public void attach(PrintWriter printWriter) {
109         this.printWriter = printWriter;
110     }
111 
112     /**
113      * Write {@link CometEvent#attachment} and resume the connection if 
114      * {@link ReflectorCometHandler#useStreaming} is <tt>false</tt>
115      * @param event
116      * @throws java.io.IOException
117      */
118     public void onEvent(CometEvent event) throws IOException {
119         try {
120             if (event.getType() != CometEvent.READ) {
121                 printWriter.println(event.attachment());
122                 printWriter.flush();
123                 
124                 if (!useStreaming){
125                     event.getCometContext().resumeCometHandler(this);
126                 }
127             }
128         } catch (Throwable t) {
129             throw new IOException(t.getMessage());
130         }
131     }
132 
133     /**
134      * Send the  {@link ReflectorCometHandler#startingMessage} before the
135      * connection get suspended.
136      * 
137      * @param event (@link CometEvent}
138      * @throws java.io.IOException
139      */
140     public void onInitialize(CometEvent event) throws IOException {
141         printWriter.println(startingMessage);
142         printWriter.flush();
143     }
144 
145     /**
146      * Send the  {@link ReflectorCometHandler#endingMessage} before the
147      * connection get terminated.
148      * 
149      * @param event (@link CometEvent}
150      * @throws java.io.IOException
151      */    
152     public void onTerminate(CometEvent event) throws IOException {
153         onInterrupt(event);
154     }
155     
156     /**
157      * Send the  {@link ReflectorCometHandler#endingMessage} before the
158      * connection get interupted.
159      * 
160      * @param event (@link CometEvent}
161      * @throws java.io.IOException
162      */
163     public void onInterrupt(CometEvent event) throws IOException {
164         printWriter.println(endingMessage);
165     }
166 }