Search in sources :

Example 31 with ExternalContext

use of javax.faces.context.ExternalContext in project joinfaces-bean-test by larmic.

the class FacesContextMock method replaceIn.

public FacesContextMock replaceIn(ApplicationContext applicationContext) {
    final FacesContext facesContext;
    final ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    if (!beanFactory.containsBean("facesContext")) {
        facesContext = mock(FacesContext.class);
        beanFactory.registerSingleton("facesContext", facesContext);
    } else {
        facesContext = (FacesContext) beanFactory.getBean("facesContext");
    }
    final ExternalContext externalContext = mock(ExternalContext.class);
    final UIViewRoot uiViewRoot = mock(UIViewRoot.class);
    when(facesContext.getExternalContext()).thenReturn(externalContext);
    when(externalContext.getRequestParameterMap()).thenReturn(parameterMap);
    when(facesContext.getViewRoot()).thenReturn(uiViewRoot);
    when(uiViewRoot.getViewMap()).thenReturn(viewMap);
    final Field field;
    try {
        field = FacesContext.class.getDeclaredField("instance");
        field.setAccessible(true);
        final ThreadLocal<FacesContext> threadLocal = (ThreadLocal<FacesContext>) field.get(null);
        threadLocal.set(facesContext);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return this;
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) FacesContext(javax.faces.context.FacesContext) Field(java.lang.reflect.Field) ExternalContext(javax.faces.context.ExternalContext) UIViewRoot(javax.faces.component.UIViewRoot) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 32 with ExternalContext

use of javax.faces.context.ExternalContext in project chuidiang-ejemplos by chuidiang.

the class UserLogin method validate.

public String validate() {
    if ("juan".equals(userName) && "pedro".equals(password)) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("userName", userName);
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        try {
            context.redirect(context.getRequestContextPath() + "view.xhtml");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "view";
    } else {
        return "login";
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) ExternalContext(javax.faces.context.ExternalContext) IOException(java.io.IOException)

Example 33 with ExternalContext

use of javax.faces.context.ExternalContext in project opentheso by miledrousset.

the class NewTreeBean method getBrowserName.

/**
 * Pour détecter les agents d'indexation
 *
 * @return
 */
public String getBrowserName() {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String userAgent = externalContext.getRequestHeaderMap().get("User-Agent");
    if (userAgent.toLowerCase().contains("slurp")) {
        return "agent";
    }
    if (userAgent.toLowerCase().contains("msnbot")) {
        return "agent";
    }
    if (userAgent.toLowerCase().contains("googlebot")) {
        return "agent";
    }
    return "notagent";
}
Also used : ExternalContext(javax.faces.context.ExternalContext)

Example 34 with ExternalContext

use of javax.faces.context.ExternalContext in project oxTrust by GluuFederation.

the class GlobalExceptionHandler method handle.

public void handle() throws FacesException {
    final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator();
    while (i.hasNext()) {
        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        final FacesContext fc = FacesContext.getCurrentInstance();
        final ExternalContext externalContext = fc.getExternalContext();
        try {
            if (isSecurityException(t)) {
                performRedirect(externalContext, "/login.htm");
            } else if (isConversationException(t)) {
                log.trace(t.getMessage(), t);
                performRedirect(externalContext, "/conversation_error.htm");
            }
            if (isViewExpiredException(t)) {
                storeRequestURI();
                performRedirect(externalContext, "/login.htm");
            } else {
                log.trace(t.getMessage(), t);
                performRedirect(externalContext, "/error.htm");
            }
            fc.renderResponse();
        } finally {
            i.remove();
        }
    }
    getWrapped().handle();
}
Also used : ExceptionQueuedEvent(javax.faces.event.ExceptionQueuedEvent) FacesContext(javax.faces.context.FacesContext) ExceptionQueuedEventContext(javax.faces.event.ExceptionQueuedEventContext) ExternalContext(javax.faces.context.ExternalContext)

Example 35 with ExternalContext

use of javax.faces.context.ExternalContext in project oxTrust by GluuFederation.

the class GlobalExceptionHandler method storeRequestURI.

protected void storeRequestURI() {
    ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
    String requestUri = ((javax.servlet.http.HttpServletRequest) extContext.getRequest()).getRequestURI();
    Identity identity = CdiUtil.bean(Identity.class);
    identity.setSavedRequestUri(requestUri);
}
Also used : ExternalContext(javax.faces.context.ExternalContext) Identity(org.gluu.oxtrust.security.Identity)

Aggregations

ExternalContext (javax.faces.context.ExternalContext)58 FacesContext (javax.faces.context.FacesContext)28 Test (org.junit.Test)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HashMap (java.util.HashMap)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 Map (java.util.Map)7 IOException (java.io.IOException)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 Locale (java.util.Locale)4 FacesException (javax.faces.FacesException)4 Flash (javax.faces.context.Flash)4 InvocationContext (javax.interceptor.InvocationContext)4 ExceptionQueuedEvent (javax.faces.event.ExceptionQueuedEvent)3 ExceptionQueuedEventContext (javax.faces.event.ExceptionQueuedEventContext)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2