Package javax.faces.context

Examples of javax.faces.context.ExternalContext


    @Override
    public boolean isKeepMessages()
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, Object> requestMap = externalContext.getRequestMap();
        Boolean keepMessages = (Boolean) requestMap.get(FLASH_KEEP_MESSAGES);
        if (keepMessages == null)
        {
            HttpServletResponse httpResponse = ExternalContextUtils.getHttpServletResponse(externalContext);
            if (httpResponse != null)
            {
                // Request values are lost after a redirect. We can create a
                // temporal cookie to pass the params between redirect calls.
                // It is better than use HttpSession object, because this cookie
                // is never sent by the server.
                Cookie cookie = (Cookie) externalContext.getRequestCookieMap()
                        .get(FLASH_KEEP_MESSAGES);
                if (cookie != null)
                {
                    keepMessages = Boolean.TRUE;
                    // It is safe to remove the cookie, setting
                    // the maxAge to 0 seconds. The effect is we passed FLASH_KEEP_MESSAGES param
                    // to this request object
                    cookie.setMaxAge(0);
                    cookie.setValue(null);
                    httpResponse.addCookie(cookie);
                    requestMap.put(FLASH_KEEP_MESSAGES, keepMessages);
                }
                else
                {
                    keepMessages = Boolean.FALSE;
                }
            }
            else
            {
                // Note that on portlet world we can't create cookies,
                // so we are forced to use the session map. Anyway,
                // according to the Bridge implementation(for example see
                // org.apache.myfaces.portlet.faces.bridge.BridgeImpl)
                // session object is created at start faces request
                Map<String, Object> sessionMap = externalContext
                        .getSessionMap();
                keepMessages = (Boolean) sessionMap.get(FLASH_KEEP_MESSAGES);
                if (keepMessages != null)
                {
                    sessionMap.remove(FLASH_KEEP_MESSAGES);
View Full Code Here


     */
    @Override
    public void setKeepMessages(boolean keepMessages)
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, Object> requestMap = externalContext.getRequestMap();
       
        Boolean previousKeepMessages = (Boolean) requestMap.get(FLASH_KEEP_MESSAGES);
        previousKeepMessages = (previousKeepMessages == null) ? Boolean.FALSE : previousKeepMessages;
       
        if (!previousKeepMessages.booleanValue() && keepMessages)
        {
            // This request contains a keep messages indication. This condition is in general
            // triggered by a NavigationHandler. After a redirect all request scope
            // values get lost, so in order to preserve this value we need to
            // pass it between request. One strategy is use a cookie that is never sent
            // to the client. Other alternative is use the session map.
            Object response = externalContext.getResponse();
            if (response instanceof HttpServletResponse)
            {
                //If no redirect this cookie will be send to the client,
                //but this behavior is expected.
                HttpServletResponse servletResponse = (HttpServletResponse) response;
                Cookie cookie = new Cookie(FLASH_KEEP_MESSAGES,"true");
                cookie.setMaxAge(-1);
                servletResponse.addCookie(cookie);
            }
            else
            {
                externalContext.getSessionMap().put(FLASH_KEEP_MESSAGES, keepMessages);
            }
        }
        requestMap.put(FLASH_KEEP_MESSAGES, keepMessages);
    }
View Full Code Here

            // Must return null immediately per spec.
           
            return null;
        }
       
        ExternalContext externalContext = context.getExternalContext();
       
        Object serializedView = externalContext.getRequestMap()
            .get(DefaultFaceletsStateManagementHelper.SERIALIZED_VIEW_REQUEST_ATTR);
       
        //Note on ajax case the method saveState could be called twice: once before start
        //document rendering and the other one when it is called StateManager.getViewState method.
        if (serializedView == null)
        {
                   
            // Make sure the client IDs are unique per the spec.
           
            checkIds (context, view, new HashSet<String>());
           
            // Create save state objects for every component.
           
            //view.visitTree (VisitContext.createVisitContext (context), new SaveStateVisitor (states));
           
            if (view.getAttributes().containsKey(COMPONENT_ADDED_AFTER_BUILD_VIEW))
            {
                ensureClearInitialState(view);
                states = new Object[]{
                            internalBuildTreeStructureToSave(view),
                            view.processSaveState(context)};
            }
            else
            {
                states = new HashMap<String, Object>();

                saveStateOnMap(context,(Map<String,Object>) states, view);
               
                if ( ((Map<String,Object>)states).isEmpty())
                {
                    states = null;
                }
            }
           
            // TODO: not sure the best way to handle dynamic adds/removes as mandated by the spec.
           
            // As required by ResponseStateManager, the return value is an Object array.  First
            // element is the structure object, second is the state map.
       
            if (context.getApplication().getStateManager().isSavingStateInClient(context))
            {
                serializedView = new Object[] { null, states };
            }
            else
            {
                // On server side state saving, the structure field is used to save the view sequence.
                // Originally, on JspStateManagerImpl this is done in writeState method, not in saveView,
                // but note that on ajax case the state is both saved and written using StateManager.getViewState,
                // so we must save it early
                serializedView = new Object[] {Integer.toString(helper.getNextViewSequence(context), Character.MAX_RADIX), states};
            }
            externalContext.getRequestMap().put(DefaultFaceletsStateManagementHelper.SERIALIZED_VIEW_REQUEST_ATTR,
                    serializedView);
        }
       
        if (!context.getApplication().getStateManager().isSavingStateInClient(context))
        {
View Full Code Here

        }
    }

    private void initializeMode(FacesContext context)
    {
        ExternalContext external = context.getExternalContext();
        String param = external.getInitParameter(PARAM_DEVELOPMENT);
        this.developmentMode = "true".equals(param);

        String restoreMode = external.getInitParameter(PARAM_BUILD_BEFORE_RESTORE);
        this.buildBeforeRestore = "true".equals(restoreMode);
    }
View Full Code Here

        this.buildBeforeRestore = "true".equals(restoreMode);
    }

    private void initializeBuffer(FacesContext context)
    {
        ExternalContext external = context.getExternalContext();
        String param = external.getInitParameter(PARAM_BUFFER_SIZE);
        this.bufferSize = (param != null && !"".equals(param)) ? Integer.parseInt(param) : -1;
    }
View Full Code Here

    /**
     * Initialize mappings, during the first request.
     */
    private void initializeMappings(FacesContext context)
    {
        ExternalContext external = context.getExternalContext();
        String viewMappings = external.getInitParameter(PARAM_VIEW_MAPPINGS);
        if ((viewMappings != null) && (viewMappings.length() > 0))
        {
            String[] mappingsArray = viewMappings.split(";");

            List<String> extensionsList = new ArrayList<String>(mappingsArray.length);
View Full Code Here

    }

    protected void initializeCompiler(Compiler c)
    {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext ext = ctx.getExternalContext();

        // load libraries
        String libParam = ext.getInitParameter(PARAM_LIBRARIES);
        if (libParam != null)
        {
            libParam = libParam.trim();
            String[] libs = libParam.split(";");
            URL src;
            TagLibrary libObj;
            for (int i = 0; i < libs.length; i++)
            {
                try
                {
                    src = ext.getResource(libs[i].trim());
                    if (src == null)
                    {
                        throw new FileNotFoundException(libs[i]);
                    }
                    libObj = TagLibraryConfig.create(src);
                    c.addTagLibrary(libObj);
                    log.fine("Successfully Loaded Library: " + libs[i]);
                }
                catch (IOException e)
                {
                    log.log(Level.SEVERE, "Error Loading Library: " + libs[i], e);
                }
            }
        }

        // load decorators
        String decParam = ext.getInitParameter(PARAM_DECORATORS);
        if (decParam != null)
        {
            decParam = decParam.trim();
            String[] decs = decParam.split(";");
            TagDecorator decObj;
            for (int i = 0; i < decs.length; i++)
            {
                try
                {
                    decObj = (TagDecorator) ReflectionUtil.forName(decs[i]).newInstance();
                    c.addTagDecorator(decObj);
                    log.fine("Successfully Loaded Decorator: " + decs[i]);
                }
                catch (Exception e)
                {
                    log.log(Level.SEVERE, "Error Loading Decorator: " + decs[i], e);
                }
            }
        }

        // skip params?
        String skipParam = ext.getInitParameter(PARAM_SKIP_COMMENTS);
        if (skipParam != null && "true".equals(skipParam))
        {
            c.setTrimmingComments(true);
        }
    }
View Full Code Here

        return this.parent;
    }

    protected ResponseWriter createResponseWriter(FacesContext context) throws IOException, FacesException
    {
        ExternalContext extContext = context.getExternalContext();
        RenderKit renderKit = context.getRenderKit();
        // Avoid a cryptic NullPointerException when the renderkit ID
        // is incorrectly set
        if (renderKit == null)
        {
            String id = context.getViewRoot().getRenderKitId();
            throw new IllegalStateException("No render kit was available for id \"" + id + "\"");
        }

        ServletResponse response = (ServletResponse) extContext.getResponse();

        // set the buffer for content
        if (this.bufferSize != -1)
        {
            response.setBufferSize(this.bufferSize);
        }

        // get our content type
        String contentType = (String) extContext.getRequestMap().get("facelets.ContentType");

        // get the encoding
        String encoding = (String) extContext.getRequestMap().get("facelets.Encoding");

        ResponseWriter writer;
        // append */* to the contentType so createResponseWriter will succeed no matter
        // the requested contentType.
        if (contentType != null && !contentType.equals("*/*"))
 
View Full Code Here

            }

            // setup writer and assign it to the context
            ResponseWriter origWriter = this.createResponseWriter(context);

            ExternalContext extContext = context.getExternalContext();
            Writer outputWriter = extContext.getResponseOutputWriter();

            // QUESTION: should we use bufferSize? Or, since the
            // StateWriter usually only needs a small bit at the end,
            // should we always use a much smaller size?
            stateWriter = new StateWriter(outputWriter, this.bufferSize != -1 ? this.bufferSize : 1024);

            ResponseWriter writer = origWriter.cloneWithWriter(stateWriter);
            context.setResponseWriter(writer);

            // force creation of session if saving state there
            StateManager stateMgr = context.getApplication().getStateManager();
            if (!stateMgr.isSavingStateInClient(context))
            {
                extContext.getSession(true);
            }

            long time = System.currentTimeMillis();

            // render the view to the response
View Full Code Here

    public String getDefaultSuffix(FacesContext context) throws FacesException
    {
        if (this.defaultSuffix == null)
        {
            ExternalContext extCtx = context.getExternalContext();
            String viewSuffix = extCtx.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME);
            this.defaultSuffix = (viewSuffix != null) ? viewSuffix : ViewHandler.DEFAULT_SUFFIX;
        }
        return this.defaultSuffix;
    }
View Full Code Here

TOP

Related Classes of javax.faces.context.ExternalContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.