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              String value = parameters.get(param);
100             if (value != null)
101             {
102                 str.append('=');
103                 QuoteUtil.quoteIfNeeded(str,value,";=");
104             }
105         }
106         return str.toString();
107     }
108 
109     public Set<String> getParameterKeys()
110     {
111         return parameters.keySet();
112     }
113 
114     /**
115      * Return parameters found in request URI.
116      * 
117      * @return the parameter map
118      */
119     public Map<String, String> getParameters()
120     {
121         return parameters;
122     }
123 
124     /**
125      * Initialize the parameters on this config from the other configuration.
126      * 
127      * @param other
128      *            the other configuration.
129      */
130     public void init(ExtensionConfig other)
131     {
132         this.parameters.clear();
133         this.parameters.putAll(other.parameters);
134     }
135 
136     public void setParameter(String key, int value)
137     {
138         parameters.put(key,Integer.toString(value));
139     }
140 
141     public void setParameter(String key, String value)
142     {
143         parameters.put(key,value);
144     }
145 
146     @Override
147     public String toString()
148     {
149         return getParameterizedName();
150     }
151 }