View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.jsr356;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.websocket.Extension;
26  
27  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
28  import org.eclipse.jetty.websocket.api.util.QuoteUtil;
29  
30  public class JsrExtension implements Extension
31  {
32      private static class JsrParameter implements Extension.Parameter
33      {
34          private String name;
35          private String value;
36  
37          private JsrParameter(String key, String value)
38          {
39              this.name = key;
40              this.value = value;
41          }
42  
43          @Override
44          public String getName()
45          {
46              return this.name;
47          }
48  
49          @Override
50          public String getValue()
51          {
52              return this.value;
53          }
54      }
55  
56      private final String name;
57      private List<Parameter> parameters = new ArrayList<>();
58  
59      /**
60       * A configured extension
61       * @param cfg the configuration for the extension 
62       */
63      public JsrExtension(ExtensionConfig cfg)
64      {
65          this.name = cfg.getName();
66          if (cfg.getParameters() != null)
67          {
68              for (Map.Entry<String, String> entry : cfg.getParameters().entrySet())
69              {
70                  parameters.add(new JsrParameter(entry.getKey(),entry.getValue()));
71              }
72          }
73      }
74  
75      /**
76       * A potential (unconfigured) extension
77       * @param name the name of the extension
78       */
79      public JsrExtension(String name)
80      {
81          this.name = name;
82      }
83  
84      @Override
85      public String getName()
86      {
87          return name;
88      }
89  
90      @Override
91      public List<Parameter> getParameters()
92      {
93          return parameters;
94      }
95  
96      @Override
97      public String toString()
98      {
99          StringBuilder str = new StringBuilder();
100         str.append(name);
101         for (Parameter param : parameters)
102         {
103             str.append(';');
104             str.append(param.getName());
105             String value = param.getValue();
106             if (value != null)
107             {
108                 str.append('=');
109                 QuoteUtil.quoteIfNeeded(str,value,";=");
110             }
111         }
112         return str.toString();
113     }
114 }