Generate metadata service without the authentication executor
[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 @Override
307 public IMetadataService getMetadataService() {
308 return (IMetadataService) getService(IMetadataService.class, "/remoting-public/metadata.service", new CommonsHttpInvokerRequestExecutor());
309 }
310
311 public static IMetadataService getMetadataService(ICdmRemoteSource remoteSource) {
312 return (IMetadataService) getService(IMetadataService.class, "/remoting-public/metadata.service", remoteSource, new CommonsHttpInvokerRequestExecutor());
313 }
314
315 @Override
316 public IGrantedAuthorityService getGrantedAuthorityService(){
317 return (IGrantedAuthorityService) getService(IGrantedAuthorityService.class, "/remoting/grantedauthority.service", new CdmServiceRequestExecutor());
318 }
319
320 @Override
321 public IService<CdmBase> getMainService(){
322 return null;
323 }
324
325
326 @Override
327 public ProviderManager getAuthenticationManager(){
328 if(authenticationManager != null) {
329 return authenticationManager;
330 }
331 Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
332 ReflectionSaltSource saltSource = new ReflectionSaltSource();
333 saltSource.setUserPropertyToUse("getUsername");
334
335 DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
336 daoAuthenticationProvider.setUserDetailsService(getUserService());
337 daoAuthenticationProvider.setSaltSource(saltSource);
338 daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
339
340 authenticationManager = new ProviderManager(Arrays.asList((AuthenticationProvider)daoAuthenticationProvider));
341 return authenticationManager;
342 }
343
344
345 @Override
346 public ConversationHolder NewConversation() {
347 return new ConversationHolderMock();
348 }
349
350 @Override
351 public ICollectionService getCollectionService(){
352 return (ICollectionService) getService(ICollectionService.class, "/remoting/collection.service", new CdmServiceRequestExecutor());
353 }
354
355 @Override
356 public IFeatureTreeService getFeatureTreeService(){
357 return (IFeatureTreeService) getService(IFeatureTreeService.class, "/remoting/featuretree.service", new CdmServiceRequestExecutor());
358 }
359
360 @Override
361 public IFeatureNodeService getFeatureNodeService(){
362 return (IFeatureNodeService) getService(IFeatureNodeService.class, "/remoting/featurenode.service", new CdmServiceRequestExecutor());
363 }
364
365 @Override
366 public IVocabularyService getVocabularyService(){
367 return (IVocabularyService) getService(IVocabularyService.class, "/remoting/vocabulary.service", new CdmServiceRequestExecutor());
368 }
369
370 @Override
371 public IIdentificationKeyService getIdentificationKeyService(){
372 return (IIdentificationKeyService) getService(IIdentificationKeyService.class, "/remoting/identificationkey.service", new CdmServiceRequestExecutor());
373 }
374
375 @Override
376 public IPolytomousKeyService getPolytomousKeyService(){
377 return (IPolytomousKeyService) getService(IPolytomousKeyService.class, "/remoting/polytomouskey.service", new CdmServiceRequestExecutor());
378 }
379
380
381 @Override
382 public IPolytomousKeyNodeService getPolytomousKeyNodeService(){
383 return (IPolytomousKeyNodeService) getService(IPolytomousKeyNodeService.class, "/remoting/polytomouskeynode.service", new CdmServiceRequestExecutor());
384 }
385
386 @Override
387 public IProgressMonitorService getProgressMonitorService() {
388 return (IProgressMonitorService) getService(IProgressMonitorService.class, "/remoting/progressmonitor.service", new CdmAuthenticatedHttpInvokerRequestExecutor());
389 }
390
391 @Override
392 public IWorkingSetService getWorkingSetService(){
393 return (IWorkingSetService) getService(IWorkingSetService.class, "/remoting/workingset.service", new CdmServiceRequestExecutor());
394 }
395
396 @Override
397 public IGroupService getGroupService(){
398 return (IGroupService) getService(IGroupService.class, "/remoting/group.service", new CdmServiceRequestExecutor());
399 }
400
401
402 @Override
403 public IEntityValidationService getEntityValidationService(){
404 return (IEntityValidationService) getService(IEntityValidationService.class, "/remoting/entityvalidation.service", new CdmServiceRequestExecutor());
405 }
406
407
408 @Override
409 public IEntityConstraintViolationService getEntityConstraintViolationService(){
410 return (IEntityConstraintViolationService) getService(IEntityConstraintViolationService.class, "/remoting/entityconstraintviolation.service", new CdmServiceRequestExecutor());
411 }
412
413 @Override
414 public ICdmPermissionEvaluator getPermissionEvaluator(){
415
416 if(cdmPermissionEvaluator != null) {
417 return cdmPermissionEvaluator;
418 }
419 List<AccessDecisionVoter> decisionVoters = new ArrayList<AccessDecisionVoter>();
420 decisionVoters.add(new GrantAlwaysVoter());
421 decisionVoters.add(new TaxonNodeVoter());
422 decisionVoters.add(new TaxonBaseVoter());
423 decisionVoters.add(new DescriptionBaseVoter());
424 decisionVoters.add(new DescriptionElementVoter());
425 UnanimousBasedUnrevokable accessDecisionManager = new UnanimousBasedUnrevokable(decisionVoters);
426
427 cdmPermissionEvaluator = new CdmPermissionEvaluator();
428 cdmPermissionEvaluator.setAccessDecisionManager(accessDecisionManager);
429
430 return cdmPermissionEvaluator;
431 }
432
433
434 @Override
435 public TransactionStatus startTransaction() throws UnsupportedOperationException {
436 throw new UnsupportedOperationException("startTransaction is not implemented for CdmApplicationRemoteConfiguration");
437 }
438
439 @Override
440 public TransactionStatus startTransaction(Boolean readOnly) throws UnsupportedOperationException {
441 throw new UnsupportedOperationException("startTransaction is not implemented for CdmApplicationRemoteConfiguration");
442 }
443
444
445 @Override
446 public void commitTransaction(TransactionStatus txStatus) throws UnsupportedOperationException {
447 throw new UnsupportedOperationException("commitTransaction is not implemented for CdmApplicationRemoteConfiguration");
448 }
449
450 @Override
451 public void authenticate(String username, String password){
452 UsernamePasswordAuthenticationToken tokenForUser = new UsernamePasswordAuthenticationToken(username, password);
453 Authentication authentication = this.getAuthenticationManager().authenticate(tokenForUser);
454 SecurityContext context = SecurityContextHolder.getContext();
455 context.setAuthentication(authentication);
456 }
457
458 public IIOService getIOService() {
459 return (IIOService) getService(IIOService.class, "/remoting/io.service", new CdmServiceRequestExecutor());
460 }
461
462 public ITestService getTestService() {
463 return (ITestService) getService(ITestService.class, "/remoting/test.service", new CdmServiceRequestExecutor());
464 }
465 }