This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository observe. See http://git.codelutin.com/observe.git commit 1391c8136e6c7267f2451ac77b346f1ed29c3ef8 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Aug 21 10:50:28 2015 +0200 Debut de travail sur la partie serveur --- .../application/web/ObserveServiceInjector.java | 55 ++++++++++++++++ .../web/ObserveWebApplicationContext.java | 12 +++- .../web/ObserveWebApplicationListener.java | 24 +++---- .../application/web/ObserveWebMotionFilter.java | 33 +++------- .../application/web/ObserveWebMotionRender.java | 5 +- .../application/web/ObserveWebRequestContext.java | 77 ++++++++++++++++++++++ .../{ => v1}/ReferentialServiceController.java | 3 +- observe-application-web/src/main/resources/mapping | 7 +- .../services/dto/gson/ObserveDtoGsonSupplier.java | 53 ++++++++------- 9 files changed, 205 insertions(+), 64 deletions(-) diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveServiceInjector.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveServiceInjector.java new file mode 100644 index 0000000..63de3be --- /dev/null +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveServiceInjector.java @@ -0,0 +1,55 @@ +package fr.ird.observe.application.web; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import fr.ird.observe.services.ObserveService; +import org.debux.webmotion.server.call.Call; +import org.debux.webmotion.server.call.HttpContext; +import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler; +import org.debux.webmotion.server.mapping.Mapping; + +import java.lang.reflect.Type; + +/** + * Created on 5/23/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class ObserveServiceInjector implements ExecutorParametersInjectorHandler.Injector { + + @Override + public ObserveService getValue(Mapping m, Call call, String name, Class type, Type generic) { + + ObserveService result = null; + if (ObserveService.class.isAssignableFrom(type)) { + + HttpContext httpContext = call.getContext(); + ObserveWebRequestContext requestContext = ObserveWebRequestContext.getRequestContext(httpContext); + result = requestContext.newService(type); + + } + + return result; + + } +} diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationContext.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationContext.java index 4feff7c..cd39ff0 100644 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationContext.java +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationContext.java @@ -1,8 +1,10 @@ package fr.ird.observe.application.web; import com.google.common.base.Preconditions; +import fr.ird.observe.services.ObserveServiceApplicationContext; import fr.ird.observe.services.ObserveServiceFactory; import fr.ird.observe.services.ObserveServiceMainFactory; +import fr.ird.observe.services.dto.gson.ObserveDtoGsonSupplier; import org.debux.webmotion.server.call.HttpContext; import javax.servlet.ServletContext; @@ -10,7 +12,7 @@ import javax.servlet.ServletContext; /** * @author Tony Chemit : chemit@codelutin.com */ -public class ObserveWebApplicationContext { +public class ObserveWebApplicationContext extends ObserveServiceApplicationContext { public static final String APPLICATION_CONTEXT_PARAMETER = ObserveWebApplicationContext.class.getName(); @@ -18,6 +20,8 @@ public class ObserveWebApplicationContext { ObserveWebApplicationContext.class.getSimpleName() + " not found. You probably forgot to" + " register " + ObserveWebApplicationListener.class.getName() + " in your web.xml"; + protected ObserveDtoGsonSupplier gsonSupplier; + protected ObserveServiceFactory serviceFactory; protected static ObserveWebApplicationContext getApplicationContext(HttpContext context) { @@ -39,6 +43,8 @@ public class ObserveWebApplicationContext { public void init() { + gsonSupplier = new ObserveDtoGsonSupplier(); + //FIXME Demander a la main factory directement la factory de topia serviceFactory = ObserveServiceMainFactory.get(); @@ -72,4 +78,8 @@ public class ObserveWebApplicationContext { public ObserveServiceFactory getServiceFactory() { return serviceFactory; } + + public ObserveDtoGsonSupplier getGsonSupplier() { + return gsonSupplier; + } } diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationListener.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationListener.java index c1dd564..52f984d 100644 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationListener.java +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebApplicationListener.java @@ -2,41 +2,43 @@ package fr.ird.observe.application.web; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; +import org.debux.webmotion.server.WebMotionServerListener; +import org.debux.webmotion.server.call.ServerContext; +import org.debux.webmotion.server.mapping.Mapping; /** * @author Tony Chemit : chemit@codelutin.com */ -public class ObserveWebApplicationListener implements ServletContextListener { +public class ObserveWebApplicationListener implements WebMotionServerListener { private static final Log log = LogFactory.getLog(ObserveWebApplicationListener.class); - protected ObserveWebApplicationContext context; + protected ObserveWebApplicationContext applicationContext; @Override - public void contextInitialized(ServletContextEvent sce) { + public void onStart(Mapping mapping, ServerContext context) { + if (log.isInfoEnabled()) { log.info("Initializing " + ObserveWebApplicationListener.class.getName()); } - context = new ObserveWebApplicationContext(); - context.init(); + applicationContext = new ObserveWebApplicationContext(); + applicationContext.init(); - sce.getServletContext().setAttribute(ObserveWebApplicationContext.APPLICATION_CONTEXT_PARAMETER, context); + context.addInjector(new ObserveServiceInjector()); } @Override - public void contextDestroyed(ServletContextEvent sce) { + public void onStop(ServerContext context) { if (log.isInfoEnabled()) { log.info("Destroying " + ObserveWebApplicationListener.class.getName()); } - context.close(); + applicationContext.close(); + } } diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionFilter.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionFilter.java index 4ed6d67..1b308ec 100644 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionFilter.java +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionFilter.java @@ -1,5 +1,6 @@ package fr.ird.observe.application.web; +import fr.ird.observe.services.configuration.ObserveDataSourceConfiguration; import org.debux.webmotion.server.WebMotionFilter; import org.debux.webmotion.server.call.HttpContext; @@ -10,21 +11,8 @@ import javax.servlet.http.HttpServletRequest; */ public class ObserveWebMotionFilter extends WebMotionFilter { -// protected ObserveTopiaPersistenceContext getTopiaContext(HttpContext context) { -// HttpServletRequest servletRequest = context.getRequest(); -// ObserveTopiaPersistenceContext result = (ObserveTopiaPersistenceContext) -// servletRequest -// .getAttribute(TopiaTransactionFilter.TOPIA_TRANSACTION_REQUEST_ATTRIBUTE); -// -// Preconditions.checkState(result != null, "TopiaContext not available"); -// -// return result; -// } - public void inject(HttpContext context) { -// final ObserveTopiaPersistenceContext persistenceContext = getTopiaContext(context); - ObserveWebApplicationContext applicationContext = ObserveWebApplicationContext.getApplicationContext(context); @@ -32,22 +20,17 @@ public class ObserveWebMotionFilter extends WebMotionFilter { String authenticationToken = request.getHeader("authenticationToken"); - //TODO Recuperation de la configuration de la base à partir du token + //TODO Recuperation de la configuration de la data source de la base à partir du token + ObserveDataSourceConfiguration dataSourceConfiguration = null; - //TODO En déduire la ser + ObserveWebRequestContext requestContext = new ObserveWebRequestContext(); + requestContext.setWebApplicationContext(applicationContext); + requestContext.setDataSourceConfiguration(dataSourceConfiguration); -// DefaultServiceContext serviceContext = new DefaultServiceContext(applicationContext.getServiceConfig(), new Supplier<ObserveTopiaPersistenceContext>() { -// @Override -// public ObserveTopiaPersistenceContext get() { -// return persistenceContext; -// } -// }, authenticationToken); -// -// ServiceFactory serviceFactory = serviceContext.getServiceFactory(); -// request.setAttribute("serviceFactory", serviceFactory); + ObserveWebRequestContext.setRequestContext(context, requestContext); doProcess(); - } + } } diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionRender.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionRender.java index 8c54993..f50f873 100644 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionRender.java +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionRender.java @@ -1,5 +1,6 @@ package fr.ird.observe.application.web; +import com.google.gson.Gson; import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.call.HttpContext; import org.debux.webmotion.server.mapping.Mapping; @@ -41,7 +42,9 @@ public class ObserveWebMotionRender<T> extends Render { boolean devMode = applicationContext.isDevMode(); - ObserveWebMotionJsonHelper gson = new ObserveWebMotionJsonHelper(devMode); + applicationContext.getGsonSupplier(); + + Gson gson = applicationContext.getGsonSupplier().get(); String json = gson.toJson(model); PrintWriter out = context.getOut(); diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebRequestContext.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebRequestContext.java new file mode 100644 index 0000000..5418c73 --- /dev/null +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebRequestContext.java @@ -0,0 +1,77 @@ +package fr.ird.observe.application.web; + +/* + * #%L + * Pollen :: Rest Api + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ + +import fr.ird.observe.services.ObserveService; +import fr.ird.observe.services.ObserveServiceFactory; +import fr.ird.observe.services.configuration.ObserveDataSourceConfiguration; +import org.debux.webmotion.server.call.HttpContext; + +/** + * Created on 4/25/14. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 2.0 + */ +public class ObserveWebRequestContext { + + protected static final String REQUEST_POLLEN_REQUEST_CONTEXT = ObserveWebRequestContext.class.getName(); + + public static ObserveWebRequestContext getRequestContext(HttpContext httpContext) { + + ObserveWebRequestContext result = (ObserveWebRequestContext) + httpContext.getRequest().getAttribute(REQUEST_POLLEN_REQUEST_CONTEXT); + return result; + } + + public static void setRequestContext(HttpContext httpContext, + ObserveWebRequestContext serviceContext) { + httpContext.getRequest().setAttribute(REQUEST_POLLEN_REQUEST_CONTEXT, serviceContext); + } + + protected ObserveWebApplicationContext webApplicationContext; + + protected ObserveDataSourceConfiguration dataSourceConfiguration; + + public ObserveWebApplicationContext getWebApplicationContext() { + return webApplicationContext; + } + + public ObserveDataSourceConfiguration getDataSourceConfiguration() { + return dataSourceConfiguration; + } + + public void setWebApplicationContext(ObserveWebApplicationContext webApplicationContext) { + this.webApplicationContext = webApplicationContext; + } + + public void setDataSourceConfiguration(ObserveDataSourceConfiguration dataSourceConfiguration) { + this.dataSourceConfiguration = dataSourceConfiguration; + } + + public <S extends ObserveService> S newService(Class<S> serviceType) { + ObserveServiceFactory serviceFactory = webApplicationContext.getServiceFactory(); + S service = serviceFactory.newService(webApplicationContext, dataSourceConfiguration, serviceType); + return service; + } + +} diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/ReferentialServiceController.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/v1/ReferentialServiceController.java similarity index 94% rename from observe-application-web/src/main/java/fr/ird/observe/application/web/controller/ReferentialServiceController.java rename to observe-application-web/src/main/java/fr/ird/observe/application/web/controller/v1/ReferentialServiceController.java index ab9b4c8..df59b76 100644 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/ReferentialServiceController.java +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/v1/ReferentialServiceController.java @@ -1,5 +1,6 @@ -package fr.ird.observe.application.web.controller; +package fr.ird.observe.application.web.controller.v1; +import fr.ird.observe.application.web.controller.ObserveWebMotionController; import fr.ird.observe.services.ObserveServiceContext; import fr.ird.observe.services.dto.FormDto; import fr.ird.observe.services.dto.ReferenceSetDto; diff --git a/observe-application-web/src/main/resources/mapping b/observe-application-web/src/main/resources/mapping index 03d0384..0aa5db6 100644 --- a/observe-application-web/src/main/resources/mapping +++ b/observe-application-web/src/main/resources/mapping @@ -1,11 +1,14 @@ [config] package.base=fr.ird.observe.application.web.controller -default.render=fr.ird.observe.application.web.ObserveWebMotionRender +package.actions=fr.ird.observe.application.web.controller.v1 +package.errors=fr.ird.observe.application.web.controller.v1 + server.controller.scope=request server.listener.class=fr.ird.observe.services.rest.wm.ObserveServerListener +default.render=fr.ird.observe.application.web.ObserveWebMotionRender [filters] -* /* fr.ird.observe.application.web.ObserveWebMotionFilter.inject +* /* ObserveWebMotionFilter.inject [actions] GET,POST /api/v1/referential/{method} action:ReferentialServiceController.{method} diff --git a/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/ObserveDtoGsonSupplier.java b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/ObserveDtoGsonSupplier.java index 453f6db..0904065 100644 --- a/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/ObserveDtoGsonSupplier.java +++ b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/ObserveDtoGsonSupplier.java @@ -13,40 +13,47 @@ import java.util.Date; */ public class ObserveDtoGsonSupplier implements Supplier<Gson> { - public static final Supplier<GsonBuilder> GSON_BUILDER_SUPPLIER = new Supplier<GsonBuilder>() { + protected GsonBuilder gsonBuilder; - @Override - public GsonBuilder get() { - GsonBuilder result = new GsonBuilder(); - - // Type adapters : base types - result.registerTypeAdapter(Integer.class, new IntegerAdapter()); - result.registerTypeAdapter(Date.class, new DateAdapter()); - result.registerTypeAdapter(Timestamp.class, new DateAdapter()); - result.registerTypeAdapter(java.sql.Date.class, new DateAdapter()); - - return result; - } - }; - - protected static GsonBuilder gsonBuilder; + protected final boolean prettyPrint; protected Gson gson; - protected static GsonBuilder getGsonBuilder() { - if (gsonBuilder == null) { - // Assign only when initialization is finished to be thread-safe - gsonBuilder = GSON_BUILDER_SUPPLIER.get(); - } - return gsonBuilder; + public ObserveDtoGsonSupplier() { + this(false); + } + + public ObserveDtoGsonSupplier(boolean prettyPrint) { + this.prettyPrint = prettyPrint; } @Override public Gson get() { if (gson == null) { - gson = getGsonBuilder().create(); + gson = getGsonBuilder(prettyPrint).create(); } return gson; } + protected GsonBuilder getGsonBuilder(boolean prettyPrint) { + + if (gsonBuilder == null) { + + // Assign only when initialization is finished to be thread-safe + gsonBuilder = new GsonBuilder(); + + if (prettyPrint) { + gsonBuilder.setPrettyPrinting(); + } + // Type adapters : base types + gsonBuilder.registerTypeAdapter(Integer.class, new IntegerAdapter()); + gsonBuilder.registerTypeAdapter(Date.class, new DateAdapter()); + gsonBuilder.registerTypeAdapter(Timestamp.class, new DateAdapter()); + gsonBuilder.registerTypeAdapter(java.sql.Date.class, new DateAdapter()); + + } + return gsonBuilder; + + } + } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.