001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.internal;
020:
021: import org.apache.beehive.netui.core.chain.Command;
022: import org.apache.beehive.netui.core.chain.Context;
023: import org.apache.beehive.netui.core.chain.web.WebChainContext;
024: import org.apache.beehive.netui.pageflow.PageFlowConstants;
025: import org.apache.beehive.netui.pageflow.interceptor.InterceptorChain;
026: import org.apache.beehive.netui.pageflow.interceptor.InterceptorException;
027: import org.apache.beehive.netui.pageflow.interceptor.request.RequestInterceptorContext;
028: import org.apache.beehive.netui.pageflow.internal.ViewRenderer;
029: import org.apache.beehive.netui.tags.AbstractClientRequestInterceptor;
030: import org.apache.beehive.netui.util.logging.Logger;
031:
032: import javax.servlet.ServletContext;
033: import javax.servlet.ServletResponse;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036: import java.io.IOException;
037:
038: /**
039: * An object to handle a command for rendering framework generated output.
040: */
041: public final class ViewRendererCRI extends
042: AbstractClientRequestInterceptor implements Command {
043: private static final Logger logger = Logger
044: .getInstance(ViewRendererCRI.class);
045:
046: public boolean execute(Context context) throws Exception {
047:
048: assert context != null;
049: assert context instanceof WebChainContext;
050: assert ((WebChainContext) context).getServletRequest() instanceof HttpServletRequest;
051: assert ((WebChainContext) context).getServletResponse() instanceof HttpServletResponse;
052:
053: WebChainContext webChainContext = (WebChainContext) context;
054: HttpServletRequest request = (HttpServletRequest) webChainContext
055: .getServletRequest();
056: HttpServletResponse response = (HttpServletResponse) webChainContext
057: .getServletResponse();
058:
059: // Create the command by striping off the context path and the extension
060: String uri = request.getRequestURI();
061: String ctxtPath = request.getContextPath();
062: String cmd = getCommand(uri, ctxtPath);
063:
064: return handleCommand(request, response, webChainContext
065: .getServletContext(), cmd);
066: }
067:
068: public void preRequest(RequestInterceptorContext ctxt,
069: InterceptorChain chain) throws InterceptorException {
070: HttpServletRequest request = ctxt.getRequest();
071:
072: // Create the command by striping off the context path and the extension
073: String uri = request.getRequestURI();
074: String ctxtPath = request.getContextPath();
075: String cmd = getCommand(uri, ctxtPath);
076:
077: handleCommand(request, ctxt.getResponse(), ctxt
078: .getServletContext(), cmd);
079:
080: chain.continueChain();
081: }
082:
083: public void postRequest(RequestInterceptorContext context,
084: InterceptorChain chain) throws InterceptorException {
085: chain.continueChain();
086: }
087:
088: private boolean handleCommand(HttpServletRequest request,
089: HttpServletResponse response,
090: ServletContext servletContext, String command) {
091: if (PageFlowConstants.VIEW_RENDERER_URI_COMMAND.equals(command)) {
092: handleViewRendering(request, response, servletContext);
093: return true;
094: } else
095: return false;
096: }
097:
098: private void handleViewRendering(HttpServletRequest request,
099: ServletResponse response, ServletContext servletContext) {
100: // get the view renderer from the request attribute
101: Object obj = request
102: .getAttribute(PageFlowConstants.VIEW_RENDERER_ATTRIBUTE_NAME);
103: if (obj == null) {
104: logger
105: .error("ViewRenderer was not found in the request attributes");
106: return;
107: }
108: if (!(obj instanceof ViewRenderer)) {
109: logger
110: .error("Named view renderer was not an instance of a ViewRenderer");
111: return;
112: }
113:
114: ViewRenderer vr = (ViewRenderer) obj;
115:
116: // render the response
117: try {
118: /*
119: * Note, this is our own framework response and just javascript
120: * that calls a javascript routine in another window, passing
121: * Unicode String values in a map.
122: * For now, we're setting the content type, but this may be a
123: * problem for people who whant to control the encoding and the
124: * content type themselves, as with a JSP.
125: * If so, then they can implement their own Command to handle the
126: * ViewRenderer objects.
127: */
128: response.setContentType("text/html; charset=UTF-8");
129:
130: vr.renderView(request, response, servletContext);
131: request
132: .removeAttribute(PageFlowConstants.VIEW_RENDERER_ATTRIBUTE_NAME);
133: } catch (IOException ioe) {
134: logger.error(
135: "Named view renderer was not able to render. Cause: "
136: + ioe, ioe);
137: }
138: }
139: }
|