diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/ExtensionConfig.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/ExtensionConfig.java new file mode 100644 index 00000000000..ad56f10f8fe --- /dev/null +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/ExtensionConfig.java @@ -0,0 +1,98 @@ +package org.eclipse.jetty.websocket.api; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jetty.util.QuotedStringTokenizer; + +/** + * Proposed interface for API (not yet settled) + */ +public class ExtensionConfig +{ + public static ExtensionConfig parse(String parameterizedName) + { + QuotedStringTokenizer tok = new QuotedStringTokenizer(parameterizedName,";"); + + ExtensionConfig ext = new ExtensionConfig(tok.nextToken().trim()); + + while (tok.hasMoreTokens()) + { + QuotedStringTokenizer nv = new QuotedStringTokenizer(tok.nextToken().trim(),"="); + String key = nv.nextToken().trim(); + String value = nv.hasMoreTokens()?nv.nextToken().trim():null; + ext.setParameter(key,value); + } + + return ext; + } + + private final String name; + private Map parameters; + + public ExtensionConfig(String name) + { + this.name = name; + this.parameters = new HashMap<>(); + } + + public String getName() + { + return name; + } + + public int getParameter(String key, int defValue) + { + String val = parameters.get(key); + if(val == null) { + return defValue; + } + return Integer.valueOf(val); + } + + public String getParameter(String key, String defValue) + { + String val = parameters.get(key); + if(val == null) { + return defValue; + } + return val; + } + + public String getParameterizedName() + { + StringBuilder str = new StringBuilder(); + str.append(name); + for (String param : parameters.keySet()) + { + str.append(';').append(param).append('=').append(QuotedStringTokenizer.quoteIfNeeded(parameters.get(param),";=")); + } + return str.toString(); + } + + /** + * Initialize the parameters on this config from the other configuration. + * @param other the other configuration. + */ + public void init(ExtensionConfig other) + { + this.parameters.clear(); + this.parameters.putAll(other.parameters); + } + + public void setParameter(String key, int value) + { + parameters.put(key,Integer.toString(value)); + } + + public void setParameter(String key, String value) + { + parameters.put(key,value); + } + + @Override + public String toString() + { + return getParameterizedName(); + } +} \ No newline at end of file diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/ExtensionRef.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/ExtensionRef.java deleted file mode 100644 index f0014b98925..00000000000 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/ExtensionRef.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.eclipse.jetty.websocket.api; - -import java.util.Map; - -/** - * Proposed interface for API (not yet settled) - */ -public interface ExtensionRef -{ - String getName(); - - Map getParameters(); -} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/AbstractExtension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/AbstractExtension.java index ad615671f0b..fe460689ad5 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/AbstractExtension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/AbstractExtension.java @@ -15,64 +15,40 @@ *******************************************************************************/ package org.eclipse.jetty.websocket.extensions; -import java.nio.ByteBuffer; -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.jetty.util.QuotedStringTokenizer; +import org.eclipse.jetty.websocket.api.ExtensionConfig; public class AbstractExtension implements Extension { - private final String name; - private final Map parameters = new HashMap(); + private final ExtensionConfig config; public AbstractExtension(String name) { - this.name = name; + this.config = new ExtensionConfig(name); } - public int getInitParameter(String name, int dft) + @Override + public ExtensionConfig getConfig() { - String v = parameters.get(name); - if (v==null) - { - return dft; - } - return Integer.valueOf(v); - } - - public String getInitParameter(String name,String dft) - { - if (!parameters.containsKey(name)) - { - return dft; - } - return parameters.get(name); + // TODO Auto-generated method stub + return null; } @Override public String getName() { - return name; + return config.getName(); } @Override public String getParameterizedName() { - StringBuilder name = new StringBuilder(); - name.append(name); - for (String param : parameters.keySet()) - { - name.append(';').append(param).append('=').append(QuotedStringTokenizer.quoteIfNeeded(parameters.get(param),";=")); - } - return name.toString(); + return config.getParameterizedName(); } @Override - public boolean init(Map parameters) + public void setConfig(ExtensionConfig config) { - parameters.putAll(parameters); - return true; + this.config.init(config); } @Override diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/Extension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/Extension.java index 88dfe55547a..715b2d1385c 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/Extension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/Extension.java @@ -15,12 +15,12 @@ *******************************************************************************/ package org.eclipse.jetty.websocket.extensions; -import java.util.Map; +import org.eclipse.jetty.websocket.api.ExtensionConfig; public interface Extension { + public ExtensionConfig getConfig(); public String getName(); public String getParameterizedName(); - - public boolean init(Map parameters); + public void setConfig(ExtensionConfig config); } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/deflate/DeflateFrameExtension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/deflate/DeflateFrameExtension.java index 13b2f69afbb..b8bde1b2ab4 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/deflate/DeflateFrameExtension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/deflate/DeflateFrameExtension.java @@ -15,12 +15,12 @@ *******************************************************************************/ package org.eclipse.jetty.websocket.extensions.deflate; -import java.util.Map; import java.util.zip.Deflater; import java.util.zip.Inflater; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.websocket.api.ExtensionConfig; import org.eclipse.jetty.websocket.extensions.AbstractExtension; /** @@ -40,22 +40,14 @@ public class DeflateFrameExtension extends AbstractExtension } @Override - public boolean init(Map parameters) + public void setConfig(ExtensionConfig config) { - if (!parameters.containsKey("minLength")) - { - parameters.put("minLength",Integer.toString(_minLength)); - } - if(super.init(parameters)) - { - _minLength=getInitParameter("minLength",_minLength); + super.setConfig(config); - _deflater=new Deflater(); - _inflater=new Inflater(); + _minLength = config.getParameter("minLength",_minLength); - return true; - } - return false; + _deflater = new Deflater(); + _inflater = new Inflater(); } /* (non-Javadoc) @@ -105,7 +97,7 @@ public class DeflateFrameExtension extends AbstractExtension getConnection().close(WebSocketConnectionRFC6455.CLOSE_BAD_PAYLOAD,e.toString()); } } - */ + */ /* (non-Javadoc) * @see org.eclipse.jetty.websocket.AbstractExtension#addFrame(byte, byte, byte[], int, int) @@ -160,5 +152,5 @@ public class DeflateFrameExtension extends AbstractExtension else super.addFrame(clearFlag(flags,1),opcode,content,offset,length); } - */ + */ } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/fragment/FragmentExtension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/fragment/FragmentExtension.java index 42bb37435d7..163c321a0e1 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/fragment/FragmentExtension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/extensions/fragment/FragmentExtension.java @@ -15,31 +15,27 @@ *******************************************************************************/ package org.eclipse.jetty.websocket.extensions.fragment; -import java.io.IOException; -import java.util.Map; - +import org.eclipse.jetty.websocket.api.ExtensionConfig; import org.eclipse.jetty.websocket.extensions.AbstractExtension; public class FragmentExtension extends AbstractExtension { private int _maxLength=-1; private int _minFragments=1; - + public FragmentExtension() { super("fragment"); } @Override - public boolean init(Map parameters) + public void setConfig(ExtensionConfig config) { - if(super.init(parameters)) - { - _maxLength=getInitParameter("maxLength",_maxLength); - _minFragments=getInitParameter("minFragments",_minFragments); - return true; - } - return false; + super.setConfig(config); + + _maxLength = config.getParameter("maxLength",_maxLength); + _minFragments = config.getParameter("minFragments",_minFragments); + } /* TODO: Migrate to new Jetty9 IO @@ -50,9 +46,9 @@ public class FragmentExtension extends AbstractExtension super.addFrame(flags,opcode,content,offset,length); return; } - + int fragments=1; - + while (_maxLength>0 && length>_maxLength) { fragments++; @@ -61,7 +57,7 @@ public class FragmentExtension extends AbstractExtension offset+=_maxLength; opcode=getConnection().continuationOpcode(); } - + while (fragments<_minFragments) { int frag=length/2; @@ -74,5 +70,5 @@ public class FragmentExtension extends AbstractExtension super.addFrame((byte)(flags|getConnection().finMask()),opcode,content,offset,length); } - */ + */ }