This repository has been archived by the owner on Apr 11, 2018. It is now read-only.
/
MediaOutputResource.java
149 lines (125 loc) · 4.83 KB
/
MediaOutputResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* License Agreement.
*
* Rich Faces - Natural Ajax for Java Server Faces (JSF)
*
* Copyright (C) 2007 Exadel, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.richfaces.resource;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import org.richfaces.component.AbstractMediaOutput;
import com.google.common.base.Strings;
/**
* @author Nick Belaevski
* @since 4.0
*/
@DynamicResource
public class MediaOutputResource extends AbstractUserResource implements StateHolder, CacheableResource {
private String contentType;
private boolean cacheable;
private MethodExpression contentProducer;
private ValueExpression expiresExpression;
/*
* TODO: add handling for expressions:
*
* 1. State saving 2. Evaluation
*/
private ValueExpression lastModifiedExpression;
private ValueExpression timeToLiveExpression;
private Object userData;
private String fileName;
public void encode(FacesContext facesContext) throws IOException {
OutputStream outStream = facesContext.getExternalContext().getResponseOutputStream();
contentProducer.invoke(facesContext.getELContext(), new Object[] { outStream, userData });
}
public boolean isTransient() {
return false;
}
public void setTransient(boolean newTransientValue) {
throw new UnsupportedOperationException();
}
public Object saveState(FacesContext context) {
Object[] state = new Object[5];
// parent fields state saving
state[0] = isCacheable(context) ? Boolean.TRUE : Boolean.FALSE;
state[1] = getContentType();
state[2] = UIComponentBase.saveAttachedState(context, userData);
state[3] = UIComponentBase.saveAttachedState(context, contentProducer);
state[4] = fileName;
return state;
}
public void restoreState(FacesContext context, Object stateObject) {
Object[] state = (Object[]) stateObject;
setCacheable((Boolean) state[0]);
setContentType((String) state[1]);
userData = UIComponentBase.restoreAttachedState(context, state[2]);
contentProducer = (MethodExpression) UIComponentBase.restoreAttachedState(context, state[3]);
fileName = (String) state[4];
}
// TODO use ResourceComponent or exchange object as argument?
@PostConstructResource
public void initialize() {
AbstractMediaOutput uiMediaOutput = (AbstractMediaOutput) UIComponent.getCurrentComponent(FacesContext
.getCurrentInstance());
this.setCacheable(uiMediaOutput.isCacheable());
this.setContentType(uiMediaOutput.getMimeType());
this.userData = uiMediaOutput.getValue();
this.contentProducer = uiMediaOutput.getCreateContent();
this.lastModifiedExpression = uiMediaOutput.getValueExpression("lastModfied");
this.expiresExpression = uiMediaOutput.getValueExpression("expires");
this.timeToLiveExpression = uiMediaOutput.getValueExpression("timeToLive");
this.fileName = uiMediaOutput.getFileName();
}
public boolean isCacheable(FacesContext context) {
return cacheable;
}
public void setCacheable(boolean cacheable) {
this.cacheable = cacheable;
}
public Date getExpires(FacesContext context) {
return null;
}
public int getTimeToLive(FacesContext context) {
return -1;
}
public String getEntityTag(FacesContext context) {
return null;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
@Override
public Map<String, String> getResponseHeaders() {
Map<String, String> headers = new HashMap<String, String>(2);
if (!Strings.isNullOrEmpty(fileName)) {
headers.put("Content-Disposition", "inline; filename=\"" + fileName + "\"");
}
return headers;
}
}