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 6d2009206aaeb9dd3981fe52148ad54327055868 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Sep 1 00:35:22 2015 +0200 Préparation du client rest --- .../application/web/ObserveWebMotionFilter.java | 39 ++++++++++++-------- .../web/request/ObserveWebRequestContext.java | 10 +---- .../services/ObserveServiceFactoryRest.java | 43 +++++++++++++++------- 3 files changed, 55 insertions(+), 37 deletions(-) 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 8678455..c54892e 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 @@ -32,7 +32,9 @@ public class ObserveWebMotionFilter extends WebMotionFilter { ObserveWebRequestSecurityContext securityContext = createSecurityContext(applicationContext, request); - ObserveWebRequestContext requestContext = createObserveWebRequestContext(applicationContext, securityContext, request); + Locale applicationLocale = getApplicationLocale(request); + ReferentialLocale referentialLocale = getReferentialLocale(request); + ObserveWebRequestContext requestContext = new ObserveWebRequestContext(applicationContext, securityContext, applicationLocale, referentialLocale); ObserveWebRequestContext.setRequestContext(context, requestContext); @@ -40,30 +42,29 @@ public class ObserveWebMotionFilter extends WebMotionFilter { } - protected ObserveWebRequestContext createObserveWebRequestContext(ObserveWebApplicationContext applicationContext, ObserveWebRequestSecurityContext securityContext, HttpServletRequest request) { - - Locale applicationLocale = null; - String applicationLocaleStr = getRequestParameterValueOrNull(request, "applicationLocale"); - if (applicationLocaleStr != null) { - applicationLocale = ConverterUtil.convert(Locale.class, applicationLocaleStr); - } - + protected ReferentialLocale getReferentialLocale(HttpServletRequest request) { ReferentialLocale referentialLocale = null; - String referentialLocaleStr = getRequestParameterValueOrNull(request, "referentialLocale"); + String referentialLocaleStr = getRequestHeaderOrParameterValueOrNull(request, "referentialLocale"); if (referentialLocaleStr != null) { Locale referentialLoca = ConverterUtil.convert(Locale.class, referentialLocaleStr); referentialLocale = ReferentialLocale.valueOf(referentialLoca); } - return new ObserveWebRequestContext(applicationContext, securityContext, applicationLocale, referentialLocale); + return referentialLocale; + } + + protected Locale getApplicationLocale(HttpServletRequest request) { + Locale applicationLocale = null; + String applicationLocaleStr = getRequestHeaderOrParameterValueOrNull(request, "applicationLocale"); + if (applicationLocaleStr != null) { + applicationLocale = ConverterUtil.convert(Locale.class, applicationLocaleStr); + } + return applicationLocale; } protected ObserveWebRequestSecurityContext createSecurityContext(ObserveWebApplicationContext applicationContext, HttpServletRequest request) throws InvalidAuthenticationTokenException, UnknownObserveWebUserForDatabaseException, BadObserveWebUserPasswordException, DataSourceConfigurationAlreadyRegistredException, UnknownObserveWebUserException { ObserveWebRequestSecurityContext securityContext = null; - String authenticationToken = getRequestParameterValueOrNull(request, "authenticationToken"); - if (authenticationToken == null) { - authenticationToken = request.getHeader("authenticationToken"); - } + String authenticationToken = getRequestHeaderOrParameterValueOrNull(request, "authenticationToken"); if (StringUtils.isNotBlank(authenticationToken)) { // Recherche de la configuration à la source de donnée @@ -94,6 +95,14 @@ public class ObserveWebMotionFilter extends WebMotionFilter { } + protected String getRequestHeaderOrParameterValueOrNull(HttpServletRequest request, String parameterName) { + String result = request.getHeader(parameterName); + if (StringUtils.isBlank(result)) { + result = getRequestParameterValueOrNull(request, parameterName); + } + return result; + } + protected String getRequestParameterValueOrNull(HttpServletRequest request, String parameterName) { String parameterValue = request.getParameter(parameterName); if (StringUtils.isBlank(parameterValue)) { diff --git a/observe-application-web/src/main/java/fr/ird/observe/application/web/request/ObserveWebRequestContext.java b/observe-application-web/src/main/java/fr/ird/observe/application/web/request/ObserveWebRequestContext.java index f72a5dd..ffecad6 100644 --- a/observe-application-web/src/main/java/fr/ird/observe/application/web/request/ObserveWebRequestContext.java +++ b/observe-application-web/src/main/java/fr/ird/observe/application/web/request/ObserveWebRequestContext.java @@ -50,14 +50,6 @@ public class ObserveWebRequestContext { this.optionalSecurityContext = Optional.fromNullable(optionalSecurityContext); } - public Locale getApplicationLocale() { - return applicationLocale; - } - - public ReferentialLocale getReferentialLocale() { - return referentialLocale; - } - public ObserveWebApplicationContext getApplicationContext() { return applicationContext; } @@ -65,12 +57,12 @@ public class ObserveWebRequestContext { public <S extends ObserveService> S newService(Class<S> serviceType) { checkSecurityContextIsPresent(); + ObserveServiceInitializerContext observeServiceInitializerContext = new ObserveServiceInitializerContext(); observeServiceInitializerContext.setApplicationLocale(applicationLocale); observeServiceInitializerContext.setReferentialLocale(referentialLocale); observeServiceInitializerContext.setDataSourceConfiguration(getSecurityContext().getDataSourceConfiguration()); - S service = applicationContext.newService(observeServiceInitializerContext, serviceType); return service; diff --git a/observe-services-rest/src/main/java/fr/ird/observe/services/ObserveServiceFactoryRest.java b/observe-services-rest/src/main/java/fr/ird/observe/services/ObserveServiceFactoryRest.java index 931cf0e..86f8043 100644 --- a/observe-services-rest/src/main/java/fr/ird/observe/services/ObserveServiceFactoryRest.java +++ b/observe-services-rest/src/main/java/fr/ird/observe/services/ObserveServiceFactoryRest.java @@ -17,7 +17,6 @@ import org.apache.commons.logging.LogFactory; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.net.URL; import java.util.Collection; /** @@ -32,6 +31,10 @@ public class ObserveServiceFactoryRest extends ObserveServiceFactorySupport { public static final String AUTHENTICATION_TOKEN = "authenticationToken"; + public static final String APPLICATION_LOCALE = "applicationLocale"; + + public static final String REFERENTIAL_LOCALE = "referentialLocale"; + protected final CachingParanamer paranamer = new CachingParanamer(); protected final Supplier<Gson> gsonSupplier = new ObserveDtoGsonSupplier(); @@ -48,14 +51,14 @@ public class ObserveServiceFactoryRest extends ObserveServiceFactorySupport { Preconditions.checkNotNull(applicationContext, "applicationContext can't be null."); Preconditions.checkNotNull(observeServiceInitializerContext, "observeServiceInitializerContext can't be null."); + Preconditions.checkNotNull(serviceType, "serviceType can't be null."); + Preconditions.checkNotNull(observeServiceInitializerContext.getApplicationLocale(), "applicationLocale can't be null."); + Preconditions.checkNotNull(observeServiceInitializerContext.getReferentialLocale(), "referentialLocale can't be null."); ObserveDataSourceConfiguration dataSourceConfiguration = observeServiceInitializerContext.getDataSourceConfiguration(); Preconditions.checkNotNull(dataSourceConfiguration, "dataSourceConfiguration can't be null."); Preconditions.checkArgument(dataSourceConfiguration instanceof ObserveDataSourceConfigurationRest, "dataSourceConfiguration must be of type ObserveRestClientDataSourceConfiguration"); - Preconditions.checkNotNull(serviceType, "serviceType can't be null."); - ObserveDataSourceConfigurationRest dataSourceConfigurationRest = (ObserveDataSourceConfigurationRest) dataSourceConfiguration; - - S service = newRemoteProxyServiceInstance(serviceType, dataSourceConfigurationRest); + S service = newRemoteProxyServiceInstance(serviceType, observeServiceInitializerContext); return service; } @@ -65,14 +68,12 @@ public class ObserveServiceFactoryRest extends ObserveServiceFactorySupport { //TODO Voir ce qu'il faut nettoyer, normalement pas grand chose } - protected <S extends ObserveService> S newRemoteProxyServiceInstance(Class<S> serviceType, ObserveDataSourceConfigurationRest dataSourceConfigurationRest) { - - String authenticationToken = dataSourceConfigurationRest.getAuthenticationToken(); - URL serverUrl = dataSourceConfigurationRest.getServerUrl(); + protected <S extends ObserveService> S newRemoteProxyServiceInstance(Class<S> serviceType, ObserveServiceInitializerContext observeServiceInitializerContext) { - RemoteInvocationHandler handler = new RemoteInvocationHandler<>(paranamer, serviceType, serverUrl, authenticationToken, gsonSupplier); + RemoteInvocationHandler handler = new RemoteInvocationHandler<>(paranamer, serviceType, observeServiceInitializerContext, gsonSupplier); S result = Reflection.newProxy(serviceType, handler); return result; + } protected static class RemoteInvocationHandler<E extends ObserveService> implements InvocationHandler { @@ -87,12 +88,22 @@ public class ObserveServiceFactoryRest extends ObserveServiceFactorySupport { protected final String serviceUrl; - public RemoteInvocationHandler(Paranamer paranamer, Class<E> serviceClass, URL serverUrl, String authenticationToken, Supplier<Gson> gsonSupplier) { + protected final String applicationLocale; + + protected final String referentialLocale; + + public RemoteInvocationHandler(Paranamer paranamer, Class<E> serviceClass, ObserveServiceInitializerContext observeServiceInitializerContext, Supplier<Gson> gsonSupplier) { this.paranamer = paranamer; this.serviceClass = serviceClass; - this.authenticationToken = authenticationToken; + + ObserveDataSourceConfigurationRest dataSourceConfiguration = (ObserveDataSourceConfigurationRest) observeServiceInitializerContext.getDataSourceConfiguration(); + this.authenticationToken = dataSourceConfiguration.getAuthenticationToken(); + this.serviceUrl = dataSourceConfiguration.getServerUrl() + "/" + serviceClass.getSimpleName(); this.gsonSupplier = gsonSupplier; - this.serviceUrl = serverUrl + "/" + serviceClass.getSimpleName(); + this.applicationLocale = observeServiceInitializerContext.getApplicationLocale().toString(); + this.referentialLocale = observeServiceInitializerContext.getReferentialLocale().toString(); + + } @Override @@ -137,6 +148,12 @@ public class ObserveServiceFactoryRest extends ObserveServiceFactorySupport { if (!Strings.isNullOrEmpty(authenticationToken)) { requestBuilder.addHeader(AUTHENTICATION_TOKEN, authenticationToken); } + if (!Strings.isNullOrEmpty(applicationLocale)) { + requestBuilder.addHeader(APPLICATION_LOCALE, applicationLocale); + } + if (!Strings.isNullOrEmpty(referentialLocale)) { + requestBuilder.addHeader(REFERENTIAL_LOCALE, referentialLocale); + } Object result; try { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.