KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > component > HtmlComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Aug 18, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.ui.component;
18
19 import java.util.List JavaDoc;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.pentaho.messages.Messages;
23 import org.pentaho.core.solution.ActionResource;
24 import org.pentaho.core.solution.IActionResource;
25 import org.pentaho.core.system.PentahoSystem;
26 import org.pentaho.core.ui.IPentahoUrlFactory;
27 import org.pentaho.ui.BaseUIComponent;
28 import org.pentaho.util.HttpUtil;
29
30 public class HtmlComponent extends BaseUIComponent {
31
32     /**
33      *
34      */

35     private static final long serialVersionUID = -7404173000559758744L;
36
37     public static int TYPE_URL = 1;
38
39     public static int TYPE_SOLUTION_FILE = 2;
40
41     private String JavaDoc location;
42
43     private String JavaDoc errorMessage;
44
45     private int type;
46
47     private static final Log logger = LogFactory.getLog(HtmlComponent.class);
48
49     public Log getLogger() {
50         return logger;
51     }
52
53     public HtmlComponent(int type, String JavaDoc location, String JavaDoc errorMessage, IPentahoUrlFactory urlFactory, List JavaDoc messages) {
54         super(urlFactory, messages, null);
55         this.type = type;
56         this.location = location;
57         this.errorMessage = errorMessage;
58     }
59
60     public boolean validate() {
61         return true;
62     }
63
64     public String JavaDoc getContent(String JavaDoc mimeType) {
65         if ("text/html".equals(mimeType)) { //$NON-NLS-1$
66
if (type == TYPE_URL) {
67                 return getUrl(location);
68             } else if (type == TYPE_SOLUTION_FILE) {
69                 return getFile(location);
70             }
71         }
72         return null;
73     }
74
75     private String JavaDoc getFile(String JavaDoc solutionPath) {
76         ActionResource resource = new ActionResource("", IActionResource.SOLUTION_FILE_RESOURCE, "text/html", solutionPath); //$NON-NLS-1$ //$NON-NLS-2$
77
try {
78             return PentahoSystem.getSolutionRepository(getSession()).getResourceAsString(resource);
79         } catch (Exception JavaDoc e) {
80             if (errorMessage != null) {
81                 return errorMessage;
82             } else {
83                 error(Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", solutionPath)); //$NON-NLS-1$
84
return Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", solutionPath); //$NON-NLS-1$
85
}
86         }
87     }
88
89     private String JavaDoc getUrl(String JavaDoc url) {
90         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
91         try {
92                 // check to see if this URL failed before thia session
93
if ( getSession() != null && getSession().getAttribute( "pentaho-HtmlComponent-failed-url-"+url ) != null ) { //$NON-NLS-1$
94
return errorMessage;
95                 }
96                 if (debug)
97                 debug(Messages.getString("Html.DEBUG_GETTING_CONTENT", url)); //$NON-NLS-1$
98
if( HttpUtil.getURLContent(url, content) ) {
99                 return content.toString();
100             } else {
101                     getSession().setAttribute( "pentaho-HtmlComponent-failed-url-"+url, "" ); //$NON-NLS-1$ //$NON-NLS-2$
102
return errorMessage;
103             }
104         } catch (Exception JavaDoc e) {
105             
106             if (errorMessage != null) {
107                 return errorMessage;
108             } else {
109                 error(Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", url)); //$NON-NLS-1$
110
return Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", url); //$NON-NLS-1$
111
}
112         }
113
114     }
115
116 }
117
Popular Tags