ko4j/src/main/java/org/netbeans/html/ko4j/KOTransfer.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 09 Dec 2014 08:33:22 +0100
branchgc
changeset 892 de02b7c13379
parent 877 ko4j/src/main/java/org/netbeans/html/ko4j/FXContext.java@75fff3637abe
parent 886 ko4j/src/main/java/org/netbeans/html/ko4j/FXContext.java@88d62267a0b5
child 929 50ee036959f5
permissions -rw-r--r--
Bringing the most recent changes in default branch to gc
jaroslav@37
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@37
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@37
     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@37
     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@37
    42
 */
jaroslav@442
    43
package org.netbeans.html.ko4j;
jaroslav@37
    44
jaroslav@446
    45
import java.io.ByteArrayOutputStream;
jaroslav@60
    46
import java.io.IOException;
jaroslav@60
    47
import java.io.InputStream;
jaroslav@446
    48
import java.io.InputStreamReader;
jtulach@886
    49
import org.netbeans.html.context.spi.Contexts;
jtulach@838
    50
import org.netbeans.html.json.spi.JSONCall;
jtulach@838
    51
import org.netbeans.html.json.spi.Transfer;
jaroslav@37
    52
jaroslav@54
    53
/** This is an implementation package - just
jaroslav@54
    54
 * include its JAR on classpath and use official {@link Context} API
jaroslav@54
    55
 * to access the functionality.
jaroslav@54
    56
 * <p>
jaroslav@37
    57
 *
jtulach@790
    58
 * @author Jaroslav Tulach
jaroslav@37
    59
 */
jtulach@886
    60
@Contexts.Id("xhr")
jtulach@886
    61
final class KOTransfer
jtulach@886
    62
implements Transfer {
jtulach@886
    63
    KOTransfer() {
jaroslav@288
    64
    }
jaroslav@150
    65
    
jaroslav@275
    66
    @Override
jaroslav@37
    67
    public void extract(Object obj, String[] props, Object[] values) {
jaroslav@37
    68
        LoadJSON.extractJSON(obj, props, values);
jaroslav@37
    69
    }
jaroslav@37
    70
jaroslav@37
    71
    @Override
jaroslav@37
    72
    public void loadJSON(final JSONCall call) {
jaroslav@446
    73
        if (call.isJSONP()) {
jaroslav@446
    74
            String me = LoadJSON.createJSONP(call);
jaroslav@446
    75
            LoadJSON.loadJSONP(call.composeURL(me), me);
jaroslav@446
    76
        } else {
jaroslav@446
    77
            String data = null;
jaroslav@446
    78
            if (call.isDoOutput()) {
jaroslav@446
    79
                try {
jaroslav@446
    80
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
jaroslav@446
    81
                    call.writeData(bos);
jaroslav@446
    82
                    data = new String(bos.toByteArray(), "UTF-8");
jaroslav@446
    83
                } catch (IOException ex) {
jaroslav@446
    84
                    call.notifyError(ex);
jaroslav@446
    85
                }
jaroslav@446
    86
            }
jaroslav@446
    87
            LoadJSON.loadJSON(call.composeURL(null), call, call.getMethod(), data);
jaroslav@446
    88
        }
jaroslav@37
    89
    }
jaroslav@38
    90
jaroslav@38
    91
    @Override
jaroslav@60
    92
    public Object toJSON(InputStream is) throws IOException {
jaroslav@446
    93
        StringBuilder sb = new StringBuilder();
jaroslav@446
    94
        InputStreamReader r = new InputStreamReader(is);
jaroslav@446
    95
        for (;;) {
jaroslav@446
    96
            int ch = r.read();
jaroslav@446
    97
            if (ch == -1) {
jaroslav@446
    98
                break;
jaroslav@446
    99
            }
jaroslav@446
   100
            sb.append((char)ch);
jaroslav@446
   101
        }
jaroslav@446
   102
        return LoadJSON.parse(sb.toString());
jaroslav@60
   103
    }
jaroslav@37
   104
}