
com.sun.mojarra.scales.renderer.FileDownloadRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mojarra-scales Show documentation
Show all versions of mojarra-scales Show documentation
This is the core for Mojarra Scales. It has everything that Scales offers minus the multi-file upload component dependencies due to their size.
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/**
*
*/
package com.sun.mojarra.scales.renderer;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import com.sun.mojarra.scales.component.FileDownload;
import com.sun.mojarra.scales.util.ScalesUtil;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.el.ValueExpression;
import javax.faces.component.UIParameter;
/**
* @author Jason Lee
*
*/
public class FileDownloadRenderer extends AbstractRenderer {
protected Object oldBinding = null;
@Override
public boolean getRendersChildren() {
return false;
}
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
if (context == null) {
throw new NullPointerException("param 'context' is null");
}
if (component == null) {
throw new NullPointerException("param 'component' is null");
}
// suppress rendering if "rendered" property on the component is false.
if (!component.isRendered()) {
return;
}
FileDownload dl = (FileDownload) component;
super.encodeBegin(context, component);
Map attrs = dl.getAttributes();
if ((attrs.get("urlVar") != null) || (component.getChildCount() > 0)){
setElValue(context, dl);
}
if (FileDownload.METHOD_DOWNLOAD.equals(attrs.get("method"))) { // || (comp.getChildCount() == 0)) {
renderLink(context, dl);
}
}
protected void setElValue(FacesContext context, FileDownload comp) {
ValueExpression ve = ScalesUtil.createValueExpression("#{"+(String)comp.getAttributes().get("urlVar")+"}", FileDownload.class);
ve.setValue(context.getELContext(), generateUri(context, comp));
}
protected void resetElValue(FacesContext context, FileDownload comp) {
ValueExpression ve = ScalesUtil.createValueExpression("#{"+(String)comp.getAttributes().get("urlVar")+"}", FileDownload.class);
ve.setValue(context.getELContext(), null);
}
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
if (context == null) {
throw new NullPointerException("param 'context' is null");
}
if (component == null) {
throw new NullPointerException("param 'component' is null");
}
// suppress rendering if "rendered" property on the component is false.
if (!component.isRendered()) {
return;
}
FileDownload dl = (FileDownload) component;
Map attrs = dl.getAttributes();
if (FileDownload.METHOD_INLINE.equals((String)attrs.get("method"))) {
renderInline(context, dl);
} else if (FileDownload.METHOD_DOWNLOAD.equals((String)attrs.get("method"))) {
ResponseWriter writer = context.getResponseWriter();
writer.endElement("a"); // finsh up the link!
}
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
Map data = new HashMap();
data.put ("filename", (String)attrs.get("fileName"));
data.put ("method", (String)attrs.get("method"));
data.put ("data", attrs.get("data"));
data.put ("mimetype", (String)attrs.get("mimeType"));
request.getSession().setAttribute("HtmlDownload-" + dl.getClientId(context), data);
resetElValue(context, dl);
super.encodeEnd(context, component);
}
protected void renderInline(FacesContext context, FileDownload comp) throws IOException {
// String width = comp.getWidth();
// String height = comp.getHeight();
ResponseWriter writer = context.getResponseWriter();
Map attrs = comp.getAttributes();
String uri = context.getExternalContext().getRequestContextPath() +
generateUri(context, comp);
if (Boolean.TRUE.equals((Boolean)attrs.get("iframe"))) {
writer.startElement("iframe", comp);
writer.writeURIAttribute("src", uri, "src");
// writer.writeAttribute("width", width, "width");
// writer.writeAttribute("height", height, "height");
ScalesUtil.renderPassThruAttributes(writer, comp);
writer.endElement("iframe");
} else {
writer.startElement("object", comp);
writer.writeURIAttribute("data", uri, "data");
writer.writeAttribute("type", (String)attrs.get("mimeType"), "type");
// writer.writeAttribute("width", width, "width");
// writer.writeAttribute("height", height, "height");
ScalesUtil.renderPassThruAttributes(writer, comp);
writer.endElement("object");
}
}
protected void renderLink(FacesContext context, FileDownload comp) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("a", comp);
writer.writeURIAttribute("href", context.getExternalContext().getRequestContextPath() +
generateUri(context, comp), "data");
ScalesUtil.renderPassThruAttributes(writer, comp);
if (comp.getChildCount() > 0) {
//
} else {
writer.writeText((String)comp.getAttributes().get("text"), null);
}
}
protected String generateUri(FacesContext context, FileDownload comp) {
StringBuilder url = new StringBuilder();
url.append(ScalesUtil.createResourceUrl(context, comp.getClientId(context)))
.append("&").append(FileDownload.FILE_DOWNLOAD_INDICATOR);
for (UIComponent child : comp.getChildren()) {
if (child instanceof UIParameter) {
UIParameter param = (UIParameter) child;
String name = param.getName();
String value = "";
try {
value = URLEncoder.encode(param.getValue().toString(), "UTF-8");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(FileDownloadRenderer.class.getName()).log(Level.SEVERE, null, ex);
}
if ((name != null) && !"".equals(name)) {
url.append("&")
.append(name)
.append("=")
.append(value);
}
}
}
return url.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy