public static class Header<X> { public static Header<String> IF_MATCH = new Header<String>("If-Match", Parser.forString()); public static Header<List<MediaType>> ACCEPT = new Header<List<MediaType>>("Accept", Parser.forListOfJaxRsMediaTypes()); private final String name; private final Parser<X> parser; /** public visibility for testing purposes only. */ public Header(final String name, final Parser<X> parser) { this.name = name; this.parser = parser; } public String getName() { return name; } public Parser<X> getParser() { return parser; } void setHeader(final ClientRequestConfigurer clientRequestConfigurer, final X t) { clientRequestConfigurer.header(getName(), parser.asString(t)); } @Override public String toString() { return getName(); } }
public static class RequestParameter<Q> { public static RequestParameter<List<List<String>>> FOLLOW_LINKS = new RequestParameter<List<List<String>>>( "x-ro-follow-links", Parser.forListOfListOfStrings(), Collections.<List<String>>emptyList()); public static RequestParameter<Integer> PAGE = new RequestParameter<Integer>("x-ro-page", Parser.forInteger(), 1); public static RequestParameter<Integer> PAGE_SIZE = new RequestParameter<Integer>("x-ro-page-size", Parser.forInteger(), 25); public static RequestParameter<List<String>> SORT_BY = new RequestParameter<List<String>>( "x-ro-sort-by", Parser.forListOfStrings(), Collections.<String>emptyList()); public static RequestParameter<DomainModel> DOMAIN_MODEL = new RequestParameter<DomainModel>( "x-ro-domain-model", DomainModel.parser(), DomainModel.SIMPLE); public static RequestParameter<Boolean> VALIDATE_ONLY = new RequestParameter<Boolean>("x-ro-validate-only", Parser.forBoolean(), false); private final String name; private final Parser<Q> parser; private final Q defaultValue; private RequestParameter(final String name, final Parser<Q> parser, final Q defaultValue) { this.name = name; this.parser = parser; this.defaultValue = defaultValue; } public String getName() { return name; } public Parser<Q> getParser() { return parser; } public Q valueOf(final JsonRepresentation parameterRepresentation) { if (parameterRepresentation == null) { return defaultValue; } if (!parameterRepresentation.isMap()) { return defaultValue; } final Q parsedValue = getParser().valueOf(parameterRepresentation.getRepresentation(getName())); return parsedValue != null ? parsedValue : defaultValue; } public Q getDefault() { return defaultValue; } @Override public String toString() { return getName(); } }
void setHeader(final ClientRequestConfigurer clientRequestConfigurer, final X t) { clientRequestConfigurer.header(getName(), parser.asString(t)); }