json/src/main/java/net/java/html/json/OnReceive.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 549 11198031c76a
child 600 f2bfd90c1d90
permissions -rw-r--r--
Updating copyright headers to mention current year
jtulach@0
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@0
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 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@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 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.
jaroslav@549
    96
 * <p>
jaroslav@549
    97
 * Visit an <a target="_blank" href="http://dew.apidesign.org/dew/#7138581">on-line demo</a>
jaroslav@549
    98
 * to see REST access via {@link OnReceive} annotation.
jtulach@0
    99
 * 
jtulach@0
   100
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@292
   101
 * @since 0.3
jtulach@0
   102
 */
jtulach@0
   103
@Retention(RetentionPolicy.SOURCE)
jtulach@0
   104
@Target(ElementType.METHOD)
jtulach@0
   105
public @interface OnReceive {
jtulach@0
   106
    /** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
jtulach@0
   107
     * Those parameters will then become variables of the associated method.
jtulach@0
   108
     * 
jtulach@0
   109
     * @return the (possibly parametrized) url to connect to
jtulach@0
   110
     */
jtulach@0
   111
    String url();
jtulach@0
   112
    
jtulach@0
   113
    /** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
jtulach@0
   114
     * a callback from the server generated page to a function defined in the
jtulach@0
   115
     * system. The name of such function is usually specified as a property
jtulach@0
   116
     * (of possibly different names). By defining the <code>jsonp</code> attribute
jtulach@0
   117
     * one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> 
jtulach@0
   118
     * transmission and specifies the name of the property. The property should
jtulach@0
   119
     * also be used in the {@link #url()} attribute on appropriate place.
jtulach@0
   120
     * 
jtulach@0
   121
     * @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
jtulach@0
   122
     *    callback function.
jtulach@0
   123
     */
jtulach@0
   124
    String jsonp() default "";
jaroslav@70
   125
    
jaroslav@70
   126
    /** The model class to be send to the server as JSON data.
jaroslav@70
   127
     * By default no data are sent. However certain {@link #method() transport methods}
jaroslav@70
   128
     * (like <code>"PUT"</code> and <code>"POST"</code>) require the 
jaroslav@70
   129
     * data to be specified.
jaroslav@70
   130
     * 
jaroslav@70
   131
     * @return name of a class generated using {@link Model @Model} annotation
jaroslav@70
   132
     * @since 0.3
jaroslav@70
   133
     */
jaroslav@70
   134
    Class<?> data() default Object.class;
jaroslav@70
   135
    
jaroslav@70
   136
    /** The HTTP transfer method to use. Defaults to <code>"GET"</code>.
jaroslav@70
   137
     * Other typical methods include <code>"HEAD"</code>, 
jaroslav@70
   138
     * <code>"DELETE"</code>, <code>"POST"</code>, <code>"PUT"</code>.
jaroslav@70
   139
     * The last two mentioned methods require {@link #data()} to be specified.
jaroslav@70
   140
     * <p>
jaroslav@70
   141
     * When {@link #jsonp() JSONP} transport is requested, the method 
jaroslav@70
   142
     * has to be <code>"GET"</code>.
jaroslav@292
   143
     * <p>
jaroslav@292
   144
     * Since version 0.5 one can specify "<a href="doc-files/websockets.html">WebSocket</a>"
jaroslav@292
   145
     * as the communication method.
jaroslav@70
   146
     * 
jaroslav@70
   147
     * @return name of the HTTP transfer method
jaroslav@70
   148
     * @since 0.3
jaroslav@70
   149
     */
jaroslav@70
   150
    String method() default "GET";
jaroslav@245
   151
    
jaroslav@245
   152
    /** Name of a method in this class which should be called in case of 
jaroslav@245
   153
     * an error. The method has to be non-private and take one model and 
jaroslav@245
   154
     * one {@link Exception} 
jaroslav@245
   155
     * parameter. If this method is not specified, the exception is just
jaroslav@245
   156
     * printed to console.
jaroslav@245
   157
     * 
jaroslav@245
   158
     * @return name of method in this class
jaroslav@245
   159
     * @since 0.5
jaroslav@245
   160
     */
jaroslav@245
   161
    public String onError() default "";    
jtulach@0
   162
}