001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.spring;
022
023 import java.util.ArrayList;
024 import java.util.HashMap;
025 import java.util.List;
026 import java.util.Map;
027
028 import javax.servlet.ServletContext;
029 import javax.servlet.http.HttpServletRequest;
030 import javax.servlet.http.HttpServletResponse;
031
032 import org.granite.config.GraniteConfig;
033 import org.granite.config.flex.Channel;
034 import org.granite.config.flex.Destination;
035 import org.granite.config.flex.EndPoint;
036 import org.granite.config.flex.Factory;
037 import org.granite.config.flex.Service;
038 import org.granite.config.flex.ServicesConfig;
039 import org.granite.logging.Logger;
040 import org.granite.messaging.amf.process.AMF3MessageInterceptor;
041 import org.granite.messaging.service.ExceptionConverter;
042 import org.granite.messaging.service.security.SecurityService;
043 import org.granite.messaging.service.tide.TideComponentAnnotatedWithMatcher;
044 import org.granite.messaging.service.tide.TideComponentInstanceOfMatcher;
045 import org.granite.messaging.service.tide.TideComponentNameMatcher;
046 import org.granite.messaging.service.tide.TideComponentTypeMatcher;
047 import org.granite.messaging.webapp.AMFEndpoint;
048 import org.granite.util.XMap;
049 import org.springframework.beans.BeansException;
050 import org.springframework.beans.factory.InitializingBean;
051 import org.springframework.context.ApplicationContext;
052 import org.springframework.context.ApplicationContextAware;
053 import org.springframework.web.context.ServletContextAware;
054 import org.springframework.web.servlet.HandlerAdapter;
055 import org.springframework.web.servlet.ModelAndView;
056
057
058 public class FlexFilter implements InitializingBean, ApplicationContextAware, ServletContextAware, HandlerAdapter {
059
060 private static final Logger log = Logger.getLogger(FlexFilter.class);
061
062 private ApplicationContext context = null;
063 private ServletContext servletContext = null;
064 private GraniteConfig graniteConfig = null;
065 private ServicesConfig servicesConfig = null;
066
067 private List<String> tideRoles = null;
068 private List<String> tideAnnotations = null;
069 private List<String> tideInterfaces = null;
070 private List<String> tideNames = null;
071 private List<String> tideTypes = null;
072 private List<Class<? extends ExceptionConverter>> exceptionConverters = null;
073 private AMF3MessageInterceptor amf3MessageInterceptor;
074 private boolean tide = false;
075
076
077 public void setApplicationContext(ApplicationContext context) throws BeansException {
078 this.context = context;
079 }
080
081 public void setServletContext(ServletContext servletContext) throws BeansException {
082 this.servletContext = servletContext;
083 }
084
085 public void afterPropertiesSet() {
086 SpringGraniteConfig springGraniteConfig = context.getBeansOfType(SpringGraniteConfig.class).values().iterator().next();
087
088 this.graniteConfig = springGraniteConfig.getGraniteConfig();
089
090 Map<String, SecurityService> securityServices = context.getBeansOfType(SecurityService.class);
091 if (securityServices.size() > 1)
092 log.error("More than one Security Service bean defined");
093 else if (!securityServices.isEmpty()) {
094 log.debug("Security Service bean " + securityServices.keySet().iterator().next() + " selected");
095 SecurityService securityService = securityServices.values().iterator().next();
096 this.graniteConfig.setSecurityService(securityService);
097 }
098
099 if (tideAnnotations != null) {
100 for (String ta : tideAnnotations) {
101 try {
102 this.graniteConfig.getTideComponentMatchers().add(new TideComponentAnnotatedWithMatcher(ta, false));
103 log.debug("Enabled components annotated with %s for Tide remoting", ta);
104 }
105 catch (Exception e) {
106 log.error(e, "Could not add tide-component annotation %s", ta);
107 }
108 }
109 }
110 if (tideInterfaces != null) {
111 for (String ti : tideInterfaces) {
112 try {
113 this.graniteConfig.getTideComponentMatchers().add(new TideComponentInstanceOfMatcher(ti, false));
114 log.debug("Enabled components extending %s for Tide remoting", ti);
115 }
116 catch (Exception e) {
117 log.error(e, "Could not add tide-component interface %s", ti);
118 }
119 }
120 }
121 if (tideNames != null) {
122 for (String tn : tideNames) {
123 try {
124 this.graniteConfig.getTideComponentMatchers().add(new TideComponentNameMatcher(tn, false));
125 log.debug("Enabled components named like %s for Tide remoting", tn);
126 }
127 catch (Exception e) {
128 log.error(e, "Could not add tide-component name %s", tn);
129 }
130 }
131 }
132 if (tideTypes != null) {
133 for (String tt : tideTypes) {
134 try {
135 this.graniteConfig.getTideComponentMatchers().add(new TideComponentTypeMatcher(tt, false));
136 log.debug("Enabled components with type %s for Tide remoting", tt);
137 }
138 catch (Exception e) {
139 log.error(e, "Could not add tide-component type %s", tt);
140 }
141 }
142 }
143 if (exceptionConverters != null) {
144 for (Class<? extends ExceptionConverter> ec : exceptionConverters) {
145 this.graniteConfig.registerExceptionConverter(ec);
146 log.debug("Registered exception converter %s", ec);
147 }
148 }
149 if (amf3MessageInterceptor != null)
150 this.graniteConfig.setAmf3MessageInterceptor(amf3MessageInterceptor);
151
152 servicesConfig = springGraniteConfig.getServicesConfig();
153
154 Channel channel = servicesConfig.findChannelById("graniteamf");
155 if (channel == null) {
156 channel = new Channel("graniteamf", "mx.messaging.channels.AMFChannel",
157 new EndPoint("http://{server.name}:{server.port}/{context.root}/graniteamf/amf", "flex.messaging.endpoints.AMFEndpoint"),
158 new XMap());
159 servicesConfig.addChannel(channel);
160 }
161
162 if (tide) {
163 Factory factory = servicesConfig.findFactoryById("tide-spring-factory");
164 if (factory == null) {
165 factory = new Factory("tide-spring-factory", "org.granite.tide.spring.SpringServiceFactory", new XMap());
166 servicesConfig.addFactory(factory);
167 }
168
169 Service service = servicesConfig.findServiceById("granite-service");
170 if (service == null) {
171 service = new Service("granite-service", "flex.messaging.services.RemotingService",
172 "flex.messaging.messages.RemotingMessage", null, null, new HashMap<String, Destination>());
173 }
174 Destination destination = servicesConfig.findDestinationById("flex.messaging.messages.RemotingMessage", "spring");
175 if (destination == null) {
176 List<String> channelIds = new ArrayList<String>();
177 channelIds.add("graniteamf");
178 destination = new Destination("spring", channelIds, new XMap(), tideRoles, null, null);
179 destination.getProperties().put("factory", factory.getId());
180 destination.getProperties().put("validator-name", "tideValidator");
181 service.getDestinations().put(destination.getId(), destination);
182 servicesConfig.addService(service);
183 }
184
185 log.info("Registered Tide/Spring service factory and destination");
186 }
187 else {
188 Factory factory = servicesConfig.findFactoryById("spring-factory");
189 if (factory == null) {
190 factory = new Factory("spring-factory", "org.granite.spring.SpringServiceFactory", new XMap());
191 servicesConfig.addFactory(factory);
192 }
193
194 Service service = servicesConfig.findServiceById("granite-service");
195 if (service == null) {
196 service = new Service("granite-service", "flex.messaging.services.RemotingService",
197 "flex.messaging.messages.RemotingMessage", null, null, new HashMap<String, Destination>());
198 servicesConfig.addService(service);
199 }
200
201 servicesConfig.scan(null);
202
203 log.info("Registered Spring service factory");
204 }
205 }
206
207 public void setTideRoles(List<String> tideRoles) {
208 this.tideRoles = tideRoles;
209 }
210
211 public void setTideAnnotations(List<String> tideAnnotations) {
212 this.tideAnnotations = tideAnnotations;
213 }
214
215 public void setTideInterfaces(List<String> tideInterfaces) {
216 this.tideInterfaces = tideInterfaces;
217 }
218
219 public void setTideNames(List<String> tideNames) {
220 this.tideNames = tideNames;
221 }
222
223 public void setTideTypes(List<String> tideTypes) {
224 this.tideTypes = tideTypes;
225 }
226
227 public void setExceptionConverters(List<Class<? extends ExceptionConverter>> exceptionConverters) {
228 this.exceptionConverters = exceptionConverters;
229 }
230
231 public void setAmf3MessageInterceptor(AMF3MessageInterceptor amf3MessageInterceptor) {
232 this.amf3MessageInterceptor = amf3MessageInterceptor;
233 }
234
235 public void setTide(boolean tide) {
236 this.tide = tide;
237 }
238
239
240 public long getLastModified(HttpServletRequest request, Object handler) {
241 return -1;
242 }
243
244 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
245 AMFEndpoint.service(graniteConfig, servicesConfig, servletContext, request, response);
246 return null;
247 }
248
249 public boolean supports(Object handler) {
250 return handler instanceof FlexFilter;
251 }
252 }