All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ratpack.parse.NoOptParserSupport Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ratpack.parse;

import com.google.common.reflect.TypeToken;
import ratpack.handling.Context;
import ratpack.http.TypedData;

/**
 * A convenience base for parsers that don't require options.
 * 

* The following is an example of an implementation that parses to an {@code Integer}. *

 * import ratpack.parse.NullParseOpts;
 * import ratpack.parse.NoOptParserSupport;
 * import ratpack.http.TypedData;
 * import ratpack.handling.Context;
 * import ratpack.handling.Handler;
 * import ratpack.util.Types;
 * import ratpack.func.Action;
 * import ratpack.registry.RegistrySpec;
 *
 * import com.google.common.reflect.TypeToken;
 *
 * import ratpack.test.handling.HandlingResult;
 * import ratpack.test.handling.RequestFixture;
 *
 * public class Example {
 *   public static class IntParser extends NoOptParserSupport {
 *     public IntParser() {
 *       super("text/plain");
 *     }
 *
 *     public <T> T parse(Context context, TypedData body, TypeToken<T> type) {
 *       if (type.getRawType().equals(Integer.class)) {
 *         return Types.cast(Integer.valueOf(body.getText()));
 *       } else {
 *         return null;
 *       }
 *     }
 *   }
 *
 *   public static class ExampleHandler implements Handler {
 *     public void handle(Context context) {
 *       Integer integer = context.parse(Integer.class);
 *       context.render(integer.toString());
 *     }
 *   }
 *
 *   // unit test
 *   public static void main(String[] args) throws Exception {
 *     HandlingResult result = RequestFixture.handle(new ExampleHandler(), new Action<RequestFixture>() {
 *       public void execute(RequestFixture fixture) throws Exception {
 *         fixture
 *           .body("10", "text/plain")
 *           .registry(new Action<RegistrySpec>() {
 *             public void execute(RegistrySpec registry) {
 *               registry.add(new IntParser());
 *             }
 *           });
 *       }
 *     });
 *
 *     assert result.rendered(String.class).equals("10");
 *   }
 *
 * }
 * 
*/ public abstract class NoOptParserSupport extends ParserSupport { /** * Constructor. * * @param contentType the type of request this parser can handle */ protected NoOptParserSupport(String contentType) { super(contentType); } /** * Delegates to {@link #parse(ratpack.handling.Context, ratpack.http.TypedData, TypeToken)}, discarding the {@code} opts object of the given {@code parse}. * * @param context The context to deserialize * @param requestBody The request body to deserialize * @param parse The description of how to parse the request body * @param the type of object to construct from the request body * @return the result of calling {@link #parse(ratpack.handling.Context, ratpack.http.TypedData, TypeToken)} * @throws Exception any exception thrown by {@link #parse(ratpack.handling.Context, ratpack.http.TypedData, TypeToken)} */ @Override public final T parse(Context context, TypedData requestBody, Parse parse) throws Exception { return parse(context, requestBody, parse.getType()); } /** * The parser implementation. * * @param context The context to deserialize * @param requestBody The request body to deserialize * @param type the type of object to construct from the request body * @param the type of object to construct from the request body * @return an instance of {@code T} if this parser can construct this type, otherwise {@code null} * @throws Exception any exception thrown while parsing */ abstract protected T parse(Context context, TypedData requestBody, TypeToken type) throws Exception; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy