/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.apache.webbeans.jsf;

import javax.enterprise.context.Conversation;
import javax.faces.application.ViewHandler;
import javax.faces.application.ViewHandlerWrapper;
import javax.faces.context.FacesContext;

import org.apache.webbeans.conversation.ConversationManager;
import org.apache.webbeans.util.JSFUtil;

public class ConversationAwareViewHandler extends ViewHandlerWrapper
{
	/** Conversation manager */
	private static final ConversationManager conversationManager = ConversationManager.getInstance();
	
    private final ViewHandler delegate;

    public ConversationAwareViewHandler(ViewHandler delegate)
    {
        this.delegate = delegate;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getActionURL(FacesContext context, String viewId)
    {
        String url = delegate.getActionURL(context, viewId);

        Conversation conversation = conversationManager.getConversationBeanReference();
        if (conversation != null && !conversation.isTransient())
        {
            url = JSFUtil.getRedirectViewIdWithCid(url, conversation.getId());
        }

        return url;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ViewHandler getWrapped()
    {
        return delegate;
    }
}

@@ -814,16 +814,62 @@
 s.jsf;%0A%0A
+import java.util.List;%0Aimport java.util.Map;%0A%0A
 import j
@@ -1956,32 +1956,949 @@
 urn url;%0A    %7D%0A%0A
+    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public String getRedirectURL(FacesContext context, String viewId,%0A            Map%3CString, List%3CString%3E%3E parameters, boolean includeViewParams)%0A    %7B%0A        String url = delegate.getRedirectURL(context, viewId, parameters, includeViewParams);%0A        int indexOfQuery = url.indexOf('?');%0A        if (indexOfQuery %3E 0) %0A        %7B%0A        %09String queryString = url.substring(indexOfQuery);%0A        %09// If the query string already has a cid parameter, return url directly.%0A        %09if (queryString.contains(%22?cid=%22) %7C%7C queryString.contains(%22&cid=%22))%0A        %09%09return url;%0A        %7D%0A        Conversation conversation = conversationManager.getConversationBeanReference();%0A        if (conversation != null && !conversation.isTransient())%0A        %7B%0A            url = JSFUtil.getRedirectViewIdWithCid(url, conversation.getId());%0A        %7D%0A        return url;%0A    %7D%0A    %0A
     /**%0A     * %7B

add getRedirectURL method to support jsf 2.0.