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
|
package eu.etaxonomy.taxeditor.httpinvoker;
|
10
|
|
11
|
import java.lang.reflect.Field;
|
12
|
import java.util.Map;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.junit.BeforeClass;
|
16
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
17
|
import org.springframework.security.core.Authentication;
|
18
|
import org.springframework.security.core.context.SecurityContextHolder;
|
19
|
import org.unitils.database.DatabaseUnitils;
|
20
|
import org.unitils.database.annotations.Transactional;
|
21
|
import org.unitils.database.util.TransactionMode;
|
22
|
import org.unitils.spring.annotation.SpringApplicationContext;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
|
25
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
26
|
import eu.etaxonomy.cdm.api.application.CdmDataChangeService;
|
27
|
import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
|
28
|
import eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
|
29
|
import eu.etaxonomy.cdm.model.permission.User;
|
30
|
import eu.etaxonomy.taxeditor.remoting.cache.ConversationalTransientEntityCacher;
|
31
|
import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
|
32
|
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
|
33
|
import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
|
34
|
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
|
35
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
36
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
37
|
import net.sf.ehcache.CacheManager;
|
38
|
|
39
|
|
40
|
/**
|
41
|
* Base class for remoting tests, responsible for
|
42
|
* - starting / stop the cdm server
|
43
|
* - running some basic connection tests
|
44
|
* - setting up the remote configuration.
|
45
|
*
|
46
|
*/
|
47
|
@Transactional(TransactionMode.DISABLED)
|
48
|
@SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
|
49
|
public abstract class BaseRemotingTest extends ThreadedTest {
|
50
|
@SuppressWarnings("unused")
|
51
|
private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
|
52
|
|
53
|
private static ICdmRemoteSource cdmRemoteSource;
|
54
|
private static CdmPersistentRemoteSource remotePersistentSource;
|
55
|
protected static ICdmEntitySessionManager cdmEntitySessionManager;
|
56
|
|
57
|
@BeforeClass
|
58
|
public static void initializeBaseRemotingTest() {
|
59
|
|
60
|
DatabaseUnitils.disableConstraints();
|
61
|
|
62
|
try {
|
63
|
initializeController(sourceName,
|
64
|
host,
|
65
|
httpPort,
|
66
|
contextPath,
|
67
|
user,
|
68
|
password);
|
69
|
} catch (Exception e) {
|
70
|
System.err.println("user: " + user + " password: " + password);
|
71
|
e.printStackTrace();
|
72
|
// Assert.fail("Server failed to start. Reason : " + e.getMessage());
|
73
|
}
|
74
|
|
75
|
|
76
|
}
|
77
|
|
78
|
public static void emptyAllCachesExceptModelCache() {
|
79
|
CacheManager cm = CacheManager.create();
|
80
|
String[] cacheNames = CacheManager.create().getCacheNames();
|
81
|
for(String cacheName : cacheNames) {
|
82
|
if(!cacheName.equals(CdmRemoteCacheManager.CDM_MODEL_CACHE_NAME)) {
|
83
|
cm.removeCache(cacheName);
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
|
88
|
|
89
|
public static void initializeController(String sourceName, String host, int port, String contextPath, String username, String password) {
|
90
|
|
91
|
if(CdmApplicationState.getCurrentAppConfig() != null) {
|
92
|
return;
|
93
|
}
|
94
|
cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath);
|
95
|
CdmApplicationRemoteController remoteApplicationController =
|
96
|
CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
|
97
|
null,
|
98
|
null);
|
99
|
CdmApplicationState.setCurrentAppConfig(remoteApplicationController);
|
100
|
CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
|
101
|
|
102
|
cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
|
103
|
|
104
|
CdmApplicationState.setCurrentDataChangeService(new CdmDataChangeService());
|
105
|
|
106
|
authenticate(username, password);
|
107
|
|
108
|
}
|
109
|
|
110
|
protected static void authenticate(String username, String password) {
|
111
|
|
112
|
//FIXME:Remoting the authentication code should be replaced by a method call which actually
|
113
|
// does the authentication in the editor code so that the 'real' authentication can be tested
|
114
|
SecurityContextHolder.clearContext();
|
115
|
|
116
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
117
|
Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
|
118
|
|
119
|
|
120
|
User user = (User) authentication.getPrincipal();
|
121
|
/* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
|
122
|
* see http://dev.e-taxonomy.eu/trac/ticket/4053 */
|
123
|
user.initAuthorities();
|
124
|
authentication = new UsernamePasswordAuthenticationToken(user,password, authentication.getAuthorities());
|
125
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
126
|
CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
|
127
|
|
128
|
|
129
|
|
130
|
|
131
|
|
132
|
// SecurityContextHolder.clearContext();
|
133
|
// SecurityContextImpl sc = new SecurityContextImpl();
|
134
|
// Authentication token = new UsernamePasswordAuthenticationToken(username,password);
|
135
|
// Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
|
136
|
//
|
137
|
// authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
|
138
|
// sc.setAuthentication(authentication);
|
139
|
//
|
140
|
// SecurityContextHolder.setContext(sc);
|
141
|
// CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
|
142
|
|
143
|
}
|
144
|
|
145
|
|
146
|
protected static CdmApplicationRemoteController getRemoteApplicationController() {
|
147
|
return (CdmApplicationRemoteController) CdmApplicationState.getCurrentAppConfig();
|
148
|
}
|
149
|
|
150
|
protected static ICdmRemoteSource getCdmRemoteSource() {
|
151
|
return cdmRemoteSource;
|
152
|
}
|
153
|
|
154
|
protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
|
155
|
return remotePersistentSource;
|
156
|
}
|
157
|
|
158
|
protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
|
159
|
return cdmEntitySessionManager;
|
160
|
}
|
161
|
|
162
|
|
163
|
protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
|
164
|
Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
|
165
|
(Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
|
166
|
return ownerSessionMap.get(sessionOwner);
|
167
|
}
|
168
|
|
169
|
protected static CdmEntitySession getActiveSession() {
|
170
|
return ((InheritableThreadLocal<CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "tlActiveSession")).get();
|
171
|
}
|
172
|
|
173
|
protected static ConversationalTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
|
174
|
return (ConversationalTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
|
175
|
}
|
176
|
|
177
|
|
178
|
protected static Object getFieldValueViaReflection(Object object, String fieldName) {
|
179
|
Class<?> clazz = object.getClass();
|
180
|
try {
|
181
|
Field field = clazz.getDeclaredField(fieldName);
|
182
|
field.setAccessible(true);
|
183
|
return field.get(object);
|
184
|
} catch (NoSuchFieldException e) {
|
185
|
e.printStackTrace();
|
186
|
} catch (SecurityException e) {
|
187
|
e.printStackTrace();
|
188
|
} catch (IllegalArgumentException e) {
|
189
|
e.printStackTrace();
|
190
|
} catch (IllegalAccessException e) {
|
191
|
e.printStackTrace();
|
192
|
}
|
193
|
return null;
|
194
|
}
|
195
|
|
196
|
|
197
|
protected static void authenticateDefaultUser() {
|
198
|
authenticate(user, password);
|
199
|
}
|
200
|
|
201
|
|
202
|
}
|