]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
82244d93ed6c62039780720c884aabf6995e117c
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.jaxrs;
2
3 import static org.nuxeo.elasticsearch.ElasticSearchConstants.ES_ENABLED_PROPERTY;
4
5 import javax.servlet.ServletContextEvent;
6 import javax.ws.rs.core.Response;
7
8 import org.jboss.resteasy.core.Dispatcher;
9 import org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap;
10
11 import org.collectionspace.authentication.CSpaceTenant;
12 import org.collectionspace.services.account.Tenant;
13 import org.collectionspace.services.account.TenantResource;
14 import org.collectionspace.services.authorization.AuthZ;
15 import org.collectionspace.services.client.AuthorityClient;
16
17 import org.collectionspace.services.common.CSWebApplicationException;
18 import org.collectionspace.services.common.ResourceMap;
19 import org.collectionspace.services.common.ServiceMain;
20 import org.collectionspace.services.common.api.RefName;
21 import org.collectionspace.services.common.config.ConfigUtils;
22 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
23 import org.collectionspace.services.common.vocabulary.AuthorityResource;
24
25 import org.collectionspace.services.config.service.AuthorityInstanceType;
26 import org.collectionspace.services.config.service.ServiceBindingType;
27 import org.collectionspace.services.config.service.ServiceBindingType.AuthorityInstanceList;
28 import org.collectionspace.services.config.service.Term;
29 import org.collectionspace.services.config.service.TermList;
30 import org.collectionspace.services.config.tenant.TenantBindingType;
31 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
32
33 import org.nuxeo.elasticsearch.ElasticSearchComponent;
34 import org.nuxeo.elasticsearch.api.ElasticSearchService;
35 import org.nuxeo.runtime.api.Framework;
36
37 import java.lang.reflect.Constructor;
38 import java.util.Date;
39 import java.util.Hashtable;
40 import java.util.List;
41 import java.util.logging.Level;
42
43 public class CSpaceResteasyBootstrap extends ResteasyBootstrap {
44
45         java.util.logging.Logger logger = java.util.logging.Logger.getAnonymousLogger();
46         static final String RESET_AUTHORITIES_PROPERTY = "org.collectionspace.services.authorities.reset";
47         private static final String RESET_ELASTICSEARCH_INDEX_PROPERTY = "org.collectionspace.services.elasticsearch.reset";
48         private static final String QUICK_BOOT_PROPERTY = "org.collectionspace.services.quickboot";
49
50         @Override
51         public void  contextInitialized(ServletContextEvent event) {
52                 try {
53                         //
54                 // This call to super instantiates and initializes our JAX-RS application class.
55                 // The application class is org.collectionspace.services.jaxrs.CollectionSpaceJaxRsApplication.
56                 //
57                         logger.log(Level.INFO, String.format("%tc [INFO] Starting up the CollectionSpace Services' JAX-RS application.", new Date()));
58                         super.contextInitialized(event);
59                         CollectionSpaceJaxRsApplication app = (CollectionSpaceJaxRsApplication)deployment.getApplication();
60                         Dispatcher disp = deployment.getDispatcher();
61                         disp.getDefaultContextObjects().put(ResourceMap.class, app.getResourceMap());
62
63                         String quickBoot = System.getProperty(QUICK_BOOT_PROPERTY, Boolean.FALSE.toString()); // Property can be set in the tomcat/bin/setenv.sh (or setenv.bat) file
64                         if (Boolean.valueOf(quickBoot) == false) {
65                                 String resetAuthsString = System.getProperty(RESET_AUTHORITIES_PROPERTY, Boolean.FALSE.toString()); // Property can be set in the tomcat/bin/setenv.sh (or setenv.bat) file
66                                 initializeAuthorities(app.getResourceMap(), Boolean.valueOf(resetAuthsString));
67
68                                 String resetElasticsearchIndexString = System.getProperty(RESET_ELASTICSEARCH_INDEX_PROPERTY, Boolean.FALSE.toString()); // Property can be set in the tomcat/bin/setenv.sh (or setenv.bat) file
69
70                                 if (Boolean.valueOf(resetElasticsearchIndexString) == true) {
71                                         resetElasticSearchIndex();
72                                 }
73                         }
74
75                         logger.log(Level.INFO, String.format("%tc [INFO] CollectionSpace Services' JAX-RS application started.", new Date()));
76                 } catch (Exception e) {
77                         e.printStackTrace();
78                         throw new RuntimeException(e);
79                 }
80         }
81
82
83         @Override
84         public void contextDestroyed(ServletContextEvent event) {
85                 logger.log(Level.INFO, "[INFO] Shutting down the CollectionSpace Services' JAX-RS application.");
86                 //Do something if needed.
87                 logger.log(Level.INFO, "[INFO] CollectionSpace Services' JAX-RS application stopped.");
88         }
89
90         public void resetElasticSearchIndex() throws Exception {
91                 boolean isEnabled = Boolean.parseBoolean(Framework.getProperty(ES_ENABLED_PROPERTY, "true"));
92
93                 if (!isEnabled) {
94                         return;
95                 }
96
97                 ElasticSearchComponent es = (ElasticSearchComponent) Framework.getService(ElasticSearchService.class);
98
99                 for (String repositoryName : es.getRepositoryNames()) {
100                         logger.log(Level.INFO, String.format("%tc [INFO] Rebuilding Elasticsearch index for repository %s", new Date(), repositoryName));
101
102                         es.dropAndInitRepositoryIndex(repositoryName);
103                 }
104
105                 TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
106                 Hashtable<String, TenantBindingType> tenantBindingsTable = tenantBindingConfigReader.getTenantBindings(false);
107
108                 for (TenantBindingType tenantBinding : tenantBindingsTable.values()) {
109                         CSpaceTenant tenant = new CSpaceTenant(tenantBinding.getId(), tenantBinding.getName());
110
111                         AuthZ.get().login(tenant);
112
113                         for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
114                                 Boolean isElasticsearchIndexed = serviceBinding.isElasticsearchIndexed();
115                                 String servicesRepoDomainName = serviceBinding.getRepositoryDomain();
116
117                                 if (isElasticsearchIndexed && servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
118                                         String repositoryName = ConfigUtils.getRepositoryName(tenantBinding, servicesRepoDomainName);
119                                         String docType = NuxeoUtils.getTenantQualifiedDocType(tenantBinding.getId(), serviceBinding.getObject().getName());
120
121                                         logger.log(Level.INFO, String.format("%tc [INFO] Starting Elasticsearch reindexing for docType %s in repository %s", new Date(), docType, repositoryName));
122
123                                         es.runReindexingWorker(repositoryName, String.format("SELECT ecm:uuid FROM %s", docType));
124                                 }
125                         }
126                 }
127         }
128
129     /**
130      * Initialize all authorities and vocabularies defined in the service bindings.
131      * @param resourceMap
132      * @throws Exception
133      */
134     public void initializeAuthorities(ResourceMap resourceMap, boolean reset) throws Exception {
135         TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
136         Hashtable<String, TenantBindingType> tenantBindingsTable = tenantBindingConfigReader.getTenantBindings(false);
137         for (TenantBindingType tenantBindings : tenantBindingsTable.values()) {
138                         CSpaceTenant tenant = new CSpaceTenant(tenantBindings.getId(), tenantBindings.getName());
139                         if (shouldInitializeAuthorities(tenant, reset) == true) {
140                                 logger.log(Level.INFO, String.format("Initializing vocabularies and authorities of tenant '%s'.",
141                                                 tenant.getId()));
142                         for (ServiceBindingType serviceBinding : tenantBindings.getServiceBindings()) {
143                                 AuthorityInstanceList element = serviceBinding.getAuthorityInstanceList();
144                                 if (element != null && element.getAuthorityInstance() != null) {
145                                         List<AuthorityInstanceType> authorityInstanceList = element.getAuthorityInstance();
146                                         for (AuthorityInstanceType authorityInstance : authorityInstanceList) {
147                                                 try {
148                                                         initializeAuthorityInstance(resourceMap, authorityInstance, serviceBinding, tenant, reset);
149                                                 } catch (Exception e) {
150                                                         logger.log(Level.SEVERE, "Could not initialize authorities and authority terms: " + e.getMessage());
151                                                         throw e;
152                                                 }
153                                         }
154                                 }
155                         }
156                         //
157                         // If we made it this far, we've either created the tenant's authorities and terms or we've reset them.  Either way,
158                         // we should mark the isAuthoritiesInitialized field of the tenant to 'true'.
159                         //
160                         setAuthoritiesInitialized(tenant, true);
161                         }
162         }
163         }
164
165     @SuppressWarnings("rawtypes")
166         private AuthorityClient getAuthorityClient(String classname) throws Exception {
167         Class clazz = Class.forName(classname.trim());
168         Constructor co = clazz.getConstructor(null);
169         Object classInstance = co.newInstance(null);
170         return (AuthorityClient) classInstance;
171     }
172
173     private boolean shouldInitializeAuthorities(CSpaceTenant cspaceTenant, boolean reset) {
174                 AuthZ.get().login(); // login as super admin
175                 TenantResource tenantResource = new TenantResource();
176                 Tenant tenantState = tenantResource.getTenant(cspaceTenant.getId());
177
178                 //
179                 // If the tenant's authorities have been initialized and
180                 // we're not being asked to reset them, we'll return 'false'
181                 // making any changes
182                 //
183                 return tenantState.isAuthoritiesInitialized() == false || reset == true;
184     }
185
186     private void setAuthoritiesInitialized(CSpaceTenant cspaceTenant, boolean initState) {
187                 AuthZ.get().login(); // login as super admin
188                 TenantResource tenantResource = new TenantResource();
189                 Tenant tenantState = tenantResource.getTenant(cspaceTenant.getId());
190
191                 tenantState.setAuthoritiesInitialized(initState);
192                 tenantResource.updateTenant(cspaceTenant.getId(), tenantState);
193         }
194
195
196     /*
197      * Check to see if an an authority instance and its corresponding terms exist.  If not, try to create them.
198      */
199     private void initializeAuthorityInstance(ResourceMap resourceMap,
200                 AuthorityInstanceType authorityInstance,
201                 ServiceBindingType serviceBinding,
202                 CSpaceTenant cspaceTenant,
203                 boolean reset) throws Exception {
204         int status = -1;
205         Response response = null;
206                 String serviceName = serviceBinding.getName();
207
208                 AuthZ.get().login(cspaceTenant);
209                 String clientClassName = serviceBinding.getClientHandler();
210                 AuthorityClient client = getAuthorityClient(clientClassName);
211                 String authoritySpecifier = RefName.shortIdToPath(authorityInstance.getTitleRef());  // e.g., urn:cspace:name(ulan)
212
213                 //
214                 // Test to see if the authority instance exists already.
215                 //
216                 AuthorityResource authorityResource = (AuthorityResource) resourceMap.get(serviceName.toLowerCase());
217                 try {
218                         response = authorityResource.get(null, null, null, authoritySpecifier);
219                 } catch (CSWebApplicationException e) {
220                         response = e.getResponse();  // If the authority doesn't exist, we expect a 404 error
221                 }
222
223                 //
224                 // If it doesn't exist (status is not 200), then try to create the authority instance
225                 //
226                 status = response.getStatus();
227                 if (status != Response.Status.OK.getStatusCode()) {
228                         String xmlPayload = client.createAuthorityInstance(authorityInstance.getTitleRef(), authorityInstance.getTitle());
229                         response = authorityResource.createAuthority(xmlPayload);
230                         status = response.getStatus();
231                         if (status != Response.Status.CREATED.getStatusCode()) {
232                                 throw new CSWebApplicationException(response);
233                         }
234                 }
235
236                 if (status == Response.Status.OK.getStatusCode()) {
237                         logger.log(Level.FINE, String.format("Authority of type '%s' with the short ID of '%s' existed already.",
238                                         serviceName, authorityInstance.getTitleRef()));
239                 } else if (status == Response.Status.CREATED.getStatusCode()) {
240                         logger.log(Level.FINE, String.format("Created a new authority of type '%s' with the short ID of '%s'.",
241                                         serviceName, authorityInstance.getTitleRef()));
242                 } else {
243                         logger.log(Level.WARNING, String.format("Unknown status '%d' encountered when creating or fetching authority of type '%s' with the short ID of '%s'.",
244                                         serviceName, authorityInstance.getTitleRef()));
245                 }
246
247                 //
248                 // Next, try to create or verify the authority terms.
249                 //
250                 initializeAuthorityInstanceTerms(authorityResource, client, authoritySpecifier, resourceMap, authorityInstance, serviceName, cspaceTenant);
251         }
252
253     private void initializeAuthorityInstanceTerms(
254                 AuthorityResource authorityResource,
255                 AuthorityClient client,
256                 String authoritySpecifier,
257                 ResourceMap resourceMap,
258                 AuthorityInstanceType authorityInstance,
259                 String serviceName,
260                 CSpaceTenant tenant) throws Exception {
261
262         int status = -1;
263         Response response = null;
264
265         TermList termListElement = authorityInstance.getTermList();
266         if (termListElement == null) {
267                 return;
268         }
269
270         for (Term term : termListElement.getTerm()) {
271                 //
272                 // Check to see if the term already exists
273                 //
274                 try {
275                         String termSpecifier = RefName.shortIdToPath(term.getId());
276                         authorityResource.getAuthorityItem(null, null, resourceMap, authoritySpecifier, termSpecifier);
277                         status = Response.Status.OK.getStatusCode();
278                 } catch (CSWebApplicationException e) {
279                         response = e.getResponse();  // If the authority doesn't exist, we expect a 404 error
280                         status = response.getStatus();
281                 }
282
283                 //
284                 // If the term doesn't exist, create it.
285                 //
286                 if (status != Response.Status.OK.getStatusCode()) {
287                         String termShortId = term.getId();
288                         String termDisplayName = term.getContent().trim();
289                         String xmlPayload = client.createAuthorityItemInstance(termShortId, termDisplayName);
290                         try {
291                                 authorityResource.createAuthorityItem(resourceMap, null, authoritySpecifier, xmlPayload);
292                                 logger.log(Level.FINE, String.format("Tenant:%s:Created a new term '%s:%s' in the authority of type '%s' with the short ID of '%s'.",
293                                                 tenant.getName(), termDisplayName, termShortId, serviceName, authorityInstance.getTitleRef()));
294                         } catch (CSWebApplicationException e) {
295                                 response = e.getResponse();
296                                 status = response.getStatus();
297                                 if (status != Response.Status.CREATED.getStatusCode()) {
298                                         throw new CSWebApplicationException(response);
299                                 }
300                         }
301                 }
302         }
303     }
304 }