HttpOutputHandler.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.pentaho.core.solution |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2006 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*
* @created Jul 11, 2005
* @author James Dixon
*
*/
package org.pentaho.core.solution;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.core.output.IContentOutputHandler;
import org.pentaho.core.repository.IContentItem;
import org.pentaho.core.runtime.IRuntimeContext;
import org.pentaho.core.services.HttpContentItem;
import org.pentaho.core.session.IPentahoSession;
import org.pentaho.core.system.PentahoSystem;
import org.pentaho.messages.Messages;
public class HttpOutputHandler implements IOutputHandler, MimeTypeListener {
private static final Log logger = LogFactory.getLog(HttpOutputHandler.class);
private HttpServletResponse response;
private IContentItem outputContent;
private IContentItem feedbackContent;
boolean allowFeedback;
private boolean contentGenerated;
private IPentahoSession session;
private MimeTypeListener mimeTypeListener;
protected IRuntimeContext runtimeContext;
private int outputType = OUTPUT_TYPE_DEFAULT;
public HttpOutputHandler(HttpServletResponse response, OutputStream outputStream, boolean allowFeedback) {
this.response = response;
outputContent = new HttpContentItem(outputStream, this);
((HttpContentItem) outputContent).setMimeTypeListener(this);
feedbackContent = new HttpContentItem(outputStream, this);
this.allowFeedback = allowFeedback;
contentGenerated = false;
}
public void setSession(IPentahoSession session) {
this.session = session;
}
public IPentahoSession getSession() {
return session;
}
public void setOutputPreference(int outputType) {
this.outputType = outputType;
}
public int getOutputPreference() {
return outputType;
}
public void setMimeType(String mimeType) {
if (mimeTypeListener != null) {
mimeTypeListener.setMimeType(mimeType);
}
response.setContentType(mimeType);
}
public boolean contentDone() {
return contentGenerated;
}
public boolean allowFeedback() {
return allowFeedback;
}
/*
* (non-Javadoc)
*
* @see org.pentaho.core.solution.IOutputHandler#getOutputDefs()
*/
public Map getOutputDefs() {
return null;
}
/*
* (non-Javadoc)
*
* @see org.pentaho.core.solution.IOutputHandler#getOutputDef(java.lang.String)
*/
public IOutputDef getOutputDef(String name) {
return null;
}
/*
* (non-Javadoc)
*
* @see org.pentaho.core.solution.IOutputHandler#getFeedbackStream()
*/
public IContentItem getFeedbackContentItem() {
if (allowFeedback) {
// assume that content is generated becuase of this
contentGenerated = true;
return feedbackContent;
}
return null;
}
/*
* (non-Javadoc)
*
* @see org.pentaho.core.solution.IOutputHandler#getContentStream()
*/
public IContentItem getOutputContentItem(String objectName, String contentName, String solution, String instanceId,
String mimeType) {
if (objectName.equals(IOutputHandler.RESPONSE) && contentName.equals(IOutputHandler.CONTENT)) {
// assume that content is generated becuase of this
// change the content type if necessary
outputContent.setMimeType(mimeType);
contentGenerated = true;
return outputContent;
} else {
IContentOutputHandler output = null;
// this code allows us to stay backwards compatible
if (contentName != null && contentName.indexOf(":") == -1) { //$NON-NLS-1$
output = PentahoSystem.getOutputDestinationFromContentRef(objectName + ":" + contentName, session); //$NON-NLS-1$
} else {
output = PentahoSystem.getOutputDestinationFromContentRef(contentName, session);
if (output == null) {
output = PentahoSystem.getOutputDestinationFromContentRef(objectName + ":" + contentName, session); //$NON-NLS-1$
}
}
if (output != null) {
output.setInstanceId(instanceId);
output.setMimeType(mimeType);
output.setSolutionName(solution);
return output.getFileOutputContentItem();
}
}
return null;
}
public IContentItem getOutputContentItem(String objectName, String contentName, String title, String url,
String solution, String instanceId, String mimeType) {
return getOutputContentItem(objectName, contentName, solution, instanceId, mimeType);
}
public void setContentItem(IContentItem content, String objectName, String contentName) {
if (objectName.equals(IOutputHandler.RESPONSE) && contentName.equals(IOutputHandler.CONTENT)) {
setMimeType(content.getMimeType());
}
}
public void setOutput(String name, Object value) {
if (value == null) {
logger.warn(Messages.getString("HttpOutputHandler.WARN_0001_VALUE_IS_NULL"));
return;
}
if ("redirect".equalsIgnoreCase(name)) { //$NON-NLS-1$
try {
response.sendRedirect(value.toString());
} catch (IOException ioe) {
logger.error(Messages.getString("HttpOutputHandler.ERROR_0001_REDIRECT_FAILED",value.toString()), ioe);
}
} else if ("header".equalsIgnoreCase(name)) { //$NON-NLS-1$
try {
if (value instanceof Map) {
for (Iterator it = ((Map) value).entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
response.addHeader(entry.getKey().toString(), entry.getValue().toString());
}
} else {
String strVal = value.toString();
int i = strVal.indexOf('=');
String headerName = strVal.substring(0, i);
String headerValue = strVal.substring(i + 1);
response.addHeader(headerName, headerValue);
}
} catch (IndexOutOfBoundsException e) {
logger.error(e.getLocalizedMessage());
}
} else if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) {
try {
if (value instanceof IContentItem) {
IContentItem content = (IContentItem) value;
if ((response.getContentType() == null)
|| (!response.getContentType().equalsIgnoreCase(content.getMimeType()))) {
// response.setContentType( content.getMimeType() );
setMimeType(content.getMimeType());
}
InputStream inStr = content.getInputStream();
try {
OutputStream outStr = response.getOutputStream();
int inCnt = 0;
byte[] buf = new byte[4096];
while (-1 != (inCnt = inStr.read(buf))) {
outStr.write(buf, 0, inCnt);
}
} finally {
try {
inStr.close();
} catch (IOException ignored) {
}
}
contentGenerated = true;
} else {
if (response.getContentType() == null) {
setMimeType("text/html"); //$NON-NLS-1$
}
response.getOutputStream().write(value.toString().getBytes());
contentGenerated = true;
}
} catch (IOException ioe) {
logger.error(null, ioe);
}
}
}
public MimeTypeListener getMimeTypeListener() {
return mimeTypeListener;
}
public void setMimeTypeListener(MimeTypeListener mimeTypeListener) {
this.mimeTypeListener = mimeTypeListener;
}
public IContentItem getOutputContent() {
return outputContent;
}
public void setOutputContent(IContentItem outputContent) {
this.outputContent = outputContent;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public void setRuntimeContext(IRuntimeContext runtimeContext) {
this.runtimeContext = runtimeContext;
}
}
The table below shows all metrics for HttpOutputHandler.java.




