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;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.eclipse.jetty.io.Buffer;
26  import org.eclipse.jetty.util.QuotedStringTokenizer;
27  import org.eclipse.jetty.websocket.WebSocketParser.FrameHandler;
28  
29  public class AbstractExtension implements Extension
30  {
31      private static final int[] __mask = { -1, 0x04, 0x02, 0x01};
32      private final String _name;
33      private final Map<String,String> _parameters=new HashMap<String, String>();
34      private FrameHandler _inbound;
35      private WebSocketGenerator _outbound;
36      private WebSocket.FrameConnection _connection;
37      
38      public AbstractExtension(String name)
39      {
40          _name = name;
41      }
42      
43      public WebSocket.FrameConnection getConnection()
44      {
45          return _connection;
46      }
47  
48      public boolean init(Map<String, String> parameters)
49      {
50          _parameters.putAll(parameters);
51          return true;
52      }
53      
54      public String getInitParameter(String name)
55      {
56          return _parameters.get(name);
57      }
58  
59      public String getInitParameter(String name,String dft)
60      {
61          if (!_parameters.containsKey(name))
62              return dft;
63          return _parameters.get(name);
64      }
65  
66      public int getInitParameter(String name, int dft)
67      {
68          String v=_parameters.get(name);
69          if (v==null)
70              return dft;
71          return Integer.valueOf(v);
72      }
73      
74      
75      public void bind(WebSocket.FrameConnection connection, FrameHandler incoming, WebSocketGenerator outgoing)
76      {
77          _connection=connection;
78          _inbound=incoming;
79          _outbound=outgoing;
80      }
81  
82      public String getName()
83      {
84          return _name;
85      }
86  
87      public String getParameterizedName()
88      {
89          StringBuilder name = new StringBuilder();
90          name.append(_name);
91          for (String param : _parameters.keySet())
92              name.append(';').append(param).append('=').append(QuotedStringTokenizer.quoteIfNeeded(_parameters.get(param),";="));
93          return name.toString();
94      }
95  
96      public void onFrame(byte flags, byte opcode, Buffer buffer)
97      {
98          // System.err.printf("onFrame %s %x %x %d\n",getExtensionName(),flags,opcode,buffer.length());
99          _inbound.onFrame(flags,opcode,buffer);
100     }
101 
102     public void close(int code, String message)
103     {
104         _inbound.close(code,message);
105     }
106 
107     public int flush() throws IOException
108     {
109         return _outbound.flush();
110     }
111 
112     public boolean isBufferEmpty()
113     {
114         return _outbound.isBufferEmpty();
115     }
116 
117     public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException
118     {
119         // System.err.printf("addFrame %s %x %x %d\n",getExtensionName(),flags,opcode,length);
120         _outbound.addFrame(flags,opcode,content,offset,length);
121     }
122     
123     public byte setFlag(byte flags,int rsv)
124     {
125         if (rsv<1||rsv>3)
126             throw new IllegalArgumentException("rsv"+rsv);
127         byte b=(byte)(flags | __mask[rsv]);
128         return b;
129     }
130     
131     public byte clearFlag(byte flags,int rsv)
132     {
133         if (rsv<1||rsv>3)
134             throw new IllegalArgumentException("rsv"+rsv);
135         return (byte)(flags & ~__mask[rsv]);
136     }
137 
138     public boolean isFlag(byte flags,int rsv)
139     {
140         if (rsv<1||rsv>3)
141             throw new IllegalArgumentException("rsv"+rsv);
142         return (flags & __mask[rsv])!=0;
143     }
144     
145     public String toString()
146     {
147         return getParameterizedName();
148     }
149 }