All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xlcloud.console.controllers.navigation.NavigationBean Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed 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.xlcloud.console.controllers.navigation;

import java.io.UnsupportedEncodingException;

import javax.annotation.PostConstruct;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base32;
import org.xlcloud.config.ConfigParam;
import org.xlcloud.console.context.request.QueryHttpServletRequest;
import org.xlcloud.console.extensions.scope.ViewScoped;

/**
 * Util Bean that is able to return current path and recreate previous path from query param
 * @author Konrad Król, AMG.net
 * @author Andrzej Stasiak, AMG.net
 */
@Named
@ViewScoped
public class NavigationBean {

    private static final String BACK_NAVIGATION_PARAMETER = "back";
    
    private String currentPath;
    private String backNavigationPath;
    
    @Inject
    @ConfigParam
    private String webConsoleId;
    
    @PostConstruct
    public void init() throws UnsupportedEncodingException {
        backNavigationPath = getExternalContext().getRequestParameterMap().get(BACK_NAVIGATION_PARAMETER);
        resolveCurrentPath();
    }
    
    /**
     * This method returns current path extracted from {@link HttpServletRequest}
     * 
     * Notice that most ajax requests preserve the servlet path, but lost query parameters.
     * Because of that, this method should be called in layout.xhtml as a preRenver event.
     * This ensures that the current path is resolved every time when the new page is rendered and the path is cached until further navigation happens. 
     * 
     * @return current path extracted from {@link HttpServletRequest}
     */
    public String getCurrentPath() {
        return currentPath;
    }
    
    /**
     * Returns back navigation path extracted from {@link #BACK_NAVIGATION_PARAMETER}
     * @return back navigation path
     * @throws UnsupportedEncodingException 
     */
    public String getBackNavigationPath() throws UnsupportedEncodingException {
        if (backNavigationPath == null) {
            return null;
        }
        String returnPath = new String(new Base32().decode(backNavigationPath), "UTF-8");
        returnPath += (returnPath.contains("?") ? "&" : "?");
        returnPath += "faces-redirect=true";
        return returnPath;
    }

    /**
     * @return {@link #BACK_NAVIGATION_PARAMETER} name
     */
    public String getBackNavigationParameter() {
        return BACK_NAVIGATION_PARAMETER;
    }

    private void resolveCurrentPath() throws UnsupportedEncodingException {
        if(getExternalContext().getRequest() instanceof HttpServletRequest) {
            currentPath = new QueryHttpServletRequest((HttpServletRequest) getExternalContext().getRequest()).getFullRequestURI();
            currentPath = currentPath.replaceFirst("/" + webConsoleId, "");
            currentPath = new Base32().encodeToString(currentPath.getBytes("UTF-8"));
            currentPath = currentPath.replaceAll("=", "");
        } else {
            currentPath = "";
        }
    }
    
    private ExternalContext getExternalContext() {
        return FacesContext.getCurrentInstance().getExternalContext();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy