View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.common.extensions;
20  
21  import java.io.IOException;
22  
23  import org.eclipse.jetty.io.ByteBufferPool;
24  import org.eclipse.jetty.util.annotation.ManagedAttribute;
25  import org.eclipse.jetty.util.annotation.ManagedObject;
26  import org.eclipse.jetty.util.component.ContainerLifeCycle;
27  import org.eclipse.jetty.util.log.Log;
28  import org.eclipse.jetty.util.log.Logger;
29  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
30  import org.eclipse.jetty.websocket.api.WriteCallback;
31  import org.eclipse.jetty.websocket.api.extensions.Extension;
32  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
33  import org.eclipse.jetty.websocket.api.extensions.Frame;
34  import org.eclipse.jetty.websocket.api.extensions.IncomingFrames;
35  import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames;
36  import org.eclipse.jetty.websocket.common.LogicalConnection;
37  
38  @ManagedObject("Abstract Extension")
39  public abstract class AbstractExtension extends ContainerLifeCycle implements Extension
40  {
41      private final Logger log;
42      private WebSocketPolicy policy;
43      private ByteBufferPool bufferPool;
44      private ExtensionConfig config;
45      private LogicalConnection connection;
46      private OutgoingFrames nextOutgoing;
47      private IncomingFrames nextIncoming;
48  
49      public AbstractExtension()
50      {
51          log = Log.getLogger(this.getClass());
52      }
53  
54      @Override
55      public void dump(Appendable out, String indent) throws IOException
56      {
57          super.dump(out, indent);
58          // incoming
59          dumpWithHeading(out, indent, "incoming", this.nextIncoming);
60          dumpWithHeading(out, indent, "outgoing", this.nextOutgoing);
61      }
62  
63      protected void dumpWithHeading(Appendable out, String indent, String heading, Object bean) throws IOException
64      {
65          out.append(indent).append(" +- ");
66          out.append(heading).append(" : ");
67          out.append(bean.toString());
68      }
69  
70      public ByteBufferPool getBufferPool()
71      {
72          return bufferPool;
73      }
74  
75      @Override
76      public ExtensionConfig getConfig()
77      {
78          return config;
79      }
80  
81      public LogicalConnection getConnection()
82      {
83          return connection;
84      }
85  
86      @Override
87      public String getName()
88      {
89          return config.getName();
90      }
91  
92      @ManagedAttribute(name = "Next Incoming Frame Handler", readonly = true)
93      public IncomingFrames getNextIncoming()
94      {
95          return nextIncoming;
96      }
97  
98      @ManagedAttribute(name = "Next Outgoing Frame Handler", readonly = true)
99      public OutgoingFrames getNextOutgoing()
100     {
101         return nextOutgoing;
102     }
103 
104     public WebSocketPolicy getPolicy()
105     {
106         return policy;
107     }
108 
109     @Override
110     public void incomingError(Throwable e)
111     {
112         nextIncomingError(e);
113     }
114 
115     /**
116      * Used to indicate that the extension makes use of the RSV1 bit of the base websocket framing.
117      * <p>
118      * This is used to adjust validation during parsing, as well as a checkpoint against 2 or more extensions all simultaneously claiming ownership of RSV1.
119      * 
120      * @return true if extension uses RSV1 for its own purposes.
121      */
122     @Override
123     public boolean isRsv1User()
124     {
125         return false;
126     }
127 
128     /**
129      * Used to indicate that the extension makes use of the RSV2 bit of the base websocket framing.
130      * <p>
131      * This is used to adjust validation during parsing, as well as a checkpoint against 2 or more extensions all simultaneously claiming ownership of RSV2.
132      * 
133      * @return true if extension uses RSV2 for its own purposes.
134      */
135     @Override
136     public boolean isRsv2User()
137     {
138         return false;
139     }
140 
141     /**
142      * Used to indicate that the extension makes use of the RSV3 bit of the base websocket framing.
143      * <p>
144      * This is used to adjust validation during parsing, as well as a checkpoint against 2 or more extensions all simultaneously claiming ownership of RSV3.
145      * 
146      * @return true if extension uses RSV3 for its own purposes.
147      */
148     @Override
149     public boolean isRsv3User()
150     {
151         return false;
152     }
153 
154     protected void nextIncomingError(Throwable e)
155     {
156         this.nextIncoming.incomingError(e);
157     }
158 
159     protected void nextIncomingFrame(Frame frame)
160     {
161         log.debug("nextIncomingFrame({})",frame);
162         this.nextIncoming.incomingFrame(frame);
163     }
164 
165     protected void nextOutgoingFrame(Frame frame, WriteCallback callback)
166     {
167         log.debug("nextOutgoingFrame({})",frame);
168         this.nextOutgoing.outgoingFrame(frame,callback);
169     }
170 
171     public void setBufferPool(ByteBufferPool bufferPool)
172     {
173         this.bufferPool = bufferPool;
174     }
175 
176     public void setConfig(ExtensionConfig config)
177     {
178         this.config = config;
179     }
180 
181     public void setConnection(LogicalConnection connection)
182     {
183         this.connection = connection;
184     }
185 
186     @Override
187     public void setNextIncomingFrames(IncomingFrames nextIncoming)
188     {
189         this.nextIncoming = nextIncoming;
190     }
191 
192     @Override
193     public void setNextOutgoingFrames(OutgoingFrames nextOutgoing)
194     {
195         this.nextOutgoing = nextOutgoing;
196     }
197 
198     public void setPolicy(WebSocketPolicy policy)
199     {
200         this.policy = policy;
201     }
202 
203     @Override
204     public String toString()
205     {
206         return String.format("%s[%s]",this.getClass().getSimpleName(),config.getParameterizedName());
207     }
208 }