| 1 |
/* |
|---|
| 2 |
* (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. |
|---|
| 3 |
* |
|---|
| 4 |
* All rights reserved. This program and the accompanying materials |
|---|
| 5 |
* are made available under the terms of the GNU Lesser General Public License |
|---|
| 6 |
* (LGPL) version 2.1 which accompanies this distribution, and is available at |
|---|
| 7 |
* http://www.gnu.org/licenses/lgpl.html |
|---|
| 8 |
* |
|---|
| 9 |
* This library is distributed in the hope that it will be useful, |
|---|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 |
* Lesser General Public License for more details. |
|---|
| 13 |
* |
|---|
| 14 |
* Contributors: |
|---|
| 15 |
* Nuxeo - initial API and implementation |
|---|
| 16 |
* |
|---|
| 17 |
* $Id$ |
|---|
| 18 |
*/ |
|---|
| 19 |
|
|---|
| 20 |
package org.nuxeo.ecm.platform.ui.web.binding; |
|---|
| 21 |
|
|---|
| 22 |
import java.io.Serializable; |
|---|
| 23 |
|
|---|
| 24 |
import javax.el.ELContext; |
|---|
| 25 |
import javax.el.MethodExpression; |
|---|
| 26 |
import javax.el.MethodInfo; |
|---|
| 27 |
import javax.el.ValueExpression; |
|---|
| 28 |
import javax.faces.context.FacesContext; |
|---|
| 29 |
|
|---|
| 30 |
import org.apache.commons.logging.Log; |
|---|
| 31 |
import org.apache.commons.logging.LogFactory; |
|---|
| 32 |
import org.nuxeo.ecm.core.api.Blob; |
|---|
| 33 |
import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils; |
|---|
| 34 |
|
|---|
| 35 |
/** |
|---|
| 36 |
* Download method expression for blobs. |
|---|
| 37 |
* |
|---|
| 38 |
* @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> |
|---|
| 39 |
*/ |
|---|
| 40 |
public class DownloadMethodExpression extends MethodExpression implements |
|---|
| 41 |
Serializable { |
|---|
| 42 |
|
|---|
| 43 |
private static final Log log = LogFactory.getLog(DownloadMethodExpression.class); |
|---|
| 44 |
|
|---|
| 45 |
private static final long serialVersionUID = 9010857019674405375L; |
|---|
| 46 |
|
|---|
| 47 |
private final ValueExpression blobExpression; |
|---|
| 48 |
|
|---|
| 49 |
private final ValueExpression fileNameExpression; |
|---|
| 50 |
|
|---|
| 51 |
public DownloadMethodExpression(ValueExpression blobExpression, |
|---|
| 52 |
ValueExpression fileNameExpression) { |
|---|
| 53 |
this.blobExpression = blobExpression; |
|---|
| 54 |
this.fileNameExpression = fileNameExpression; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
// Expression interface |
|---|
| 58 |
|
|---|
| 59 |
@Override |
|---|
| 60 |
public boolean equals(Object o) { |
|---|
| 61 |
if (this == o) { |
|---|
| 62 |
return true; |
|---|
| 63 |
} |
|---|
| 64 |
if (!(o instanceof DownloadMethodExpression)) { |
|---|
| 65 |
return false; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
DownloadMethodExpression that = (DownloadMethodExpression) o; |
|---|
| 69 |
|
|---|
| 70 |
if (blobExpression != null ? !blobExpression.equals(that.blobExpression) |
|---|
| 71 |
: that.blobExpression != null) { |
|---|
| 72 |
return false; |
|---|
| 73 |
} |
|---|
| 74 |
if (fileNameExpression != null ? !fileNameExpression.equals(that.fileNameExpression) |
|---|
| 75 |
: that.fileNameExpression != null) { |
|---|
| 76 |
return false; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
return true; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
@Override |
|---|
| 83 |
public int hashCode() { |
|---|
| 84 |
int result = blobExpression != null ? blobExpression.hashCode() : 0; |
|---|
| 85 |
result = 31 * result |
|---|
| 86 |
+ (fileNameExpression != null ? fileNameExpression.hashCode() |
|---|
| 87 |
: 0); |
|---|
| 88 |
return result; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
@Override |
|---|
| 92 |
public String getExpressionString() { |
|---|
| 93 |
// return only the blob one |
|---|
| 94 |
return blobExpression == null ? null |
|---|
| 95 |
: blobExpression.getExpressionString(); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
@Override |
|---|
| 99 |
public boolean isLiteralText() { |
|---|
| 100 |
return blobExpression == null ? null : blobExpression.isLiteralText(); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
// MethodExpression interface |
|---|
| 104 |
|
|---|
| 105 |
@Override |
|---|
| 106 |
public MethodInfo getMethodInfo(ELContext context) { |
|---|
| 107 |
return null; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
@Override |
|---|
| 111 |
public Object invoke(ELContext context, Object[] params) { |
|---|
| 112 |
FacesContext faces = FacesContext.getCurrentInstance(); |
|---|
| 113 |
Blob blob = getBlob(context); |
|---|
| 114 |
String filename = getFilename(context); |
|---|
| 115 |
return ComponentUtils.download(faces, blob, filename); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
protected String getFilename(ELContext context) { |
|---|
| 119 |
if (fileNameExpression == null) { |
|---|
| 120 |
return null; |
|---|
| 121 |
} else { |
|---|
| 122 |
return (String) fileNameExpression.getValue(context); |
|---|
| 123 |
} |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
protected Blob getBlob(ELContext context) { |
|---|
| 127 |
if (blobExpression == null) { |
|---|
| 128 |
return null; |
|---|
| 129 |
} else { |
|---|
| 130 |
return (Blob) blobExpression.getValue(context); |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
} |
|---|