01: /*
02: * $Id: HttpViewHandler.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
03: *
04: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: *
24: */
25: package org.ofbiz.content.webapp.view;
26:
27: import java.io.IOException;
28:
29: import javax.servlet.ServletContext;
30: import javax.servlet.http.HttpServletRequest;
31: import javax.servlet.http.HttpServletResponse;
32:
33: import org.ofbiz.base.util.Debug;
34: import org.ofbiz.base.util.HttpClient;
35: import org.ofbiz.base.util.HttpClientException;
36:
37: /**
38: * ViewHandlerException - View Handler Exception
39: *
40: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
41: * @version $Revision: 1.2 $
42: * @since 2.0
43: */
44: public class HttpViewHandler implements ViewHandler {
45:
46: public static final String module = HttpViewHandler.class.getName();
47:
48: protected ServletContext context;
49:
50: public void init(ServletContext context)
51: throws ViewHandlerException {
52: this .context = context;
53: }
54:
55: public void render(String name, String page, String info,
56: String contentType, String encoding,
57: HttpServletRequest request, HttpServletResponse response)
58: throws ViewHandlerException {
59: // some containers call filters on EVERY request, even forwarded ones,
60: // so let it know that it came from the control servlet
61:
62: if (request == null)
63: throw new ViewHandlerException(
64: "Null HttpServletRequest object");
65: if (page == null || page.length() == 0)
66: throw new ViewHandlerException("Null or empty source");
67:
68: if (Debug.infoOn())
69: Debug.logInfo("Retreiving HTTP resource at: " + page,
70: module);
71: try {
72: HttpClient httpClient = new HttpClient(page);
73: String pageText = httpClient.get();
74:
75: // TODO: parse page and remove harmful tags like <HTML>, <HEAD>, <BASE>, etc - look into the OpenSymphony piece for an example
76: response.getWriter().print(pageText);
77: } catch (IOException e) {
78: throw new ViewHandlerException("IO Error in view", e);
79: } catch (HttpClientException e) {
80: throw new ViewHandlerException(e.getNonNestedMessage(), e
81: .getNested());
82: }
83: }
84: }
|