|
1 | 1 | == Rewrite Annotations |
2 | 2 |
|
3 | | -Docs coming soon. |
| 3 | +The Rewrite annotation support offers an alternative way to configure Rewrite. Compared to configuring |
| 4 | +Rewrite using a +ConfigurationProvider+, the annotations allow to place the configuration very close |
| 5 | +to the affected code, which some people prefer over a central configuration. |
| 6 | + |
| 7 | +Please note that the Rewrite annotations are not as flexible as a +ConfigurationProvider+. |
| 8 | +The main focus of the annotations is to simplify the most common usecases which usually occur |
| 9 | +many times in an application. A good example for this are +Join+ rules which are used very often. |
| 10 | + |
| 11 | +NOTE: Of cause you can use a +ConfigurationProvider+ and the annotations side by side. |
| 12 | + |
| 13 | +=== Configuration |
| 14 | + |
| 15 | +The annotation support of Rewrite is enabled by default. So you usually don't have do add any |
| 16 | +additional configuration to use annotations. |
| 17 | + |
| 18 | +If you care about the performance of the annotation scanning process, you can tell Rewrite which |
| 19 | +packages to scan for annotations. Restricting the packages may increase performance if you have |
| 20 | +a large number of classes in your application. |
| 21 | + |
| 22 | +To tell Rewrite to scan only a specific package including all subpackes, add the following entry |
| 23 | +to your +web.xml+: |
| 24 | + |
| 25 | +[source,xml] |
| 26 | +.Restrict annotation scanning to a specific package |
| 27 | +---- |
| 28 | +<context-param> |
| 29 | + <param-name>org.ocpsoft.rewrite.annotation.BASE_PACKAGES</param-name> |
| 30 | + <param-value>com.example.myapp</param-value> |
| 31 | +</context-param> |
| 32 | +---- |
| 33 | + |
| 34 | +If you want to disable the annotation scanning completely, set the package to scan to +none+ like this: |
| 35 | + |
| 36 | +[source,xml] |
| 37 | +.Disable annotation scanning |
| 38 | +---- |
| 39 | +<context-param> |
| 40 | + <param-name>org.ocpsoft.rewrite.annotation.BASE_PACKAGES</param-name> |
| 41 | + <param-value>none</param-value> |
| 42 | +</context-param> |
| 43 | +---- |
| 44 | + |
| 45 | +By default Rewrite will only scan the contents of your +/WEB-INF/classes+ directory. |
| 46 | +Sometimes it makes sense to also scan the JAR files located in |
| 47 | ++/WEB-INF/lib+. This may be the case if your are developing a highly modular application |
| 48 | +which is split into different JAR. |
| 49 | + |
| 50 | +You can tell Rewrite to also scan the JAR files in +/WEB-INF/lib+ by adding the |
| 51 | +following configuration to your +web.xml+: |
| 52 | + |
| 53 | +[source,xml] |
| 54 | +.Tell Rewrite to scan JAR files for annotations |
| 55 | +---- |
| 56 | +<context-param> |
| 57 | + <param-name>org.ocpsoft.rewrite.annotation.SCAN_LIB_DIRECTORY</param-name> |
| 58 | + <param-value>true</param-value> |
| 59 | +</context-param> |
| 60 | +---- |
| 61 | + |
| 62 | +=== Defining rules with annotations |
| 63 | + |
| 64 | +One of the most common Rewrite rule types is the +Join+ which maps a _virtual_ path to a physical |
| 65 | +server resource. If you are using the +ConfigurationProvider+ way of configuration, adding a join |
| 66 | +usually looks like this: |
| 67 | + |
| 68 | +[source,java] |
| 69 | +.A basic join using the fluent Java API |
| 70 | +---- |
| 71 | +.addRule(Join.path("/welcome").to("/faces/welcome.xhtml")) |
| 72 | +---- |
| 73 | + |
| 74 | +If you want to configure this rule using the Rewrite annotations API, you can do it like this: |
| 75 | + |
| 76 | +[source,java] |
| 77 | +.A basic join using Rewrite annotations |
| 78 | +---- |
| 79 | +@Named |
| 80 | +@RequestScoped |
| 81 | +@Join(path="/welcome", to="/faces/welcome.xhtml") |
| 82 | +public class WelcomePage { |
| 83 | + // your code |
| 84 | +} |
| 85 | +---- |
| 86 | + |
| 87 | +As you can see in this example, using the Rewrite annotations is straight forward. Just add |
| 88 | +a +@Join+ annotation to your class and set the +path+ and +to+ attributes just like you would |
| 89 | +using the fluent Java API. |
| 90 | + |
| 91 | +TIP: When using web frameworks like JSF, it is recommended to place the annotation on the class |
| 92 | +which is mainly responsible for the corresponding page. |
| 93 | + |
| 94 | +In some cases it is desired to add an additional +Condition+ to a rule. A good example for this |
| 95 | +are additional security checks for an URL. To add such a constraint, just add the corresponding annotation to |
| 96 | +your class like shown in the following example. |
| 97 | + |
| 98 | +[source,java] |
| 99 | +.Adding a security constraint to a Join |
| 100 | +---- |
| 101 | +@Named |
| 102 | +@RequestScoped |
| 103 | +@RolesRequired("ADMIN") |
| 104 | +@Join(path="/users", to="/faces/user-list.xhtml") |
| 105 | +public class UserListPage { |
| 106 | + // your code |
| 107 | +} |
| 108 | +---- |
| 109 | + |
| 110 | +In this example the +Join+ rules is only applied if the user as the required JAAS role. |
| 111 | + |
| 112 | +TIP: If Rewrite doesn't provide an existing annotation for your specific usecase, you can create your |
| 113 | +own one very easily. Have a look at <<custom-annotations,Building custom annotations>> to learn how |
| 114 | +to do this. |
| 115 | + |
| 116 | + |
| 117 | +=== Handling parameters |
| 118 | + |
| 119 | +TODO |
| 120 | + |
| 121 | +=== Invoking methods |
| 122 | + |
| 123 | +TODO |
| 124 | + |
| 125 | +[[custom-annotations]] |
| 126 | +=== Building custom annotations |
| 127 | + |
| 128 | +TODO |
0 commit comments