All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.exhibitor.core.rest.jersey.JerseySupport Maven / Gradle / Ivy

/*
 * Copyright 2012 Netflix, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.netflix.exhibitor.core.rest.jersey;

import com.google.common.collect.Sets;
import com.netflix.exhibitor.core.rest.ClusterResource;
import com.netflix.exhibitor.core.rest.ConfigResource;
import com.netflix.exhibitor.core.rest.ExplorerResource;
import com.netflix.exhibitor.core.rest.IndexResource;
import com.netflix.exhibitor.core.rest.UIContext;
import com.netflix.exhibitor.core.rest.UIContextResolver;
import com.netflix.exhibitor.core.rest.UIResource;
import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import java.util.Set;

@SuppressWarnings("UnusedDeclaration")
public class JerseySupport
{
    public static void      addSingletons(ResourceConfig config, UIContext context)
    {
        config.getSingletons().addAll(getSingletons(context));
    }

    public static void      addClasses(ResourceConfig config)
    {
        config.getClasses().addAll(getClasses());
    }

    /**
     * Return a new Jersey config instance that correctly supplies all needed Exhibitor
     * objects
     *
     * @param context the UIContext
     * @return new config
     */
    public static DefaultResourceConfig newApplicationConfig(UIContext context)
    {
        final Set singletons = getSingletons(context);
        final Set> classes = getClasses();

        return new DefaultResourceConfig()
        {
            @Override
            public Set> getClasses()
            {
                return classes;
            }

            @Override
            public Set getSingletons()
            {
                return singletons;
            }
        };
    }

    private static Set> getClasses()
    {
        final Set>     classes = Sets.newHashSet();
        classes.add(UIResource.class);
        classes.add(IndexResource.class);
        classes.add(ExplorerResource.class);
        classes.add(ClusterResource.class);
        classes.add(ConfigResource.class);
        return classes;
    }

    private static Set getSingletons(UIContext context)
    {
        final Set singletons = Sets.newHashSet();
        singletons.add(new UIContextResolver(context));
        singletons.add(new NaturalNotationContextResolver());
        return singletons;
    }

    private JerseySupport()
    {
    }
}