src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/mapper/JSONMappingFactory.java
author hb
Tue, 02 Jan 2018 13:20:18 +0530
branchjmx-rest-api
changeset 56003 4f7f76f6be2f
parent 56002 60ab3b595a8e
permissions -rw-r--r--
* Start Rest adapter via command line arguments * Rest adapter config via management.properties
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
     1
/*
55997
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     4
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    10
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    15
 * accompanied this code).
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    16
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    20
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    23
 * questions.
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    24
 */
55997
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    25
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    26
package com.oracle.jmx.remote.rest.mapper;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    27
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    28
import com.oracle.jmx.remote.rest.json.JSONArray;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    29
import com.oracle.jmx.remote.rest.json.JSONElement;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    30
import com.oracle.jmx.remote.rest.json.JSONObject;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    31
import com.oracle.jmx.remote.rest.json.JSONPrimitive;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    32
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    33
import javax.management.MalformedObjectNameException;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    34
import javax.management.ObjectName;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    35
import javax.management.openmbean.*;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    36
import java.lang.reflect.Array;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    37
import java.math.BigDecimal;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    38
import java.math.BigInteger;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    39
import java.text.DateFormat;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    40
import java.text.ParseException;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    41
import java.text.SimpleDateFormat;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
    42
import java.util.*;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    43
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    44
/**
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    45
 * @author harsha
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    46
 */
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    47
public final class JSONMappingFactory {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    48
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    49
    public static final JSONMappingFactory INSTANCE;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    50
    private static final Map<Class<?>, JSONMapper> typeMapper;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    51
    private static final Map<String, Class<?>> primitiveMap = new HashMap<>();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    52
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    53
    static {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    54
        // Make order of Initialization explicit
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    55
        typeMapper = new HashMap<>();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    56
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    57
        primitiveMap.put("boolean", Boolean.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    58
        primitiveMap.put("int", Integer.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    59
        primitiveMap.put("long", Long.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    60
        primitiveMap.put("double", Double.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    61
        primitiveMap.put("float", Float.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    62
        primitiveMap.put("bool", Boolean.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    63
        primitiveMap.put("char", Character.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    64
        primitiveMap.put("byte", Byte.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    65
        primitiveMap.put("void", Void.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    66
        primitiveMap.put("short", Short.TYPE);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    67
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    68
        INSTANCE = new JSONMappingFactory();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    69
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    70
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    71
    private JSONMappingFactory() {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    72
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
    73
        typeMapper.put(void.class, new VoidMapper());
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
    74
        typeMapper.put(Void.class, new VoidMapper());
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    75
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    76
        typeMapper.put(boolean.class, new BooleanMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    77
        typeMapper.put(Boolean.class, new BooleanMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    78
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    79
        typeMapper.put(byte.class, new ByteMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    80
        typeMapper.put(Byte.class, new ByteMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    81
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    82
        typeMapper.put(short.class, new ShortMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    83
        typeMapper.put(Short.class, new ShortMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    84
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    85
        typeMapper.put(int.class, new IntegerMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    86
        typeMapper.put(Integer.class, new IntegerMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    87
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    88
        typeMapper.put(long.class, new LongMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    89
        typeMapper.put(Long.class, new LongMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    90
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    91
        typeMapper.put(float.class, new FloatMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    92
        typeMapper.put(Float.class, new FloatMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    93
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    94
        typeMapper.put(double.class, new DoubleMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    95
        typeMapper.put(Double.class, new DoubleMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    96
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    97
        typeMapper.put(char.class, new CharacterMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    98
        typeMapper.put(Character.class, new CharacterMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
    99
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   100
        typeMapper.put(String.class, new StringMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   101
        typeMapper.put(BigInteger.class, new BigIntegerMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   102
        typeMapper.put(BigDecimal.class, new BigDecimalMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   103
        typeMapper.put(ObjectName.class, new ObjectNameMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   104
        typeMapper.put(Date.class, new DateMapper());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   105
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   106
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   107
    private Class<?> getArrayComponent(Class<?> cls) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   108
        if (cls == null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   109
            return cls;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   110
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   111
        Class<?> compType = cls;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   112
        // TODO: Add check for max array dimention of 15
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   113
        while (compType.isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   114
            compType = compType.getComponentType();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   115
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   116
        return compType;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   117
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   118
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   119
    private Object getArrayElement(Object arrayObj) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   120
        if (arrayObj != null && arrayObj.getClass().isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   121
            while (arrayObj.getClass().isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   122
                Class<?> componentType = arrayObj.getClass().getComponentType();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   123
                if (Array.getLength(arrayObj) > 0) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   124
                    Object component = null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   125
                    for (int i = 0; i < Array.getLength(arrayObj); i++) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   126
                        component = Array.get(arrayObj, i);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   127
                        if (component != null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   128
                            break;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   129
                        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   130
                    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   131
                    if (component == null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   132
                        return null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   133
                    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   134
                    if (componentType.isPrimitive()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   135
                        componentType = component.getClass();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   136
                    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   137
                    arrayObj = componentType.cast(component);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   138
                } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   139
                    return null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   140
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   141
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   142
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   143
        return arrayObj;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   144
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   145
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   146
    // TODO: This should be access controlled. Define new permissions
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   147
    public void addMapping(Class<?> cls, JSONMapper mapper) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   148
        Class<?> input = cls;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   149
        if (cls.isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   150
            input = getArrayComponent(cls);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   151
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   152
        if (!typeMapper.containsKey(input)) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   153
            typeMapper.put(input, mapper);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   154
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   155
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   156
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   157
    public JSONMapper getTypeMapper(Object object) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   158
        return getTypeMapper(object, true);
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   159
    }
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   160
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   161
    private JSONMapper getTypeMapper(Object object, boolean deepTypeCheck) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   162
        if (object == null) return null;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   163
        Object obj = object;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   164
        Class<?> cls = object.getClass();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   165
        if (cls.isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   166
            Object arrayElement = getArrayElement(obj);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   167
            if (arrayElement instanceof CompositeData) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   168
                CompositeData cds = (CompositeData) arrayElement;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   169
                return new OpenArrayTypeMapper(cls, cds.getCompositeType());
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   170
            } else if (arrayElement instanceof TabularData) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   171
                TabularData tds = (TabularData) arrayElement;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   172
                return new OpenArrayTypeMapper(cls, tds.getTabularType());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   173
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   174
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   175
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   176
        if (object instanceof CompositeData) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   177
            CompositeData cd = (CompositeData) object;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   178
            return getTypeMapper(cd.getCompositeType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   179
        } else if (object instanceof TabularData) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   180
            TabularData cds = (TabularData) object;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   181
            return getTypeMapper(cds.getTabularType());
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   182
        } else if (object instanceof Collection<?>) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   183
            if (deepTypeCheck) {
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   184
                Collection<?> c = (Collection<?>) object;
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   185
                boolean unknownMapper = c.stream().anyMatch(k -> (k != null) && (getTypeMapper(k) == null));
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   186
                if (unknownMapper)
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   187
                    return null;
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   188
            }
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   189
            return new CollectionMapper();
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   190
        } else if (object instanceof Map<?, ?>) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   191
            if (deepTypeCheck) {
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   192
                Map<?, ?> map = (Map<?, ?>) object;
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   193
                boolean unknownMapper = map.keySet().stream().
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   194
                        anyMatch(k -> ((k != null) && (getTypeMapper(k) == null)
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   195
                                || (map.get(k) != null && getTypeMapper(map.get(k)) == null)));
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   196
                if (unknownMapper)
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   197
                    return null;
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   198
            }
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   199
            return new MapMapper();
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   200
        } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   201
            return getTypeMapper(cls);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   202
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   203
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   204
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   205
    public JSONMapper getTypeMapper(Class<?> type) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   206
        if (type == null) return null;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   207
        if (type.isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   208
            Class<?> compType = getArrayComponent(type);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   209
            if (typeMapper.get(compType) != null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   210
                return new GenericArrayMapper(type);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   211
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   212
                return null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   213
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   214
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   215
        return typeMapper.get(type);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   216
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   217
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   218
    public JSONMapper getTypeMapper(OpenType<?> type) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   219
        if (type instanceof CompositeType) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   220
            return new OpenCompositeTypeMapper((CompositeType) type);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   221
        } else if (type instanceof SimpleType) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   222
            try {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   223
                return getTypeMapper(Class.forName(((SimpleType) type).getClassName()));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   224
            } catch (ClassNotFoundException ex) { // This should not happen as SimpleTypes are always loaded
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   225
                throw new RuntimeException(ex);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   226
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   227
        } else if (type instanceof ArrayType) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   228
            try {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   229
                ArrayType<?> at = (ArrayType) type;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   230
                Class<?> arrayClass = Class.forName(type.getClassName());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   231
                return new OpenArrayTypeMapper(arrayClass, at.getElementOpenType());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   232
            } catch (ClassNotFoundException ex) { // This should not happen as SimpleTypes are always loaded
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   233
                throw new RuntimeException(ex);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   234
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   235
        } else if (type instanceof TabularType) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   236
            return new OpenTabularTypeMapper((TabularType) type);
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   237
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   238
        return null; //keep compiler happy
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   239
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   240
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   241
    public boolean isTypeMapped(String type) throws ClassNotFoundException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   242
        if (primitiveMap.get(type) != null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   243
            return true;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   244
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   245
        Class<?> inputCls = Class.forName(type);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   246
        inputCls = getArrayComponent(inputCls);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   247
        if (inputCls.equals(CompositeData.class) || inputCls.equals(TabularData.class)
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   248
                || inputCls.equals(CompositeDataSupport.class) || inputCls.equals(TabularDataSupport.class)) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   249
            return true;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   250
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   251
        return JSONMappingFactory.INSTANCE.getTypeMapper(inputCls) != null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   252
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   253
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   254
    private static class OpenArrayTypeMapper extends GenericArrayMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   255
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   256
        OpenArrayTypeMapper(Class<?> type, OpenType<?> elementOpenType) {
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   257
            super(type);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   258
            mapper = JSONMappingFactory.INSTANCE.getTypeMapper(elementOpenType);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   259
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   260
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   261
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   262
    // This mapper array type for any java class
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   263
    private static class GenericArrayMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   264
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   265
        private final Class<?> type;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   266
        protected JSONMapper mapper;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   267
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   268
        GenericArrayMapper(Class<?> type) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   269
            this.type = type;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   270
            mapper = JSONMappingFactory.INSTANCE.getTypeMapper(JSONMappingFactory.INSTANCE.getArrayComponent(type));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   271
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   272
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   273
        private Object handleArrayType(Class<?> classType, JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   274
            if (!(jsonValue instanceof JSONArray)) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   275
                throw new JSONDataException("Invald JSON data format");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   276
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   277
            JSONArray jarr = (JSONArray) jsonValue;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   278
            Class<?> compType = classType.getComponentType();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   279
            Object resultArray = Array.newInstance(compType, jarr.size());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   280
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   281
            for (int i = 0; i < jarr.size(); i++) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   282
                if (compType != null && compType.isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   283
                    Array.set(resultArray, i, handleArrayType(compType, jarr.get(i)));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   284
                } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   285
                    Array.set(resultArray, i, mapper.toJavaObject(jarr.get(i)));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   286
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   287
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   288
            return resultArray;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   289
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   290
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   291
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   292
        public Object toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   293
            return handleArrayType(type, jsonValue);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   294
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   295
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   296
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   297
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   298
            if (data == null) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   299
                return null;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   300
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   301
            if (!data.getClass().equals(type)) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   302
                throw new JSONMappingException("Illegal type : " + data.getClass());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   303
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   304
            return getJasonValue(data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   305
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   306
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   307
        private JSONElement getJasonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   308
            if (data == null) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   309
                return null;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   310
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   311
            if (!data.getClass().isArray()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   312
                return mapper.toJsonValue(data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   313
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   314
                JSONArray jArray = new JSONArray();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   315
                for (int i = 0; i < Array.getLength(data); i++) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   316
                    jArray.add(getJasonValue(Array.get(data, i)));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   317
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   318
                return jArray;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   319
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   320
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   321
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   322
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   323
    /*
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   324
    Mapper for compositeType. CompositeData cannot be mapped without it's associated
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   325
    OpenType
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   326
     */
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   327
    private static class OpenCompositeTypeMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   328
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   329
        private final CompositeType type;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   330
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   331
        OpenCompositeTypeMapper(CompositeType type) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   332
            this.type = type;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   333
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   334
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   335
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   336
        public CompositeDataSupport toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   337
            if (!(jsonValue instanceof JSONObject)) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   338
                throw new JSONDataException("JSON String not an object");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   339
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   340
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   341
            JSONObject jObject = (JSONObject) jsonValue;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   342
            Map<String, Object> compositeDataMap = new HashMap<>();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   343
            for (String itemName : type.keySet()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   344
                OpenType<?> oType = type.getType(itemName);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   345
                JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(oType);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   346
                compositeDataMap.put(itemName, typeMapper.toJavaObject(jObject.get(itemName)));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   347
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   348
            try {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   349
                return new CompositeDataSupport(type, compositeDataMap);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   350
            } catch (OpenDataException ex) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   351
                throw new JSONDataException("Could not create CompositeData", ex);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   352
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   353
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   354
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   355
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   356
        public JSONElement toJsonValue(Object d) throws JSONMappingException {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   357
            CompositeData data = (CompositeData) d;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   358
            if (data == null) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   359
                return null;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   360
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   361
            JSONObject jObject = new JSONObject();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   362
            for (String itemName : type.keySet()) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   363
                OpenType<?> oType = type.getType(itemName);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   364
                JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(oType);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   365
                if (typeMapper != null) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   366
                    jObject.put(itemName, typeMapper.toJsonValue(data.get(itemName)));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   367
                } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   368
                    System.out.println("Unable to find mapper for : " + oType);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   369
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   370
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   371
            return jObject;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   372
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   373
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   374
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   375
    private static class OpenTabularTypeMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   376
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   377
        private final TabularType type;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   378
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   379
        public OpenTabularTypeMapper(TabularType type) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   380
            this.type = type;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   381
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   382
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   383
        /*
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   384
        Tabular data in JSON can follow below schema
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   385
        {
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   386
            "keys" : [<list of elements>],
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   387
            "rows": [{ <CompositeData> }]
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   388
        }
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   389
         */
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   390
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   391
        public TabularDataSupport toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   392
            throw new UnsupportedOperationException();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   393
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   394
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   395
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   396
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   397
            if (data == null) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   398
                return null;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   399
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   400
            TabularDataSupport tds = (TabularDataSupport) data;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   401
            JSONArray jsonArray = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   402
            for (Map.Entry<Object, Object> a : tds.entrySet()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   403
                CompositeData cds = (CompositeData) a.getValue();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   404
                JSONMapper cdsMapper = JSONMappingFactory.INSTANCE.getTypeMapper(cds);
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   405
                if (cdsMapper != null) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   406
                    jsonArray.add(cdsMapper.toJsonValue(cds));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   407
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   408
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   409
            return jsonArray;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   410
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   411
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   412
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   413
    private static class VoidMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   414
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   415
        public Void toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   416
            return null;
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   417
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   418
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   419
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   420
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   421
            return null;
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   422
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   423
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   424
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   425
    private static class BooleanMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   426
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   427
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   428
        public Boolean toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   429
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Boolean) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   430
                return (Boolean) ((JSONPrimitive) jsonValue).getValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   431
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   432
                throw new JSONDataException("Invalid type convertion - cannot convert "
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   433
                        + ((JSONPrimitive) jsonValue).getValue()
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   434
                        + "(" + ((JSONPrimitive) jsonValue).getValue().getClass() + ")" + " to boolean");
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   435
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   436
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   437
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   438
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   439
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   440
            return new JSONPrimitive((Boolean) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   441
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   442
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   443
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   444
    private static class ByteMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   445
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   446
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   447
        public Byte toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   448
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   449
                return ((Long) ((JSONPrimitive) jsonValue).getValue()).byteValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   450
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   451
                throw new JSONDataException("Invalid type convertion - cannot convert "
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   452
                        + (((JSONPrimitive) jsonValue).getValue().getClass()) + " to byte");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   453
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   454
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   455
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   456
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   457
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   458
            return new JSONPrimitive((Byte) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   459
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   460
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   461
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   462
    private static class ShortMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   463
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   464
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   465
        public Short toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   466
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   467
                return ((Long) ((JSONPrimitive) jsonValue).getValue()).shortValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   468
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   469
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   470
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   471
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   472
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   473
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   474
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   475
            return new JSONPrimitive((Short) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   476
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   477
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   478
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   479
    private static class IntegerMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   480
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   481
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   482
        public Integer toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   483
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   484
                return ((Long) ((JSONPrimitive) jsonValue).getValue()).intValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   485
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   486
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   487
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   488
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   489
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   490
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   491
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   492
            return new JSONPrimitive((Integer) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   493
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   494
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   495
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   496
    private static class LongMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   497
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   498
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   499
        public Long toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   500
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   501
                return (Long) ((JSONPrimitive) jsonValue).getValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   502
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   503
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   504
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   505
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   506
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   507
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   508
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   509
            return new JSONPrimitive((Long) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   510
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   511
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   512
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   513
    private static class FloatMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   514
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   515
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   516
        public Float toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   517
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Double) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   518
                return ((Double) ((JSONPrimitive) jsonValue).getValue()).floatValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   519
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   520
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   521
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   522
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   523
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   524
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   525
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   526
            return new JSONPrimitive((Float) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   527
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   528
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   529
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   530
    private static class DoubleMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   531
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   532
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   533
        public Double toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   534
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   535
                return (Double) ((JSONPrimitive) jsonValue).getValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   536
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   537
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   538
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   539
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   540
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   541
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   542
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   543
            return new JSONPrimitive((Double) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   544
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   545
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   546
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   547
    private static class CharacterMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   548
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   549
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   550
        public Character toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   551
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   552
                String data = ((String) ((JSONPrimitive) jsonValue).getValue());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   553
                if (data.length() < 1) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   554
                    throw new JSONDataException("Invalid char");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   555
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   556
                return data.charAt(0);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   557
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   558
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   559
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   560
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   561
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   562
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   563
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   564
            return new JSONPrimitive((Character) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   565
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   566
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   567
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   568
    private static class StringMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   569
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   570
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   571
        public String toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   572
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   573
                return (String) ((JSONPrimitive) jsonValue).getValue();
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   574
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   575
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   576
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   577
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   578
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   579
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   580
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   581
            return new JSONPrimitive((String) data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   582
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   583
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   584
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   585
    private static class BigDecimalMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   586
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   587
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   588
        public BigDecimal toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   589
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Double) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   590
                return BigDecimal.valueOf((Double) ((JSONPrimitive) jsonValue).getValue());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   591
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   592
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   593
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   594
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   595
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   596
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   597
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   598
            return new JSONPrimitive(((BigDecimal) data).doubleValue());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   599
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   600
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   601
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   602
    private static class BigIntegerMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   603
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   604
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   605
        public BigInteger toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   606
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof Long) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   607
                return BigInteger.valueOf((Long) ((JSONPrimitive) jsonValue).getValue());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   608
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   609
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   610
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   611
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   612
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   613
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   614
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   615
            return new JSONPrimitive(((BigInteger) data).longValue());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   616
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   617
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   618
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   619
    private static class ObjectNameMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   620
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   621
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   622
        public ObjectName toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   623
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   624
                try {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   625
                    return new ObjectName((String) ((JSONPrimitive) jsonValue).getValue());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   626
                } catch (MalformedObjectNameException ex) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   627
                    throw new JSONDataException("Invalid Objectname");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   628
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   629
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   630
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   631
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   632
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   633
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   634
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   635
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   636
            return new JSONPrimitive(data.toString());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   637
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   638
    }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   639
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   640
    private static class DateMapper implements JSONMapper {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   641
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   642
        private final DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   643
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   644
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   645
        public Date toJavaObject(JSONElement jsonValue) throws JSONDataException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   646
            if (jsonValue instanceof JSONPrimitive && ((JSONPrimitive) jsonValue).getValue() instanceof String) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   647
                String data = ((String) ((JSONPrimitive) jsonValue).getValue());
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   648
                try {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   649
                    return df.parse(data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   650
                } catch (ParseException ex) {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   651
                    throw new JSONDataException("Invalid Data " + data);
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   652
                }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   653
            } else {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   654
                throw new JSONDataException("Invalid JSON");
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   655
            }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   656
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   657
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   658
        @Override
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   659
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   660
            return new JSONPrimitive(df.format((Date) data));
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   661
        }
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   662
    }
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   663
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   664
    private static final class MapMapper implements JSONMapper {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   665
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   666
        @Override
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   667
        public Map<String, Object> toJavaObject(JSONElement jsonValue) throws JSONDataException {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   668
            if (jsonValue instanceof JSONObject) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   669
                JSONObject obj = (JSONObject) jsonValue;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   670
                Map<String, Object> result = new HashMap<>(obj.size());
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   671
                for (String k : result.keySet()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   672
                    JSONElement elem = obj.get(k);
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   673
                    if (elem instanceof JSONPrimitive) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   674
                        result.put(k, ((JSONPrimitive) elem).getValue());
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   675
                    } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   676
                        JSONMapper mapper;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   677
                        if (elem instanceof JSONObject) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   678
                            mapper = new MapMapper();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   679
                            result.put(k, mapper.toJavaObject(elem));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   680
                        } else if (elem instanceof JSONArray) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   681
                            mapper = new CollectionMapper();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   682
                            result.put(k, mapper.toJavaObject(elem));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   683
                        } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   684
                            throw new JSONDataException("Unable to map : " + elem.getClass());
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   685
                        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   686
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   687
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   688
                return result;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   689
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   690
            throw new JSONDataException("Inalid input");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   691
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   692
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   693
        @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   694
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   695
            if (data instanceof Map) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   696
                JSONObject jobj = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   697
                Map<?, ?> input = (Map<?, ?>) data;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   698
                for (Object k : input.keySet()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   699
                    String key = k.toString();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   700
                    final Object value = input.get(k);
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   701
                    if (value == null) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   702
                        jobj.put(key, (JSONElement) null);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   703
                    } else {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   704
                        JSONMapper mapper = JSONMappingFactory.INSTANCE
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   705
                                .getTypeMapper(value,false); // Disable repeated type checking
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   706
                        if (mapper == null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   707
                            throw new JSONMappingException("Unable to map : " + value);
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   708
                        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   709
                        jobj.put(key, mapper.toJsonValue(value));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   710
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   711
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   712
                return jobj;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   713
            } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   714
                throw new JSONMappingException("Invalid Input");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   715
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   716
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   717
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   718
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   719
    private static final class CollectionMapper implements JSONMapper {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   720
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   721
        public CollectionMapper() {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   722
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   723
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   724
        @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   725
        @SuppressWarnings("unchecked")
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   726
        public Object toJavaObject(JSONElement jsonValue) throws JSONDataException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   727
            if (jsonValue instanceof JSONArray) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   728
                JSONArray jarr = (JSONArray) jsonValue;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   729
                List<Object> result = new ArrayList<>(jarr.size());
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   730
                for (JSONElement elem : jarr) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   731
                    if (elem instanceof JSONPrimitive) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   732
                        result.add(((JSONPrimitive) elem).getValue());
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   733
                    } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   734
                        JSONMapper mapper;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   735
                        if (elem instanceof JSONObject) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   736
                            mapper = new MapMapper();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   737
                            result.add(mapper.toJavaObject(elem));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   738
                        } else if (elem instanceof JSONArray) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   739
                            mapper = new CollectionMapper();
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   740
                            result.add(mapper.toJavaObject(elem));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   741
                        } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   742
                            throw new JSONDataException("Unable to map : " + elem.getClass());
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   743
                        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   744
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   745
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   746
                return result;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   747
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   748
            throw new JSONDataException("Inalid input");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   749
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   750
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   751
        @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   752
        @SuppressWarnings("unchecked")
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   753
        public JSONElement toJsonValue(Object data) throws JSONMappingException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   754
            if (data instanceof Collection) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   755
                JSONArray jarr = new JSONArray();
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   756
                Collection<?> c = (Collection<?>) data;
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   757
                Iterator<?> itr = c.iterator();
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   758
                while (itr.hasNext()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   759
                    Object next = itr.next();
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   760
                    JSONMapper typeMapper = JSONMappingFactory.INSTANCE.
60ab3b595a8e Lot of bug fixes
hb
parents: 55997
diff changeset
   761
                            getTypeMapper(next,false); // Disable repeated type checking for collection
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   762
                    if (typeMapper == null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   763
                        throw JSONMappingException.UNABLE_TO_MAP;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   764
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   765
                    jarr.add(typeMapper.toJsonValue(next));
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   766
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   767
                return jarr;
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   768
            } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   769
                throw new JSONMappingException("Invalid Input");
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   770
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   771
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents: 55985
diff changeset
   772
    }
55985
0c5a02edfdef REST Adapter Initial commit
hb
parents:
diff changeset
   773
}