001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Aug 18, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui.component;
018:
019: import java.util.List;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.pentaho.messages.Messages;
023: import org.pentaho.core.solution.ActionResource;
024: import org.pentaho.core.solution.IActionResource;
025: import org.pentaho.core.system.PentahoSystem;
026: import org.pentaho.core.ui.IPentahoUrlFactory;
027: import org.pentaho.ui.BaseUIComponent;
028: import org.pentaho.util.HttpUtil;
029:
030: public class HtmlComponent extends BaseUIComponent {
031:
032: /**
033: *
034: */
035: private static final long serialVersionUID = -7404173000559758744L;
036:
037: // TODO sbarkdull convert these 2 TYPE_* to enumerated type
038: public static int TYPE_URL = 1;
039:
040: public static int TYPE_SOLUTION_FILE = 2;
041:
042: private String location;
043:
044: private String errorMessage;
045:
046: private int type;
047:
048: private static final Log logger = LogFactory
049: .getLog(HtmlComponent.class);
050:
051: public Log getLogger() {
052: return logger;
053: }
054:
055: public HtmlComponent(int type, String location,
056: String errorMessage, IPentahoUrlFactory urlFactory,
057: List messages) {
058: super (urlFactory, messages, null);
059: this .type = type;
060: this .location = location;
061: this .errorMessage = errorMessage;
062: }
063:
064: public boolean validate() {
065: return true;
066: }
067:
068: public String getContent(String mimeType) {
069: if ("text/html".equals(mimeType)) { //$NON-NLS-1$
070: if (type == TYPE_URL) {
071: return getUrl(location);
072: } else if (type == TYPE_SOLUTION_FILE) {
073: return getFile(location);
074: }
075: }
076: return null;
077: }
078:
079: private String getFile(String solutionPath) {
080: ActionResource resource = new ActionResource(
081: "", IActionResource.SOLUTION_FILE_RESOURCE, "text/html", solutionPath); //$NON-NLS-1$ //$NON-NLS-2$
082: try {
083: return PentahoSystem.getSolutionRepository(getSession())
084: .getResourceAsString(resource);
085: } catch (Exception e) {
086: if (errorMessage != null) {
087: return errorMessage;
088: } else {
089: error(Messages
090: .getErrorString(
091: "Html.ERROR_0001_COULD_NOT_GET_CONTENT", solutionPath)); //$NON-NLS-1$
092: return Messages
093: .getErrorString(
094: "Html.ERROR_0001_COULD_NOT_GET_CONTENT", solutionPath); //$NON-NLS-1$
095: }
096: }
097: }
098:
099: private String getUrl(String url) {
100: StringBuffer content = new StringBuffer();
101: try {
102: // check to see if this URL failed before thia session
103: if (getSession() != null
104: && getSession().getAttribute(
105: "pentaho-HtmlComponent-failed-url-" + url) != null) { //$NON-NLS-1$
106: return errorMessage;
107: }
108: if (debug)
109: debug(Messages.getString(
110: "Html.DEBUG_GETTING_CONTENT", url)); //$NON-NLS-1$
111: if (HttpUtil.getURLContent(url, content)) {
112: return content.toString();
113: } else {
114: getSession().setAttribute(
115: "pentaho-HtmlComponent-failed-url-" + url, ""); //$NON-NLS-1$ //$NON-NLS-2$
116: return errorMessage;
117: }
118: } catch (Exception e) {
119:
120: if (errorMessage != null) {
121: return errorMessage;
122: } else {
123: error(Messages.getErrorString(
124: "Html.ERROR_0001_COULD_NOT_GET_CONTENT", url)); //$NON-NLS-1$
125: return Messages.getErrorString(
126: "Html.ERROR_0001_COULD_NOT_GET_CONTENT", url); //$NON-NLS-1$
127: }
128: }
129:
130: }
131:
132: }
|