org.cgiar.ccafs.ap.interceptor.ValidateDeliverableParameterInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for org.cgiar.ccafs.ap.interceptor.ValidateDeliverableParameterInterceptor.java

Source

/*****************************************************************
 * This file is part of CCAFS Planning and Reporting Platform.
 * CCAFS P&R is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * at your option) any later version.
 * CCAFS P&R is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with CCAFS P&R. If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************/
package org.cgiar.ccafs.ap.interceptor;

import org.cgiar.ccafs.ap.action.BaseAction;
import org.cgiar.ccafs.ap.config.APConstants;
import org.cgiar.ccafs.ap.data.manager.DeliverableManager;

import java.util.Map;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This interceptor is used to validate if the activity parameter is well passed through the URL.
 *
 * @author Hctor Fabio Tobn R.
 */
public class ValidateDeliverableParameterInterceptor extends AbstractInterceptor {

    private static final long serialVersionUID = 1524902722153661399L;

    // LOG
    private static final Logger LOG = LoggerFactory.getLogger(ValidateDeliverableParameterInterceptor.class);

    // Managers
    private DeliverableManager deliverableManager;

    @Inject
    public ValidateDeliverableParameterInterceptor(DeliverableManager deliverableManager) {
        this.deliverableManager = deliverableManager;
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        LOG.debug("=> ValidateDeliverableParameterInterceptor");
        // String actionName = ServletActionContext.getActionMapping().getName();

        Map<String, Object> parameters = invocation.getInvocationContext().getParameters();
        // Validate if deliverable parameter exists in the URL.
        if (parameters.get(APConstants.DELIVERABLE_REQUEST_ID) != null) {
            String deliverableParameter = ((String[]) parameters.get(APConstants.DELIVERABLE_REQUEST_ID))[0];
            // Validate if the parameter is a number.
            if (StringUtils.isNumeric(deliverableParameter)) {
                int deliverableID = Integer.parseInt(deliverableParameter);
                // If the deliverable doesn't exist.
                if (!deliverableManager.existDeliverable(deliverableID)) {
                    return BaseAction.NOT_FOUND;
                } else {
                    // If deliverable exists, continue!
                    return invocation.invoke();
                }
            } else {
                // If parameter is not a number.
                return BaseAction.NOT_FOUND;
            }
        } else {
            // if parameter does not exist.
            return BaseAction.NOT_FOUND;
        }
    }

}