Renaming ExtensionRef to ExtensionConfig, fleshing it out bunch more, and using it in Extension instance

This commit is contained in:
Joakim Erdfelt 2012-06-25 12:50:56 -07:00
parent 3cbcb20257
commit e919c0dff8
6 changed files with 132 additions and 83 deletions

View File

@ -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<String, String> 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();
}
}

View File

@ -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<String, String> getParameters();
}

View File

@ -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<String, String> parameters = new HashMap<String, String>();
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<String, String> parameters)
public void setConfig(ExtensionConfig config)
{
parameters.putAll(parameters);
return true;
this.config.init(config);
}
@Override

View File

@ -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<String,String> parameters);
public void setConfig(ExtensionConfig config);
}

View File

@ -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<String, String> 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);
}
*/
*/
}

View File

@ -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<String, String> 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);
}
*/
*/
}