1/*2 * $Id: DispatchRenderer.java 1375743 2012-08-21 20:05:58Z nlebas $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.request.render;
2223import java.io.IOException;
2425import org.apache.tiles.request.Request;
26import org.apache.tiles.request.DispatchRequest;
27import org.apache.tiles.request.RequestWrapper;
2829/**30 * Renders an attribute that contains a reference to a template.31 *32 * @version $Rev: 1375743 $ $Date: 2012-08-22 06:05:58 +1000 (Wed, 22 Aug 2012) $33 */34publicclassDispatchRendererimplementsRenderer {
3536/** {@inheritDoc} */37 @Override
38publicvoid render(String path, Request request) throws IOException {
39if (path == null) {
40thrownewCannotRenderException("Cannot dispatch a null path");
41 }
42DispatchRequest dispatchRequest = getDispatchRequest(request);
43if (dispatchRequest == null) {
44thrownewCannotRenderException("Cannot dispatch outside of a web environment");
45 }
4647 dispatchRequest.dispatch(path);
48 }
4950/** {@inheritDoc} */51publicboolean isRenderable(String path, Request request) {
52return path != null && getDispatchRequest(request) != null && path.startsWith("/");
53 }
5455privateDispatchRequest getDispatchRequest(Request request) {
56Request result = request;
57while (!(result instanceof DispatchRequest) && result instanceof RequestWrapper) {
58 result = ((RequestWrapper) result).getWrappedRequest();
59 }
60if (!(result instanceof DispatchRequest)) {
61 result = null;
62 }
63return (DispatchRequest) result;
64 }
65 }