Skip to content

Commit

Permalink
Improve page session attributes resolution
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Oct 9, 2023
1 parent e5f6d64 commit b623d74
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import jakarta.el.ELResolver;
import jakarta.el.PropertyNotFoundException;
import jakarta.faces.component.UIViewRoot;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;

import java.io.Serializable;
Expand All @@ -44,7 +45,7 @@ public class PageSessionResolver extends ELResolver {

/**
* <p>
* The name an expression must use when it explicitly specifies page session. ("pageSession")
* The name an expression must use when it explicitly specifies page session ("pageSession").
* </p>
*/
public static final String PAGE_SESSION = "pageSession";
Expand All @@ -58,7 +59,7 @@ public class PageSessionResolver extends ELResolver {

/**
* <p>
* Checks "page session" to see if the value exists.
* Checks standard scopes and "page session" to see if the value exists.
* </p>
*/
@Override
Expand All @@ -71,31 +72,60 @@ public Object getValue(ELContext elContext, Object base, Object property) {
throw new PropertyNotFoundException();
}

elContext.setPropertyResolved(true);

FacesContext facesContext = (FacesContext) elContext.getContext(FacesContext.class);
ExternalContext externalContext = facesContext.getExternalContext();
UIViewRoot viewRoot = facesContext.getViewRoot();
Map<String, Serializable> pageSession = getPageSession(facesContext, viewRoot);
String attribute = (String) property;

Object value = null;
// Check to see if expression explicitly asks for PAGE_SESSION
if (property.equals(PAGE_SESSION)) {
// It does, return the Map
if (pageSession == null) {
// No Map! That's ok, create one...
pageSession = createPageSession(facesContext, viewRoot);
}
value = pageSession;
} else {
if (pageSession != null) {
// Check page session
value = pageSession.get(property.toString());
return pageSession;
}

// Check page session exists and contains a property
if (pageSession == null || !pageSession.containsKey(attribute)) {
elContext.setPropertyResolved(false);
return null;
}

// Check request map
Object value = externalContext.getRequestMap().get(attribute);
if (value != null) {
return value;
}

// Check view map
Map<String, Object> viewMap = viewRoot.getViewMap(false);
if (viewMap != null) {
value = viewMap.get(attribute);
if (value != null) {
return value;
}
}

if (value != null || (pageSession != null && pageSession.containsKey(property.toString()))) {
elContext.setPropertyResolved(true);
// Check session map
value = externalContext.getSessionMap().get(attribute);
if (value != null) {
return value;
}

// Check application map
value = externalContext.getApplicationMap().get(attribute);
if (value != null) {
return value;
}

return value;
// Not found updated property in the standard scopes.
// Return value from page session.
return pageSession.get(attribute);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion jsftemplating/src/main/resources/META-INF/faces-config.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 Contributors to the Eclipse Foundation. All rights reserved.
Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation. All rights reserved.
Copyright (c) 2006, 2022 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
Expand Down

0 comments on commit b623d74

Please sign in to comment.