json/src/main/java/org/netbeans/html/json/spi/JSONCall.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 json/src/main/java/org/apidesign/html/json/spi/JSONCall.java@30f20d9c0986
child 940 bdec4103bdb2
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
jaroslav@24
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@24
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@24
     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.
jaroslav@24
     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.
jaroslav@24
    42
 */
jtulach@838
    43
package org.netbeans.html.json.spi;
jaroslav@24
    44
jaroslav@75
    45
import java.io.IOException;
jaroslav@75
    46
import java.io.OutputStream;
jaroslav@262
    47
import net.java.html.BrwsrCtx;
jaroslav@362
    48
import org.netbeans.html.json.impl.RcvrJSON;
jaroslav@75
    49
jaroslav@24
    50
/** Description of a JSON call request that is supposed to be processed
jtulach@838
    51
 * by {@link Transfer#loadJSON(org.netbeans.html.json.spi.JSONCall)} implementors.
jaroslav@24
    52
 *
jtulach@790
    53
 * @author Jaroslav Tulach
jaroslav@24
    54
 */
jaroslav@24
    55
public final class JSONCall {
jaroslav@255
    56
    private final RcvrJSON whenDone;
jaroslav@24
    57
    private final String urlBefore;
jaroslav@24
    58
    private final String urlAfter;
jaroslav@74
    59
    private final String method;
jaroslav@75
    60
    private final Object data;
jaroslav@262
    61
    private final BrwsrCtx ctx;
jaroslav@24
    62
jaroslav@262
    63
    JSONCall(BrwsrCtx ctx, RcvrJSON whenDone, String urlBefore, String urlAfter, String method, Object data) {
jaroslav@262
    64
        this.ctx = ctx;
jaroslav@24
    65
        this.whenDone = whenDone;
jaroslav@24
    66
        this.urlBefore = urlBefore;
jaroslav@24
    67
        this.urlAfter = urlAfter;
jaroslav@74
    68
        this.method = method;
jaroslav@75
    69
        this.data = data;
jaroslav@75
    70
    }
jaroslav@75
    71
    
jaroslav@75
    72
    /** Do we have some data to send? Can the {@link #writeData(java.io.OutputStream)} method be 
jaroslav@75
    73
     * called?
jaroslav@75
    74
     * 
jaroslav@75
    75
     * @return true, if the call has some data to send
jaroslav@75
    76
     */
jaroslav@75
    77
    public boolean isDoOutput() {
jaroslav@75
    78
        return this.data != null;
jaroslav@75
    79
    }
jaroslav@75
    80
    
jaroslav@75
    81
    public void writeData(OutputStream os) throws IOException {
jaroslav@75
    82
        if (this.data == null) {
jaroslav@75
    83
            throw new IOException("No data!");
jaroslav@75
    84
        }
jaroslav@75
    85
        os.write(this.data.toString().getBytes("UTF-8"));
jaroslav@75
    86
        os.flush();
jaroslav@74
    87
    }
jaroslav@74
    88
    
jaroslav@74
    89
    public String getMethod() {
jaroslav@74
    90
        return method;
jaroslav@24
    91
    }
jaroslav@24
    92
    
jaroslav@24
    93
    public boolean isJSONP() {
jaroslav@24
    94
        return urlAfter != null;
jaroslav@24
    95
    }
jaroslav@24
    96
    
jaroslav@24
    97
    public String composeURL(String jsonpCallback) {
jaroslav@24
    98
        if ((urlAfter == null) != (jsonpCallback == null)) {
jaroslav@24
    99
            throw new IllegalStateException();
jaroslav@24
   100
        }
jaroslav@24
   101
        if (urlAfter != null) {
jaroslav@24
   102
            return urlBefore + jsonpCallback + urlAfter;
jaroslav@24
   103
        } else {
jaroslav@24
   104
            return urlBefore;
jaroslav@24
   105
        }
jaroslav@24
   106
    }
jaroslav@24
   107
jaroslav@24
   108
    public void notifySuccess(Object result) {
jaroslav@258
   109
        if (result == null) {
jaroslav@262
   110
            dispatch(RcvrJSON.MsgEvnt.createOpen());
jaroslav@258
   111
        } else {
jaroslav@262
   112
            dispatch(RcvrJSON.MsgEvnt.createMessage(result));
jaroslav@258
   113
        }
jaroslav@24
   114
    }
jaroslav@24
   115
    
jaroslav@24
   116
    public void notifyError(Throwable error) {
jaroslav@258
   117
        if (error == null) {
jaroslav@262
   118
            dispatch(RcvrJSON.MsgEvnt.createClose());
jaroslav@258
   119
        } else {
jaroslav@262
   120
            dispatch(RcvrJSON.MsgEvnt.createError(error));
jaroslav@258
   121
        }
jaroslav@255
   122
    }
jaroslav@262
   123
    
jaroslav@262
   124
    private void dispatch(final RcvrJSON.MsgEvnt ev) {
jaroslav@574
   125
        ctx.execute(new Runnable() {
jaroslav@262
   126
            @Override
jaroslav@262
   127
            public void run() {
jaroslav@262
   128
                ev.dispatch(whenDone);
jaroslav@262
   129
            }
jaroslav@262
   130
        });
jaroslav@262
   131
    }
jaroslav@255
   132
jaroslav@255
   133
    public String getMessage() {
jaroslav@255
   134
        return this.data.toString();
jaroslav@24
   135
    }
jaroslav@24
   136
}