Search in sources :

Example 1 with AcceptHeaderByFileSuffixFilter

use of org.jboss.resteasy.core.AcceptHeaderByFileSuffixFilter in project sofa-rpc by sofastack.

the class SofaResteasyDeployment method start.

@Override
public void start() {
    // this allows each WAR to have their own set of providers
    if (providerFactory == null) {
        providerFactory = new ResteasyProviderFactory();
    }
    providerFactory.setRegisterBuiltins(registerBuiltin);
    if (deploymentSensitiveFactoryEnabled) {
        // and still be able to call ResteasyProviderFactory.getInstance()
        if (!(providerFactory instanceof ThreadLocalResteasyProviderFactory)) {
            if (ResteasyProviderFactory.peekInstance() == null || !(ResteasyProviderFactory.peekInstance() instanceof ThreadLocalResteasyProviderFactory)) {
                threadLocalProviderFactory = new ThreadLocalResteasyProviderFactory(providerFactory);
                ResteasyProviderFactory.setInstance(threadLocalProviderFactory);
            }
        }
    } else {
        ResteasyProviderFactory.setInstance(providerFactory);
    }
    if (asyncJobServiceEnabled) {
        AsynchronousDispatcher asyncDispatcher = new AsynchronousDispatcher(providerFactory);
        asyncDispatcher.setMaxCacheSize(asyncJobServiceMaxJobResults);
        asyncDispatcher.setMaxWaitMilliSeconds(asyncJobServiceMaxWait);
        asyncDispatcher.setThreadPoolSize(asyncJobServiceThreadPoolSize);
        asyncDispatcher.setBasePath(asyncJobServiceBasePath);
        asyncDispatcher.getUnwrappedExceptions().addAll(unwrappedExceptions);
        dispatcher = asyncDispatcher;
        asyncDispatcher.start();
    } else {
        // CHANGE: 只改了这里
        SynchronousDispatcher dis = new SofaSynchronousDispatcher(providerFactory);
        dis.getUnwrappedExceptions().addAll(unwrappedExceptions);
        dispatcher = dis;
    }
    registry = dispatcher.getRegistry();
    if (widerRequestMatching) {
        ((ResourceMethodRegistry) registry).setWiderMatching(widerRequestMatching);
    }
    dispatcher.getDefaultContextObjects().putAll(defaultContextObjects);
    dispatcher.getDefaultContextObjects().put(Configurable.class, providerFactory);
    dispatcher.getDefaultContextObjects().put(Providers.class, providerFactory);
    dispatcher.getDefaultContextObjects().put(Registry.class, registry);
    dispatcher.getDefaultContextObjects().put(Dispatcher.class, dispatcher);
    dispatcher.getDefaultContextObjects().put(InternalDispatcher.class, InternalDispatcher.getInstance());
    // push context data so we can inject it
    Map contextDataMap = ResteasyProviderFactory.getContextDataMap();
    contextDataMap.putAll(dispatcher.getDefaultContextObjects());
    try {
        if (injectorFactoryClass != null) {
            InjectorFactory injectorFactory;
            try {
                Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(injectorFactoryClass);
                injectorFactory = (InjectorFactory) clazz.newInstance();
            } catch (ClassNotFoundException cnfe) {
                throw new RuntimeException("Unable to find InjectorFactory implementation.", cnfe);
            } catch (Exception e) {
                throw new RuntimeException("Unable to instantiate InjectorFactory implementation.", e);
            }
            providerFactory.setInjectorFactory(injectorFactory);
        }
        // see ResteasyContextParameters.RESTEASY_CONTEXT_OBJECTS
        if (constructedDefaultContextObjects != null && constructedDefaultContextObjects.size() > 0) {
            for (Map.Entry<String, String> entry : constructedDefaultContextObjects.entrySet()) {
                Class<?> key = null;
                try {
                    key = Thread.currentThread().getContextClassLoader().loadClass(entry.getKey());
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException("Unable to instantiate context object " + entry.getKey(), e);
                }
                Object obj = createFromInjectorFactory(entry.getValue(), providerFactory);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Creating context object <" + entry.getKey() + " : " + entry.getValue() + ">");
                }
                defaultContextObjects.put(key, obj);
                dispatcher.getDefaultContextObjects().put(key, obj);
                contextDataMap.put(key, obj);
            }
        }
        if (interceptorPrecedences != null) {
            for (String precedence : interceptorPrecedences) {
                providerFactory.appendInterceptorPrecedence(precedence.trim());
            }
        }
        if (interceptorBeforePrecedences != null) {
            for (Map.Entry<String, String> ext : interceptorBeforePrecedences.entrySet()) {
                providerFactory.insertInterceptorPrecedenceBefore(ext.getKey().trim(), ext.getValue().trim());
            }
        }
        if (interceptorAfterPrecedences != null) {
            for (Map.Entry<String, String> ext : interceptorAfterPrecedences.entrySet()) {
                providerFactory.insertInterceptorPrecedenceAfter(ext.getKey().trim(), ext.getValue().trim());
            }
        }
        if (securityEnabled) {
            providerFactory.register(RoleBasedSecurityFeature.class);
        }
        if (registerBuiltin) {
            providerFactory.setRegisterBuiltins(true);
            RegisterBuiltin.register(providerFactory);
            // having problems using form parameters from container for a couple of TCK tests.  I couldn't figure out
            // why, specifically:
            // com/sun/ts/tests/jaxrs/spec/provider/standardhaspriority/JAXRSClient.java#readWriteMapProviderTest_from_standalone                                               Failed. Test case throws exception: [JAXRSCommonClient] null failed!  Check output for cause of failure.
            // com/sun/ts/tests/jaxrs/spec/provider/standardwithjaxrsclient/JAXRSClient.java#mapElementProviderTest_from_standalone                                             Failed. Test case throws exception: returned MultivaluedMap is null
            providerFactory.registerProviderInstance(new ServerFormUrlEncodedProvider(useContainerFormParams), null, null, true);
        } else {
            providerFactory.setRegisterBuiltins(false);
        }
        if (applicationClass != null) {
            application = createApplication(applicationClass, dispatcher, providerFactory);
        }
        // register all providers
        registration();
        if (paramMapping != null) {
            providerFactory.getContainerRequestFilterRegistry().registerSingleton(new AcceptParameterHttpPreprocessor(paramMapping));
        }
        AcceptHeaderByFileSuffixFilter suffixNegotiationFilter = null;
        if (mediaTypeMappings != null) {
            Map<String, MediaType> extMap = new HashMap<String, MediaType>();
            for (Map.Entry<String, String> ext : mediaTypeMappings.entrySet()) {
                String value = ext.getValue();
                extMap.put(ext.getKey().trim(), MediaType.valueOf(value.trim()));
            }
            if (suffixNegotiationFilter == null) {
                suffixNegotiationFilter = new AcceptHeaderByFileSuffixFilter();
                providerFactory.getContainerRequestFilterRegistry().registerSingleton(suffixNegotiationFilter);
            }
            suffixNegotiationFilter.setMediaTypeMappings(extMap);
        }
        if (languageExtensions != null) {
            if (suffixNegotiationFilter == null) {
                suffixNegotiationFilter = new AcceptHeaderByFileSuffixFilter();
                providerFactory.getContainerRequestFilterRegistry().registerSingleton(suffixNegotiationFilter);
            }
            suffixNegotiationFilter.setLanguageMappings(languageExtensions);
        }
    } finally {
        ResteasyProviderFactory.removeContextDataLevel();
    }
}
Also used : AcceptHeaderByFileSuffixFilter(org.jboss.resteasy.core.AcceptHeaderByFileSuffixFilter) AcceptParameterHttpPreprocessor(org.jboss.resteasy.core.AcceptParameterHttpPreprocessor) HashMap(java.util.HashMap) ResourceMethodRegistry(org.jboss.resteasy.core.ResourceMethodRegistry) SynchronousDispatcher(org.jboss.resteasy.core.SynchronousDispatcher) InjectorFactory(org.jboss.resteasy.spi.InjectorFactory) AsynchronousDispatcher(org.jboss.resteasy.core.AsynchronousDispatcher) ThreadLocalResteasyProviderFactory(org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory) MediaType(javax.ws.rs.core.MediaType) ThreadLocalResteasyProviderFactory(org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) HashMap(java.util.HashMap) Map(java.util.Map) ServerFormUrlEncodedProvider(org.jboss.resteasy.plugins.providers.ServerFormUrlEncodedProvider)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 MediaType (javax.ws.rs.core.MediaType)1 AcceptHeaderByFileSuffixFilter (org.jboss.resteasy.core.AcceptHeaderByFileSuffixFilter)1 AcceptParameterHttpPreprocessor (org.jboss.resteasy.core.AcceptParameterHttpPreprocessor)1 AsynchronousDispatcher (org.jboss.resteasy.core.AsynchronousDispatcher)1 ResourceMethodRegistry (org.jboss.resteasy.core.ResourceMethodRegistry)1 SynchronousDispatcher (org.jboss.resteasy.core.SynchronousDispatcher)1 ThreadLocalResteasyProviderFactory (org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory)1 ServerFormUrlEncodedProvider (org.jboss.resteasy.plugins.providers.ServerFormUrlEncodedProvider)1 InjectorFactory (org.jboss.resteasy.spi.InjectorFactory)1 ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)1