001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.core.io.registry;
021
022import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
023
024import java.io.ByteArrayInputStream;
025import java.io.ByteArrayOutputStream;
026import java.io.IOException;
027import java.lang.reflect.Type;
028import java.util.List;
029
030import org.apache.commons.lang3.reflect.TypeUtils;
031import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
032import org.nuxeo.ecm.core.io.registry.context.RenderingContext.CtxBuilder;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Quick use of {@link MarshallerRegistry}.
037 *
038 * @since 7.2
039 */
040public final class MarshallerHelper {
041
042    /**
043     * Just call static methods.
044     */
045    private MarshallerHelper() {
046    }
047
048    private static MarshallerRegistry getService() {
049        MarshallerRegistry registry = Framework.getService(MarshallerRegistry.class);
050        return registry;
051    }
052
053    /**
054     * Checks the marshallers isn't null. Throw an exception if it is.
055     */
056    private static void checkMarshaller(Type type, Marshaller<?> marshaller) {
057        if (marshaller == null) {
058            throw new MarshallingException("No marshaller found for type " + type.toString());
059        }
060    }
061
062    /**
063     * Convert the given object to json.
064     *
065     * @param object The object to convert as json.
066     * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context.
067     * @return the resulting json.
068     * @since 7.2
069     */
070    public static <T> String objectToJson(T object, RenderingContext ctx) throws IOException {
071        @SuppressWarnings("unchecked")
072        Class<T> type = (Class<T>) object.getClass();
073        Writer<T> writer = getService().getWriter(ctx, type, APPLICATION_JSON_TYPE);
074        checkMarshaller(type, writer);
075        ByteArrayOutputStream baos = new ByteArrayOutputStream();
076        writer.write(object, type, type, APPLICATION_JSON_TYPE, baos);
077        return baos.toString();
078    }
079
080    /**
081     * Convert the given object to json.
082     * <p>
083     * Specify its generic type to be sure to get the best marshaller to manage it.
084     * </p>
085     *
086     * @param genericType The generic type of the object. You can easily create parametrize type using
087     *            {@link TypeUtils#parameterize(Class, Type...)}
088     * @param object The object to convert as json.
089     * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context.
090     * @return the resulting json.
091     * @since 7.2
092     */
093    public static <T> String objectToJson(Type genericType, T object, RenderingContext ctx) throws IOException {
094        @SuppressWarnings("unchecked")
095        Class<T> type = (Class<T>) object.getClass();
096        Writer<T> writer = getService().getWriter(ctx, type, genericType, APPLICATION_JSON_TYPE);
097        checkMarshaller(genericType, writer);
098        ByteArrayOutputStream baos = new ByteArrayOutputStream();
099        writer.write(object, type, genericType, APPLICATION_JSON_TYPE, baos);
100        return baos.toString();
101    }
102
103    /**
104     * Convert the given list to json.
105     * <p>
106     * Specify the list element type to get the best marshaller to manage conversion.
107     * </p>
108     *
109     * @param elementType The element type of the list.
110     * @param list The list to convert.
111     * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context.
112     * @return the resulting json.
113     * @since 7.2
114     */
115    public static <T> String listToJson(Class<T> elementType, List<T> list, RenderingContext ctx) throws IOException {
116        Type genericType = TypeUtils.parameterize(List.class, elementType);
117        @SuppressWarnings("rawtypes")
118        Writer<List> writer = getService().getWriter(ctx, List.class, genericType, APPLICATION_JSON_TYPE);
119        checkMarshaller(genericType, writer);
120        ByteArrayOutputStream baos = new ByteArrayOutputStream();
121        writer.write(list, List.class, genericType, APPLICATION_JSON_TYPE, baos);
122        return baos.toString();
123    }
124
125    /**
126     * Read an object of the given type from given json.
127     *
128     * @param type The type of the read object.
129     * @param json The json to parse.
130     * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context.
131     * @return the resulting object.
132     * @since 7.2
133     */
134    public static <T> T objectToJson(Class<T> type, String json, RenderingContext ctx) throws IOException {
135        Reader<T> reader = getService().getReader(ctx, type, APPLICATION_JSON_TYPE);
136        checkMarshaller(type, reader);
137        return reader.read(type, type, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes()));
138    }
139
140    /**
141     * Read an object of the given type from given json.
142     * <p>
143     * Specify its generic type to be sure to get the best marshaller to manage it.
144     * </p>
145     *
146     * @param type The type of the read object.
147     * @param genericType The generic type of the object. You can easily create parametrize type using
148     *            {@link TypeUtils#parameterize(Class, Type...)}
149     * @param json The json to parse.
150     * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context.
151     * @return the resulting object.
152     * @since 7.2
153     */
154    public static <T> T objectToJson(Class<T> type, Type genericType, String json, RenderingContext ctx)
155            throws IOException {
156        Reader<T> reader = getService().getReader(ctx, type, genericType, APPLICATION_JSON_TYPE);
157        checkMarshaller(genericType, reader);
158        return reader.read(type, genericType, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes()));
159    }
160
161    /**
162     * Read an object of the given type from given json.
163     * <p>
164     * Specify the list element type to get the best marshaller to manage conversion.
165     * </p>
166     *
167     * @param elementType The element type of the list.
168     * @param json The json to parse.
169     * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context.
170     * @return the resulting list.
171     * @since 7.2
172     */
173    @SuppressWarnings("unchecked")
174    public static <T> List<T> listToJson(Class<T> elementType, String json, RenderingContext ctx) throws IOException {
175        Type genericType = TypeUtils.parameterize(List.class, elementType);
176        @SuppressWarnings("rawtypes")
177        Reader<List> reader = getService().getReader(ctx, List.class, genericType, APPLICATION_JSON_TYPE);
178        checkMarshaller(genericType, reader);
179        return reader.read(List.class, genericType, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes()));
180    }
181
182}