1 21 package org.pentaho.plugin.core; 22 23 import java.io.ByteArrayOutputStream ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.pentaho.core.repository.IContentItem; 29 import org.pentaho.messages.Messages; 30 import org.pentaho.messages.util.LocaleHelper; 31 import org.pentaho.plugin.ComponentBase; 32 33 57 public class ContentOutputComponent extends ComponentBase { 58 59 private static final long serialVersionUID = -6300339081029611956L; 60 61 private static final String INPUT_NAME_EXPECTED = "CONTENTOUTPUT"; 63 private static final String COMPONENT_SETTING_MIME_TYPE = "mime-type"; 65 69 protected boolean validateAction() { 70 if (!isDefinedInput(INPUT_NAME_EXPECTED)) { 71 error(Messages.getErrorString("ContentOutputComponent.ERROR_0001_CONTENTOUTPUT_NOT_DEFINED")); return false; 73 } 74 if (!isDefinedInput(COMPONENT_SETTING_MIME_TYPE)) { 75 error(Messages.getErrorString("ContentOutputComponent.ERROR_0006_MIME_TYPE_REQUIRED")); return false; 77 } 78 return true; 79 } 80 81 protected boolean validateSystemSettings() { 82 return true; 84 } 85 86 public void done() { 87 } 89 90 public Log getLogger() { 91 return LogFactory.getLog(ContentOutputComponent.class); 92 } 93 94 protected boolean executeAction() throws Throwable { 95 String mimeType = getInputStringValue(COMPONENT_SETTING_MIME_TYPE); 96 Object dataToOutput = getInputValue(INPUT_NAME_EXPECTED); 97 98 if (dataToOutput != null) { 99 IContentItem outputContentItem = getOutputContentItem(); 100 if (outputContentItem != null) { 101 outputContentItem.setMimeType(mimeType); 102 OutputStream outputStream = null; 103 if (dataToOutput instanceof String ) { 104 String theOutput = (String ) dataToOutput; 105 if (theOutput.length() > 0) { 106 try { 107 outputStream = outputContentItem.getOutputStream(getActionName()); 108 outputStream.write(theOutput.getBytes(LocaleHelper.getSystemEncoding())); 109 } catch (Exception e) { 110 error(Messages.getErrorString("ContentOutputComponent.ERROR_0003_WRITING_OUTPUT"), e); return false; 112 } 113 trace(theOutput); 114 return true; 115 } else { 116 error(Messages.getErrorString("ContentOutputComponent.ERROR_0002_EMPTY_OUTPUT")); return false; 118 } 119 } else if (dataToOutput instanceof InputStream ) { 120 InputStream is = (InputStream ) dataToOutput; 121 byte[] buff = new byte[1024]; 122 int len; 123 outputStream = outputContentItem.getOutputStream(null); 124 while ((len = is.read(buff)) >= 0) { 125 outputStream.write(buff, 0, len); 126 } 127 return true; 128 } else if (dataToOutput instanceof ByteArrayOutputStream ) { 129 ByteArrayOutputStream baos = (ByteArrayOutputStream ) dataToOutput; 130 outputStream = outputContentItem.getOutputStream(null); 131 outputStream.write(baos.toByteArray()); 132 return true; 133 } else { 134 error(Messages.getErrorString("ContentOutputComponent.ERROR_0007_UNKNOWN_TYPE", dataToOutput.getClass().getName())); return false; 136 } 137 138 } else { 139 error(Messages.getErrorString("ContentOutputComponent.ERROR_0005_OUTPUT_CONTENT_ITEM")); return false; 141 } 142 } 143 error(Messages.getErrorString("ContentOutputComponent.ERROR_0004_CONTENTOUTPUT_NULL")); return false; 145 } 146 147 public boolean init() { 149 return true; 150 } 151 152 } 153 | Popular Tags |