Search in sources :

Example 1 with PrimeEnvironment

use of org.primefaces.config.PrimeEnvironment in project primefaces by primefaces.

the class AbstractBehaviorHandler method apply.

@Override
public void apply(FaceletContext faceletContext, UIComponent parent) throws IOException {
    if (!ComponentHandler.isNew(parent)) {
        return;
    }
    String eventName = getEventName();
    if (UIComponent.isCompositeComponent(parent)) {
        boolean tagApplied = false;
        if (parent instanceof ClientBehaviorHolder) {
            applyAttachedObject(faceletContext, parent);
            tagApplied = true;
        }
        BeanInfo componentBeanInfo = (BeanInfo) parent.getAttributes().get(UIComponent.BEANINFO_KEY);
        if (null == componentBeanInfo) {
            throw new TagException(tag, "Composite component does not have BeanInfo attribute");
        }
        BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
        if (null == componentDescriptor) {
            throw new TagException(tag, "Composite component BeanInfo does not have BeanDescriptor");
        }
        List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) componentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
        if (null == targetList && !tagApplied) {
            throw new TagException(tag, "Composite component does not support behavior events");
        }
        boolean supportedEvent = false;
        if (targetList != null) {
            for (int i = 0; i < targetList.size(); i++) {
                AttachedObjectTarget target = targetList.get(i);
                if (target instanceof BehaviorHolderAttachedObjectTarget) {
                    BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) target;
                    if ((null != eventName && eventName.equals(behaviorTarget.getName())) || (null == eventName && behaviorTarget.isDefaultEvent())) {
                        supportedEvent = true;
                        break;
                    }
                }
            }
        }
        if (supportedEvent) {
            // Workaround to implementation specific composite component handlers
            FacesContext context = FacesContext.getCurrentInstance();
            PrimeEnvironment environment = PrimeApplicationContext.getCurrentInstance(context).getEnvironment();
            if (environment.isMojarra()) {
                addAttachedObjectHandlerToMojarra(environment, parent);
            } else {
                addAttachedObjectHandlerToMyFaces(parent, faceletContext);
            }
        } else {
            if (!tagApplied) {
                throw new TagException(tag, "Composite component does not support event " + eventName);
            }
        }
    } else if (parent instanceof ClientBehaviorHolder) {
        applyAttachedObject(faceletContext, parent);
    } else {
        throw new TagException(tag, "Unable to attach behavior to non-ClientBehaviorHolder parent");
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) BehaviorHolderAttachedObjectTarget(javax.faces.view.BehaviorHolderAttachedObjectTarget) AttachedObjectTarget(javax.faces.view.AttachedObjectTarget) BeanInfo(java.beans.BeanInfo) BehaviorHolderAttachedObjectTarget(javax.faces.view.BehaviorHolderAttachedObjectTarget) ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) BeanDescriptor(java.beans.BeanDescriptor) PrimeEnvironment(org.primefaces.config.PrimeEnvironment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with PrimeEnvironment

use of org.primefaces.config.PrimeEnvironment in project primefaces by primefaces.

the class FileUploadFilter method init.

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    String uploader = filterConfig.getServletContext().getInitParameter(Constants.ContextParams.UPLOADER);
    if (uploader == null || "auto".equals(uploader)) {
        PrimeEnvironment environment = new StartupPrimeEnvironment();
        bypass = environment.isAtLeastJsf22();
    } else if ("native".equals(uploader)) {
        bypass = true;
    } else if ("commons".equals(uploader)) {
        bypass = false;
    }
    thresholdSize = filterConfig.getInitParameter(THRESHOLD_SIZE_PARAM);
    uploadDir = filterConfig.getInitParameter(UPLOAD_DIRECTORY_PARAM);
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("FileUploadFilter initiated successfully");
    }
}
Also used : StartupPrimeEnvironment(org.primefaces.config.StartupPrimeEnvironment) StartupPrimeEnvironment(org.primefaces.config.StartupPrimeEnvironment) PrimeEnvironment(org.primefaces.config.PrimeEnvironment)

Example 3 with PrimeEnvironment

use of org.primefaces.config.PrimeEnvironment in project primefaces by primefaces.

the class PostConstructApplicationEventListener method processEvent.

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
    // temp manually instantiate as the ApplicationContext is not available yet
    PrimeEnvironment environment = new StartupPrimeEnvironment();
    LOGGER.log(Level.INFO, "Running on PrimeFaces {0}", environment.getBuildVersion());
    if (environment.isAtLeastJsf23()) {
        Jsf23Helper.addSearchKeywordResolvers();
    }
}
Also used : StartupPrimeEnvironment(org.primefaces.config.StartupPrimeEnvironment) StartupPrimeEnvironment(org.primefaces.config.StartupPrimeEnvironment) PrimeEnvironment(org.primefaces.config.PrimeEnvironment)

