/************************************************************************* * Copyright 2009-2014 Eucalyptus Systems, Inc. * * This program 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; version 3 of the License. * * This program 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 this program. If not, see http://www.gnu.org/licenses/. * * Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta * CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need * additional information or have any questions. ************************************************************************/ package edu.ucsb.eucalyptus.msgs; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.regex.Pattern; import com.eucalyptus.system.Ats; import com.eucalyptus.util.MessageValidation; import com.eucalyptus.util.Pair; /** * */ public class ComputeMessageValidation { public static class ComputeMessageValidationAssistant implements MessageValidation.ValidationAssistant { @Override public boolean validate( final Object object ) { return object instanceof EucalyptusData; } @Override public Pair<Long, Long> range( final Ats ats ) { final FieldRange range = ats.get( FieldRange.class ); return range == null ? null : Pair.pair( range.min( ), range.max( ) ); } @Override public Pattern regex( final Ats ats ) { final FieldRegex regex = ats.get( FieldRegex.class ); return regex == null ? null : regex.value( ).pattern( ); } } @Target( ElementType.FIELD) @Retention( RetentionPolicy.RUNTIME) public @interface FieldRegex { FieldRegexValue value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FieldRange { long min() default 0; long max() default Long.MAX_VALUE; } public enum FieldRegexValue { // Generic STRING_128( "(?s).{1,128}" ), STRING_255( "(?s).{1,255}" ), CIDR( "(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/(?:[0-9]|[12][0-9]|3[0-2])" ), IP_ADDRESS( "(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" ), // EC2 EC2_PROTOCOL( "icmp|tcp|udp|-1|[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]" ), EC2_ACL_ACTION("allow|deny"), EC2_ACCOUNT_OR_CIDR( "[0-9]{12}|(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/(?:[0-9]|[12][0-9]|3[0-2])" ), ; private final Pattern pattern; private FieldRegexValue( final String regex ) { this.pattern = Pattern.compile( regex ); } public Pattern pattern() { return pattern; } } }