View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.BatchMode;
30  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
31  import org.eclipse.jetty.websocket.api.WriteCallback;
32  import org.eclipse.jetty.websocket.api.extensions.Extension;
33  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
34  import org.eclipse.jetty.websocket.api.extensions.Frame;
35  import org.eclipse.jetty.websocket.api.extensions.IncomingFrames;
36  import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames;
37  import org.eclipse.jetty.websocket.common.LogicalConnection;
38  import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
39  
40  @ManagedObject("Abstract Extension")
41  public abstract class AbstractExtension extends ContainerLifeCycle implements Extension
42  {
43      private final Logger log;
44      private WebSocketPolicy policy;
45      private ByteBufferPool bufferPool;
46      private ExtensionConfig config;
47      private LogicalConnection connection;
48      private OutgoingFrames nextOutgoing;
49      private IncomingFrames nextIncoming;
50  
51      public AbstractExtension()
52      {
53          log = Log.getLogger(this.getClass());
54      }
55  
56      @Override
57      public void dump(Appendable out, String indent) throws IOException
58      {
59          super.dump(out, indent);
60          // incoming
61          dumpWithHeading(out, indent, "incoming", this.nextIncoming);
62          dumpWithHeading(out, indent, "outgoing", this.nextOutgoing);
63      }
64  
65      protected void dumpWithHeading(Appendable out, String indent, String heading, Object bean) throws IOException
66      {
67          out.append(indent).append(" +- ");
68          out.append(heading).append(" : ");
69          out.append(bean.toString());
70      }
71      
72      public void init(WebSocketContainerScope container)
73      {
74          this.policy = container.getPolicy();
75          this.bufferPool = container.getBufferPool();
76      }
77  
78      public ByteBufferPool getBufferPool()
79      {
80          return bufferPool;
81      }
82  
83      @Override
84      public ExtensionConfig getConfig()
85      {
86          return config;
87      }
88  
89      public LogicalConnection getConnection()
90      {
91          return connection;
92      }
93  
94      @Override
95      public String getName()
96      {
97          return config.getName();
98      }
99  
100     @ManagedAttribute(name = "Next Incoming Frame Handler", readonly = true)
101     public IncomingFrames getNextIncoming()
102     {
103         return nextIncoming;
104     }
105 
106     @ManagedAttribute(name = "Next Outgoing Frame Handler", readonly = true)
107     public OutgoingFrames getNextOutgoing()
108     {
109         return nextOutgoing;
110     }
111 
112     public WebSocketPolicy getPolicy()
113     {
114         return policy;
115     }
116 
117     @Override
118     public void incomingError(Throwable e)
119     {
120         nextIncomingError(e);
121     }
122 
123     /**
124      * Used to indicate that the extension makes use of the RSV1 bit of the base websocket framing.
125      * <p>
126      * This is used to adjust validation during parsing, as well as a checkpoint against 2 or more extensions all simultaneously claiming ownership of RSV1.
127      * 
128      * @return true if extension uses RSV1 for its own purposes.
129      */
130     @Override
131     public boolean isRsv1User()
132     {
133         return false;
134     }
135 
136     /**
137      * Used to indicate that the extension makes use of the RSV2 bit of the base websocket framing.
138      * <p>
139      * This is used to adjust validation during parsing, as well as a checkpoint against 2 or more extensions all simultaneously claiming ownership of RSV2.
140      * 
141      * @return true if extension uses RSV2 for its own purposes.
142      */
143     @Override
144     public boolean isRsv2User()
145     {
146         return false;
147     }
148 
149     /**
150      * Used to indicate that the extension makes use of the RSV3 bit of the base websocket framing.
151      * <p>
152      * This is used to adjust validation during parsing, as well as a checkpoint against 2 or more extensions all simultaneously claiming ownership of RSV3.
153      * 
154      * @return true if extension uses RSV3 for its own purposes.
155      */
156     @Override
157     public boolean isRsv3User()
158     {
159         return false;
160     }
161 
162     protected void nextIncomingError(Throwable e)
163     {
164         this.nextIncoming.incomingError(e);
165     }
166 
167     protected void nextIncomingFrame(Frame frame)
168     {
169         log.debug("nextIncomingFrame({})",frame);
170         this.nextIncoming.incomingFrame(frame);
171     }
172 
173     protected void nextOutgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode)
174     {
175         log.debug("nextOutgoingFrame({})",frame);
176         this.nextOutgoing.outgoingFrame(frame,callback, batchMode);
177     }
178 
179     public void setBufferPool(ByteBufferPool bufferPool)
180     {
181         this.bufferPool = bufferPool;
182     }
183 
184     public void setConfig(ExtensionConfig config)
185     {
186         this.config = config;
187     }
188 
189     public void setConnection(LogicalConnection connection)
190     {
191         this.connection = connection;
192     }
193     
194     @Override
195     public void setNextIncomingFrames(IncomingFrames nextIncoming)
196     {
197         this.nextIncoming = nextIncoming;
198     }
199 
200     @Override
201     public void setNextOutgoingFrames(OutgoingFrames nextOutgoing)
202     {
203         this.nextOutgoing = nextOutgoing;
204     }
205 
206     public void setPolicy(WebSocketPolicy policy)
207     {
208         this.policy = policy;
209     }
210 
211     @Override
212     public String toString()
213     {
214         return String.format("%s[%s]",this.getClass().getSimpleName(),config.getParameterizedName());
215     }
216 }