Example 4 with PrimeEnvironment

use of org.primefaces.config.PrimeEnvironment in project primefaces by primefaces.

the class WidgetBuilderTest method attributeDefaultValueIsNotEncoded.

@Test
public void attributeDefaultValueIsNotEncoded() throws IOException {
    CollectingResponseWriter writer = new CollectingResponseWriter();
    FacesContext context = new FacesContextMock(writer);
    PrimeConfigurationMock config = new PrimeConfigurationMock(context, new PrimeEnvironment(context));
    config.setMoveScriptsToBottom(true);
    WidgetBuilder builder = new WidgetBuilder(context, config);
    Panel panel = mock(Panel.class);
    when(panel.resolveWidgetVar(context)).thenReturn("myComponent");
    when(panel.getClientId(context)).thenReturn("myComponent1");
    builder.init("MyComponent", panel);
    String defaultValue = "'My custom default value'";
    builder.attr("someAttribute", null, defaultValue);
    builder.finish();
    String output = writer.toString();
    assertFalse(output.contains(defaultValue));
    String expectedOutput = "<script id=\"myComponent1_s\" type=\"text/javascript\">PrimeFaces" + ".cw(\"MyComponent\",\"myComponent\",{id:\"myComponent1\"});</script>";
    assertEquals(expectedOutput, output);
}
Also used : FacesContext(javax.faces.context.FacesContext) Panel(org.primefaces.component.panel.Panel) PrimeEnvironment(org.primefaces.config.PrimeEnvironment) CollectingResponseWriter(org.primefaces.mock.CollectingResponseWriter) FacesContextMock(org.primefaces.mock.FacesContextMock) PrimeConfigurationMock(org.primefaces.mock.pf.PrimeConfigurationMock) Test(org.junit.jupiter.api.Test)

Example 5 with PrimeEnvironment

use of org.primefaces.config.PrimeEnvironment in project primefaces by primefaces.

the class WidgetBuilderTest method attrJavascriptEscapeJavascript.

@Test
public void attrJavascriptEscapeJavascript() throws IOException {
    CollectingResponseWriter writer = new CollectingResponseWriter();
    FacesContext context = new FacesContextMock(writer);
    PrimeConfigurationMock config = new PrimeConfigurationMock(context, new PrimeEnvironment(context));
    config.setMoveScriptsToBottom(true);
    WidgetBuilder builder = new WidgetBuilder(context, config);
    Panel panel = mock(Panel.class);
    when(panel.resolveWidgetVar(context)).thenReturn("myComponent");
    when(panel.getClientId(context)).thenReturn("myComponent1");
    builder.init("MyComponent", panel);
    builder.attr("someAttribute", "<script>alert('Hello World!')</script>", null);
    builder.finish();
    String output = writer.toString();
    String expectedOutput = "<script id=\"myComponent1_s\" type=\"text/javascript\">PrimeFaces" + ".cw(\"MyComponent\",\"myComponent\",{id:\"myComponent1\"," + "someAttribute:\"<script>alert(\\x27Hello World!\\x27)<\\/script>\"});</script>";
    assertEquals(expectedOutput, output);
}
Also used : FacesContext(javax.faces.context.FacesContext) Panel(org.primefaces.component.panel.Panel) PrimeEnvironment(org.primefaces.config.PrimeEnvironment) CollectingResponseWriter(org.primefaces.mock.CollectingResponseWriter) FacesContextMock(org.primefaces.mock.FacesContextMock) PrimeConfigurationMock(org.primefaces.mock.pf.PrimeConfigurationMock) Test(org.junit.jupiter.api.Test)

Aggregations

PrimeEnvironment (org.primefaces.config.PrimeEnvironment)8 FacesContext (javax.faces.context.FacesContext)4 Test (org.junit.jupiter.api.Test)3 Panel (org.primefaces.component.panel.Panel)3 StartupPrimeEnvironment (org.primefaces.config.StartupPrimeEnvironment)3 CollectingResponseWriter (org.primefaces.mock.CollectingResponseWriter)3 FacesContextMock (org.primefaces.mock.FacesContextMock)3 PrimeConfigurationMock (org.primefaces.mock.pf.PrimeConfigurationMock)3 BeanDescriptor (java.beans.BeanDescriptor)1 BeanInfo (java.beans.BeanInfo)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FacesException (javax.faces.FacesException)1 UIComponent (javax.faces.component.UIComponent)1 ClientBehaviorHolder (javax.faces.component.behavior.ClientBehaviorHolder)1 Renderer (javax.faces.render.Renderer)1 AttachedObjectTarget (javax.faces.view.AttachedObjectTarget)1 BehaviorHolderAttachedObjectTarget (javax.faces.view.BehaviorHolderAttachedObjectTarget)1 ServletException (javax.servlet.ServletException)1