Issue #3458 - ExtensionConfig now delegates to core implementation
ExtensionConfig is now interface in jetty-websocket-api and implemented in jetty-websocket-common, for its static methods it now uses a ExtensionConfig.Parser found by the ServiceLoader the api tests were moved to jetty-websocket-tests Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
38787f619d
commit
86675422a6
|
@ -18,236 +18,70 @@
|
|||
|
||||
package org.eclipse.jetty.websocket.api.extensions;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.util.QuoteUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Represents an Extension Configuration, as seen during the connection Handshake process.
|
||||
*/
|
||||
public class ExtensionConfig
|
||||
public interface ExtensionConfig
|
||||
{
|
||||
/**
|
||||
* Parse a single parameterized name.
|
||||
*
|
||||
* @param parameterizedName the parameterized name
|
||||
* @return the ExtensionConfig
|
||||
*/
|
||||
public static ExtensionConfig parse(String parameterizedName)
|
||||
interface Parser
|
||||
{
|
||||
return new ExtensionConfig(parameterizedName);
|
||||
ExtensionConfig parse(String parameterizedName);
|
||||
List<ExtensionConfig> parseEnum(Enumeration<String> valuesEnum);
|
||||
List<ExtensionConfig> parseList(String... rawSecWebSocketExtensions);
|
||||
String toHeaderValue(List<ExtensionConfig> configs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse enumeration of {@code Sec-WebSocket-Extensions} header values into a {@code ExtensionConfig} list
|
||||
*
|
||||
* @param valuesEnum the raw header values enum
|
||||
* @return the list of extension configs
|
||||
*/
|
||||
public static List<ExtensionConfig> parseEnum(Enumeration<String> valuesEnum)
|
||||
private static ExtensionConfig.Parser getParser()
|
||||
{
|
||||
List<ExtensionConfig> configs = new ArrayList<>();
|
||||
|
||||
if (valuesEnum != null)
|
||||
{
|
||||
while (valuesEnum.hasMoreElements())
|
||||
{
|
||||
Iterator<String> extTokenIter = QuoteUtil.splitAt(valuesEnum.nextElement(), ",");
|
||||
while (extTokenIter.hasNext())
|
||||
{
|
||||
String extToken = extTokenIter.next();
|
||||
configs.add(ExtensionConfig.parse(extToken));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return configs;
|
||||
return ServiceLoader.load(ExtensionConfig.Parser.class).findFirst().get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse 1 or more raw {@code Sec-WebSocket-Extensions} header values into a {@code ExtensionConfig} list
|
||||
*
|
||||
* @param rawSecWebSocketExtensions the raw header values
|
||||
* @return the list of extension configs
|
||||
*/
|
||||
public static List<ExtensionConfig> parseList(String... rawSecWebSocketExtensions)
|
||||
static ExtensionConfig parse(String parameterizedName)
|
||||
{
|
||||
List<ExtensionConfig> configs = new ArrayList<>();
|
||||
|
||||
for (String rawValue : rawSecWebSocketExtensions)
|
||||
{
|
||||
Iterator<String> extTokenIter = QuoteUtil.splitAt(rawValue, ",");
|
||||
while (extTokenIter.hasNext())
|
||||
{
|
||||
String extToken = extTokenIter.next();
|
||||
configs.add(ExtensionConfig.parse(extToken));
|
||||
}
|
||||
}
|
||||
|
||||
return configs;
|
||||
return getParser().parse(parameterizedName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a list of {@code ExtensionConfig} to a header value
|
||||
*
|
||||
* @param configs the list of extension configs
|
||||
* @return the header value (null if no configs present)
|
||||
*/
|
||||
public static String toHeaderValue(List<ExtensionConfig> configs)
|
||||
static List<ExtensionConfig> parseEnum(Enumeration<String> valuesEnum)
|
||||
{
|
||||
if ((configs == null) || (configs.isEmpty()))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
StringBuilder parameters = new StringBuilder();
|
||||
boolean needsDelim = false;
|
||||
for (ExtensionConfig ext : configs)
|
||||
{
|
||||
if (needsDelim)
|
||||
{
|
||||
parameters.append(", ");
|
||||
}
|
||||
parameters.append(ext.getParameterizedName());
|
||||
needsDelim = true;
|
||||
}
|
||||
return parameters.toString();
|
||||
return getParser().parseEnum(valuesEnum);
|
||||
}
|
||||
|
||||
private final String name;
|
||||
private final Map<String, String> parameters;
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
*
|
||||
* @param copy the extension config to copy
|
||||
*/
|
||||
public ExtensionConfig(ExtensionConfig copy)
|
||||
static List<ExtensionConfig> parseList(String... rawSecWebSocketExtensions)
|
||||
{
|
||||
this.name = copy.name;
|
||||
this.parameters = new HashMap<>();
|
||||
this.parameters.putAll(copy.parameters);
|
||||
return getParser().parseList(rawSecWebSocketExtensions);
|
||||
}
|
||||
|
||||
public ExtensionConfig(String parameterizedName)
|
||||
static String toHeaderValue(List<ExtensionConfig> configs)
|
||||
{
|
||||
Iterator<String> extListIter = QuoteUtil.splitAt(parameterizedName, ";");
|
||||
this.name = extListIter.next();
|
||||
this.parameters = new HashMap<>();
|
||||
|
||||
// now for parameters
|
||||
while (extListIter.hasNext())
|
||||
{
|
||||
String extParam = extListIter.next();
|
||||
Iterator<String> extParamIter = QuoteUtil.splitAt(extParam, "=");
|
||||
String key = extParamIter.next().trim();
|
||||
String value = null;
|
||||
if (extParamIter.hasNext())
|
||||
{
|
||||
value = extParamIter.next();
|
||||
}
|
||||
parameters.put(key, value);
|
||||
}
|
||||
return getParser().toHeaderValue(configs);
|
||||
}
|
||||
|
||||
public ExtensionConfig(String name, Map<String, String> parameters)
|
||||
{
|
||||
this.name = name;
|
||||
this.parameters = parameters;
|
||||
}
|
||||
String getName();
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
int getParameter(String key, int defValue);
|
||||
|
||||
public final int getParameter(String key, int defValue)
|
||||
{
|
||||
String val = parameters.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
return Integer.parseInt(val);
|
||||
}
|
||||
String getParameter(String key, String defValue);
|
||||
|
||||
public final String getParameter(String key, String defValue)
|
||||
{
|
||||
String val = parameters.get(key);
|
||||
if (val == null)
|
||||
{
|
||||
return defValue;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
String getParameterizedName();
|
||||
|
||||
public final String getParameterizedName()
|
||||
{
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.append(name);
|
||||
for (String param : parameters.keySet())
|
||||
{
|
||||
str.append(';');
|
||||
str.append(param);
|
||||
String value = parameters.get(param);
|
||||
if (value != null)
|
||||
{
|
||||
str.append('=');
|
||||
QuoteUtil.quoteIfNeeded(str, value, ";=");
|
||||
}
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public final Set<String> getParameterKeys()
|
||||
{
|
||||
return parameters.keySet();
|
||||
}
|
||||
Set<String> getParameterKeys();
|
||||
|
||||
/**
|
||||
* Return parameters found in request URI.
|
||||
*
|
||||
* @return the parameter map
|
||||
*/
|
||||
public final Map<String, String> getParameters()
|
||||
{
|
||||
return parameters;
|
||||
}
|
||||
Map<String, String> getParameters();
|
||||
|
||||
/**
|
||||
* Initialize the parameters on this config from the other configuration.
|
||||
*
|
||||
* @param other the other configuration.
|
||||
*/
|
||||
public final void init(ExtensionConfig other)
|
||||
{
|
||||
this.parameters.clear();
|
||||
this.parameters.putAll(other.parameters);
|
||||
}
|
||||
void setParameter(String key);
|
||||
|
||||
public final void setParameter(String key)
|
||||
{
|
||||
parameters.put(key, null);
|
||||
}
|
||||
void setParameter(String key, int value);
|
||||
|
||||
public final void setParameter(String key, int value)
|
||||
{
|
||||
parameters.put(key, Integer.toString(value));
|
||||
}
|
||||
|
||||
public final void setParameter(String key, String value)
|
||||
{
|
||||
parameters.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return getParameterizedName();
|
||||
}
|
||||
void setParameter(String key, String value);
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
// ========================================================================
|
||||
//
|
||||
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.common.ExtensionConfigParser;
|
||||
|
||||
module org.eclipse.jetty.websocket.jetty.common
|
||||
{
|
||||
exports org.eclipse.jetty.websocket.common;
|
||||
|
@ -27,4 +30,6 @@ module org.eclipse.jetty.websocket.jetty.common
|
|||
requires org.eclipse.jetty.util;
|
||||
requires org.eclipse.jetty.websocket.core;
|
||||
requires org.eclipse.jetty.websocket.jetty.api;
|
||||
|
||||
provides ExtensionConfig.Parser with ExtensionConfigParser;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.eclipse.jetty.websocket.core.ExtensionConfig;
|
||||
|
||||
public class ExtensionConfigParser implements org.eclipse.jetty.websocket.api.extensions.ExtensionConfig.Parser
|
||||
{
|
||||
/**
|
||||
* Parse a single parameterized name.
|
||||
*
|
||||
* @param parameterizedName the parameterized name
|
||||
* @return the ExtensionConfig
|
||||
*/
|
||||
@Override
|
||||
public JettyExtensionConfig parse(String parameterizedName)
|
||||
{
|
||||
return new JettyExtensionConfig(ExtensionConfig.parse(parameterizedName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse enumeration of {@code Sec-WebSocket-Extensions} header values into a {@code ExtensionConfig} list
|
||||
*
|
||||
* @param valuesEnum the raw header values enum
|
||||
* @return the list of extension configs
|
||||
*/
|
||||
@Override
|
||||
public List<org.eclipse.jetty.websocket.api.extensions.ExtensionConfig> parseEnum(Enumeration<String> valuesEnum)
|
||||
{
|
||||
List<org.eclipse.jetty.websocket.api.extensions.ExtensionConfig> configs = new ArrayList<>();
|
||||
for (ExtensionConfig config : ExtensionConfig.parseEnum(valuesEnum))
|
||||
configs.add(new JettyExtensionConfig(config));
|
||||
return configs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse 1 or more raw {@code Sec-WebSocket-Extensions} header values into a {@code ExtensionConfig} list
|
||||
*
|
||||
* @param rawSecWebSocketExtensions the raw header values
|
||||
* @return the list of extension configs
|
||||
*/
|
||||
@Override
|
||||
public List<org.eclipse.jetty.websocket.api.extensions.ExtensionConfig> parseList(String... rawSecWebSocketExtensions)
|
||||
{
|
||||
List<org.eclipse.jetty.websocket.api.extensions.ExtensionConfig> configs = new ArrayList<>();
|
||||
for (ExtensionConfig config : ExtensionConfig.parseList(rawSecWebSocketExtensions))
|
||||
configs.add(new JettyExtensionConfig(config));
|
||||
return configs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a list of {@code ExtensionConfig} to a header value
|
||||
*
|
||||
* @param configs the list of extension configs
|
||||
* @return the header value (null if no configs present)
|
||||
*/
|
||||
@Override
|
||||
public String toHeaderValue(List<org.eclipse.jetty.websocket.api.extensions.ExtensionConfig> configs)
|
||||
{
|
||||
return ExtensionConfig.toHeaderValue(configs.stream()
|
||||
.map(c->new ExtensionConfig(c.getName(), c.getParameters()))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.common;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jetty.websocket.core.ExtensionConfig;
|
||||
|
||||
/**
|
||||
* Represents an Extension Configuration, as seen during the connection Handshake process.
|
||||
*/
|
||||
public class JettyExtensionConfig implements org.eclipse.jetty.websocket.api.extensions.ExtensionConfig
|
||||
{
|
||||
|
||||
private final ExtensionConfig config;
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
*
|
||||
* @param copy the extension config to copy
|
||||
*/
|
||||
public JettyExtensionConfig(JettyExtensionConfig copy)
|
||||
{
|
||||
this(copy.config);
|
||||
}
|
||||
|
||||
public JettyExtensionConfig(ExtensionConfig config)
|
||||
{
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public JettyExtensionConfig(String parameterizedName)
|
||||
{
|
||||
this.config = new ExtensionConfig(parameterizedName);
|
||||
}
|
||||
|
||||
public JettyExtensionConfig(String name, Map<String, String> parameters)
|
||||
{
|
||||
this.config = new ExtensionConfig(name, parameters);
|
||||
}
|
||||
|
||||
public ExtensionConfig getCoreConfig()
|
||||
{
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return config.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int getParameter(String key, int defValue)
|
||||
{
|
||||
return config.getParameter(key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getParameter(String key, String defValue)
|
||||
{
|
||||
return config.getParameter(key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getParameterizedName()
|
||||
{
|
||||
return config.getParameterizedName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Set<String> getParameterKeys()
|
||||
{
|
||||
return config.getParameterKeys();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return parameters found in request URI.
|
||||
*
|
||||
* @return the parameter map
|
||||
*/
|
||||
@Override
|
||||
public final Map<String, String> getParameters()
|
||||
{
|
||||
return config.getParameters();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setParameter(String key)
|
||||
{
|
||||
config.setParameter(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setParameter(String key, int value)
|
||||
{
|
||||
config.setParameter(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setParameter(String key, String value)
|
||||
{
|
||||
config.setParameter(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return config.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
org.eclipse.jetty.websocket.common.ExtensionConfigParser
|
|
@ -27,6 +27,7 @@ import java.util.stream.Collectors;
|
|||
|
||||
import org.eclipse.jetty.websocket.api.UpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.common.JettyExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
|
||||
|
||||
public class DelegatedJettyServletUpgradeRequest implements UpgradeRequest
|
||||
|
@ -60,7 +61,7 @@ public class DelegatedJettyServletUpgradeRequest implements UpgradeRequest
|
|||
public List<ExtensionConfig> getExtensions()
|
||||
{
|
||||
return this.servletRequest.getExtensions().stream()
|
||||
.map((ext) -> new org.eclipse.jetty.websocket.api.extensions.ExtensionConfig(ext.getName(), ext.getParameters()))
|
||||
.map((ext) -> new JettyExtensionConfig(ext.getName(), ext.getParameters()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
|
|
@ -18,16 +18,17 @@
|
|||
|
||||
package org.eclipse.jetty.websocket.server.internal;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.UpgradeResponse;
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.UpgradeResponse;
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.common.JettyExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
|
||||
|
||||
public class UpgradeResponseAdapter implements UpgradeResponse
|
||||
{
|
||||
private final ServletUpgradeResponse servletResponse;
|
||||
|
@ -53,7 +54,7 @@ public class UpgradeResponseAdapter implements UpgradeResponse
|
|||
public List<ExtensionConfig> getExtensions()
|
||||
{
|
||||
return this.servletResponse.getExtensions().stream()
|
||||
.map((ext) -> new org.eclipse.jetty.websocket.api.extensions.ExtensionConfig(ext.getName(), ext.getParameters()))
|
||||
.map((ext) -> new JettyExtensionConfig(ext.getName(), ext.getParameters()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
|
|
@ -16,13 +16,14 @@
|
|||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.api.extensions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
package org.eclipse.jetty.websocket.tests.extensions;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
|
@ -16,13 +16,14 @@
|
|||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.api.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
package org.eclipse.jetty.websocket.tests.util;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.util.QuoteUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
@ -16,16 +16,17 @@
|
|||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.api.util;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
package org.eclipse.jetty.websocket.tests.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.util.QuoteUtil;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
|
@ -16,13 +16,14 @@
|
|||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.api.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
package org.eclipse.jetty.websocket.tests.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.util.WSURI;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
Loading…
Reference in New Issue