1 5 package org.exoplatform.faces.core; 6 7 import java.util.ResourceBundle ; 8 import javax.faces.FactoryFinder; 9 import javax.faces.component.UIComponent; 10 import javax.faces.component.UIViewRoot; 11 import javax.faces.context.ExternalContext; 12 import javax.faces.context.FacesContext; 13 import javax.faces.event.PhaseId; 14 import javax.faces.render.RenderKit; 15 import javax.faces.render.RenderKitFactory; 16 import javax.faces.render.Renderer; 17 import org.exoplatform.container.SessionContainer; 18 import org.exoplatform.container.client.http.HttpClientInfo; 19 import org.exoplatform.faces.context.PortletExternalContext; 20 import org.exoplatform.faces.core.component.InformationProvider; 21 import org.exoplatform.faces.core.component.UIExoComponent; 22 import org.exoplatform.portal.session.PortalResources; 23 28 public class Util { 29 static public Renderer getRenderer(FacesContext context, String type, String family) { 30 if(type != null) { 31 HttpClientInfo info = 32 (HttpClientInfo) SessionContainer.getInstance().getMonitor().getClientInfo(); 33 if(HttpClientInfo.MOBILE_BROWSER_TYPE.equals(info.getPreferredMimeType())) { 34 return getXHTMLMPRenderer(context,type, family) ; 35 } 36 return getXHTMLRenderer(context,type, family) ; 37 } 38 return null ; 39 } 40 41 static public Renderer getXHTMLMPRenderer(FacesContext context, String type, String family) { 42 RenderKitFactory rkFactory = 43 (RenderKitFactory)FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); 44 RenderKit renderKit = rkFactory.getRenderKit(null, UIExoComponent.XHTMLMP_KIT); 45 Renderer renderer = renderKit.getRenderer(family, type); 46 if(renderer == null) { 47 renderer = getXHTMLRenderer(context,type, family) ; 48 } 49 return renderer ; 50 } 51 52 static public Renderer getXHTMLRenderer(FacesContext context, String type, String family) { 53 RenderKitFactory rkFactory = 54 (RenderKitFactory)FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); 55 RenderKit renderKit = 56 rkFactory.getRenderKit(context, context.getViewRoot().getRenderKitId()); 57 return renderKit.getRenderer(family, type); 58 } 59 60 static public ResourceBundle getApplicationResourceBundle() { 61 FacesContext context = FacesContext.getCurrentInstance() ; 62 javax.faces.context.ExternalContext externalContext = context.getExternalContext() ; 63 if(externalContext instanceof PortletExternalContext) { 64 PortletExternalContext econtext = (PortletExternalContext) externalContext ; 65 return econtext.getApplicationResourceBundle() ; 66 } 67 PortalResources appres = 68 (PortalResources)SessionContainer.getComponent(PortalResources.class); 69 return appres.getApplicationResource(); 70 } 71 72 static public InformationProvider findInformationProvider(UIComponent src) { 73 InformationProvider provider = null ; 74 while(provider == null) { 75 if(src instanceof InformationProvider) { 76 provider = (InformationProvider) src ; 77 } else if (src instanceof UIViewRoot){ 78 return null ; 79 } else { 80 src = src.getParent() ; 81 } 82 } 83 return provider ; 84 } 85 86 static public PhaseId getActionPhaseId(String action) { 87 if(action.startsWith("PhaseId[1].")) { 88 return PhaseId.APPLY_REQUEST_VALUES ; 89 } 90 return PhaseId.INVOKE_APPLICATION ; 91 } 92 93 static public String encodeActionPhase(String action , PhaseId phaseId) { 94 if(PhaseId.APPLY_REQUEST_VALUES == phaseId) { 95 action = "PhaseId[1]." + action ; 96 } 97 return action ; 98 } 99 100 static public String getRemoteUser() { 101 FacesContext context = FacesContext.getCurrentInstance() ; 102 ExternalContext econtext = context.getExternalContext() ; 103 return econtext.getRemoteUser() ; 104 } 105 106 static public boolean hasRole(String role) { 107 FacesContext context = FacesContext.getCurrentInstance() ; 108 ExternalContext econtext = context.getExternalContext() ; 109 return econtext.isUserInRole(role) ; 110 } 111 } | Popular Tags |