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);
_minLength = config.getParameter("minLength",_minLength);
_deflater = new Deflater();
_inflater = new Inflater();
return true;
}
return false;
}
/* (non-Javadoc)

View File

@ -15,9 +15,7 @@
*******************************************************************************/
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
@ -31,15 +29,13 @@ public class FragmentExtension extends AbstractExtension
}
@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