This is an automated email from the git hooks/post-receive script. New commit to branch feature/7463 in repository observe. See http://git.codelutin.com/observe.git commit e8d18506f0a0bea9029748d7cc620844f0022500 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Sep 6 11:09:03 2015 +0200 Utilisation d'un main handler et faire fonctionner la connexion à une source de données --- .../ObserveDataSourceConfigurationConverter.java | 47 ++++++++ .../web/ObserveWebApplicationListener.java | 30 +++++ .../application/web/ObserveWebMainHandler.java | 27 +++++ .../web/ObserveWebMotionJsonHelper.java | 130 --------------------- .../controller/v1/DataSourceServiceController.java | 10 +- observe-application-web/src/main/resources/mapping | 1 + 6 files changed, 110 insertions(+), 135 deletions(-) diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveDataSourceConfigurationConverter.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveDataSourceConfigurationConverter.java new file mode 100644 index 0000000..0e688ae --- /dev/null +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveDataSourceConfigurationConverter.java @@ -0,0 +1,47 @@ +package fr.ird.observe.application.web; + +import com.google.gson.Gson; +import fr.ird.observe.services.configuration.ObserveDataSourceConfiguration; +import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationTopiaPG; +import fr.ird.observe.services.dto.gson.ObserveDtoGsonSupplier; +import org.apache.commons.beanutils.converters.AbstractConverter; + +/** + * Created on 05/09/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ObserveDataSourceConfigurationConverter extends AbstractConverter { + + protected final Gson gson; + + public ObserveDataSourceConfigurationConverter(boolean devMode) { + gson = new ObserveDtoGsonSupplier(devMode).get(); + } + + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + + String stringValue; + + if (value instanceof String) { + + stringValue = (String) value; + + } else { + + stringValue = ((String[]) value)[0]; + + } + + T result = (T) gson.fromJson(stringValue, ObserveDataSourceConfigurationTopiaPG.class); + + return result; + + } + + @Override + protected Class<?> getDefaultType() { + return ObserveDataSourceConfiguration.class; + } +} 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 1f87b1c..f25db9d 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 @@ -1,11 +1,17 @@ package fr.ird.observe.application.web; +import fr.ird.observe.application.web.request.ObserveWebRequestContext; +import fr.ird.observe.services.configuration.ObserveDataSourceConfiguration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.debux.webmotion.server.WebMotionServerListener; +import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.call.ServerContext; +import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler; import org.debux.webmotion.server.mapping.Mapping; +import java.lang.reflect.Type; + /** * @author Tony Chemit : chemit@codelutin.com */ @@ -29,6 +35,30 @@ public class ObserveWebApplicationListener implements WebMotionServerListener { throw new ObserveWebApplicationContextInitException("Impossible d'initialiser le context applicatif", e); } + context.addInjector(new ExecutorParametersInjectorHandler.Injector() { + + @Override + public Object getValue(Mapping mapping, Call call, String name, Class<?> type, Type generic) { + + ObserveDataSourceConfiguration dataSourceConfiguration = null; + if (type.equals(ObserveDataSourceConfiguration.class)) { + + // on recupère la configuration de source de données depuis le requestContext + ObserveWebRequestContext requestContext = ObserveWebRequestContext.getRequestContext(call.getContext()); + + dataSourceConfiguration = requestContext.getDataSourceConfiguration(); + + if (log.isInfoEnabled()) { + log.info("Inject dataSourceConfiguration: " + dataSourceConfiguration); + } + + } + + return dataSourceConfiguration; + + } + }); + context.getServletContext().setAttribute( ObserveWebApplicationContext.APPLICATION_CONTEXT_PARAMETER, applicationContext); diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMainHandler.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMainHandler.java new file mode 100644 index 0000000..b94820b --- /dev/null +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMainHandler.java @@ -0,0 +1,27 @@ +package fr.ird.observe.application.web; + +import org.debux.webmotion.server.WebMotionHandler; +import org.debux.webmotion.server.WebMotionMainHandler; +import org.debux.webmotion.server.handler.ExecutorInstanceCreatorHandler; +import org.debux.webmotion.server.handler.ExecutorParametersConvertorHandler; +import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler; +import org.debux.webmotion.server.tools.OrderedList; + +/** + * On utilise un main handler pour supprimer la validation proposée par hibernate car ça ne fonctionne pas! + * + * Created on 05/09/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ObserveWebMainHandler extends WebMotionMainHandler { + + @Override + public OrderedList<Class<? extends WebMotionHandler>> getExecutorHandlers() { + return OrderedList.asList( + ExecutorInstanceCreatorHandler.class, + ExecutorParametersInjectorHandler.class, + ExecutorParametersConvertorHandler.class + ); + } +} diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionJsonHelper.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionJsonHelper.java deleted file mode 100644 index ca01d38..0000000 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/ObserveWebMotionJsonHelper.java +++ /dev/null @@ -1,130 +0,0 @@ -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 com.google.gson.ExclusionStrategy; -import com.google.gson.FieldAttributes; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonParseException; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.Type; -import java.util.Date; - -/** - * Created on 5/24/14. - * - * @author Tony Chemit - chemit@codelutin.com - * @since 2.0 - */ -public class ObserveWebMotionJsonHelper { - - private final Gson gson; - - public ObserveWebMotionJsonHelper(boolean prettyPrint) { - - GsonBuilder gsonBuilder = new GsonBuilder(); - - gsonBuilder.setExclusionStrategies(new ExclusionStrategy() { - - @Override - public boolean shouldSkipField(FieldAttributes f) { - return false; - } - - @Override - public boolean shouldSkipClass(Class<?> clazz) { - return clazz == Class.class; - } - }); - - gsonBuilder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { - - @Override - public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { - - JsonElement result; - - if (src == null) { - - result = JsonNull.INSTANCE; - - } else { - - result = new JsonPrimitive(src.getTime()); - - } - - return result; - - } - - }); - - gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { - - @Override - public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - return new Date(json.getAsLong()); - } - - }); - - if (prettyPrint) { - - gsonBuilder.setPrettyPrinting(); - - } - - this.gson = gsonBuilder.create(); - - } - - public String toJson(Object model) { - - String json = gson.toJson(model); - return json; - - } - - public <O> O fromJson(String json, Class<O> type) { - - O o = gson.fromJson(json, type); - return o; - - } - - public <O> O fromJson(String json, Type type) { - - O o = gson.fromJson(json, type); - return o; - - } - -} diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/v1/DataSourceServiceController.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/v1/DataSourceServiceController.java index 8d79e93..28a2972 100644 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/v1/DataSourceServiceController.java +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/controller/v1/DataSourceServiceController.java @@ -30,23 +30,23 @@ public class DataSourceServiceController extends ObserveServiceControllerSupport } @Override - public ObserveDataSourceConnection create(ObserveDataSourceConfiguration dataSourceConfiguration, DataSourceCreateConfigurationDto dataSourceCreateConfiguration) throws IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException { + public ObserveDataSourceConnectionRest create(ObserveDataSourceConfiguration dataSourceConfiguration, DataSourceCreateConfigurationDto dataSourceCreateConfiguration) throws IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException { ObserveDataSourceConnection observeDataSourceConnection = service.create(dataSourceConfiguration, dataSourceCreateConfiguration); String authenticationToken = registerDataSourceConfiguration(); - ObserveDataSourceConnection dataSourceConnection = createDataSourceConnection(observeDataSourceConnection, authenticationToken); + ObserveDataSourceConnectionRest dataSourceConnection = createDataSourceConnection(observeDataSourceConnection, authenticationToken); return dataSourceConnection; } @Override - public ObserveDataSourceConnection open(ObserveDataSourceConfiguration dataSourceConfiguration) throws DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException { + public ObserveDataSourceConnectionRest open(ObserveDataSourceConfiguration dataSourceConfiguration) throws DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException { ObserveDataSourceConnection observeDataSourceConnection = service.open(dataSourceConfiguration); String authenticationToken = registerDataSourceConfiguration(); - ObserveDataSourceConnection dataSourceConnection = createDataSourceConnection(observeDataSourceConnection, authenticationToken); + ObserveDataSourceConnectionRest dataSourceConnection = createDataSourceConnection(observeDataSourceConnection, authenticationToken); return dataSourceConnection; } @@ -91,7 +91,7 @@ public class DataSourceServiceController extends ObserveServiceControllerSupport } - protected ObserveDataSourceConnection createDataSourceConnection(ObserveDataSourceConnection observeDataSourceConnection, String authenticationToken) { + protected ObserveDataSourceConnectionRest createDataSourceConnection(ObserveDataSourceConnection observeDataSourceConnection, String authenticationToken) { ObserveDataSourceConnectionRest dataSourceConnection = new ObserveDataSourceConnectionRest( getApplicationConfiguration().getApiUrl(), diff --git a/observe-application-web/src/main/resources/mapping b/observe-application-web/src/main/resources/mapping index b5936f8..60d2791 100644 --- a/observe-application-web/src/main/resources/mapping +++ b/observe-application-web/src/main/resources/mapping @@ -31,6 +31,7 @@ package.filters=fr.ird.observe.application.web package.actions=fr.ird.observe.application.web.controller.v1 package.errors=fr.ird.observe.application.web.controller.v1 +server.main.handler.class=fr.ird.observe.application.web.ObserveWebMainHandler server.controller.scope=request server.listener.class=fr.ird.observe.application.web.ObserveWebApplicationListener default.render=fr.ird.observe.application.web.ObserveWebMotionRender -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.