O - the type of option object this parser acceptspublic abstract class ParserSupport<O> extends Object implements Parser<O>
Parser implementations.
Specializations only need to implement the Parser.parse(ratpack.handling.Context, ratpack.http.TypedData, Parse) method.
import ratpack.handling.Handler;
import ratpack.handling.Context;
import ratpack.http.TypedData;
import ratpack.parse.Parse;
import ratpack.parse.ParserSupport;
import ratpack.parse.ParseException;
public class StringParseOpts {
private int maxLength;
public StringParseOpts(int maxLength) {
this.maxLength = maxLength;
}
public int getMaxLength() {
return maxLength;
}
}
// A parser for this parser type…
public class MaxLengthStringParser extends ParserSupport {
public MaxLengthStringParser() {
super("text/plain");
}
public <T> T parse(Context context, TypedData requestBody, Parse<T, StringParseOpts> parse) throws UnsupportedEncodingException {
if (!parse.getType().equals(String.class)) {
return null;
}
String rawString = requestBody.getText();
if (rawString.length() < parse.getOpts().getMaxLength()) {
return parse.getType().cast(rawString);
} else {
return parse.getType().cast(rawString.substring(0, parse.getOpts().getMaxLength()));
}
}
}
// Assuming the parser above has been registered upstream…
public class ExampleHandler implements Handler {
public void handle(Context context) throws ParseException {
String string = context.parse(String.class, new StringParseOpts(20));
// …
}
}
NoOptParserSupport| Modifier | Constructor and Description |
|---|---|
protected |
ParserSupport(String contentType)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
String |
getContentType()
The content type that this parser knows how to deserialize.
|
Class<O> |
getOptsType()
The type of option object that this parser accepts.
|
protected ParserSupport(String contentType)
contentType - the type of request this parser can handlepublic final String getContentType()
getContentType in interface Parser<O>public final Class<O> getOptsType()
getOptsType in interface Parser<O>ParserSupport