001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons;
019
020import org.fcrepo.http.commons.session.HttpSession;
021import org.fcrepo.http.commons.session.SessionProvider;
022import org.glassfish.hk2.utilities.binding.AbstractBinder;
023import org.glassfish.jersey.jackson.JacksonFeature;
024import org.glassfish.jersey.logging.LoggingFeature;
025import org.glassfish.jersey.media.multipart.MultiPartFeature;
026import org.glassfish.jersey.server.ResourceConfig;
027import org.glassfish.jersey.process.internal.RequestScoped;
028
029import com.codahale.metrics.jersey2.InstrumentedResourceMethodApplicationListener;
030import com.codahale.metrics.MetricRegistry;
031
032import java.util.logging.Logger;
033
034import static org.slf4j.LoggerFactory.getLogger;
035
036/**
037 * @author cabeer
038 * @since 9/22/14
039 */
040public class FedoraApplication extends ResourceConfig {
041
042    private static final org.slf4j.Logger LOGGER = getLogger(FedoraApplication.class);
043
044    /**
045     * THIS IS OUR RESOURCE CONFIG!
046     */
047    public FedoraApplication() {
048        super();
049        packages("org.fcrepo");
050        register(new FactoryBinder());
051        register(MultiPartFeature.class);
052        register(JacksonFeature.class);
053
054        if (LOGGER.isDebugEnabled()) {
055            register(new LoggingFeature(Logger.getLogger(LoggingFeature.class.getName())));
056        }
057
058        register(new InstrumentedResourceMethodApplicationListener(new MetricRegistry()));
059    }
060
061    static class FactoryBinder extends AbstractBinder {
062
063        @Override
064        protected void configure() {
065            bindFactory(SessionProvider.class)
066                    .to(HttpSession.class)
067                    .in(RequestScoped.class);
068        }
069    }
070}