001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * 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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.web.jaxrs;
020
021import javax.ws.rs.core.Application;
022import javax.ws.rs.core.Feature;
023import javax.ws.rs.core.FeatureContext;
024import javax.ws.rs.ext.Provider;
025
026
027/**
028 * Shiro JAX-RS feature which includes {@link ExceptionMapper}, {@link SubjectPrincipalRequestFilter}, and
029 * {@link ShiroAnnotationFilterFeature}.
030 *
031 * Typically a JAX-RS {@link Application} class will include this Feature class in the
032 * classes returned from {@link Application#getClasses()} method, for example:
033 * <blockquote><pre>
034 *     public class SampleApplication extends Application {
035 *
036 *         @Override
037 *         public Set<Class<?>> getClasses() {
038 *             Set<Class<?>> classes = new HashSet<Class<?>>();
039 *
040 *             // register Shiro
041 *             classes.add(ShiroFeature.class);
042 *             ...
043 *             return classes;
044 *         }
045 *     }
046 * </pre></blockquote>
047 * @since 1.4
048 */
049@Provider // NOTE: Apache CXF requires this annotation on this feature (jersey and resteasy do not)
050public class ShiroFeature implements Feature {
051
052    @Override
053    public boolean configure(FeatureContext context) {
054
055        context.register(ExceptionMapper.class);
056        context.register(SubjectPrincipalRequestFilter.class);
057        context.register(ShiroAnnotationFilterFeature.class);
058
059        return true;
060    }
061}