T - the type of object this parser parses toP - the type of parse object this parser can handlepublic abstract class ParserSupport<T,P extends Parse<T>> extends Object implements Parser<T,P>
Parser that implements the type methods based on the implementation.
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.ParseSupport;
import ratpack.parse.ParserSupport;
import ratpack.parse.ParseException;
public class MaxLengthStringParse extends ParseSupport<String> {
private int maxLength;
public MaxLengthStringParse(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");
}
String parse(Context context, TypedData requestBody, MaxLengthStringParse parse) throws UnsupportedEncodingException {
String rawString = requestBody.getText();
if (rawString.length() < parse.getMaxLength()) {
return rawString;
} else {
return rawString.substring(0, parse.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(new MaxLengthStringParse(20));
// …
}
}
| 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<T> |
getParsedType()
The type that this parser can deserialize to.
|
Class<P> |
getParseType()
The type of the
Parse object for this parser. |
protected ParserSupport(String contentType)
contentType - the type of request this parser can handle