1 /*
2 * $Id: DispatchRenderer.java 1375743 2012-08-21 20:05:58Z nlebas $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.tiles.request.render;
22
23 import java.io.IOException;
24
25 import org.apache.tiles.request.Request;
26 import org.apache.tiles.request.DispatchRequest;
27 import org.apache.tiles.request.RequestWrapper;
28
29 /**
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 */
34 public class DispatchRenderer implements Renderer {
35
36 /** {@inheritDoc} */
37 @Override
38 public void render(String path, Request request) throws IOException {
39 if (path == null) {
40 throw new CannotRenderException("Cannot dispatch a null path");
41 }
42 DispatchRequest dispatchRequest = getDispatchRequest(request);
43 if (dispatchRequest == null) {
44 throw new CannotRenderException("Cannot dispatch outside of a web environment");
45 }
46
47 dispatchRequest.dispatch(path);
48 }
49
50 /** {@inheritDoc} */
51 public boolean isRenderable(String path, Request request) {
52 return path != null && getDispatchRequest(request) != null && path.startsWith("/");
53 }
54
55 private DispatchRequest getDispatchRequest(Request request) {
56 Request result = request;
57 while (!(result instanceof DispatchRequest) && result instanceof RequestWrapper) {
58 result = ((RequestWrapper) result).getWrappedRequest();
59 }
60 if (!(result instanceof DispatchRequest)) {
61 result = null;
62 }
63 return (DispatchRequest) result;
64 }
65 }