Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Gyrex » Cannot set the request filter to my JaxRsApplication
icon5.gif  Cannot set the request filter to my JaxRsApplication [message #1703085] Tue, 28 July 2015 14:00 Go to next message
Sean Zhang is currently offline Sean ZhangFriend
Messages: 20
Registered: July 2015
Location: China
Junior Member

I have my own jaxrs application which extend JaxRsApplication, and I try to add customized container request filter, but It cannot work. Could you please help me to find out my mistake?

package com.octech.mamalianmeng.httpservice.application.base;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.Application;

import org.eclipse.gyrex.context.IRuntimeContext;
import org.eclipse.gyrex.http.jaxrs.JaxRsApplication;

import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.sun.jersey.spi.container.ContainerResponseFilter;

public abstract class AbstractMumUnionJaxRsApplication extends JaxRsApplication {

	public AbstractMumUnionJaxRsApplication(String id, IRuntimeContext context) {
		super(id, context);
	}

	protected Application createJaxRsApplication() {
		Application application = super.createJaxRsApplication();
		if (application.getClass().getName().equals(DefaultResourceConfig.class.getName())) {
			DefaultResourceConfig defaultResourceConfig = (DefaultResourceConfig) application;
			this.setContainerRequestFilters(defaultResourceConfig);
			this.setContainerResponseFilters(defaultResourceConfig);
			return defaultResourceConfig;
		} else {
			return application;
		}
	}
	
	/**
	 * 设置请求过滤器
	 * @param defaultResourceConfig
	 */
	private final void setContainerRequestFilters(DefaultResourceConfig defaultResourceConfig) {
		if (defaultResourceConfig == null) {
			return;
		}
		List<Class<?>> containerFilters = this.getContainerRequestFilters();
		if (containerFilters != null && containerFilters.size() > 0) {
			List<String> filters = new ArrayList<String>();
			if (defaultResourceConfig.getProperties().containsKey(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS)) {
				filters.add((String) defaultResourceConfig.getProperty(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS));
			}
			for (int i = 0; i < containerFilters.size(); i++) {
				if (ContainerRequestFilter.class.isAssignableFrom(containerFilters.get(i))) {
					filters.add(containerFilters.get(i).getName());
				}
			}
			defaultResourceConfig.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, filters.toArray());
		}
	}
	
	/**
	 * 设置相应过滤器
	 * @param defaultResourceConfig
	 */
	private final void setContainerResponseFilters(DefaultResourceConfig defaultResourceConfig) {
		if (defaultResourceConfig == null) {
			return ;
		}
		List<Class<?>> containerFilters = this.getContainerResponseFilters();
		if (containerFilters != null && containerFilters.size() > 0) {
			List<String> filters = new ArrayList<String>();
			if (defaultResourceConfig.getProperties().containsKey(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS)) {
				filters.add((String) defaultResourceConfig.getProperty(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS));
			}
			for(int i = 0; i < containerFilters.size(); i++) {
				if (ContainerResponseFilter.class.isAssignableFrom(containerFilters.get(i))) {
					filters.add(containerFilters.get(i).getName());
				}
			}
			defaultResourceConfig.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, filters.toArray());
		}
	}
	
	/**
	 * 返回请求过滤器的类数组,filter的执行顺序以加入list中的顺序为准
	 * @return
	 */
	protected abstract List<Class<?>> getContainerRequestFilters();
	
	/**
	 * 返回相应过滤器的类数组,filter的执行顺序以加入list中的顺序为准
	 * @return
	 */
	protected abstract List<Class<?>> getContainerResponseFilters();
}



package com.octech.mamalianmeng.httpservice.application;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.gyrex.context.IRuntimeContext;

import com.octech.mamalianmeng.httpservice.application.base.AbstractMumUnionJaxRsApplication;
import com.octech.mamalianmeng.httpservice.application.filter.AuthorizationFilter;
import com.octech.mamalianmeng.httpservice.resource.OrderServiceResource;

public class DefaultMumUnionJaxRsApplication extends AbstractMumUnionJaxRsApplication {

	public DefaultMumUnionJaxRsApplication(String id, IRuntimeContext context) {
		super(id, context);
	}

	@Override
	protected List<Class<?>> getContainerRequestFilters() {
		List<Class<?>> filters = new ArrayList<Class<?>>();
		filters.add(AuthorizationFilter.class);
		return filters;
	}

