View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2011 Intalio, Inc.
3    * ======================================================================
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * and Apache License v2.0 which accompanies this distribution.
7    *
8    *   The Eclipse Public License is available at
9    *   http://www.eclipse.org/legal/epl-v10.html
10   *
11   *   The Apache License v2.0 is available at
12   *   http://www.opensource.org/licenses/apache2.0.php
13   *
14   * You may elect to redistribute this code under either of these licenses.
15   *******************************************************************************/
16  package org.eclipse.jetty.websocket;
17  
18  import java.io.IOException;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.eclipse.jetty.io.Buffer;
23  import org.eclipse.jetty.util.QuotedStringTokenizer;
24  import org.eclipse.jetty.websocket.WebSocketParser.FrameHandler;
25  
26  public class AbstractExtension implements Extension
27  {
28      private static final int[] __mask = { -1, 0x04, 0x02, 0x01};
29      private final String _name;
30      private final Map<String,String> _parameters=new HashMap<String, String>();
31      private FrameHandler _inbound;
32      private WebSocketGenerator _outbound;
33      private WebSocket.FrameConnection _connection;
34      
35      public AbstractExtension(String name)
36      {
37          _name = name;
38      }
39      
40      public WebSocket.FrameConnection getConnection()
41      {
42          return _connection;
43      }
44  
45      public boolean init(Map<String, String> parameters)
46      {
47          _parameters.putAll(parameters);
48          return true;
49      }
50      
51      public String getInitParameter(String name)
52      {
53          return _parameters.get(name);
54      }
55  
56      public String getInitParameter(String name,String dft)
57      {
58          if (!_parameters.containsKey(name))
59              return dft;
60          return _parameters.get(name);
61      }
62  
63      public int getInitParameter(String name, int dft)
64      {
65          String v=_parameters.get(name);
66          if (v==null)
67              return dft;
68          return Integer.valueOf(v);
69      }
70      
71      
72      public void bind(WebSocket.FrameConnection connection, FrameHandler incoming, WebSocketGenerator outgoing)
73      {
74          _connection=connection;
75          _inbound=incoming;
76          _outbound=outgoing;
77      }
78  
79      public String getName()
80      {
81          return _name;
82      }
83  
84      public String getParameterizedName()
85      {
86          StringBuilder name = new StringBuilder();
87          name.append(_name);
88          for (String param : _parameters.keySet())
89              name.append(';').append(param).append('=').append(QuotedStringTokenizer.quoteIfNeeded(_parameters.get(param),";="));
90          return name.toString();
91      }
92  
93      public void onFrame(byte flags, byte opcode, Buffer buffer)
94      {
95          // System.err.printf("onFrame %s %x %x %d\n",getExtensionName(),flags,opcode,buffer.length());
96          _inbound.onFrame(flags,opcode,buffer);
97      }
98  
99      public void close(int code, String message)
100     {
101         _inbound.close(code,message);
102     }
103 
104     public int flush() throws IOException
105     {
106         return _outbound.flush();
107     }
108 
109     public boolean isBufferEmpty()
110     {
111         return _outbound.isBufferEmpty();
112     }
113 
114     public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException
115     {
116         // System.err.printf("addFrame %s %x %x %d\n",getExtensionName(),flags,opcode,length);
117         _outbound.addFrame(flags,opcode,content,offset,length);
118     }
119     
120     public byte setFlag(byte flags,int rsv)
121     {
122         if (rsv<1||rsv>3)
123             throw new IllegalArgumentException("rsv"+rsv);
124         byte b=(byte)(flags | __mask[rsv]);
125         return b;
126     }
127     
128     public byte clearFlag(byte flags,int rsv)
129     {
130         if (rsv<1||rsv>3)
131             throw new IllegalArgumentException("rsv"+rsv);
132         return (byte)(flags & ~__mask[rsv]);
133     }
134 
135     public boolean isFlag(byte flags,int rsv)
136     {
137         if (rsv<1||rsv>3)
138             throw new IllegalArgumentException("rsv"+rsv);
139         return (flags & __mask[rsv])!=0;
140     }
141     
142     public String toString()
143     {
144         return getParameterizedName();
145     }
146 }