View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import java.lang.annotation.Documented;
16  import java.lang.annotation.ElementType;
17  import java.lang.annotation.Retention;
18  import java.lang.annotation.RetentionPolicy;
19  import java.lang.annotation.Target;
20  
21  import net.sf.oval.ConstraintTarget;
22  import net.sf.oval.ConstraintViolation;
23  import net.sf.oval.configuration.annotation.Constraint;
24  import net.sf.oval.configuration.annotation.Constraints;
25  import net.sf.oval.constraint.AssertURLCheck.URIScheme;
26  
27  /**
28   * Check if the value is a valid URL.
29   * 
30   * <br><br>
31   * <b>Note:</b> This constraint is also satisfied when the value to validate is null, therefore you might also need to specified @NotNull
32   * 
33   * @author Sebastian Thomschke
34   */
35  @Documented
36  @Retention(RetentionPolicy.RUNTIME)
37  @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
38  @Constraint(checkWith = AssertURLCheck.class)
39  public @interface AssertURL
40  {
41  	@Documented
42  	@Retention(RetentionPolicy.RUNTIME)
43  	@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
44  	@Constraints
45  	public @interface List
46  	{
47  		/**
48  		 * The AssertURL constraints.
49  		 */
50  		AssertURL[] value();
51  
52  		/**
53  		 * Formula returning <code>true</code> if this constraint shall be evaluated and
54  		 * <code>false</code> if it shall be ignored for the current validation.
55  		 * <p>
56  		 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
57  		 * E.g. <code>groovy:_this.amount > 10</code>
58  		 * <p>
59  		 * Available context variables are:<br>
60  		 * <b>_this</b> -&gt; the validated bean<br>
61  		 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
62  		 *    or the validated bean for object level constraints)
63  		 */
64  		String when() default "";
65  	}
66  
67  	/**
68  	 * <p>In case the constraint is declared for an array, collection or map this controls how the constraint is applied to it and it's child objects.
69  	 * 
70  	 * <p><b>Default:</b> ConstraintTarget.VALUES
71  	 * 
72  	 * <p><b>Note:</b> This setting is ignored for object types other than array, map and collection.
73  	 */
74  	ConstraintTarget[] appliesTo() default ConstraintTarget.VALUES;
75  
76  	/**
77  	 * Specifies if a connection to the URL should be attempted to verify its validity. 
78  	 */
79  	boolean connect() default false;
80  
81  	/**
82  	 * error code passed to the ConstraintViolation object
83  	 */
84  	String errorCode() default "net.sf.oval.constraint.AssertURL";
85  
86  	/**
87  	 * message to be used for the ContraintsViolatedException
88  	 * 
89  	 * @see ConstraintViolation
90  	 */
91  	String message() default "net.sf.oval.constraint.AssertURL.violated";
92  
93  	/**
94  	 * Specifies the allowed URL schemes.
95  	 */
96  	URIScheme[] permittedURISchemes() default {URIScheme.HTTP, URIScheme.HTTPS, URIScheme.FTP};
97  
98  	/**
99  	 * The associated constraint profiles.
100 	 */
101 	String[] profiles() default {};
102 
103 	/**
104 	 * severity passed to the ConstraintViolation object
105 	 */
106 	int severity() default 0;
107 
108 	/**
109 	 * An expression to specify where in the object graph relative from this object the expression
110 	 * should be applied.
111 	 * <p>
112 	 * Examples:
113 	 * <li>"owner" would apply this constraint to the current object's property <code>owner</code>
114 	 * <li>"owner.id" would apply this constraint to the current object's <code>owner</code>'s property <code>id</code>
115 	 * <li>"jxpath:owner/id" would use the JXPath implementation to traverse the object graph to locate the object where this constraint should be applied.
116 	 */
117 	String target() default "";
118 
119 	/**
120 	 * Formula returning <code>true</code> if this constraint shall be evaluated and
121 	 * <code>false</code> if it shall be ignored for the current validation.
122 	 * <p>
123 	 * <b>Important:</b> The formula must be prefixed with the name of the scripting language that is used.
124 	 * E.g. <code>groovy:_this.amount > 10</code>
125 	 * <p>
126 	 * Available context variables are:<br>
127 	 * <b>_this</b> -&gt; the validated bean<br>
128 	 * <b>_value</b> -&gt; the value to validate (e.g. the field value, parameter value, method return value,
129 	 *    or the validated bean for object level constraints)
130 	 */
131 	String when() default "";
132 }