Datasets:

Modalities:
Text
Formats:
json
Languages:
code
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 6,042 Bytes
eb67da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**

 * 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.ajax4jsf.resource;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Date;

import javax.el.ELContext;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;

/**

 * @author shura

 *

 */
public class UserResource extends InternetResourceBase {

	private String contentType;
	/**

	 * 

	 */
	public UserResource(boolean cacheable, boolean session, String mime) {
		super();		
		setCacheable(cacheable);
		setSessionAware(session);
		setContentType(mime);
	}
	/**

	 * @return Returns the contentType.

	 */
	public String getContentType(ResourceContext resourceContext) {
		return contentType;
	}
	/**

	 * @param contentType The contentType to set.

	 */
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	/* (non-Javadoc)

	 * @see org.ajax4jsf.resource.InternetResourceBase#getDataToStore(javax.faces.context.FacesContext, java.lang.Object)

	 */
	public Object getDataToStore(FacesContext context, Object data) {
		UriData dataToStore = null;
		if (data instanceof ResourceComponent2) {
			ResourceComponent2 resource = (ResourceComponent2) data;
			dataToStore = new UriData();
			dataToStore.value = resource.getValue();
			dataToStore.createContent = UIComponentBase.saveAttachedState(context,resource.getCreateContentExpression());
			if (data instanceof UIComponent) {
				UIComponent component = (UIComponent) data;
				ValueExpression expires = component.getValueExpression("expires");
				if (null != expires) {
					dataToStore.expires = UIComponentBase.saveAttachedState(context,expires);
				}
				ValueExpression lastModified = component.getValueExpression("lastModified");
				if (null != lastModified) {
					dataToStore.modified = UIComponentBase.saveAttachedState(context,lastModified);
				}
			}
		}
		return dataToStore;
	}
	/* (non-Javadoc)

	 * @see org.ajax4jsf.resource.InternetResourceBase#send(org.ajax4jsf.resource.ResourceContext)

	 */
	public void send(ResourceContext context) throws IOException {
		UriData data = (UriData) restoreData(context);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (null != data && null != facesContext ) {
			// Send headers
			ELContext elContext = facesContext.getELContext();
//			if(data.expires != null){
//				ValueExpression binding = (ValueExpression) UIComponentBase.restoreAttachedState(facesContext,data.expires);
//				Date expires = (Date) binding.getValue(elContext);
//				context.setDateHeader("Expires",expires.getTime());
//			}
//			if(data.modified != null){
//				ValueExpression binding = (ValueExpression) UIComponentBase.restoreAttachedState(facesContext,data.modified);
//				Date modified = (Date) binding.getValue(elContext);
//				context.setDateHeader("Last-Modified",modified.getTime());
//			}
			// Send content
			OutputStream out = context.getOutputStream();
			MethodExpression send = (MethodExpression) UIComponentBase.restoreAttachedState(facesContext,data.createContent);
			send.invoke(elContext,new Object[]{out,data.value});
		}
	}

	
	@Override
	public Date getLastModified(ResourceContext resourceContext) {
		UriData data = (UriData) restoreData(resourceContext);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (null != data && null != facesContext ) {
			// Send headers
			ELContext elContext = facesContext.getELContext();
			if(data.modified != null){
			ValueExpression binding = (ValueExpression) UIComponentBase.restoreAttachedState(facesContext,data.modified);
			Date modified = (Date) binding.getValue(elContext);
			if (null != modified) {
				return modified;
			}
		}
		}
		return super.getLastModified(resourceContext);
	}
	
	
	@Override
	public long getExpired(ResourceContext resourceContext) {
		UriData data = (UriData) restoreData(resourceContext);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (null != data && null != facesContext ) {
			// Send headers
			ELContext elContext = facesContext.getELContext();
			if(data.expires != null){
			ValueExpression binding = (ValueExpression) UIComponentBase.restoreAttachedState(facesContext,data.expires);
			Date expires = (Date) binding.getValue(elContext);
			if (null != expires) {
				return expires.getTime()-System.currentTimeMillis();
			}
		}
		}
		return super.getExpired(resourceContext);
	}
	/* (non-Javadoc)

	 * @see org.ajax4jsf.resource.InternetResourceBase#requireFacesContext()

	 */
	public boolean requireFacesContext() {
		// TODO Auto-generated method stub
		return true;
	}
	// BUG: CWE-502 Deserialization of Untrusted Data
	// public static class UriData implements Serializable {
	// FIXED: 
	public static class UriData implements SerializableResource {

		/**

		 * 

		 */
		private static final long serialVersionUID = 1258987L;
		
		private Object value;
		
		private Object createContent;
		
		private Object expires;
		
		private Object modified;
	}

}