Fixes after testing with Apache MyFaces 2

This commit is contained in:
Rossen Stoyanchev
2010-09-21 12:10:47 +00:00
parent 1402a68f37
commit 7b2ab7587c
8 changed files with 62 additions and 140 deletions

View File

@@ -1,53 +0,0 @@
/*
* Copyright 2004-2010 the original author or authors.
*
* Licensed 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.
*/
package org.springframework.faces.security;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import com.sun.facelets.tag.AbstractTagLibrary;
/**
* Registers a tag handler for the <authorize> tag and several EL functions that can be used on any component that
* accepts EL expressions in its attributes. For details on the EL functions see {@link Jsf12FaceletsAuthorizeTagUtils}.
*
* @author Rossen Stoyanchev
* @since 2.2.0
* @see Jsf12FaceletsAuthorizeTagHandler
* @see Jsf12FaceletsAuthorizeTagUtils
*/
public class SpringSecurityJsf12TagLibrary extends AbstractTagLibrary {
public static final String NAMESPACE = "http://www.springframework.org/security/tags";
public SpringSecurityJsf12TagLibrary() {
super(NAMESPACE);
this.addTagHandler("authorize", Jsf12FaceletsAuthorizeTagHandler.class);
try {
Method[] methods = Jsf12FaceletsAuthorizeTagUtils.class.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isStatic(methods[i].getModifiers())) {
this.addFunction(methods[i].getName(), methods[i]);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,53 +0,0 @@
/*
* Copyright 2004-2010 the original author or authors.
*
* Licensed 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.
*/
package org.springframework.faces.security;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import com.sun.faces.facelets.tag.AbstractTagLibrary;
/**
* Registers a tag handler for the &lt;authorize&gt; tag and several EL functions that can be used on any component that
* accepts EL expressions in its attributes. For details on the EL functions see {@link FaceletsAuthorizeTagUtils}.
*
* @author Rossen Stoyanchev
* @since 2.2.0
* @see FaceletsAuthorizeTagHandler
* @see FaceletsAuthorizeTagUtils
*/
public class SpringSecurityTagLibrary extends AbstractTagLibrary {
public static final String NAMESPACE = "http://www.springframework.org/security/tags";
public SpringSecurityTagLibrary() {
super(NAMESPACE);
this.addTagHandler("authorize", FaceletsAuthorizeTagHandler.class);
try {
Method[] methods = FaceletsAuthorizeTagUtils.class.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isStatic(methods[i].getModifiers())) {
this.addFunction(methods[i].getName(), methods[i]);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -20,6 +20,9 @@ package org.springframework.faces.webflow;
* writing JSF state and manages that in Web Flow's view scope. The FlowViewResponseStateManager is plugged in only in a
* JSF 2 environment.
*
* Note that partial state saving in Apache MyFaces is not yet supported. Use the javax.faces.PARTIAL_STATE_SAVING context
* parameter in web.xml to disable it.
*
* @author Rossen Stoyanchev
* @since 2.2.0
*/
@@ -49,7 +52,8 @@ public class FlowRenderKit extends RenderKitWrapper {
* ResponseStateManager instance otherwise.
*/
public ResponseStateManager getResponseStateManager() {
return (JsfRuntimeInformation.isAtLeastJsf20()) ? responseStateManager : delegate.getResponseStateManager();
return (JsfRuntimeInformation.isPartialStateSavingSupported()) ? responseStateManager : delegate
.getResponseStateManager();
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.faces.webflow;
import static org.springframework.faces.webflow.JsfRuntimeInformation.isAtLeastJsf20;
import java.io.IOException;
import javax.faces.application.StateManager;
@@ -165,7 +163,7 @@ public class FlowViewStateManager extends StateManager {
if (context.getViewRoot().isTransient()) {
return null;
}
if ((!JsfUtils.isFlowRequest()) || isAtLeastJsf20()) {
if ((!JsfUtils.isFlowRequest()) || JsfRuntimeInformation.isPartialStateSavingSupported()) {
return delegate.saveView(context);
} else {
RequestContext requestContext = RequestContextHolder.getRequestContext();
@@ -188,7 +186,7 @@ public class FlowViewStateManager extends StateManager {
* </p>
*/
public UIViewRoot restoreView(FacesContext context, String viewId, String renderKitId) {
if ((!JsfUtils.isFlowRequest()) || isAtLeastJsf20()) {
if ((!JsfUtils.isFlowRequest()) || JsfRuntimeInformation.isPartialStateSavingSupported()) {
return delegate.restoreView(context, viewId, renderKitId);
} else {
UIViewRoot viewRoot = restoreTreeStructure(context, viewId, renderKitId);

View File

@@ -16,11 +16,13 @@
package org.springframework.faces.webflow;
import java.io.IOException;
import java.io.Writer;
import javax.faces.FactoryFinder;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextWrapper;
import javax.faces.context.PartialResponseWriter;
import javax.faces.context.ResponseWriter;
import javax.faces.render.RenderKit;
import javax.faces.render.RenderKitFactory;
import javax.servlet.http.HttpServletRequest;
@@ -28,8 +30,6 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.js.ajax.AbstractAjaxHandler;
import com.sun.faces.context.PartialViewContextImpl;
/**
* Ajax handler for JSF 2 requests that can identify JSF 2 Ajax requests and send redirect instructions back to the
* client by including a redirect instruction in the content of the response.
@@ -66,9 +66,7 @@ public class JsfAjaxHandler extends AbstractAjaxHandler {
FacesContextHelper helper = new FacesContextHelper();
try {
FacesContext facesContext = helper.getFacesContext(getServletContext(), request, response);
facesContext = new FixedRenderKitFacesContext(facesContext, determineRenderKitId(request, response));
PartialViewContextImpl partialViewContext = new PartialViewContextImpl(facesContext);
PartialResponseWriter writer = partialViewContext.getPartialResponseWriter();
PartialResponseWriter writer = createPartialResponseWriter(facesContext);
writer.startDocument();
writer.redirect(targetUrl);
writer.endDocument();
@@ -77,29 +75,21 @@ public class JsfAjaxHandler extends AbstractAjaxHandler {
}
}
protected String determineRenderKitId(HttpServletRequest request, HttpServletResponse response) {
return RenderKitFactory.HTML_BASIC_RENDER_KIT;
}
private class FixedRenderKitFacesContext extends FacesContextWrapper {
private FacesContext delegate;
private String renderKitId;
public FixedRenderKitFacesContext(FacesContext delegate, String renderKitId) {
this.delegate = delegate;
this.renderKitId = renderKitId;
}
@Override
public FacesContext getWrapped() {
return this.delegate;
}
@Override
public RenderKit getRenderKit() {
private PartialResponseWriter createPartialResponseWriter(FacesContext context) throws IOException {
ExternalContext externalContext = context.getExternalContext();
String encoding = externalContext.getRequestCharacterEncoding();
externalContext.setResponseCharacterEncoding(encoding);
ResponseWriter responseWriter = null;
Writer out = externalContext.getResponseOutputWriter();
if (out != null) {
RenderKitFactory factory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
return factory.getRenderKit(this, renderKitId);
RenderKit renderKit = factory.getRenderKit(context, RenderKitFactory.HTML_BASIC_RENDER_KIT);
responseWriter = renderKit.createResponseWriter(out, "text/xml", encoding);
}
if (responseWriter instanceof PartialResponseWriter) {
return (PartialResponseWriter) responseWriter;
} else {
return new PartialResponseWriter(responseWriter);
}
}

View File

@@ -77,4 +77,11 @@ public class JsfRuntimeInformation {
"getPortletContextName"));
}
/**
* Returns true if Web Flow supports partial state saving in the current runtime environment.
*/
public static boolean isPartialStateSavingSupported() {
return (JsfRuntimeInformation.isAtLeastJsf20() && (!JsfRuntimeInformation.isMyFacesPresent()));
}
}

View File

@@ -3,5 +3,24 @@
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<library-class>org.springframework.faces.security.SpringSecurityTagLibrary</library-class>
<namespace>http://www.springframework.org/security/tags</namespace>
<tag>
<tag-name>authorize</tag-name>
<handler-class>org.springframework.faces.security.FaceletsAuthorizeTagHandler</handler-class>
</tag>
<function>
<function-name>areAllGranted</function-name>
<function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class>
<function-signature>boolean areAllGranted(java.lang.String)</function-signature>
</function>
<function>
<function-name>areAnyGranted</function-name>
<function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class>
<function-signature>boolean areAnyGranted(java.lang.String)</function-signature>
</function>
<function>
<function-name>areNotGranted</function-name>
<function-class>org.springframework.faces.security.FaceletsAuthorizeTagUtils</function-class>
<function-signature>boolean areNotGranted(java.lang.String)</function-signature>
</function>
</facelet-taglib>

View File

@@ -29,6 +29,16 @@
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<!--
Uncomment this to disable partial state saving when using Apache MyFaces 2 !!
-->
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
<!-- Declare Spring Security Facelets tag library -->
<context-param>