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 java.lang.reflect.Type;
023
024import javax.inject.Inject;
025import javax.ws.rs.core.MediaType;
026
027import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
028import org.nuxeo.ecm.core.io.registry.reflect.Setup;
029import org.nuxeo.ecm.core.io.registry.reflect.Supports;
030
031/**
032 * Interface of Java type converter.
033 * <p>
034 * You must add {@link Setup} annotation to every class implementing this interface. You should add {@link Supports}
035 * annotation to define supported mimetype. You can add {@link Inject} annotation to your properties to get current
036 * {@link RenderingContext} or any Nuxeo service.
037 * </p>
038 * <p>
039 * To get an instance of this class with injected properties, call
040 * {@link MarshallerRegistry#getInstance(RenderingContext, Class)}.
041 * </p>
042 *
043 * @param <EntityType> The managed Java type.
044 * @since 7.2
045 */
046public interface Marshaller<EntityType> {
047
048    /**
049     * Checks if this marshaller can handle the marshalling request.
050     * <p>
051     * Please note it's useless to check that clazz is an instance of EntityType or if generic type and entity type are
052     * compatible (unlike JAX-RS which just checks the clazz, not the generic type). It's also useless to check
053     * {@link Supports} is compatible with mediatype. This is already done by the {@link MarshallerRegistry}
054     * </p>
055     * <p>
056     * This method implementation can use injected properties. So you can check the current {@link RenderingContext} to
057     * accept or reject a marshalling request.
058     * </p>
059     *
060     * @param clazz The type to marshall.
061     * @param genericType The generic type to marshall.
062     * @param mediatype The managed mimetype.
063     * @return true if this converter handle the request, false otherwise.
064     * @since 7.2
065     */
066    public boolean accept(Class<?> clazz, Type genericType, MediaType mediatype);
067
068}