#257039: @Model.instance() to allow storage of private (and non-JSON like) state in a model
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.json;
45 import java.lang.annotation.ElementType;
46 import java.lang.annotation.Retention;
47 import java.lang.annotation.RetentionPolicy;
48 import java.lang.annotation.Target;
50 /** Static methods in classes annotated by {@link Model}
51 * can be marked by this annotation to establish a
52 * <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>
53 * communication point. The first argument should be the
54 * associated {@link Model} class. The second argument can
55 * be another class generated by {@link Model} annotation,
56 * or array of such classes or (since 0.8.1 version) a
57 * {@link java.util.List} of such classes.
58 * The associated model class then gets new method to invoke a network
59 * connection asynchronously. Example follows:
62 * {@link Model @Model}(className="MyModel", properties={
63 * {@link Property @Property}(name = "people", type=Person.class, array=true)
66 * {@link Model @Model}(className="Person", properties={
67 * {@link Property @Property}(name = "firstName", type=String.class),
68 * {@link Property @Property}(name = "lastName", type=String.class)
70 * static class PersonImpl {
71 * {@link ComputedProperty @ComputedProperty}
72 * static String fullName(String firstName, String lastName) {
73 * return firstName + " " + lastName;
77 * {@link OnReceive @OnReceive}(url = "{protocol}://your.server.com/person/{name}")
78 * static void getANewPerson(MyModel m, Person p) {
79 * System.out.println("Adding " + p.getFullName() + '!');
80 * m.getPeople().add(p);
83 * // the above will generate method <code>getANewPerson</code> in class <code>MyModel</code>.
84 * // with <code>protocol</code> and <code>name</code> arguments
85 * // which asynchronously contacts the server and in case of success calls
86 * // your {@link OnReceive @OnReceive} with parsed in data
88 * {@link Function @Function}
89 * static void requestSmith(MyModel m) {
90 * m.getANewPerson("http", "Smith");
94 * When the server returns <code>{ "firstName" : "John", "lastName" : "Smith" }</code>
95 * the system will print a message <em>Adding John Smith!</em>. It is not
96 * necessary to fully describe the server message - enumerate only the fields
97 * in the response you are interested in. The others will be discarded. So,
98 * if the server <code>{ "firstName" : "John", "lastName" : "Smith", "age" : 33 }</code>
99 * the above code will behave the same (e.g. ignore the <code>age</code>
102 * One can use this method to communicate with the server
103 * via <a href="doc-files/websockets.html">WebSocket</a> protocol since version 0.6.
104 * Read the <a href="doc-files/websockets.html">tutorial</a> to see how.
105 * The method shall be non-private
106 * and unless {@link Model#instance() instance mode} is on also static.
108 * Visit an <a target="_blank" href="http://dew.apidesign.org/dew/#7138581">on-line demo</a>
109 * to see REST access via {@link OnReceive} annotation.
111 * @author Jaroslav Tulach
114 @Retention(RetentionPolicy.SOURCE)
115 @Target(ElementType.METHOD)
116 public @interface OnReceive {
117 /** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
118 * Those names will then become parameters of the associated method.
120 * @return the (possibly parametrized) url to connect to
124 /** Specifies HTTP request headers. Array of header lines
125 * can contain variable names surrounded by '{' and '}'.
126 * Those names will then become parameters of the associated method
127 * (in addition to those added by {@link #url()} specification)
128 * and can only be used with plain JSON(P) requests.
129 * Headers are currently <b>not</b> supported by the
130 * <a href="doc-files/websockets.html">WebSockets protocol</a>.
131 * A sample follows. If you want to transmit <b>X-Birthday</b> header,
132 * you can do it like this:
134 * {@code @}{@link OnReceive}(url="http://your.server.org", headers = {
135 * "X-Birthday: {dayOfBirth}"
137 * <b>static void</b> knowingTheBirth({@link Model YourModel} model) {
138 * // handle the reply
140 * a method <b>knowingTheBirth</b> is generated in
141 * <code>YourModel</code> class with the <code>dayOfBirth</code> argument
142 * which can be called like this:
144 * yourModel.knowingTheBirth("10. 12. 1973");
147 * @return array of header lines - each line should be plain text with
148 * a header name, followed by ":" and value usually specified as
149 * '{' and '}' surrounded variable. The line shouldn't contain
150 * newline or other control characters
153 String[] headers() default {};
155 /** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
156 * a callback from the server generated page to a function defined in the
157 * system. The name of such function is usually specified as a property
158 * (of possibly different names). By defining the <code>jsonp</code> attribute
159 * one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
160 * transmission and specifies the name of the property. The property should
161 * also be used in the {@link #url()} attribute on appropriate place.
163 * @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
166 String jsonp() default "";
168 /** The model class to be send to the server as JSON data.
169 * By default no data are sent. However certain {@link #method() transport methods}
170 * (like <code>"PUT"</code> and <code>"POST"</code>) require the
171 * data to be specified.
173 * @return name of a class generated using {@link Model @Model} annotation
176 Class<?> data() default Object.class;
178 /** The HTTP transfer method to use. Defaults to <code>"GET"</code>.
179 * Other typical methods include <code>"HEAD"</code>,
180 * <code>"DELETE"</code>, <code>"POST"</code>, <code>"PUT"</code>.
181 * The last two mentioned methods require {@link #data()} to be specified.
183 * When {@link #jsonp() JSONP} transport is requested, the method
184 * has to be <code>"GET"</code>.
186 * Since version 0.5 one can specify "<a href="doc-files/websockets.html">WebSocket</a>"
187 * as the communication method.
189 * @return name of the HTTP transfer method
192 String method() default "GET";
194 /** Name of a method in this class which should be called in case of
195 * an error. The method has to be non-private and take one model and
196 * one {@link Exception}
197 * parameter. If this method is not specified, the exception is just
198 * printed to console.
200 * @return name of method in this class
203 public String onError() default "";