json/src/main/java/org/apidesign/html/json/spi/JSONCall.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 574 7958c9c205d9
permissions -rw-r--r--
Updating copyright headers to mention current year
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
 */
jaroslav@24
    43
package org.apidesign.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.JSON;
jaroslav@362
    49
import org.netbeans.html.json.impl.RcvrJSON;
jaroslav@75
    50
jaroslav@24
    51
/** Description of a JSON call request that is supposed to be processed
jaroslav@24
    52
 * by {@link Transfer#loadJSON(org.apidesign.html.json.spi.JSONCall)} implementors.
jaroslav@24
    53
 *
jaroslav@24
    54
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@24
    55
 */
jaroslav@24
    56
public final class JSONCall {
jaroslav@255
    57
    private final RcvrJSON whenDone;
jaroslav@24
    58
    private final String urlBefore;
jaroslav@24
    59
    private final String urlAfter;
jaroslav@74
    60
    private final String method;
jaroslav@75
    61
    private final Object data;
jaroslav@262
    62
    private final BrwsrCtx ctx;
jaroslav@24
    63
jaroslav@262
    64
    JSONCall(BrwsrCtx ctx, RcvrJSON whenDone, String urlBefore, String urlAfter, String method, Object data) {
jaroslav@262
    65
        this.ctx = ctx;
jaroslav@24
    66
        this.whenDone = whenDone;
jaroslav@24
    67
        this.urlBefore = urlBefore;
jaroslav@24
    68
        this.urlAfter = urlAfter;
jaroslav@74
    69
        this.method = method;
jaroslav@75
    70
        this.data = data;
jaroslav@75
    71
    }
jaroslav@75
    72
    
jaroslav@75
    73
    /** Do we have some data to send? Can the {@link #writeData(java.io.OutputStream)} method be 
jaroslav@75
    74
     * called?
jaroslav@75
    75
     * 
jaroslav@75
    76
     * @return true, if the call has some data to send
jaroslav@75
    77
     */
jaroslav@75
    78
    public boolean isDoOutput() {
jaroslav@75
    79
        return this.data != null;
jaroslav@75
    80
    }
jaroslav@75
    81
    
jaroslav@75
    82
    public void writeData(OutputStream os) throws IOException {
jaroslav@75
    83
        if (this.data == null) {
jaroslav@75
    84
            throw new IOException("No data!");
jaroslav@75
    85
        }
jaroslav@75
    86
        os.write(this.data.toString().getBytes("UTF-8"));
jaroslav@75
    87
        os.flush();
jaroslav@74
    88
    }
jaroslav@74
    89
    
jaroslav@74
    90
    public String getMethod() {
jaroslav@74
    91
        return method;
jaroslav@24
    92
    }
jaroslav@24
    93
    
jaroslav@24
    94
    public boolean isJSONP() {
jaroslav@24
    95
        return urlAfter != null;
jaroslav@24
    96
    }
jaroslav@24
    97
    
jaroslav@24
    98
    public String composeURL(String jsonpCallback) {
jaroslav@24
    99
        if ((urlAfter == null) != (jsonpCallback == null)) {
jaroslav@24
   100
            throw new IllegalStateException();
jaroslav@24
   101
        }
jaroslav@24
   102
        if (urlAfter != null) {
jaroslav@24
   103
            return urlBefore + jsonpCallback + urlAfter;
jaroslav@24
   104
        } else {
jaroslav@24
   105
            return urlBefore;
jaroslav@24
   106
        }
jaroslav@24
   107
    }
jaroslav@24
   108
jaroslav@24
   109
    public void notifySuccess(Object result) {
jaroslav@258
   110
        if (result == null) {
jaroslav@262
   111
            dispatch(RcvrJSON.MsgEvnt.createOpen());
jaroslav@258
   112
        } else {
jaroslav@262
   113
            dispatch(RcvrJSON.MsgEvnt.createMessage(result));
jaroslav@258
   114
        }
jaroslav@24
   115
    }
jaroslav@24
   116
    
jaroslav@24
   117
    public void notifyError(Throwable error) {
jaroslav@258
   118
        if (error == null) {
jaroslav@262
   119
            dispatch(RcvrJSON.MsgEvnt.createClose());
jaroslav@258
   120
        } else {
jaroslav@262
   121
            dispatch(RcvrJSON.MsgEvnt.createError(error));
jaroslav@258
   122
        }
jaroslav@255
   123
    }
jaroslav@262
   124
    
jaroslav@262
   125
    private void dispatch(final RcvrJSON.MsgEvnt ev) {
jaroslav@262
   126
        JSON.runInBrowser(ctx, new Runnable() {
jaroslav@262
   127
            @Override
jaroslav@262
   128
            public void run() {
jaroslav@262
   129
                ev.dispatch(whenDone);
jaroslav@262
   130
            }
jaroslav@262
   131
        });
jaroslav@262
   132
    }
jaroslav@255
   133
jaroslav@255
   134
    public String getMessage() {
jaroslav@255
   135
        return this.data.toString();
jaroslav@24
   136
    }
jaroslav@24
   137
}