1 17 18 package org.pentaho.core.solution; 19 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.pentaho.core.repository.IContentItem; 28 import org.pentaho.core.services.HttpContentItem; 29 30 public class HttpOutputHandler implements IOutputHandler { 31 32 private HttpServletResponse response; 33 34 private IContentItem outputContent; 35 36 private IContentItem feedbackContent; 37 38 boolean allowFeedback; 39 40 private boolean contentGenerated; 41 42 private int outputType = OUTPUT_TYPE_DEFAULT; 43 44 public HttpOutputHandler(HttpServletResponse response, OutputStream outputStream, boolean allowFeedback) { 45 this.response = response; 46 47 outputContent = new HttpContentItem(outputStream, this); 48 feedbackContent = new HttpContentItem(outputStream, this); 49 50 this.allowFeedback = allowFeedback; 51 contentGenerated = false; 52 } 53 54 public void setOutputPreference(int outputType) { 55 this.outputType = outputType; 56 } 57 58 public int getOutputPreference() { 59 return outputType; 60 } 61 62 public void setMimeType(String mimeType) { 63 response.setContentType(mimeType); 64 } 65 66 public boolean contentDone() { 67 return contentGenerated; 68 69 } 70 71 public boolean allowFeedback() { 72 return allowFeedback; 73 } 74 75 80 public Map getOutputDefs() { 81 return null; 83 } 84 85 90 public IOutputDef getOutputDef(String name) { 91 return null; 93 } 94 95 100 public IContentItem getFeedbackContentItem() { 101 if (allowFeedback) { 102 contentGenerated = true; 104 return feedbackContent; 105 } 106 return null; 107 } 108 109 114 public IContentItem getOutputContentItem(String objectName, String contentName) { 115 if (IOutputHandler.FILE.equals(objectName)) { 116 return FileOutputHandler.getFileOutputContentItem(contentName); 117 } 118 if (objectName.equals(IOutputHandler.RESPONSE) && contentName.equals(IOutputHandler.CONTENT)) { 119 contentGenerated = true; 121 return outputContent; 122 } else { 123 return null; 124 } 125 } 126 127 public IContentItem getOutputContentItem(String objectName, String contentName, String title, String url) { 128 return getOutputContentItem( objectName, contentName ); 129 } 130 131 public void setContentItem(IContentItem content, String objectName, String contentName) { 132 if (objectName.equals(IOutputHandler.RESPONSE) && contentName.equals(IOutputHandler.CONTENT)) { 133 response.setContentType(content.getMimeType()); 134 } 135 } 136 137 public void setOutput(String name, Object value) { 138 if ( value == null ) { 139 return; 140 } 141 142 if ("redirect".equalsIgnoreCase(name)) { try { 144 response.sendRedirect( value.toString() ); 145 } catch (Exception e) { 146 147 } 148 } 149 else if ("header".equalsIgnoreCase(name)) { try { 151 if ( value instanceof Map ) { 152 for ( Iterator it = ((Map )value).entrySet().iterator(); it.hasNext(); ) { 153 Map.Entry entry = (Map.Entry )it.next(); 154 response.addHeader( entry.getKey().toString(), entry.getValue().toString() ); 155 } 156 } 157 else { 158 String strVal = value.toString(); 159 int i = strVal.indexOf( '=' ); 160 String headerName = strVal.substring( 0, i ); 161 String headerValue = strVal.substring( i+1 ); 162 response.addHeader( headerName, headerValue ); 163 } 164 } catch (Exception e) { 165 } 167 } 168 169 else if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) { 170 try { 171 if ( value instanceof IContentItem ) { 172 IContentItem content = (IContentItem)value; 173 if ( (response.getContentType() == null) || (!response.getContentType().equalsIgnoreCase( content.getMimeType() ) ) ) { 174 response.setContentType( content.getMimeType() ); 175 } 176 content.getMimeType(); 177 InputStream inStr = content.getInputStream(); 178 try { 179 OutputStream outStr = response.getOutputStream(); 180 int inCnt = 0; 181 byte[] buf = new byte[4096]; 182 while (-1 != (inCnt = inStr.read(buf))) { 183 outStr.write(buf, 0, inCnt); 184 } 185 } finally { 186 try { 187 inStr.close(); 188 } catch (Exception ignored) {} 189 } 190 contentGenerated = true; 191 } 192 else { 193 if ( response.getContentType() == null ) { 194 response.setContentType( "text/html" ); } 196 197 response.getOutputStream().write( value.toString().getBytes() ); 198 contentGenerated = true; 199 } 200 } catch (Exception e) { 201 202 } 203 } 204 } 205 206 } 207 | Popular Tags |