001/*
002 * (C) Copyright 2015-2019 Nuxeo (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.webengine.jaxrs.coreiodelegate;
021
022import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
023
024import java.lang.annotation.Annotation;
025import java.lang.reflect.Type;
026import java.util.Optional;
027
028import javax.ws.rs.Produces;
029import javax.ws.rs.core.Context;
030import javax.ws.rs.core.MediaType;
031import javax.ws.rs.ext.MessageBodyWriter;
032import javax.ws.rs.ext.Provider;
033
034import org.nuxeo.ecm.core.api.NuxeoException;
035import org.nuxeo.ecm.core.io.registry.Reader;
036import org.nuxeo.ecm.core.io.registry.Writer;
037import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
038import org.nuxeo.ecm.platform.web.common.RequestContext;
039
040import com.sun.jersey.core.spi.component.ComponentContext;
041import com.sun.jersey.core.spi.component.ComponentScope;
042import com.sun.jersey.spi.inject.Injectable;
043import com.sun.jersey.spi.inject.InjectableProvider;
044
045/**
046 * A JAX-RS {@link MessageBodyWriter} that try to delegate the marshalling to all nuxeo-core-io {@link Writer} and
047 * {@link Reader}.
048 *
049 * @since 7.2
050 * @implNote since 11.1, this singleton is also registering an injection of {@link RenderingContext}
051 * @deprecated since 11.1. Use {@link org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.CoreIODelegate} instead.
052 */
053@Deprecated(since = "11.1", forRemoval = true)
054@Provider
055@Produces(APPLICATION_JSON)
056public final class JsonCoreIODelegate extends PartialCoreIODelegate
057        implements InjectableProvider<Context, Type>, Injectable<RenderingContext> {
058
059    @Override
060    protected boolean accept(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
061        return true;
062    }
063
064    /**
065     * @since 11.1
066     */
067    @Override
068    public RenderingContext getValue() {
069        return Optional.ofNullable(RequestContext.getActiveContext())
070                       .map(RequestContext::getRequest)
071                       .map(RenderingContextWebUtils::getContext)
072                       .orElseThrow(() -> new NuxeoException("No RenderingContext in the request")); // shouldn't happen
073    }
074
075    /**
076     * @since 11.1
077     */
078    @Override
079    public ComponentScope getScope() {
080        return ComponentScope.PerRequest;
081    }
082
083    /**
084     * @since 11.1
085     */
086    @Override
087    public Injectable getInjectable(ComponentContext ic, Context a, Type c) {
088        if (!c.equals(RenderingContext.class)) {
089            return null;
090        }
091        return this;
092    }
093}