001// Generated by delombok at Thu Mar 19 15:24:58 GMT 2020
002/*
003 *  Licensed to the Apache Software Foundation (ASF) under one
004 *  or more contributor license agreements.  See the NOTICE file
005 *  distributed with this work for additional information
006 *  regarding copyright ownership.  The ASF licenses this file
007 *  to you under the Apache License, Version 2.0 (the
008 *  "License"); you may not use this file except in compliance
009 *  with the License.  You may obtain a copy of the License at
010 *
011 *        http://www.apache.org/licenses/LICENSE-2.0
012 *
013 *  Unless required by applicable law or agreed to in writing,
014 *  software distributed under the License is distributed on an
015 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 *  KIND, either express or implied.  See the License for the
017 *  specific language governing permissions and limitations
018 *  under the License.
019 */
020package org.apache.isis.viewer.restfulobjects.rendering.service.acceptheader;
021
022import java.io.IOException;
023import java.util.List;
024import java.util.stream.Collectors;
025import javax.annotation.PostConstruct;
026import javax.annotation.PreDestroy;
027import javax.inject.Named;
028import javax.ws.rs.container.ContainerRequestContext;
029import javax.ws.rs.container.ContainerRequestFilter;
030import javax.ws.rs.container.ContainerResponseContext;
031import javax.ws.rs.container.ContainerResponseFilter;
032import javax.ws.rs.core.MediaType;
033import javax.ws.rs.ext.Provider;
034import org.springframework.beans.factory.annotation.Qualifier;
035import org.springframework.context.annotation.Primary;
036import org.springframework.core.annotation.Order;
037import org.springframework.stereotype.Component;
038import org.springframework.stereotype.Service;
039import org.apache.isis.applib.annotation.OrderPrecedence;
040import org.apache.isis.applib.annotation.IsisSessionScope;
041import org.apache.isis.applib.services.acceptheader.AcceptHeaderService;
042import org.apache.isis.core.commons.internal.base._NullSafe;
043import static org.apache.isis.core.commons.internal.base._NullSafe.stream;
044
045@Service
046@Named("isisRoRendering.AcceptHeaderServiceForRest")
047@Order(OrderPrecedence.MIDPOINT)
048@Primary
049@Qualifier("ForRest")
050@IsisSessionScope
051public class AcceptHeaderServiceForRest implements AcceptHeaderService {
052    @java.lang.SuppressWarnings("all")
053    private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(AcceptHeaderServiceForRest.class);
054    private static ThreadLocal<List<MediaType>> mediaTypesByThread = new ThreadLocal<>();
055
056    /**
057     * Not API - called by RO viewer filter.
058     */
059    private static void setMediaTypes(List<MediaType> mediaTypes) {
060        mediaTypesByThread.set(mediaTypes);
061    }
062
063    /**
064     * Not API - called by RO viewer filter.
065     */
066    private static void removeMediaTypes() {
067        mediaTypesByThread.remove();
068    }
069
070    @Override
071    public List<MediaType> getAcceptableMediaTypes() {
072        return mediaTypesByThread.get();
073    }
074
075
076    @Component
077    @Provider
078    public static class RequestFilter implements ContainerRequestFilter {
079        @Override
080        public void filter(final ContainerRequestContext requestContext) throws IOException {
081            final List<MediaType> acceptableMediaTypes = requestContext.getAcceptableMediaTypes();
082            final List<MediaType> mediaTypes = stream(acceptableMediaTypes).filter(_NullSafe::isPresent).collect(Collectors.toList());
083            setMediaTypes(mediaTypes);
084        }
085    }
086
087
088    @Component
089    @Provider
090    public static class ResponseFilter implements ContainerResponseFilter {
091        @Override
092        public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException {
093            removeMediaTypes();
094        }
095    }
096}