json/src/main/java/net/java/html/json/OnReceive.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Mon, 16 Dec 2013 17:39:56 +0100
changeset 365 5c93ad8c7a15
parent 358 80702021b851
child 549 11198031c76a
permissions -rw-r--r--
The work on this project started in 2013
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package net.java.html.json;
    44 
    45 import java.lang.annotation.ElementType;
    46 import java.lang.annotation.Retention;
    47 import java.lang.annotation.RetentionPolicy;
    48 import java.lang.annotation.Target;
    49 
    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.
    54  * The associated model class then gets new method to invoke a network
    55  * connection. Example follows:
    56  * 
    57  * <pre>
    58  * {@link Model @Model}(className="MyModel", properties={
    59  *   {@link Property @Property}(name = "people", type=Person.class, array=true)
    60  * })
    61  * class MyModelImpl {
    62  *   {@link Model @Model}(className="Person", properties={
    63  *     {@link Property @Property}(name = "firstName", type=String.class),
    64  *     {@link Property @Property}(name = "lastName", type=String.class)
    65  *   })
    66  *   static class PersonImpl {
    67  *     {@link ComputedProperty @ComputedProperty}
    68  *     static String fullName(String firstName, String lastName) {
    69  *       return firstName + " " + lastName;
    70  *     }
    71  *   }
    72  * 
    73  *   {@link OnReceive @OnReceive}(url = "{protocol}://your.server.com/person/{name}")
    74  *   static void getANewPerson(MyModel m, Person p) {
    75  *     alert("Adding " + p.getFullName() + '!');
    76  *     m.getPeople().add(p);
    77  *   }
    78  * 
    79  *   // the above will generate method <code>getANewPerson</code> in class <code>MyModel</code>.
    80  *   // with <code>protocol</code> and <code>name</code> arguments
    81  *   // which asynchronously contacts the server and in case of success calls
    82  *   // your {@link OnReceive @OnReceive} with parsed in data
    83  * 
    84  *   {@link Function @Function}
    85  *   static void requestSmith(MyModel m) {
    86  *     m.getANewPerson("http", "Smith");
    87  *   }
    88  * }
    89  * </pre>
    90  * When the server returns <code>{ "firstName" : "John", "lastName" : "Smith" }</code>
    91  * the browser will show alert message <em>Adding John Smith!</em>.
    92  * <p>
    93  * One can use this method to communicate with the server
    94  * via <a href="doc-files/websockets.html">WebSocket</a> protocol since version 0.6.
    95  * Read the <a href="doc-files/websockets.html">tutorial</a> to see how.
    96  * 
    97  * @author Jaroslav Tulach <jtulach@netbeans.org>
    98  * @since 0.3
    99  */
   100 @Retention(RetentionPolicy.SOURCE)
   101 @Target(ElementType.METHOD)
   102 public @interface OnReceive {
   103     /** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
   104      * Those parameters will then become variables of the associated method.
   105      * 
   106      * @return the (possibly parametrized) url to connect to
   107      */
   108     String url();
   109     
   110     /** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
   111      * a callback from the server generated page to a function defined in the
   112      * system. The name of such function is usually specified as a property
   113      * (of possibly different names). By defining the <code>jsonp</code> attribute
   114      * one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> 
   115      * transmission and specifies the name of the property. The property should
   116      * also be used in the {@link #url()} attribute on appropriate place.
   117      * 
   118      * @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
   119      *    callback function.
   120      */
   121     String jsonp() default "";
   122     
   123     /** The model class to be send to the server as JSON data.
   124      * By default no data are sent. However certain {@link #method() transport methods}
   125      * (like <code>"PUT"</code> and <code>"POST"</code>) require the 
   126      * data to be specified.
   127      * 
   128      * @return name of a class generated using {@link Model @Model} annotation
   129      * @since 0.3
   130      */
   131     Class<?> data() default Object.class;
   132     
   133     /** The HTTP transfer method to use. Defaults to <code>"GET"</code>.
   134      * Other typical methods include <code>"HEAD"</code>, 
   135      * <code>"DELETE"</code>, <code>"POST"</code>, <code>"PUT"</code>.
   136      * The last two mentioned methods require {@link #data()} to be specified.
   137      * <p>
   138      * When {@link #jsonp() JSONP} transport is requested, the method 
   139      * has to be <code>"GET"</code>.
   140      * <p>
   141      * Since version 0.5 one can specify "<a href="doc-files/websockets.html">WebSocket</a>"
   142      * as the communication method.
   143      * 
   144      * @return name of the HTTP transfer method
   145      * @since 0.3
   146      */
   147     String method() default "GET";
   148     
   149     /** Name of a method in this class which should be called in case of 
   150      * an error. The method has to be non-private and take one model and 
   151      * one {@link Exception} 
   152      * parameter. If this method is not specified, the exception is just
   153      * printed to console.
   154      * 
   155      * @return name of method in this class
   156      * @since 0.5
   157      */
   158     public String onError() default "";    
   159 }