1 17 18 package org.pentaho.core.solution; 19 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.pentaho.core.repository.IContentItem; 26 import org.pentaho.core.services.SimpleContentItem; 27 28 public class SimpleOutputHandler implements IOutputHandler { 29 30 private Map responseAttributes; 31 32 private IContentItem feedbackContent; 33 34 boolean allowFeedback; 35 36 private String mimeType; 37 38 private int outputType = OUTPUT_TYPE_DEFAULT; 39 40 private boolean contentGenerated; 41 42 private Map outputs; 43 44 public SimpleOutputHandler(OutputStream outputStream, boolean allowFeedback) { 45 46 this.allowFeedback = allowFeedback; 47 if (allowFeedback) { 48 feedbackContent = new SimpleContentItem(outputStream); 49 } 50 responseAttributes = new HashMap (); 51 contentGenerated = false; 52 outputs = new HashMap (); 53 setOutputStream(outputStream, IOutputHandler.RESPONSE, IOutputHandler.CONTENT); 54 } 55 56 public void setOutputStream(OutputStream outputStream, String outputName, String contentName) { 57 String key = outputName + "." + contentName; SimpleContentItem item = new SimpleContentItem(outputStream); 59 outputs.put(key, item); 60 } 61 62 public void setOutputPreference(int outputType) { 63 this.outputType = outputType; 64 } 65 66 public boolean contentDone() { 67 return contentGenerated; 68 69 } 70 71 public int getOutputPreference() { 72 return outputType; 73 } 74 75 public void setMimeType(String mimeType) { 76 this.mimeType = mimeType; 77 } 78 79 public String getMimeType() { 80 return mimeType; 81 } 82 83 public boolean allowFeedback() { 84 return allowFeedback; 85 } 86 87 92 public Map getOutputDefs() { 93 return null; 95 } 96 97 102 public IOutputDef getOutputDef(String name) { 103 return null; 105 } 106 107 112 public IContentItem getFeedbackContentItem() { 113 if (allowFeedback) { 114 contentGenerated = true; 115 return feedbackContent; 116 } 117 return null; 118 } 119 120 125 public IContentItem getOutputContentItem(String outputName, String contentName) { 126 if (IOutputHandler.FILE.equals(outputName)) { 127 return FileOutputHandler.getFileOutputContentItem(contentName); 128 } 129 String key = outputName + "." + contentName; return (SimpleContentItem) outputs.get(key); 131 } 132 133 public IContentItem getOutputContentItem(String objectName, String contentName, String title, String url) { 134 return getOutputContentItem( objectName, contentName ); 135 } 136 137 public void setContentItem(IContentItem content, String objectName, String contentName) { 138 mimeType = content.getMimeType(); 139 } 140 141 public void setOutput(String name, Object value) { 142 if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) { 143 IContentItem response = getOutputContentItem( "response", IOutputHandler.CONTENT ); if ( response != null ) { 145 try { 146 if ( value instanceof IContentItem ) { 147 IContentItem content = (IContentItem)value; 148 if ( (response.getMimeType() == null) || (!response.getMimeType().equalsIgnoreCase( content.getMimeType() ) ) ) { 149 response.setMimeType( content.getMimeType() ); 150 } 151 152 InputStream inStr = content.getInputStream(); 153 try { 154 OutputStream outStr = response.getOutputStream( response.getActionName() ); 155 int inCnt = 0; 156 byte[] buf = new byte[4096]; 157 while (-1 != (inCnt = inStr.read(buf))) { 158 outStr.write(buf, 0, inCnt); 159 } 160 } finally { 161 try { 162 inStr.close(); 163 } catch (Exception ignored) {} 164 } 165 contentGenerated = true; 166 } 167 else { 168 if ( response.getMimeType() == null ) { 169 response.setMimeType( "text/html" ); } 171 172 response.getOutputStream( response.getActionName() ).write( value.toString().getBytes() ); 173 contentGenerated = true; 174 } 175 } catch (Exception e) { 176 177 } 178 } 179 } 180 else { 181 responseAttributes.put(name, value); 182 } 183 184 } 185 186 public Map getResponseArrtibutes() { 187 return responseAttributes; 188 } 189 } 190
| Popular Tags
|