package org.w3c.jigsaw.frames ;
import java.io.* ;
import java.util.*;
import org.w3c.tools.resources.*;
import org.w3c.jigsaw.forms.*;
import org.w3c.www.mime.* ;
import org.w3c.www.http.* ;
import org.w3c.jigsaw.http.* ;
import org.w3c.jigsaw.html.HtmlGenerator ;
public class PostableFrame extends HTTPFrame {
private static HttpTokenList _post_allowed = null;
private static HttpTokenList _put_allowed = null;
static {
String post_allowed[] = { "GET", "HEAD", "OPTIONS", "POST", "TRACE" } ;
_post_allowed = HttpFactory.makeStringList(post_allowed);
String put_allowed[] = { "GET", "HEAD", "OPTIONS", "PUT",
"POST", "TRACE" } ;
_put_allowed = HttpFactory.makeStringList(put_allowed);
}
private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;
protected static int ATTR_OVERIDE = -1 ;
protected static int ATTR_CONVERT_GET = -1 ;
static {
Attribute a = null ;
Class cls = null ;
try {
cls = Class.forName("org.w3c.jigsaw.frames.PostableFrame") ;
} catch (Exception ex) {
ex.printStackTrace() ;
System.exit(1) ;
}
a = new BooleanAttribute("override",
Boolean.FALSE,
Attribute.EDITABLE);
ATTR_OVERIDE = AttributeRegistry.registerAttribute(cls, a) ;
a = new BooleanAttribute("convert-get",
Boolean.TRUE,
Attribute.EDITABLE) ;
ATTR_CONVERT_GET = AttributeRegistry.registerAttribute(cls, a) ;
}
public boolean getConvertGetFlag() {
return getBoolean(ATTR_CONVERT_GET, false) ;
}
public boolean getOverrideFlag() {
return getBoolean(ATTR_OVERIDE, true) ;
}
@param idx@param value
public synchronized void setValue(int idx, Object value) {
super.setValue(idx, value);
if (idx == ATTR_PUTABLE) {
if (value == Boolean.TRUE)
allowed = _put_allowed;
else
allowed = _post_allowed;
}
}
@param request@exception ProtocolException
public Reply get (Request request)
throws ProtocolException, NotAProtocolException
{
if ((! getConvertGetFlag()) || ( ! request.hasState("query")))
return super.get (request) ;
String query = request.getQueryString() ;
InputStream in = new StringBufferInputStream(query) ;
URLDecoder d = new URLDecoder (in, getOverrideFlag()) ;
try {
d.parse () ;
} catch (URLDecoderException e) {
Reply error = request.makeReply(HTTP.BAD_REQUEST) ;
error.setContent("Invalid request:unable to decode form data.");
throw new HTTPException (error) ;
} catch (IOException e) {
Reply error = request.makeReply(HTTP.BAD_REQUEST) ;
error.setContent("Invalid request: unable to read form data.");
throw new HTTPException (error) ;
}
return handle (request, d) ;
}
public Reply post (Request request)
throws ProtocolException, NotAProtocolException
{
if ((! request.hasContentType())
|| (type.match(request.getContentType()) < 0) ) {
Reply error = request.makeReply(HTTP.UNSUPPORTED_MEDIA_TYPE) ;
error.setContent("Invalid request content type.");
throw new HTTPException (error) ;
}
URLDecoder dec = null;
try {
InputStream in = request.getInputStream() ;
Client client = request.getClient();
if ( client != null )
client.sendContinue();
dec = new URLDecoder (in, getOverrideFlag()) ;
dec.parse () ;
} catch (URLDecoderException e) {
Reply error = request.makeReply(HTTP.BAD_REQUEST) ;
error.setContent("Invalid request: unable to decode form data.") ;
throw new HTTPException (error) ;
} catch (IOException ex) {
Reply error = request.makeReply(HTTP.BAD_REQUEST) ;
error.setContent("Invalid request: unable to read form data.") ;
throw new ClientException(request.getClient(), ex) ;
}
return handle (request, dec) ;
}
@param request@param data@exception ProtocolException@see org.w3c.jigsaw.forms.URLDecoder
public Reply handle (Request request, URLDecoder data)
throws ProtocolException
{
Enumeration e = data.keys() ;
HtmlGenerator g = new HtmlGenerator ("Form decoded values") ;
g.append ("<p>List of variables and values:</p><ul>") ;
while ( e.hasMoreElements () ) {
String name = (String) e.nextElement() ;
g.append ("<li><em>"+
name+"</em> = <b>"+
data.getValue(name)+
"</b></li>");
}
g.append ("</ul>") ;
Reply reply = request.makeReply(HTTP.OK) ;
reply.setStream (g) ;
return reply ;
}
}