47757c9863f8fc05198f383250ff08ddeeb690a9
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationRemoteConfiguration.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.api.application;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.apache.log4j.Logger;
19 import org.springframework.beans.BeansException;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.ApplicationContextAware;
22 import org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor;
23 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
24 import org.springframework.security.access.AccessDecisionVoter;
25 import org.springframework.security.authentication.AuthenticationProvider;
26 import org.springframework.security.authentication.ProviderManager;
27 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
28 import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
29 import org.springframework.security.authentication.dao.ReflectionSaltSource;
30 import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
31 import org.springframework.security.core.Authentication;
32 import org.springframework.security.core.context.SecurityContext;
33 import org.springframework.security.core.context.SecurityContextHolder;
34 import org.springframework.stereotype.Component;
35 import org.springframework.transaction.PlatformTransactionManager;
36 import org.springframework.transaction.TransactionStatus;
37
38 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
39 import eu.etaxonomy.cdm.api.conversation.ConversationHolderMock;
40 import eu.etaxonomy.cdm.api.service.IAgentService;
41 import eu.etaxonomy.cdm.api.service.IAnnotationService;
42 import eu.etaxonomy.cdm.api.service.IClassificationService;
43 import eu.etaxonomy.cdm.api.service.ICollectionService;
44 import eu.etaxonomy.cdm.api.service.ICommonService;
45 import eu.etaxonomy.cdm.api.service.IDatabaseService;
46 import eu.etaxonomy.cdm.api.service.IDescriptionService;
47 import eu.etaxonomy.cdm.api.service.IEntityConstraintViolationService;
48 import eu.etaxonomy.cdm.api.service.IEntityValidationService;
49 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
50 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
51 import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
52 import eu.etaxonomy.cdm.api.service.IGroupService;
53 import eu.etaxonomy.cdm.api.service.IIdentificationKeyService;
54 import eu.etaxonomy.cdm.api.service.ILocationService;
55 import eu.etaxonomy.cdm.api.service.IMediaService;
56 import eu.etaxonomy.cdm.api.service.IMetadataService;
57 import eu.etaxonomy.cdm.api.service.INameService;
58 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
59 import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
60 import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
61 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
62 import eu.etaxonomy.cdm.api.service.IReferenceService;
63 import eu.etaxonomy.cdm.api.service.IService;
64 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
65 import eu.etaxonomy.cdm.api.service.ITaxonService;
66 import eu.etaxonomy.cdm.api.service.ITermService;
67 import eu.etaxonomy.cdm.api.service.ITestService;
68 import eu.etaxonomy.cdm.api.service.IUserService;
69 import eu.etaxonomy.cdm.api.service.IVocabularyService;
70 import eu.etaxonomy.cdm.api.service.IWorkingSetService;
71 import eu.etaxonomy.cdm.api.service.molecular.IAmplificationService;
72 import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
73 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
74 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
75 import eu.etaxonomy.cdm.io.service.IIOService;
76 import eu.etaxonomy.cdm.model.common.CdmBase;
77 import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmPermissionEvaluator;
78 import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
79 import eu.etaxonomy.cdm.persistence.hibernate.permission.UnanimousBasedUnrevokable;
80 import eu.etaxonomy.cdm.persistence.hibernate.permission.voter.DescriptionBaseVoter;
81 import eu.etaxonomy.cdm.persistence.hibernate.permission.voter.DescriptionElementVoter;
82 import eu.etaxonomy.cdm.persistence.hibernate.permission.voter.GrantAlwaysVoter;
83 import eu.etaxonomy.cdm.persistence.hibernate.permission.voter.TaxonBaseVoter;
84 import eu.etaxonomy.cdm.persistence.hibernate.permission.voter.TaxonNodeVoter;
85 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
86 import eu.etaxonomy.taxeditor.service.CachedCommonServiceImpl;
87 import eu.etaxonomy.taxeditor.service.CdmAuthenticatedHttpInvokerRequestExecutor;
88 import eu.etaxonomy.taxeditor.service.CdmServiceRequestExecutor;
89 import eu.etaxonomy.taxeditor.service.ICachedCommonService;
90 import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
91 import eu.etaxonomy.taxeditor.session.CdmEntitySessionManager;
92 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
93
94
95 /**
96 * CDM Application Configuration class which manages the configuration for remoting
97 * clients
98 *
99 */
100 @Component
101 public class CdmApplicationRemoteConfiguration implements ICdmApplicationConfiguration, ApplicationContextAware {
102
103 @SuppressWarnings("unused")
104 private static final Logger logger = Logger.getLogger(CdmApplicationRemoteConfiguration.class);
105
106 protected ApplicationContext applicationContext;
107
108 private ICdmRemoteSource remoteSource;
109
110 private Map serviceMap = new HashMap<Class<IService>, IService>();
111
112 private ICdmEntitySessionManager cdmEntitySessionManager;
113
114 private CdmPermissionEvaluator cdmPermissionEvaluator;
115
116 private ProviderManager authenticationManager;
117
118 private ICachedCommonService cachedCommonService;
119
120 public CdmApplicationRemoteConfiguration() {}
121
122 public CdmApplicationRemoteConfiguration(ICdmRemoteSource remoteSource) {
123 this.remoteSource = remoteSource;
124
125 }
126
127 public void setRemoteSource(ICdmRemoteSource remoteSource) {
128 this.remoteSource = remoteSource;
129 }
130
131 private Object getService(Class<?> clazz, String serviceSuffix, CommonsHttpInvokerRequestExecutor executor) {
132 if(serviceMap.containsKey(clazz)) {
133 return serviceMap.get(clazz);
134 }
135 Object service = getService(clazz, serviceSuffix, remoteSource, executor);
136 serviceMap.put(clazz, service);
137 return service;
138 }
139
140 public static Object getService(Class<?> clazz,
141 String serviceSuffix,
142 ICdmRemoteSource remoteSource,
143 CommonsHttpInvokerRequestExecutor executor) {
144
145 String baseUrl;
146 if(remoteSource.getContextPath() == null || remoteSource.getContextPath().equals("")) {
147 baseUrl = "http://" + remoteSource.getServer() + ":" + String.valueOf(remoteSource.getPort());
148 } else {
149 baseUrl = "http://" + remoteSource.getServer() + ":" + String.valueOf(remoteSource.getPort()) + "/" + remoteSource.getContextPath();
150 }
151 HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
152 proxy.setServiceInterface(clazz);
153 proxy.setServiceUrl(baseUrl + serviceSuffix);
154 if(executor != null) {
155 executor.setReadTimeout(0);
156 proxy.setHttpInvokerRequestExecutor(executor);
157 }
158 proxy.afterPropertiesSet();
159 return proxy.getObject();
160 }
161
162 // ****************************** APPLICATION CONTEXT *************************************************/
163
164 @Override
165 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
166 this.applicationContext = applicationContext;
167 }
168
169 /* (non-Javadoc)
170 * @see eu.etaxonomy.cdm.api.application.CdmApplicationDefaultConfiguration#getTransactionManager()
171 */
172 @Override
173 public PlatformTransactionManager getTransactionManager() throws UnsupportedOperationException {
174 throw new UnsupportedOperationException("getTransactionManager is not implemented for CdmApplicationRemoteConfiguration");
175 }
176
177
178
179 /**
180 * @return
181 */
182 public IEditGeoService getEditGeoService() {
183 return (IEditGeoService) getService(IEditGeoService.class, "/remoting/editgeo.service", new CdmServiceRequestExecutor());
184 }
185
186 /**
187 * @return
188 */
189 public ICachedCommonService getCachedCommonService(){
190 if(cachedCommonService == null) {
191 cachedCommonService = new CachedCommonServiceImpl();
192 }
193 return cachedCommonService;
194 }
195
196
197 public ICdmEntitySessionManager getCdmEntitySessionManager() {
198 if(cdmEntitySessionManager == null) {
199 cdmEntitySessionManager = new CdmEntitySessionManager();
200 }
201 return cdmEntitySessionManager;
202 }
203
204
205
206
207
208
209 // ****************************** GETTER *************************************************/
210
211 @Override
212 public final Object getBean(String name){
213 throw new UnsupportedOperationException("getBean is not implemented for CdmApplicationRemoteConfiguration");
214 }
215
216 @Override
217 public IAgentService getAgentService(){
218 return (IAgentService) getService(IAgentService.class, "/remoting/agent.service", new CdmServiceRequestExecutor());
219 }
220
221 @Override
222 public IAnnotationService getAnnotationService(){
223 return (IAnnotationService) getService(IAnnotationService.class, "/remoting/annotation.service", new CdmServiceRequestExecutor());
224 }
225
226 @Override
227 public IDatabaseService getDatabaseService(){
228 return (IDatabaseService) getService(IDatabaseService.class, "/remoting/database.service", new CdmServiceRequestExecutor());
229 }
230
231 @Override
232 public INameService getNameService(){
233 return (INameService) getService(INameService.class, "/remoting/name.service", new CdmServiceRequestExecutor());
234 }
235
236 @Override
237 public IReferenceService getReferenceService(){
238 return (IReferenceService) getService(IReferenceService.class, "/remoting/reference.service", new CdmServiceRequestExecutor());
239 }
240
241 @Override
242 public ITaxonService getTaxonService(){
243 return (ITaxonService) getService(ITaxonService.class, "/remoting/taxon.service", new CdmServiceRequestExecutor());
244 }
245
246 @Override
247 public IClassificationService getClassificationService(){
248 return (IClassificationService) getService(IClassificationService.class, "/remoting/classification.service", new CdmServiceRequestExecutor());
249 }
250
251 @Override
252 public ITaxonNodeService getTaxonNodeService(){
253 return (ITaxonNodeService) getService(ITaxonNodeService.class, "/remoting/taxonnode.service", new CdmServiceRequestExecutor());
254 }
255
256 @Override
257 public IDescriptionService getDescriptionService(){
258 return (IDescriptionService) getService(IDescriptionService.class, "/remoting/description.service", new CdmServiceRequestExecutor());
259 }
260
261 @Override
262 public IOccurrenceService getOccurrenceService(){
263 return (IOccurrenceService) getService(IOccurrenceService.class, "/remoting/occurrence.service", new CdmServiceRequestExecutor());
264 }
265
266 @Override
267 public IPrimerService getPrimerService(){
268 return (IPrimerService) getService(IPrimerService.class, "/remoting/primer.service", new CdmServiceRequestExecutor());
269 }
270
271 @Override
272 public IAmplificationService getAmplificationService(){
273 return (IAmplificationService) getService(IAmplificationService.class, "/remoting/amplification.service", new CdmServiceRequestExecutor());
274 }
275
276 @Override
277 public ISequenceService getSequenceService(){
278 return (ISequenceService) getService(ISequenceService.class, "/remoting/sequence.service", new CdmServiceRequestExecutor());
279 }
280
281 @Override
282 public IMediaService getMediaService(){
283 return (IMediaService) getService(IMediaService.class, "/remoting/media.service", new CdmServiceRequestExecutor());
284 }
285
286 @Override
287 public ITermService getTermService(){
288 return (ITermService) getService(ITermService.class, "/remoting/term.service", new TermServiceRequestExecutor());
289 }
290
291 @Override
292 public ICommonService getCommonService(){
293 return (ICommonService) getService(ICommonService.class, "/remoting/common.service", new CdmServiceRequestExecutor());
294 }
295
296 @Override
297 public ILocationService getLocationService(){
298 return (ILocationService) getService(ILocationService.class, "/remoting/location.service", new CdmServiceRequestExecutor());
299 }
300
301 @Override
302 public IUserService getUserService(){
303 return (IUserService) getService(IUserService.class, "/remoting-public/user.service", new CdmServiceRequestExecutor());
304 }
305
306
307 public static IUserService getUserService(ICdmRemoteSource remoteSource) {
308 return (IUserService) getService(IUserService.class, "/remoting-public/user.service", remoteSource, new CommonsHttpInvokerRequestExecutor());
309 }
310
311 @Override
312 public IMetadataService getMetadataService() {
313 return (IMetadataService) getService(IMetadataService.class, "/remoting-public/metadata.service", new CommonsHttpInvokerRequestExecutor());
314 }
315
316 public static IMetadataService getMetadataService(ICdmRemoteSource remoteSource) {
317 return (IMetadataService) getService(IMetadataService.class, "/remoting-public/metadata.service", remoteSource, new CommonsHttpInvokerRequestExecutor());
318 }
319
320 @Override
321 public IGrantedAuthorityService getGrantedAuthorityService(){
322 return (IGrantedAuthorityService) getService(IGrantedAuthorityService.class, "/remoting/grantedauthority.service", new CdmServiceRequestExecutor());
323 }
324
325 @Override
326 public IService<CdmBase> getMainService(){
327 return null;
328 }
329
330
331 @Override
332 public ProviderManager getAuthenticationManager(){
333 if(authenticationManager != null) {
334 return authenticationManager;
335 }
336
337 authenticationManager = getAuthenticationManager(getUserService());
338 return authenticationManager;
339 }
340
341 public static ProviderManager getAuthenticationManager(IUserService userService) {
342 Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
343 ReflectionSaltSource saltSource = new ReflectionSaltSource();
344 saltSource.setUserPropertyToUse("getUsername");
345
346 DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
347 daoAuthenticationProvider.setUserDetailsService(userService);
348 daoAuthenticationProvider.setSaltSource(saltSource);
349 daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
350
351 return new ProviderManager(Arrays.asList((AuthenticationProvider)daoAuthenticationProvider));
352 }
353
354
355 @Override
356 public ConversationHolder NewConversation() {
357 return new ConversationHolderMock();
358 }
359
360 @Override
361 public ICollectionService getCollectionService(){
362 return (ICollectionService) getService(ICollectionService.class, "/remoting/collection.service", new CdmServiceRequestExecutor());
363 }
364
365 @Override
366 public IFeatureTreeService getFeatureTreeService(){
367 return (IFeatureTreeService) getService(IFeatureTreeService.class, "/remoting/featuretree.service", new CdmServiceRequestExecutor());
368 }
369
370 @Override
371 public IFeatureNodeService getFeatureNodeService(){
372 return (IFeatureNodeService) getService(IFeatureNodeService.class, "/remoting/featurenode.service", new CdmServiceRequestExecutor());
373 }
374
375 @Override
376 public IVocabularyService getVocabularyService(){
377 return (IVocabularyService) getService(IVocabularyService.class, "/remoting/vocabulary.service", new CdmServiceRequestExecutor());
378 }
379
380 @Override
381 public IIdentificationKeyService getIdentificationKeyService(){
382 return (IIdentificationKeyService) getService(IIdentificationKeyService.class, "/remoting/identificationkey.service", new CdmServiceRequestExecutor());
383 }
384
385 @Override
386 public IPolytomousKeyService getPolytomousKeyService(){
387 return (IPolytomousKeyService) getService(IPolytomousKeyService.class, "/remoting/polytomouskey.service", new CdmServiceRequestExecutor());
388 }
389
390
391 @Override
392 public IPolytomousKeyNodeService getPolytomousKeyNodeService(){
393 return (IPolytomousKeyNodeService) getService(IPolytomousKeyNodeService.class, "/remoting/polytomouskeynode.service", new CdmServiceRequestExecutor());
394 }
395
396 @Override
397 public IProgressMonitorService getProgressMonitorService() {
398 return (IProgressMonitorService) getService(IProgressMonitorService.class, "/remoting/progressmonitor.service", new CdmAuthenticatedHttpInvokerRequestExecutor());
399 }
400
401 @Override
402 public IWorkingSetService getWorkingSetService(){
403 return (IWorkingSetService) getService(IWorkingSetService.class, "/remoting/workingset.service", new CdmServiceRequestExecutor());
404 }
405
406 @Override
407 public IGroupService getGroupService(){
408 return (IGroupService) getService(IGroupService.class, "/remoting/group.service", new CdmServiceRequestExecutor());
409 }
410
411
412 @Override
413 public IEntityValidationService getEntityValidationService(){
414 return (IEntityValidationService) getService(IEntityValidationService.class, "/remoting/entityvalidation.service", new CdmServiceRequestExecutor());
415 }
416
417
418 @Override
419 public IEntityConstraintViolationService getEntityConstraintViolationService(){
420 return (IEntityConstraintViolationService) getService(IEntityConstraintViolationService.class, "/remoting/entityconstraintviolation.service", new CdmServiceRequestExecutor());
421 }
422
423 @Override
424 public ICdmPermissionEvaluator getPermissionEvaluator(){
425
426 if(cdmPermissionEvaluator != null) {
427 return cdmPermissionEvaluator;
428 }
429 List<AccessDecisionVoter> decisionVoters = new ArrayList<AccessDecisionVoter>();
430 decisionVoters.add(new GrantAlwaysVoter());
431 decisionVoters.add(new TaxonNodeVoter());
432 decisionVoters.add(new TaxonBaseVoter());
433 decisionVoters.add(new DescriptionBaseVoter());
434 decisionVoters.add(new DescriptionElementVoter());
435 UnanimousBasedUnrevokable accessDecisionManager = new UnanimousBasedUnrevokable(decisionVoters);
436
437 cdmPermissionEvaluator = new CdmPermissionEvaluator();
438 cdmPermissionEvaluator.setAccessDecisionManager(accessDecisionManager);
439
440 return cdmPermissionEvaluator;
441 }
442
443
444 @Override
445 public TransactionStatus startTransaction() throws UnsupportedOperationException {
446 throw new UnsupportedOperationException("startTransaction is not implemented for CdmApplicationRemoteConfiguration");
447 }
448
449 @Override
450 public TransactionStatus startTransaction(Boolean readOnly) throws UnsupportedOperationException {
451 throw new UnsupportedOperationException("startTransaction is not implemented for CdmApplicationRemoteConfiguration");
452 }
453
454
455 @Override
456 public void commitTransaction(TransactionStatus txStatus) throws UnsupportedOperationException {
457 throw new UnsupportedOperationException("commitTransaction is not implemented for CdmApplicationRemoteConfiguration");
458 }
459
460 @Override
461 public void authenticate(String username, String password){
462 UsernamePasswordAuthenticationToken tokenForUser = new UsernamePasswordAuthenticationToken(username, password);
463 Authentication authentication = this.getAuthenticationManager().authenticate(tokenForUser);
464 SecurityContext context = SecurityContextHolder.getContext();
465 context.setAuthentication(authentication);
466 }
467
468 public IIOService getIOService() {
469 return (IIOService) getService(IIOService.class, "/remoting/io.service", new CdmServiceRequestExecutor());
470 }
471
472 public ITestService getTestService() {
473 return (ITestService) getService(ITestService.class, "/remoting/test.service", new CdmServiceRequestExecutor());
474 }
475 }