Java tutorial
/* * Copyright 2014 Dmytro Titov * * 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 io.github.autsia.crowly.controllers.aspects; import io.github.autsia.crowly.services.sentiment.Sentiment; import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.social.ApiBinding; import org.springframework.stereotype.Component; import java.util.Map; import java.util.regex.Pattern; import static io.github.autsia.crowly.controllers.rest.CampaignsController.SEPARATOR; /** * Created by Dmytro on 5/9/2014 at 17:03 * Project: crowly */ @Aspect @Component("createCampaignAspect") public class CampaignAspect { public static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; protected Pattern pattern = Pattern.compile(EMAIL_PATTERN); private static final Logger logger = Logger.getLogger(CampaignAspect.class); protected Map<String, ApiBinding> socialEndpoints; @Around("execution(* io.github.autsia.crowly.controllers.rest.CampaignsController.create(..))") public Object checkCreateCampaign(ProceedingJoinPoint joinPoint) throws Throwable { try { Object[] args = joinPoint.getArgs(); String name = (String) args[0]; String socialEndpoints = (String) args[1]; String language = (String) args[2]; String keywords = (String) args[3]; String regionNames = (String) args[4]; String startDate = (String) args[5]; String endDate = (String) args[6]; String cronExpression = (String) args[7]; String sentiment = (String) args[8]; float threshold = (Float) args[9]; String emails = (String) args[10]; boolean isNameNotEmpty = !name.isEmpty(); boolean isSocialEndpointsCorrect = socialEndpoints.split(SEPARATOR).length != 0; boolean isLanguageCorrect = language.split(SEPARATOR).length != 0; boolean isKeywordsCorrect = keywords.split(SEPARATOR).length != 0; boolean isRegionNamesNotEmpty = !regionNames.isEmpty(); boolean isStartDatNotEmpty = !startDate.isEmpty(); boolean isEndDateNotEmpty = !endDate.isEmpty(); boolean isCronExpressionNotEmpty = !cronExpression.isEmpty(); boolean isSentimentCorrect = Sentiment.get(sentiment) != null; boolean isThresholdCorrect = threshold >= 0 && threshold <= 100; boolean isEmailsValid = true; if (emails != null) { for (String email : emails.split(SEPARATOR)) { if (!pattern.matcher(email).matches()) { isEmailsValid = false; break; } } } if (isNameNotEmpty && isSocialEndpointsCorrect && isLanguageCorrect && isKeywordsCorrect && isRegionNamesNotEmpty && isStartDatNotEmpty && isEndDateNotEmpty && isCronExpressionNotEmpty && isSentimentCorrect && isThresholdCorrect && isEmailsValid) { return joinPoint.proceed(joinPoint.getArgs()); } else { throw new IllegalArgumentException(); } } catch (Exception e) { logger.error(e.getMessage(), e); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @Autowired public void setSocialEndpoints(Map<String, ApiBinding> socialEndpoints) { this.socialEndpoints = socialEndpoints; } }