| 1 16 17 package org.pentaho.ui.component; 18 19 import java.util.List ; 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 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 location; 42 43 private String 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 location, String errorMessage, IPentahoUrlFactory urlFactory, List 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 getContent(String mimeType) { 65 if ("text/html".equals(mimeType)) { 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 getFile(String solutionPath) { 76 ActionResource resource = new ActionResource("", IActionResource.SOLUTION_FILE_RESOURCE, "text/html", solutionPath); try { 78 return PentahoSystem.getSolutionRepository(getSession()).getResourceAsString(resource); 79 } catch (Exception e) { 80 if (errorMessage != null) { 81 return errorMessage; 82 } else { 83 error(Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", solutionPath)); return Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", solutionPath); } 86 } 87 } 88 89 private String getUrl(String url) { 90 StringBuffer content = new StringBuffer (); 91 try { 92 if ( getSession() != null && getSession().getAttribute( "pentaho-HtmlComponent-failed-url-"+url ) != null ) { return errorMessage; 95 } 96 if (debug) 97 debug(Messages.getString("Html.DEBUG_GETTING_CONTENT", url)); if( HttpUtil.getURLContent(url, content) ) { 99 return content.toString(); 100 } else { 101 getSession().setAttribute( "pentaho-HtmlComponent-failed-url-"+url, "" ); return errorMessage; 103 } 104 } catch (Exception e) { 105 106 if (errorMessage != null) { 107 return errorMessage; 108 } else { 109 error(Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", url)); return Messages.getErrorString("Html.ERROR_0001_COULD_NOT_GET_CONTENT", url); } 112 } 113 114 } 115 116 } 117 | Popular Tags |