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

ratpack.handling.ChainAction 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.handling;

import ratpack.api.Nullable;
import ratpack.func.Action;
import ratpack.launch.LaunchConfig;
import ratpack.registry.Registry;
import ratpack.registry.RegistrySpec;

/**
 * Convenient super class for packaging up Groovy handler chain logic.
 * 

* Implementations can naturally use the {@link Chain} DSL in their implementation of {@link #execute()}. *

 * import ratpack.handling.Handler;
 * import ratpack.handling.Context;
 * import ratpack.handling.ChainAction;
 *
 * public class MyHandlers extends ChainAction {
 *   protected void execute() {
 *     get("foo", new Handler() {
 *       public void handle(Context context) {
 *          context.render("foo")
 *       }
 *     });
 *     get("bar", new Handler() {
 *       public void handle(Context context) {
 *          context.render("bar")
 *       }
 *     });
 *   }
 * }
 *
 * // Tests (Groovy) …
 *
 * import static ratpack.test.http.TestHttpClients.testHttpClient
 * import static ratpack.groovy.test.embed.EmbeddedApplications.embeddedApp
 *
 * def app = embeddedApp {
 *   handlers {
 *     handler chain(new MyHandlers())
 *   }
 * }
 *
 * def client = testHttpClient(app)
 *
 * assert client.getText("foo") == "foo"
 * assert client.getText("bar") == "bar"
 *
 * app.close()
 *
 * // Factoring out into ChainAction implementations means they can be unit tested in isolation…
 *
 * import ratpack.test.handling.HandlingResult;
 * import ratpack.test.handling.RequestFixtureAction;
 *
 * import static ratpack.test.UnitTest.handle;
 *
 * HandlingResult result = handle(new MyHandlers(), new RequestFixtureAction() {
 *   protected void execute() {
 *     uri("foo");
 *   }
 * });
 * assert result.rendered(String.class).equals("foo");
 *
 * result = handle(new MyHandlers(), new RequestFixtureAction() {
 *   protected void execute() {
 *     uri("bar");
 *   }
 * });
 * assert result.rendered(String.class).equals("bar");
 * 
*

* This class implements the {@link Chain} interface by delegating each method to the chain returned by {@link #getChain()}. * This method only returns a value during execution of {@link Action#execute(Object)}, which is the given chain. */ public abstract class ChainAction implements Action, Chain { private Chain chain; @Override public Chain assets(String path, String... indexFiles) { return getChain().assets(path, indexFiles); } @Override public Handler chain(Action action) throws Exception { return getChain().chain(action); } @Override public Chain delete(String path, Handler handler) { return getChain().delete(path, handler); } @Override public Chain delete(Handler handler) { return getChain().delete(handler); } /** * Delegates to {@link #execute()}, using the given {@code chain} for delegation. * * @param chain The chain to add handlers to * @throws Exception Any thrown by {@link #execute()} */ public final void execute(Chain chain) throws Exception { try { this.chain = chain; execute(); } finally { this.chain = null; } } /** * Implementations can naturally use the {@link Chain} DSL for the duration of this method. *

* See the {@link ChainAction class level documentation} for an implementation example. * * @throws Exception Any exception thrown while defining the handlers */ protected abstract void execute() throws Exception; @Override public Chain fileSystem(String path, Handler handler) { return getChain().fileSystem(path, handler); } @Override public Chain fileSystem(String path, Action action) throws Exception { return getChain().fileSystem(path, action); } @Override public Chain get(String path, Handler handler) { return getChain().get(path, handler); } @Override public Chain get(Handler handler) { return getChain().get(handler); } /** * The current chain. * * @return the current chain * @throws IllegalStateException if called outside of the {@link #execute()} method (i.e. there is no current chain) */ protected Chain getChain() throws IllegalStateException { if (chain == null) { throw new IllegalStateException("no chain set - Chain methods should only be called during execute()"); } return chain; } @Override public LaunchConfig getLaunchConfig() { return getChain().getLaunchConfig(); } @Override @Nullable public Registry getRegistry() { return getChain().getRegistry(); } @Override public Chain handler(Handler handler) { return getChain().handler(handler); } @Override public Chain handler(String path, Handler handler) { return getChain().handler(path, handler); } @Override public Chain header(String headerName, String headerValue, Handler handler) { return getChain().header(headerName, headerValue, handler); } @Override public Chain host(String hostName, Action action) throws Exception { return getChain().host(hostName, action); } @Override public Chain patch(String path, Handler handler) { return getChain().patch(path, handler); } @Override public Chain patch(Handler handler) { return getChain().patch(handler); } @Override public Chain post(String path, Handler handler) { return getChain().post(path, handler); } @Override public Chain post(Handler handler) { return getChain().post(handler); } @Override public Chain prefix(String prefix, Handler handler) { return getChain().prefix(prefix, handler); } @Override public Chain prefix(String prefix, Action action) throws Exception { return getChain().prefix(prefix, action); } @Override public Chain put(String path, Handler handler) { return getChain().put(path, handler); } @Override public Chain put(Handler handler) { return getChain().put(handler); } @Override public Chain register(Registry registry, Handler handler) { return getChain().register(registry, handler); } @Override public Chain register(Registry registry, Action action) throws Exception { return getChain().register(registry, action); } @Override public Chain register(Action registryAction, Handler handler) throws Exception { return getChain().register(registryAction, handler); } @Override public Chain register(Action registryAction, Action action) throws Exception { return getChain().register(registryAction, action); } @Override public Chain register(Registry registry) { return getChain().register(registry); } @Override public Chain register(Action action) throws Exception { return getChain().register(action); } @Override public Chain insert(Action action) throws Exception { return getChain().insert(action); } @Override public Chain redirect(int code, String location) { return getChain().redirect(code, location); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy