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