001/*
002 * (C) Copyright 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 *     Salem Aouana
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}. This singleton is also registering an injection of {@link RenderingContext}
048 *
049 * @since 11.1
050 */
051@Provider
052@Produces({ APPLICATION_JSON, "text/csv" })
053public class CoreIODelegate extends PartialCoreIODelegate
054        implements InjectableProvider<Context, Type>, Injectable<RenderingContext> {
055
056    @Override
057    protected boolean accept(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
058        return true;
059    }
060
061    @Override
062    public RenderingContext getValue() {
063        return Optional.ofNullable(RequestContext.getActiveContext())
064                       .map(RequestContext::getRequest)
065                       .map(RenderingContextWebUtils::getContext)
066                       .orElseThrow(() -> new NuxeoException("No RenderingContext in the request")); // shouldn't happen
067    }
068
069    @Override
070    public ComponentScope getScope() {
071        return ComponentScope.PerRequest;
072    }
073
074    @Override
075    public Injectable getInjectable(ComponentContext ic, Context a, Type c) {
076        if (!c.equals(RenderingContext.class)) {
077            return null;
078        }
079        return this;
080    }
081}