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

org.xlcloud.console.controllers.request.RequestParameters 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.request;

import java.io.Serializable;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.context.RequestContext;
import org.xlcloud.console.controllers.request.exception.RequestParameterFormatException;
import org.xlcloud.console.controllers.request.path.ConsolePathParams;

/**
 * This class is meant to give (simplify) an access to parameters of the current request.
 * @author "Konrad Król", AMG.net
 */
@Named
public class RequestParameters implements Serializable {

    /**
     * Documentation serialVersionUID
     */
    private static final long serialVersionUID = 1L;
    
    /**
     * Returns all request parameters
     * @return map with all request parameters
     */
    public Map getRequestParameters() {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    }

    /**
     * Returns value of the parameter identified by the given name
     * @param parameterName parameter name
     * @return parameter value
     */
    public String getParameter(String parameterName) {
        return getRequestParameters().get(parameterName);
    }
    
    /**
     * Returns value of the parameter identified by the given name
     * @param parameterName parameter name
     * @return parameter value
     */
    public String getParameter(ConsolePathParams parameter) {
        return getParameter(parameter.value());
    }

    /**
     * Returns value of the parameter identified by the given name.
     * The method casts parameter value to Long.
     * If casting fails RequestParameterFormatException is thrown. 
     * 
     * @param parameterName parameter name
     * @throws RequestParameterFormatException
     * @return parameter value
     */
    public Long getParameterAsLong(String parameterName) {
        String stringValue = getRequestParameters().get(parameterName);
        try {
            return Long.valueOf(stringValue);
        } catch (NumberFormatException exception) {
            throw new RequestParameterFormatException("Parameter " + parameterName + " format is not valid. Parameters value: "
                    + stringValue, exception.getCause());
        }
    }
    
    /**
     * Returns value of the parameter identified by the given name.
     * The method casts parameter value to Long.
     * If casting fails RequestParameterFormatException is thrown. 
     * 
     * @param parameterName parameter name
     * @throws RequestParameterFormatException
     * @return parameter value
     */
    public Long getParameterAsLong(ConsolePathParams parameter) {
        return getParameterAsLong(parameter.value());
    }

    /**
     * Returns value of the parameter identified by the given name.
     * The method casts parameter value to Integer.
     * If casting fails RequestParameterFormatException is thrown. 
     * 
     * @param parameterName parameter name
     * @throws RequestParameterFormatException
     * @return parameter value
     */
    public Integer getParameterAsInt(String parameterName) {
        try {
            return Integer.valueOf(getRequestParameters().get(parameterName));
        } catch (NumberFormatException exception) {
            throw new RequestParameterFormatException("Parameter " + parameterName + " format is not valid ("+getRequestParameters().get(parameterName)+")", exception.getCause());
        }
    }
    
    /**
     * Returns value of the parameter identified by the given name.
     * The method casts parameter value to Integer.
     * If casting fails RequestParameterFormatException is thrown. 
     * 
     * @param parameterName parameter name
     * @throws RequestParameterFormatException
     * @return parameter value
     */
    public Integer getParameterAsInt(ConsolePathParams parameter) {
        return getParameterAsInt(parameter.value());
    }

    /**
     * Indicates whether parameters map contains a parameter identified by the specified key.
     * @param parameter parameter name
     * @return {@code true} if parameter map contains a parameter, {@code false} otrherwise 
     */
    public boolean hasParameter(String parameter) {
        return getRequestParameters().containsKey(parameter) && getParameter(parameter)!=null;
    }
    
    /**
     * Indicates whether parameters map contains a parameter identified by the specified key.
     * @param parameter parameter
     * @return {@code true} if parameter map contains a parameter, {@code false} otrherwise 
     */
    public boolean hasParameter(ConsolePathParams parameter) {
        return hasParameter(parameter.value());
    }
    
    /**
     * Adds parameter to ajax callback response.
     * @param parameterName parameter name
     * @param value parameter value
     */
    public void addCallbackParameter(String parameterName, Object value) {
        RequestContext requestContext = RequestContext.getCurrentInstance();
        requestContext.addCallbackParam(parameterName, value);
    }
    
    /**
     * Adds parameter to ajax callback response.
     * @param parameterName parameter name
     * @param value parameter value
     */
    public void addCallbackParameter(ConsolePathParams parameter, Object value) {
        addCallbackParameter(parameter.value(), value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy