001/* 002 * Contributed by Asaf Shakarchi <asaf000@gmail.com> 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016package org.atteo.moonshine.resteasy; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import javax.inject.Inject; 022import javax.xml.bind.annotation.XmlElement; 023import javax.xml.bind.annotation.XmlIDREF; 024import javax.xml.bind.annotation.XmlRootElement; 025 026import org.atteo.config.XmlDefaultValue; 027import org.atteo.moonshine.ServiceConfiguration; 028import org.atteo.moonshine.jaxrs.Jaxrs; 029import org.atteo.moonshine.services.ImportService; 030import org.atteo.moonshine.webserver.WebServerService; 031import org.jboss.resteasy.plugins.guice.GuiceResourceFactory; 032import org.jboss.resteasy.plugins.server.servlet.FilterDispatcher; 033import org.jboss.resteasy.spi.ResourceFactory; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037import com.google.inject.Module; 038import com.google.inject.PrivateModule; 039import com.google.inject.Singleton; 040 041/** 042 * Starts RESTEasy JAXRS implementation. 043 */ 044@XmlRootElement(name = "resteasy") 045@ServiceConfiguration(autoConfiguration = "" 046 + "<prefix>${oneof:${resteasy.prefix},${jaxrs.prefix},}</prefix>" 047 + "<discoverResources>true</discoverResources>") 048public class Resteasy extends Jaxrs { 049 Logger log = LoggerFactory.getLogger(Resteasy.class); 050 051 @XmlElement 052 @XmlIDREF 053 @ImportService 054 private org.atteo.moonshine.webserver.ServletContainer servletContainer; 055 056 @XmlElement 057 @XmlIDREF 058 @ImportService 059 private WebServerService webServer; 060 061 /** 062 * Prefix under which JAXRS resources should be registered. 063 */ 064 @XmlElement 065 @XmlDefaultValue("/") 066 private String prefix; 067 068 @Override 069 public Module configure() { 070 return new PrivateModule() { 071 @Override 072 protected void configure() { 073 Map<String, String> params = new HashMap<>(); 074 params.put("resteasy.servlet.mapping.prefix", prefix); 075 076 bind(FilterDispatcher.class).in(Singleton.class); 077 servletContainer.addFilter(getProvider(FilterDispatcher.class), params, prefix + "/*"); 078 079 registerResources(binder()); 080 registerProviders(binder()); 081 } 082 }; 083 } 084 085 @Inject 086 private FilterDispatcher filterDispatcher; 087 088 @Override 089 public void start() { 090 for (final JaxrsResource<?> resourceWithProvider : getResources()) { 091 final ResourceFactory resourceFactory = new GuiceResourceFactory( 092 new com.google.inject.Provider<Object>() { 093 @Override 094 public Object get() { 095 return resourceWithProvider.getProvider().get(); 096 } 097 }, resourceWithProvider.getResourceClass()); 098 filterDispatcher.getDispatcher().getRegistry().addResourceFactory(resourceFactory); 099 } 100 101 for (JaxrsResource<?> provider : getProviders()) { 102 filterDispatcher.getDispatcher().getProviderFactory().registerProviderInstance( 103 provider.getProvider().get()); 104 } 105 } 106}