json/src/main/java/net/java/html/json/OnReceive.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 647 989ce2017405
child 940 bdec4103bdb2
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
     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. 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:
    60  * 
    61  * <pre>
    62  * {@link Model @Model}(className="MyModel", properties={
    63  *   {@link Property @Property}(name = "people", type=Person.class, array=true)
    64  * })
    65  * class MyModelImpl {
    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)
    69  *   })
    70  *   static class PersonImpl {
    71  *     {@link ComputedProperty @ComputedProperty}
    72  *     static String fullName(String firstName, String lastName) {
    73  *       return firstName + " " + lastName;
    74  *     }
    75  *   }
    76  * 
    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);
    81  *   }
    82  * 
    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
    87  * 
    88  *   {@link Function @Function}
    89  *   static void requestSmith(MyModel m) {
    90  *     m.getANewPerson("http", "Smith");
    91  *   }
    92  * }
    93  * </pre>
    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>
   100  * value).
   101  * <p>
   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  * <p>
   106  * Visit an <a target="_blank" href="http://dew.apidesign.org/dew/#7138581">on-line demo</a>
   107  * to see REST access via {@link OnReceive} annotation.
   108  * 
   109  * @author Jaroslav Tulach
   110  * @since 0.3
   111  */
   112 @Retention(RetentionPolicy.SOURCE)
   113 @Target(ElementType.METHOD)
   114 public @interface OnReceive {
   115     /** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
   116      * Those parameters will then become variables of the associated method.
   117      * 
   118      * @return the (possibly parametrized) url to connect to
   119      */
   120     String url();
   121     
   122     /** Support for <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> requires
   123      * a callback from the server generated page to a function defined in the
   124      * system. The name of such function is usually specified as a property
   125      * (of possibly different names). By defining the <code>jsonp</code> attribute
   126      * one turns on the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> 
   127      * transmission and specifies the name of the property. The property should
   128      * also be used in the {@link #url()} attribute on appropriate place.
   129      * 
   130      * @return name of a property to carry the name of <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>
   131      *    callback function.
   132      */
   133     String jsonp() default "";
   134     
   135     /** The model class to be send to the server as JSON data.
   136      * By default no data are sent. However certain {@link #method() transport methods}
   137      * (like <code>"PUT"</code> and <code>"POST"</code>) require the 
   138      * data to be specified.
   139      * 
   140      * @return name of a class generated using {@link Model @Model} annotation
   141      * @since 0.3
   142      */
   143     Class<?> data() default Object.class;
   144     
   145     /** The HTTP transfer method to use. Defaults to <code>"GET"</code>.
   146      * Other typical methods include <code>"HEAD"</code>, 
   147      * <code>"DELETE"</code>, <code>"POST"</code>, <code>"PUT"</code>.
   148      * The last two mentioned methods require {@link #data()} to be specified.
   149      * <p>
   150      * When {@link #jsonp() JSONP} transport is requested, the method 
   151      * has to be <code>"GET"</code>.
   152      * <p>
   153      * Since version 0.5 one can specify "<a href="doc-files/websockets.html">WebSocket</a>"
   154      * as the communication method.
   155      * 
   156      * @return name of the HTTP transfer method
   157      * @since 0.3
   158      */
   159     String method() default "GET";
   160     
   161     /** Name of a method in this class which should be called in case of 
   162      * an error. The method has to be non-private and take one model and 
   163      * one {@link Exception} 
   164      * parameter. If this method is not specified, the exception is just
   165      * printed to console.
   166      * 
   167      * @return name of method in this class
   168      * @since 0.5
   169      */
   170     public String onError() default "";    
   171 }