Skip to content

Instantly share code, notes, and snippets.

@sharmanaetor
Created April 25, 2013 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharmanaetor/28b33c7a2cfd8f5a4679 to your computer and use it in GitHub Desktop.
Save sharmanaetor/28b33c7a2cfd8f5a4679 to your computer and use it in GitHub Desktop.
<?xml version="1.0"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<resource-handler>com.company.reports.utility.ReportResourceHandler</resource-handler>
</application>
<lifecycle>
<phase-listener>com.liferay.faces.util.lifecycle.DebugPhaseListener</phase-listener>
</lifecycle>
</faces-config>
package com.company.reports;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import com.company.reports.utility.ReportExportResource;
@ManagedBean
@ViewScoped
public class MyReport {
private String reportResourceURL;
public MyReport() {
ReportExportResource reportExportResource = new ReportExportResource();
reportResourceURL = reportExportResource.getRequestPath();
}
public String getReportResourceURL() {
return reportResourceURL;
}
public void setReportResourceURL(String reportResourceURL) {
this.reportResourceURL = reportResourceURL;
}
}
package com.company.reports.utility;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.context.FacesContext;
public class ReportExportResource extends Resource {
// Public Constants
public static final String CONTENT_TYPE = "text/plain";
public static final String RESOURCE_NAME = "export";
// Private Data Members
private String requestPath;
public ReportExportResource() {
setLibraryName(ReportResourceHandler.LIBRARY_NAME);
setResourceName(RESOURCE_NAME);
setContentType(CONTENT_TYPE);
}
@Override
public InputStream getInputStream() throws IOException {
String str = "This is a dummy report";
return new ByteArrayInputStream(str.getBytes("UTF-8"));
}
@Override
public String getRequestPath() {
if (requestPath == null) {
StringBuilder buf = new StringBuilder();
buf.append(ResourceHandler.RESOURCE_IDENTIFIER);
buf.append("/");
buf.append(getResourceName());
buf.append("?ln=");
buf.append(getLibraryName());
requestPath = buf.toString();
}
return requestPath;
}
@Override
public Map<String, String> getResponseHeaders() {
// TODO Auto-generated method stub
return null;
}
@Override
public URL getURL() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean userAgentNeedsUpdate(FacesContext arg0) {
// TODO Auto-generated method stub
return false;
}
}
package com.company.reports.utility;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.application.ResourceHandlerWrapper;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import com.liferay.faces.util.logging.Logger;
import com.liferay.faces.util.logging.LoggerFactory;
public class ReportResourceHandler extends ResourceHandlerWrapper {
// Public Constants
public static final String LIBRARY_NAME = "pdfReports";
// Logger
private static final Logger logger = LoggerFactory.getLogger(ReportResourceHandler.class);
// Private Data Members
private ResourceHandler wrappedResourceHandler;
public ReportResourceHandler(ResourceHandler wrappedResourceHandler) {
this.wrappedResourceHandler = wrappedResourceHandler;
}
@Override
public Resource createResource(String resourceName, String libraryName) {
if (LIBRARY_NAME.equals(libraryName)) {
if (ReportExportResource.RESOURCE_NAME.equals(resourceName)) {
return new ReportExportResource();
} else {
return wrappedResourceHandler.createResource(resourceName, libraryName);
}
} else {
return wrappedResourceHandler.createResource(resourceName, libraryName);
}
}
@Override
public void handleResourceRequest(FacesContext facesContext) throws IOException {
ExternalContext externalContext = facesContext.getExternalContext();
String libraryName = externalContext.getRequestParameterMap().get("ln");
String resourceName = externalContext.getRequestParameterMap().get("javax.faces.resource");
if (LIBRARY_NAME.equals(libraryName) && ReportExportResource.RESOURCE_NAME.equals(resourceName)) {
Resource resource = createResource(resourceName, libraryName);
ReadableByteChannel readableByteChannel = null;
WritableByteChannel writableByteChannel = null;
InputStream inputStream = null;
int bufferSize = 1024;
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
try {
// Open an input stream in order to read the resource's
// contents/data.
inputStream = resource.getInputStream();
if (inputStream != null) {
// Set the response buffer size.
int responseBufferSize = byteBuffer.capacity();
externalContext.setResponseBufferSize(responseBufferSize);
String responseContentType = resource.getContentType();
if (responseContentType != null) {
externalContext.setResponseContentType(responseContentType);
}
// Copy the bytes in the resource's input stream to the
// response's output stream.
int responseContentLength = 0;
readableByteChannel = Channels.newChannel(inputStream);
writableByteChannel = Channels.newChannel(externalContext.getResponseOutputStream());
int bytesRead = readableByteChannel.read(byteBuffer);
int bytesWritten = 0;
while (bytesRead != -1) {
byteBuffer.rewind();
byteBuffer.limit(bytesRead);
do {
bytesWritten += writableByteChannel.write(byteBuffer);
} while (bytesWritten < responseContentLength);
byteBuffer.clear();
responseContentLength += bytesRead;
bytesRead = readableByteChannel.read(byteBuffer);
if (logger.isTraceEnabled()) {
// Surround with isTraceEnabled check in order to
// avoid unnecessary conversion
// of int to String.
logger.trace("Handling - MORE bytesRead=[{0}]", Integer.toString(bytesRead));
}
}
// Now that we know how big the file is, set the response
// content length.
externalContext.setResponseContentLength(responseContentLength);
externalContext.setResponseStatus(HttpServletResponse.SC_OK);
logger.debug("HANDLED (SC_OK) resourceName=[{0}], libraryName[{1}], responseContentType=[{2}], responseContentLength=[{3}]", new Object[] {
resourceName, libraryName, responseContentType, responseContentLength });
}
} catch (IOException e) {
externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
logger.error("NOT HANDLED (SC_NOT_FOUND) resourceName=[{0}], libraryName[{1}], errorMessage=[{2}]",
new Object[] { resourceName, libraryName, e.getMessage() }, e);
} finally {
if (writableByteChannel != null) {
writableByteChannel.close();
}
if (readableByteChannel != null) {
readableByteChannel.close();
}
if (inputStream != null) {
inputStream.close();
}
}
} else {
super.handleResourceRequest(facesContext);
}
}
@Override
public boolean libraryExists(String libraryName) {
if (LIBRARY_NAME.equals(libraryName)) {
return true;
} else {
return super.libraryExists(libraryName);
}
}
@Override
public ResourceHandler getWrapped() {
return wrappedResourceHandler;
}
}
<?xml version="1.0"?>
<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:aui="http://alloy.liferay.com/tld/aui">
<h:head />
<h:body>
<h:form>
<iframe src="#{myReport.reportResourceURL}"></iframe>
</h:form>
</h:body>
</f:view>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment