Skip to content

Commit

Permalink
Using clientId and expression String info to generate uniqueId (solve…
Browse files Browse the repository at this point in the history
…s issue with ViewScoped bean and AJAX update.) solves #2 and #4.
  • Loading branch information
Rudy De Busscher committed Jun 27, 2015
1 parent 3bf4900 commit 2f8e495
Show file tree
Hide file tree
Showing 11 changed files with 1,071 additions and 275 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package be.rubus.web.jsf.primefaces;

import be.rubus.web.jsf.primefaces.util.StringEncrypter;
import org.primefaces.component.graphicimage.GraphicImage;
import org.primefaces.component.graphicimage.GraphicImageRenderer;
import org.primefaces.model.StreamedContent;
Expand All @@ -29,80 +30,97 @@
import javax.faces.component.UIParameter;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*/
public class AdvancedGraphicImageRenderer extends GraphicImageRenderer {

private boolean determineIfAdvancedRendering(GraphicImage image) {
boolean result = false;

boolean isStreamedContent = image.getValue() instanceof StreamedContent;
Boolean advancedMarker = (Boolean) image.getAttributes().get(AdvancedRendererHandler.ADVANCED_RENDERING);

if (isStreamedContent && advancedMarker == null) {
result = determineSpecificParents(image);
}

if (!result && advancedMarker != null && advancedMarker) {
result = true;
}

return result;
}

private boolean determineSpecificParents(GraphicImage image) {
boolean result = false;
UIComponent current = image;
while (!result && !(current instanceof UIViewRoot)) {
result = current instanceof UIData;
if (!result) {
result = UIComponent.isCompositeComponent(current);
}
current = current.getParent();
}
return result;
}

@Override
protected String getImageSrc(FacesContext context, GraphicImage image) {
if (determineIfAdvancedRendering(image)) {
String src;
Object value = image.getValue();
StreamedContent streamedContent = (StreamedContent) value;


Resource resource = context.getApplication().getResourceHandler()
.createResource("dynamiccontent", "advancedPrimefaces", streamedContent.getContentType());
String resourcePath = resource.getRequestPath();
String rid = createUniqueContentId(context);
StringBuilder builder = new StringBuilder(resourcePath);
GraphicImageManager graphicImageManager = GraphicImageUtil.retrieveManager(context);
graphicImageManager.registerImage(streamedContent, rid);

builder.append("&").append(Constants.DYNAMIC_CONTENT_PARAM).append("=").append(rid);

for (UIComponent kid : image.getChildren()) {
if (kid instanceof UIParameter) {
UIParameter param = (UIParameter) kid;

builder.append("&").append(param.getName()).append("=").append(param.getValue());
}
}

src = builder.toString();

if (!image.isCache()) {
src += src.contains("?") ? "&" : "?";
src += "primefaces_image=" + UUID.randomUUID().toString();
}

src = context.getExternalContext().encodeResourceURL(src);
return src;

} else {
return super.getImageSrc(context, image);
}
}
private static final Logger LOG = Logger.getLogger(AdvancedGraphicImageRenderer.class.getName());
private static final StringEncrypter ENCRYPTER = new StringEncrypter(AdvancedGraphicImageRenderer.class.getName());

private boolean determineIfAdvancedRendering(GraphicImage image) {
boolean result = false;

boolean isStreamedContent = image.getValue() instanceof StreamedContent;
Boolean advancedMarker = (Boolean) image.getAttributes().get(AdvancedRendererHandler.ADVANCED_RENDERING);

if (isStreamedContent && advancedMarker == null) {
result = determineSpecificParents(image);
}

if (!result && advancedMarker != null && advancedMarker) {
result = true;
}

return result;
}

private boolean determineSpecificParents(GraphicImage image) {
boolean result = false;
UIComponent current = image;
while (!result && !(current instanceof UIViewRoot)) {
result = current instanceof UIData;
if (!result) {
result = UIComponent.isCompositeComponent(current);
}
current = current.getParent();
}
return result;
}

@Override
protected String getImageSrc(FacesContext context, GraphicImage image) {
if (determineIfAdvancedRendering(image)) {
String src;
Object value = image.getValue();
StreamedContent streamedContent = (StreamedContent) value;


Resource resource = context.getApplication().getResourceHandler()
.createResource("dynamiccontent", "advancedPrimefaces", streamedContent.getContentType());
String resourcePath = resource.getRequestPath();

String uniqueId = String.format("%1$s_%2$s", image.getClientId(context), image.getValueExpression("value").getExpressionString());
String encrypted = ENCRYPTER.encrypt(uniqueId);
String rid = null;
try {
rid = URLEncoder.encode(encrypted, "UTF-8");
} catch (UnsupportedEncodingException e) {
// Should never happen
LOG.log(Level.SEVERE, e.getMessage());
}

StringBuilder builder = new StringBuilder(resourcePath);
GraphicImageManager graphicImageManager = GraphicImageUtil.retrieveManager(context);
graphicImageManager.registerImage(streamedContent, rid);

builder.append("&").append(Constants.DYNAMIC_CONTENT_PARAM).append("=").append(rid);

for (UIComponent kid : image.getChildren()) {
if (kid instanceof UIParameter) {
UIParameter param = (UIParameter) kid;

builder.append("&").append(param.getName()).append("=").append(param.getValue());
}
}

src = builder.toString();

if (!image.isCache()) {
src += src.contains("?") ? "&" : "?";
src += "primefaces_image=" + UUID.randomUUID().toString();
}

src = context.getExternalContext().encodeResourceURL(src);
return src;

} else {
return super.getImageSrc(context, image);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package be.rubus.web.jsf.primefaces;

Expand All @@ -29,18 +29,18 @@
*/
public class AdvancedRendererHandler extends TagHandler {

public static final String ADVANCED_RENDERING = "ADVANCED_RENDERING";
public static final String ADVANCED_RENDERING = "ADVANCED_RENDERING";

private Boolean needAdvancedRendering;
private Boolean needAdvancedRendering;

public AdvancedRendererHandler(TagConfig config) {
super(config);
TagAttribute advancedRendering = config.getTag().getAttributes().get("value");
needAdvancedRendering = Boolean.valueOf(advancedRendering.getValue());
}
public AdvancedRendererHandler(TagConfig config) {
super(config);
TagAttribute advancedRendering = config.getTag().getAttributes().get("value");
needAdvancedRendering = Boolean.valueOf(advancedRendering.getValue());
}

@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
parent.getAttributes().put(ADVANCED_RENDERING, needAdvancedRendering);
}
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
parent.getAttributes().put(ADVANCED_RENDERING, needAdvancedRendering);
}
}
Loading

0 comments on commit 2f8e495

Please sign in to comment.