Search in sources :

Example 1 with ExternalContext

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

the class ServerUtil method getRequestOrNull.

/**
     * Safe retrieves http request from FacesContext
     *
     * @return http
     */
public static HttpServletRequest getRequestOrNull() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (facesContext == null)
        return null;
    ExternalContext externalContext = facesContext.getExternalContext();
    if (externalContext == null)
        return null;
    Object request = externalContext.getRequest();
    if (request == null || !(request instanceof HttpServletRequest))
        return null;
    return (HttpServletRequest) request;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext)

Example 2 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class ExceptionHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    FacesContextMocker.mockFacesContext();
    ExternalContext externalContext = mock(ExternalContext.class);
    when(FacesContext.getCurrentInstance().getExternalContext()).thenReturn(externalContext);
    servletContext = mock(ServletContext.class);
    when(FacesContext.getCurrentInstance().getExternalContext().getContext()).thenReturn(servletContext);
    InputStream input = new FileInputStream(ERROR_MESSAGES_PROPERTIES_PATH);
    when(servletContext.getResourceAsStream(anyString())).thenReturn(input);
    UIViewRoot viewRoot = mock(UIViewRoot.class);
    when(FacesContext.getCurrentInstance().getViewRoot()).thenReturn(viewRoot);
    when(viewRoot.getChildren()).thenReturn(new ArrayList<>());
    wrappedExceptionHandler = mock(javax.faces.context.ExceptionHandler.class);
}
Also used : ExternalContext(javax.faces.context.ExternalContext) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ServletContext(javax.servlet.ServletContext) UIViewRoot(javax.faces.component.UIViewRoot) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 3 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class EffortsBeanTest method testGetGrouping_GroupingIsNullCookieIsNull.

@Test
public void testGetGrouping_GroupingIsNullCookieIsNull() throws NoSuchFieldException {
    EffortsGrouping defaultGrouping = BY_PROJECTS;
    FacesContext facesContext = createMock(FacesContext.class);
    ExternalContext externalContext = createMock(ExternalContext.class);
    HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class);
    HttpServletResponse httpServletResponse = createMock(HttpServletResponse.class);
    Cookie[] cookies = new Cookie[0];
    setField(effortsBean, "facesContext", facesContext);
    expect(facesContext.getExternalContext()).andReturn(externalContext).anyTimes();
    expect(externalContext.getRequest()).andReturn(httpServletRequest);
    expect(externalContext.getResponse()).andReturn(httpServletResponse);
    expect(httpServletRequest.getCookies()).andReturn(cookies).anyTimes();
    replay(facesContext, externalContext, httpServletRequest);
    EffortsGrouping expected = defaultGrouping;
    EffortsGrouping actual = effortsBean.getGrouping();
    assertEquals(expected, actual);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class EffortsBeanTest method testGetGrouping_GroupingIsNullCookieIsNotNull.

@Test
public void testGetGrouping_GroupingIsNullCookieIsNotNull() throws NoSuchFieldException {
    FacesContext facesContext = createMock(FacesContext.class);
    ExternalContext externalContext = createMock(ExternalContext.class);
    HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class);
    HttpServletResponse httpServletResponse = createMock(HttpServletResponse.class);
    Cookie[] cookies = new Cookie[] { new Cookie("efforts_grouping", BY_PROJECTS.toString()) };
    setField(effortsBean, "facesContext", facesContext);
    expect(facesContext.getExternalContext()).andReturn(externalContext).anyTimes();
    expect(externalContext.getRequest()).andReturn(httpServletRequest);
    expect(externalContext.getResponse()).andReturn(httpServletResponse);
    expect(httpServletRequest.getCookies()).andReturn(cookies);
    replay(facesContext, externalContext, httpServletRequest);
    EffortsGrouping expected = BY_PROJECTS;
    EffortsGrouping actual = effortsBean.getGrouping();
    assertEquals(expected, actual);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ExternalContext

use of javax.faces.context.ExternalContext in project ART-TIME by Artezio.

the class FacesMessageInterceptor method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(InvocationContext ic) throws Exception {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Object result = ic.proceed();
    if (facesContext == null)
        return result;
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.getFlash().setKeepMessages(true);
    FacesMessage annotation = ic.getMethod().getAnnotation(FacesMessage.class);
    String onCompleteMessageKey = annotation.onCompleteMessageKey();
    Locale locale = externalContext.getRequestLocale();
    String messageBundleName = facesContext.getApplication().getMessageBundle();
    ResourceBundle messageBundle = ResourceBundle.getBundle(messageBundleName, locale);
    facesContext.addMessage(null, new javax.faces.application.FacesMessage(messageBundle.getString(onCompleteMessageKey)));
    return result;
}
Also used : Locale(java.util.Locale) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) ResourceBundle(java.util.ResourceBundle) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

ExternalContext (javax.faces.context.ExternalContext)56 FacesContext (javax.faces.context.FacesContext)26 Test (org.junit.Test)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HashMap (java.util.HashMap)11 Map (java.util.Map)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 IOException (java.io.IOException)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Method (java.lang.reflect.Method)4 FacesException (javax.faces.FacesException)4 Flash (javax.faces.context.Flash)4 InvocationContext (javax.interceptor.InvocationContext)4 File (java.io.File)3 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 Locale (java.util.Locale)2