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.api.extensions;
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.eclipse.jetty.websocket.api.util.QuoteUtil;
27  
28  /**
29   * Represents an Extension Configuration, as seen during the connection Handshake process.
30   */
31  public class ExtensionConfig
32  {
33      public static ExtensionConfig parse(String parameterizedName)
34      {
35          Iterator<String> extListIter = QuoteUtil.splitAt(parameterizedName,";");
36          String extToken = extListIter.next();
37  
38          ExtensionConfig ext = new ExtensionConfig(extToken);
39  
40          // now for parameters
41          while (extListIter.hasNext())
42          {
43              String extParam = extListIter.next();
44              Iterator<String> extParamIter = QuoteUtil.splitAt(extParam,"=");
45              String key = extParamIter.next().trim();
46              String value = null;
47              if (extParamIter.hasNext())
48              {
49                  value = extParamIter.next();
50              }
51              ext.setParameter(key,value);
52          }
53  
54          return ext;
55      }
56  
57      private final String name;
58      private Map<String, String> parameters;
59  
60      public ExtensionConfig(String name)
61      {
62          this.name = name;
63          this.parameters = new HashMap<>();
64      }
65  
66      public String getName()
67      {
68          return name;
69      }
70  
71      public int getParameter(String key, int defValue)
72      {
73          String val = parameters.get(key);
74          if (val == null)
75          {
76              return defValue;
77          }
78          return Integer.valueOf(val);
79      }
80  
81      public String getParameter(String key, String defValue)
82      {
83          String val = parameters.get(key);
84          if (val == null)
85          {
86              return defValue;
87          }
88          return val;
89      }
90  
91      public String getParameterizedName()
92      {
93          StringBuilder str = new StringBuilder();
94          str.append(name);
95          for (String param : parameters.keySet())
96          {
97              str.append(';');
98              str.append(param);
99              str.append('=');
100             QuoteUtil.quoteIfNeeded(str,parameters.get(param),";=");
101         }
102         return str.toString();
103     }
104 
105     public Set<String> getParameterKeys()
106     {
107         return parameters.keySet();
108     }
109 
110     /**
111      * Return parameters in way similar to how {@link javax.net.websocket.extensions.Extension#getParameters()} works.
112      * 
113      * @return the parameter map
114      */
115     public Map<String, String> getParameters()
116     {
117         return parameters;
118     }
119 
120     /**
121      * Initialize the parameters on this config from the other configuration.
122      * 
123      * @param other
124      *            the other configuration.
125      */
126     public void init(ExtensionConfig other)
127     {
128         this.parameters.clear();
129         this.parameters.putAll(other.parameters);
130     }
131 
132     public void setParameter(String key, int value)
133     {
134         parameters.put(key,Integer.toString(value));
135     }
136 
137     public void setParameter(String key, String value)
138     {
139         parameters.put(key,value);
140     }
141 
142     @Override
143     public String toString()
144     {
145         return getParameterizedName();
146     }
147 }