KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > HttpViewHandler


1 /*
2  * $Id: HttpViewHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * 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.webapp.view;
26
27 import java.io.IOException JavaDoc;
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.HttpClient;
34 import org.ofbiz.base.util.HttpClientException;
35
36 /**
37  * ViewHandlerException - View Handler Exception
38  *
39  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
40  * @version $Rev: 5462 $
41  * @since 2.0
42  */

43 public class HttpViewHandler implements ViewHandler {
44     
45     public static final String JavaDoc module = HttpViewHandler.class.getName();
46
47     protected ServletContext JavaDoc context;
48
49     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
50         this.context = context;
51     }
52
53     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
54         // some containers call filters on EVERY request, even forwarded ones,
55
// so let it know that it came from the control servlet
56

57         if (request == null)
58             throw new ViewHandlerException("Null HttpServletRequest object");
59         if (page == null || page.length() == 0)
60             throw new ViewHandlerException("Null or empty source");
61
62         if (Debug.infoOn()) Debug.logInfo("Retreiving HTTP resource at: " + page, module);
63         try {
64             HttpClient httpClient = new HttpClient(page);
65             String JavaDoc pageText = httpClient.get();
66
67             // TODO: parse page and remove harmful tags like <HTML>, <HEAD>, <BASE>, etc - look into the OpenSymphony piece for an example
68
response.getWriter().print(pageText);
69         } catch (IOException JavaDoc e) {
70             throw new ViewHandlerException("IO Error in view", e);
71         } catch (HttpClientException e) {
72             throw new ViewHandlerException(e.getNonNestedMessage(), e.getNested());
73         }
74     }
75 }
76
Popular Tags