jtulach@0
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@0
|
3 |
*
|
jaroslav@365
|
4 |
* Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
|
jtulach@0
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jtulach@0
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@358
|
30 |
* Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jtulach@0
|
42 |
*/
|
jtulach@0
|
43 |
package net.java.html.json;
|
jtulach@0
|
44 |
|
jtulach@0
|
45 |
import java.lang.annotation.ElementType;
|
jtulach@0
|
46 |
import java.lang.annotation.Retention;
|
jtulach@0
|
47 |
import java.lang.annotation.RetentionPolicy;
|
jtulach@0
|
48 |
import java.lang.annotation.Target;
|
jtulach@0
|
49 |
|
jaroslav@32
|
50 |
/** Static methods in classes annotated by {@link Model}
|
jtulach@0
|
51 |
* can be marked by this annotation to establish a
|
jtulach@0
|
52 |
* <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>
|
jtulach@0
|
53 |
* communication point.
|
jaroslav@32
|
54 |
* The associated model class then gets new method to invoke a network
|
jtulach@0
|
55 |
* connection. Example follows:
|
jtulach@0
|
56 |
*
|
jtulach@0
|
57 |
* <pre>
|
jaroslav@32
|
58 |
* {@link Model @Model}(className="MyModel", properties={
|
jtulach@0
|
59 |
* {@link Property @Property}(name = "people", type=Person.class, array=true)
|
jtulach@0
|
60 |
* })
|
jtulach@0
|
61 |
* class MyModelImpl {
|
jtulach@0
|
62 |
* {@link Model @Model}(className="Person", properties={
|
jtulach@0
|
63 |
* {@link Property @Property}(name = "firstName", type=String.class),
|
jtulach@0
|
64 |
* {@link Property @Property}(name = "lastName", type=String.class)
|
jtulach@0
|
65 |
* })
|
jtulach@0
|
66 |
* static class PersonImpl {
|
jtulach@0
|
67 |
* {@link ComputedProperty @ComputedProperty}
|
jtulach@0
|
68 |
* static String fullName(String firstName, String lastName) {
|
jtulach@0
|
69 |
* return firstName + " " + lastName;
|
jtulach@0
|
70 |
* }
|
jtulach@0
|
71 |
* }
|
jtulach@0
|
72 |
*
|
jtulach@0
|
73 |
* {@link OnReceive @OnReceive}(url = "{protocol}://your.server.com/person/{name}")
|
jtulach@0
|
74 |
* static void getANewPerson(MyModel m, Person p) {
|
jaroslav@32
|
75 |
* alert("Adding " + p.getFullName() + '!');
|
jtulach@0
|
76 |
* m.getPeople().add(p);
|
jtulach@0
|
77 |
* }
|
jtulach@0
|
78 |
*
|
jtulach@0
|
79 |
* // the above will generate method <code>getANewPerson</code> in class <code>MyModel</code>.
|
jtulach@0
|
80 |
* // with <code>protocol</code> and <code>name</code> arguments
|
jtulach@0
|
81 |
* // which asynchronously contacts the server and in case of success calls
|
jtulach@0
|
82 |
* // your {@link OnReceive @OnReceive} with parsed in data
|
jtulach@0
|
83 |
*
|
jaroslav@32
|
84 |
* {@link Function @Function}
|
jtulach@0
|
85 |
* static void requestSmith(MyModel m) {
|
jtulach@0
|
86 |
* m.getANewPerson("http", "Smith");
|
jtulach@0
|
87 |
* }
|
jtulach@0
|
88 |
* }
|
jtulach@0
|
89 |
* </pre>
|
jtulach@0
|
90 |
* When the server returns <code>{ "firstName" : "John", "lastName" : "Smith" }</code>
|
jtulach@0
|
91 |
* the browser will show alert message <em>Adding John Smith!</em>.
|
jaroslav@291
|
92 |
* <p>
|
jaroslav@291
|
93 |
* One can use this method to communicate with the server
|
jaroslav@291
|
94 |
* via <a href="doc-files/websockets.html">WebSocket</a> protocol since version 0.6.
|
jaroslav@291
|
95 |
* Read the <a href="doc-files/websockets.html">tutorial</a> to see how.
|
jtulach@0
|
96 |
*
|
jtulach@0
|
97 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@292
|
98 |
* @since 0.3
|
jtulach@0
|
99 |
*/
|
jtulach@0
|
100 |
@Retention(RetentionPolicy.SOURCE)
|
jtulach@0
|
101 |
@Target(ElementType.METHOD)
|
jtulach@0
|
102 |
public @interface OnReceive {
|
jtulach@0
|
103 |
/** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
|
jtulach@0
|
104 |
* Those parameters will then become variables of the associated method.
|
jtulach@0
|
105 |
*
|
jtulach@0
|
106 |
* @return the (possibly parametrized) url to connect to
|
jtulach@0
|
107 |
*/
|
jtulach@0
|
108 |
String url();
|
jtulach@0
|
109 |
|
jtulach@0
|
110 |
/** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
|
jtulach@0
|
111 |
* a callback from the server generated page to a function defined in the
|
jtulach@0
|
112 |
* system. The name of such function is usually specified as a property
|
jtulach@0
|
113 |
* (of possibly different names). By defining the <code>jsonp</code> attribute
|
jtulach@0
|
114 |
* one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
|
jtulach@0
|
115 |
* transmission and specifies the name of the property. The property should
|
jtulach@0
|
116 |
* also be used in the {@link #url()} attribute on appropriate place.
|
jtulach@0
|
117 |
*
|
jtulach@0
|
118 |
* @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
|
jtulach@0
|
119 |
* callback function.
|
jtulach@0
|
120 |
*/
|
jtulach@0
|
121 |
String jsonp() default "";
|
jaroslav@70
|
122 |
|
jaroslav@70
|
123 |
/** The model class to be send to the server as JSON data.
|
jaroslav@70
|
124 |
* By default no data are sent. However certain {@link #method() transport methods}
|
jaroslav@70
|
125 |
* (like <code>"PUT"</code> and <code>"POST"</code>) require the
|
jaroslav@70
|
126 |
* data to be specified.
|
jaroslav@70
|
127 |
*
|
jaroslav@70
|
128 |
* @return name of a class generated using {@link Model @Model} annotation
|
jaroslav@70
|
129 |
* @since 0.3
|
jaroslav@70
|
130 |
*/
|
jaroslav@70
|
131 |
Class<?> data() default Object.class;
|
jaroslav@70
|
132 |
|
jaroslav@70
|
133 |
/** The HTTP transfer method to use. Defaults to <code>"GET"</code>.
|
jaroslav@70
|
134 |
* Other typical methods include <code>"HEAD"</code>,
|
jaroslav@70
|
135 |
* <code>"DELETE"</code>, <code>"POST"</code>, <code>"PUT"</code>.
|
jaroslav@70
|
136 |
* The last two mentioned methods require {@link #data()} to be specified.
|
jaroslav@70
|
137 |
* <p>
|
jaroslav@70
|
138 |
* When {@link #jsonp() JSONP} transport is requested, the method
|
jaroslav@70
|
139 |
* has to be <code>"GET"</code>.
|
jaroslav@292
|
140 |
* <p>
|
jaroslav@292
|
141 |
* Since version 0.5 one can specify "<a href="doc-files/websockets.html">WebSocket</a>"
|
jaroslav@292
|
142 |
* as the communication method.
|
jaroslav@70
|
143 |
*
|
jaroslav@70
|
144 |
* @return name of the HTTP transfer method
|
jaroslav@70
|
145 |
* @since 0.3
|
jaroslav@70
|
146 |
*/
|
jaroslav@70
|
147 |
String method() default "GET";
|
jaroslav@245
|
148 |
|
jaroslav@245
|
149 |
/** Name of a method in this class which should be called in case of
|
jaroslav@245
|
150 |
* an error. The method has to be non-private and take one model and
|
jaroslav@245
|
151 |
* one {@link Exception}
|
jaroslav@245
|
152 |
* parameter. If this method is not specified, the exception is just
|
jaroslav@245
|
153 |
* printed to console.
|
jaroslav@245
|
154 |
*
|
jaroslav@245
|
155 |
* @return name of method in this class
|
jaroslav@245
|
156 |
* @since 0.5
|
jaroslav@245
|
157 |
*/
|
jaroslav@245
|
158 |
public String onError() default "";
|
jtulach@0
|
159 |
}
|