Newer
Older
Sonia Zorba
committed
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
package it.inaf.oats.ia2.tapschemamanager.webapp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.context.PartialResponseWriter;
import javax.faces.context.ResponseWriter;
import javax.faces.event.AjaxBehaviorEvent;
/**
*
* @author Sonia Zorba <zorba at oats.inaf.it>
*/
public class CustomPartialResponseWriter extends PartialResponseWriter {
private static class JavascriptCustomUpdate {
private final String componentId;
private final String value;
JavascriptCustomUpdate(String componentId, String value) {
this.componentId = componentId;
this.value = value;
}
}
private final List<JavascriptCustomUpdate> customUpdates;
public CustomPartialResponseWriter(ResponseWriter wrapped) {
super(wrapped);
customUpdates = new ArrayList<JavascriptCustomUpdate>();
}
public void addCustomUpdate(String componentId, String value) {
customUpdates.add(new JavascriptCustomUpdate(componentId, value));
}
public void addCustomUpdate(AjaxBehaviorEvent event, String value) {
this.addCustomUpdate(event.getComponent().getClientId(), value);
}
@Override
public void endDocument() throws IOException {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
for (Map.Entry<String, String> entry : params.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
if (!customUpdates.isEmpty()) {
startExtension(Collections.singletonMap("id", "jsupdates"));
for (JavascriptCustomUpdate update : customUpdates) {
write(String.format("<jsupdate src=\"%s\">%s</jsupdate>", update.componentId, update.value));
}
endExtension();
}
super.endDocument();
}
}