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

io.gravitee.management.rest.resource.GraviteeApplication Maven / Gradle / Ivy

There is a newer version: 1.30.31
Show newest version
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.management.rest.resource;

import com.fasterxml.jackson.databind.JavaType;
import io.gravitee.common.util.Version;
import io.gravitee.management.rest.filter.MaintenanceFilter;
import io.gravitee.management.rest.filter.PermissionsFilter;
import io.gravitee.management.rest.filter.SecurityContextFilter;
import io.gravitee.management.rest.mapper.ObjectMapperResolver;
import io.gravitee.management.rest.provider.*;
import io.gravitee.management.rest.resource.auth.OAuth2AuthenticationResource;
import io.gravitee.management.rest.resource.search.SearchResource;
import io.gravitee.management.security.authentication.AuthenticationProviderManager;
import io.swagger.converter.ModelConverter;
import io.swagger.converter.ModelConverterContext;
import io.swagger.converter.ModelConverters;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
import io.swagger.models.Model;
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.Property;
import io.swagger.util.Json;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;

import javax.inject.Inject;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Iterator;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
 * @author GraviteeSource Team
 */
public class GraviteeApplication extends ResourceConfig {

    private final AuthenticationProviderManager authenticationProviderManager;

    @Inject
    public GraviteeApplication(AuthenticationProviderManager authenticationProviderManager) {
        this.authenticationProviderManager = authenticationProviderManager;

        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion(Version.RUNTIME_VERSION.MAJOR_VERSION);
        beanConfig.setResourcePackage("io.gravitee.management.rest.resource");
        beanConfig.setTitle("Gravitee.io - Rest API");
        beanConfig.setScan(true);

        ModelConverters.getInstance().addConverter(new ModelConverter() {
            @Override
            public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator chain) {
                final JavaType jType = Json.mapper().constructType(type);
                if (jType != null) {
                    final Class cls = jType.getRawClass();
                    if (Date.class.isAssignableFrom(cls)) {
                        //DateTimeProperty property =
                        //        (DateTimeProperty) chain.next().resolveProperty(type, context, annotations, chain);
                        return new LongProperty();
                    }
                }

                return chain.hasNext() ?
                        chain.next().resolveProperty(type, context, annotations, chain)
                        : null;
            }

            @Override
            public Model resolve(Type type, ModelConverterContext context, Iterator chain) {
                return chain.next().resolve(type, context, chain);
            }
        });
        register(ApisResource.class);
        register(ApplicationsResource.class);
        register(SubscriptionsResource.class);
        register(UsersResource.class);
        register(PoliciesResource.class);
        register(NotifiersResource.class);
        register(ServicesDiscoveryResource.class);
        register(FetchersResource.class);
        register(ResourcesResource.class);
        register(InstancesResource.class);
        register(CurrentUserResource.class);
        register(PlatformResource.class);
        register(ConfigurationResource.class);
        register(GroupsResource.class);
        register(PortalResource.class);
        register(AuditResource.class);
        register(SearchResource.class);
        register(MessagesResource.class);
        register(MultiPartFeature.class);

        register(ObjectMapperResolver.class);
        register(ManagementExceptionMapper.class);
        register(ConstraintValidationExceptionMapper.class);
        register(UnrecognizedPropertyExceptionMapper.class);
        register(ThrowableMapper.class);
        register(NotFoundExceptionMapper.class);
        register(NotAllowedExceptionMapper.class);
        register(BadRequestExceptionMapper.class);

        // Dynamic authentication provider endpoints
        register(OAuth2AuthenticationResource.class);

        register(SecurityContextFilter.class);
        register(PermissionsFilter.class);
        register(UriBuilderRequestFilter.class);
        register(MaintenanceFilter.class);
        register(ByteArrayOutputStreamWriter.class);
        register(JacksonFeature.class);

        register(ApiListingResource.class);
        register(SwaggerSerializers.class);

        property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
        property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy