it.f2informatica.core.aop.ServicesAspect.java Source code

Java tutorial

Introduction

Here is the source code for it.f2informatica.core.aop.ServicesAspect.java

Source

/*
 * =============================================================================
 *
 *   Copyright (c) 2014, Fernando Aspiazu
 *
 *   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 it.f2informatica.core.aop;

import it.f2informatica.core.exception.PageNotFoundException;
import org.apache.commons.lang3.math.NumberUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ServicesAspect {

    @Pointcut("execution(public * it.f2informatica.core.gateway.mysql.*MySQL.findOne*(..))")
    private void idCorrectness() {
    }

    @Before("idCorrectness()")
    public void handleIdCorrectness(JoinPoint joinPoint) {
        for (Object arg : joinPoint.getArgs()) {
            if (isNotNumber(String.valueOf(arg))) {
                throw new PageNotFoundException("Id is not correct");
            }
        }
    }

    private static boolean isNotNumber(String value) {
        return !NumberUtils.isNumber(value);
    }

}