	@Override
	protected List<Class<?>> getContainerResponseFilters() {
		return Collections.emptyList();
	}
	
	@Override
	protected Set<Class<?>> getJaxRsClasses() {
		Set<Class<?>> classes = new HashSet<Class<?>>();
		classes.add(OrderServiceResource.class);
		return classes;
	}
}


Study every day, and love every day!
Re: Cannot set the request filter to my JaxRsApplication [message #1703087 is a reply to message #1703085] Tue, 28 July 2015 14:02 Go to previous messageGo to next message
Sean Zhang is currently offline Sean ZhangFriend
Messages: 20
Registered: July 2015
Location: China
Junior Member

I add my request filter 'AuthorizationFilter' in DefaultMumUnionJaxRsApplication.

Thanks


Study every day, and love every day!
Re: Cannot set the request filter to my JaxRsApplication [message #1703101 is a reply to message #1703085] Tue, 28 July 2015 14:27 Go to previous messageGo to next message
Gunnar Wagenknecht is currently offline Gunnar WagenknechtFriend
Messages: 486
Registered: July 2009
Location: San Francisco ✈ Germany
Senior Member

On 2015-07-28 14:00:26 +0000, Sean Zhang said:

> I have my own jaxrs application which extend JaxRsApplication, and I
> try to add customized container request filter, but It cannot work.
> Could you please help me to find out my mistake?

I wonder if you could simply provide them as @Provider annotated
classes and use the bundle scanning feature. Is that possible with
filters?

-Gunnar

--
Gunnar Wagenknecht
gunnar@xxxxxxxx
Re: Cannot set the request filter to my JaxRsApplication [message #1703309 is a reply to message #1703087] Thu, 30 July 2015 13:29 Go to previous message
Tomas Tamošaitis is currently offline Tomas TamošaitisFriend
Messages: 2
Registered: October 2012
Junior Member
Hi,
I had the same issue.
My soltion was
public class JaxRsAppExt extends JaxRsApplication {

    private ContextServiceInjectableProvider contextServiceInjector;
    private InjectServiceInjectableProvider injectServiceInjector;
    private final Bundle bundle;

    public JaxRsAppExt(final String id, final IRuntimeContext context, final Bundle bundle) {
	super(id, context);
	this.bundle = bundle;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Application createJaxRsApplication() {
	final Application app = super.createJaxRsApplication();
	if (null != this.contextServiceInjector) {
	    app.getSingletons().add(this.contextServiceInjector);
	}
	if (null != this.injectServiceInjector) {
	    app.getSingletons().add(this.injectServiceInjector);
	}
	((ResourceConfig) app).getContainerRequestFilters().add(0, APIAuthFilter.class);
	((ResourceConfig) app).getContainerResponseFilters().add(0, APIAuthFilter.class);
	((ResourceConfig) app).getResourceFilterFactories().add(0, RolesAllowedResourceFilterFactory.class);
	return app;
    }

    @Override
    protected void doDestroy() {
	try {
	    // call super
	    super.doDestroy();
	} finally {
	    if (null != this.contextServiceInjector) {
		this.contextServiceInjector.dispose();
		this.contextServiceInjector = null;
	    }
	    if (null != this.injectServiceInjector) {
		this.injectServiceInjector.dispose();
		this.injectServiceInjector = null;
	    }
	}
    }

    @Override
    protected void doInit() throws IllegalStateException, Exception {
	// create service injector
	this.contextServiceInjector = new ContextServiceInjectableProvider(this.bundle.getBundleContext());
	this.injectServiceInjector = new InjectServiceInjectableProvider(this.bundle.getBundleContext());
	// call super
	super.doInit();
    }

    @Override
    protected Set<Class<?>> getJaxRsClasses() {
	return new BundleScanningResourceConfig(this.bundle).getClasses();
    }
}

Then you adding over porperties ir

[Updated on: Thu, 30 July 2015 13:31]

Report message to a moderator

Previous Topic:About the mounts of the JAX-RS demo
Next Topic:Why the created job cannot be started automatically.
Goto Forum:
  


Current Time: Fri Nov 16 16:06:50 GMT 2018

Powered by FUDForum. Page generated in 0.02394 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top