Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for https://github.com/TheCoder4eu/BootsFaces-OSP/issues/750 #1182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/net/bootsfaces/component/ajax/AJAXRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,20 @@ public static void generateBootsFacesAJAXAndJavaScript(FacesContext context, Cli
}

public static void generateBootsFacesAJAXAndJavaScript(FacesContext context, ClientBehaviorHolder component,
ResponseWriter rw, String specialEvent, String specialEventHandler, boolean isJQueryCallback,
ResponseWriter _rw, String specialEvent, String specialEventHandler, boolean isJQueryCallback,
boolean suppressAJAX) throws IOException {
boolean generatedAJAXCall = false;

/**
* Ugly fix for https://github.com/TheCoder4eu/BootsFaces-OSP/issues/750:
* The following logic calls rw.writeAttribute for the onclick handler
* twice, but the ResponseRenderer implementation keeps the first version,
* while the second one is correct.
* This wrapper holds back all calls to writeAttribute and only keeps the
* last version per attribute name.
*/
AttributeOverwritingResponseWriter rw = new AttributeOverwritingResponseWriter(_rw);

Collection<String> eventNames = component.getEventNames();
Map<String, String> jQueryEvents = ((IAJAXComponent) component).getJQueryEvents();
if (null != eventNames) {
Expand Down Expand Up @@ -264,6 +275,7 @@ public static void generateBootsFacesAJAXAndJavaScript(FacesContext context, Cli
}
}
// TODO: what about composite components?
rw.flushCachedAttributes();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package net.bootsfaces.component.ajax;

import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.faces.component.UIComponent;
import javax.faces.context.ResponseWriter;

/**
* Decorator for ResponseWriter, which caches all calls to
* writeAttribute. See https://github.com/TheCoder4eu/BootsFaces-OSP/issues/750
* @author Thomas Oster <[email protected]>
*/
public class AttributeOverwritingResponseWriter extends ResponseWriter {

private final ResponseWriter wrapped;

private static class AttributeToWrite {

Object o;
String string1;
}

public void flushCachedAttributes() throws IOException {
for (Entry<String, AttributeToWrite> e : attributeCache.entrySet()) {
wrapped.writeAttribute(e.getKey(), e.getValue().o, e.getValue().string1);
}
attributeCache.clear();
}

public static Map<String, AttributeToWrite> attributeCache = new LinkedHashMap<>();

public AttributeOverwritingResponseWriter(ResponseWriter w) {
this.wrapped = w;
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
wrapped.write(cbuf, off, len);
}

@Override
public void close() throws IOException {
wrapped.close();
}

@Override
public String getContentType() {
return wrapped.getContentType();
}

@Override
public String getCharacterEncoding() {
return wrapped.getCharacterEncoding();
}

@Override
public void flush() throws IOException {
wrapped.flush();
}

@Override
public void startDocument() throws IOException {
wrapped.startDocument();
}

@Override
public void endDocument() throws IOException {
wrapped.endDocument();
}

@Override
public void startElement(String string, UIComponent uic) throws IOException {
wrapped.startElement(string, uic);
}

@Override
public void endElement(String string) throws IOException {
wrapped.endElement(string);
}

@Override
public void writeAttribute(String string, Object o, String string1) throws IOException {
AttributeToWrite value = new AttributeToWrite();
value.o = o;
value.string1 = string1;
attributeCache.put(string, value);
}

@Override
public void writeURIAttribute(String string, Object o, String string1) throws IOException {
wrapped.writeURIAttribute(string, o, string1);
}

@Override
public void startCDATA() throws IOException {
wrapped.startCDATA();
}

@Override
public void endCDATA() throws IOException {
wrapped.endCDATA();
}

@Override
public void writeComment(Object o) throws IOException {
wrapped.writeComment(o);
}

@Override
public void writePreamble(String preamble) throws IOException {
wrapped.writePreamble(preamble);
}

@Override
public void writeDoctype(String doctype) throws IOException {
wrapped.writeDoctype(doctype);
}

@Override
public void writeText(Object o, String string) throws IOException {
wrapped.writeText(o, string);
}

@Override
public void writeText(Object text, UIComponent component, String property) throws IOException {
wrapped.writeText(text, component, property);
}

@Override
public void writeText(char[] chars, int i, int i1) throws IOException {
wrapped.writeText(chars, i, i1);
}

@Override
public ResponseWriter cloneWithWriter(Writer writer) {
return wrapped.cloneWithWriter(writer);
}

}