From: remillet Date: Thu, 2 Nov 2017 22:15:40 +0000 (-0700) Subject: DRYD-177: Supports definition of authorities/vocabularies and terms in the service... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=4fa31f183083a8bc7efeb112a1a057f06c408e1f;p=tmp%2Fjakarta-migration.git DRYD-177: Supports definition of authorities/vocabularies and terms in the service bindings. --- diff --git a/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CSpaceResteasyBootstrap.java b/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CSpaceResteasyBootstrap.java index 495c2b0fa..29936c9c5 100644 --- a/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CSpaceResteasyBootstrap.java +++ b/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CSpaceResteasyBootstrap.java @@ -5,26 +5,33 @@ import javax.ws.rs.core.Response; import org.jboss.resteasy.core.Dispatcher; import org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap; + import org.collectionspace.authentication.CSpaceTenant; import org.collectionspace.services.authorization.AuthZ; -import org.collectionspace.services.client.PersonAuthorityClient; -import org.collectionspace.services.client.PersonAuthorityClientUtils; -import org.collectionspace.services.client.PoxPayloadOut; -import org.collectionspace.services.person.PersonAuthorityResource; +import org.collectionspace.services.client.AuthorityClient; +import org.collectionspace.services.common.CSWebApplicationException; import org.collectionspace.services.common.ResourceMap; import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.api.RefName; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; +import org.collectionspace.services.common.vocabulary.AuthorityResource; import org.collectionspace.services.config.service.AuthorityInstanceType; import org.collectionspace.services.config.service.ServiceBindingType; import org.collectionspace.services.config.service.ServiceBindingType.AuthorityInstanceList; +import org.collectionspace.services.config.service.Term; +import org.collectionspace.services.config.service.TermList; import org.collectionspace.services.config.tenant.TenantBindingType; +import java.lang.reflect.Constructor; import java.util.Date; import java.util.Hashtable; import java.util.List; +import java.util.logging.Level; public class CSpaceResteasyBootstrap extends ResteasyBootstrap { + java.util.logging.Logger logger = java.util.logging.Logger.getAnonymousLogger(); + @Override public void contextInitialized(ServletContextEvent event) { try { @@ -32,7 +39,7 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap { // This call to super instantiates and initializes our JAX-RS application class. // The application class is org.collectionspace.services.jaxrs.CollectionSpaceJaxRsApplication. // - System.out.println(String.format("%tc [INFO] Starting up the CollectionSpace Services' JAX-RS application.", new Date())); + logger.log(Level.INFO, String.format("%tc [INFO] Starting up the CollectionSpace Services' JAX-RS application.", new Date())); super.contextInitialized(event); CollectionSpaceJaxRsApplication app = (CollectionSpaceJaxRsApplication)deployment.getApplication(); Dispatcher disp = deployment.getDispatcher(); @@ -40,58 +47,153 @@ public class CSpaceResteasyBootstrap extends ResteasyBootstrap { initializeAuthorities(app.getResourceMap()); - System.out.println(String.format("%tc [INFO] CollectionSpace Services' JAX-RS application started.", new Date())); - } catch (Throwable e) { + logger.log(Level.INFO, String.format("%tc [INFO] CollectionSpace Services' JAX-RS application started.", new Date())); + } catch (Exception e) { e.printStackTrace(); - throw e; + throw new RuntimeException(e); } } @Override public void contextDestroyed(ServletContextEvent event) { - System.out.println("[INFO] Shutting down the CollectionSpace Services' JAX-RS application."); + logger.log(Level.INFO, "[INFO] Shutting down the CollectionSpace Services' JAX-RS application."); //Do something if needed. - System.out.println("[INFO] CollectionSpace Services' JAX-RS application stopped."); + logger.log(Level.INFO, "[INFO] CollectionSpace Services' JAX-RS application stopped."); } - public void initializeAuthorities(ResourceMap resourceMap) { + /** + * Initialize all authorities and vocabularies defined in the service bindings. + * @param resourceMap + * @throws Exception + */ + public void initializeAuthorities(ResourceMap resourceMap) throws Exception { TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader(); Hashtable tenantBindingsTable = tenantBindingConfigReader.getTenantBindings(false); for (TenantBindingType tenantBindings : tenantBindingsTable.values()) { + CSpaceTenant tenant = new CSpaceTenant(tenantBindings.getId(), tenantBindings.getName()); for (ServiceBindingType serviceBinding : tenantBindings.getServiceBindings()) { AuthorityInstanceList element = serviceBinding.getAuthorityInstanceList(); if (element != null && element.getAuthorityInstance() != null) { List authorityInstanceList = element.getAuthorityInstance(); for (AuthorityInstanceType authorityInstance : authorityInstanceList) { - CSpaceTenant tenant = new CSpaceTenant(tenantBindings.getId(), tenantBindings.getName()); - initializeAuthorityInstance(resourceMap, authorityInstance, serviceBinding.getName(), tenant); + try { + initializeAuthorityInstance(resourceMap, authorityInstance, serviceBinding, tenant); + } catch (Exception e) { + logger.log(Level.SEVERE, "Could not initialize authorities and authority terms: " + e.getMessage()); + throw e; + } } } } } } + + @SuppressWarnings("rawtypes") + private AuthorityClient getAuthorityClient(String classname) throws Exception { + Class clazz = Class.forName(classname.trim()); + Constructor co = clazz.getConstructor(null); + Object classInstance = co.newInstance(null); + return (AuthorityClient) classInstance; + } + + /* + * Check to see if an an authority instance and its corresponding terms exist. If not, try to create them. + */ + private void initializeAuthorityInstance(ResourceMap resourceMap, AuthorityInstanceType authorityInstance, ServiceBindingType serviceBinding, CSpaceTenant tenant) throws Exception { + int status = -1; + Response response = null; + String serviceName = serviceBinding.getName(); + + AuthZ.get().login(tenant); + String clientClassName = serviceBinding.getClientHandler(); + AuthorityClient client = getAuthorityClient(clientClassName); + String authoritySpecifier = RefName.shortIdToPath(authorityInstance.getTitleRef()); // e.g., urn:cspace:name(ulan) - private void initializeAuthorityInstance(ResourceMap resourceMap, AuthorityInstanceType authorityInstance, String serviceName, CSpaceTenant tenant) { - // TODO Auto-generated method stub + // + // Test to see if the authority instance exists already. + // + AuthorityResource authorityResource = (AuthorityResource) resourceMap.get(serviceName.toLowerCase()); try { - AuthZ.get().login(tenant); - PersonAuthorityClient client = new PersonAuthorityClient(); - PoxPayloadOut xmlPayloadOut = PersonAuthorityClientUtils.createPersonAuthorityInstance( - authorityInstance.getTitle(), authorityInstance.getTitleRef(), client.getCommonPartName()); - String xmlPayload = xmlPayloadOut.asXML(); - PersonAuthorityResource personAuthorityResource = (PersonAuthorityResource) resourceMap.get(serviceName.toLowerCase()); - Response response = personAuthorityResource.createAuthority(xmlPayload); - int status = response.getStatus(); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + response = authorityResource.get(null, null, authoritySpecifier); + } catch (CSWebApplicationException e) { + response = e.getResponse(); // If the authority doesn't exist, we expect a 404 error } - } - - private void initializeVocabularies() { - // TODO Auto-generated method stub + // + // If it doesn't exist (status is not 200), then try to create the authority instance + // + status = response.getStatus(); + if (status != Response.Status.OK.getStatusCode()) { + String xmlPayload = client.createAuthorityInstance(authorityInstance.getTitleRef(), authorityInstance.getTitle()); + response = authorityResource.createAuthority(xmlPayload); + status = response.getStatus(); + if (status != Response.Status.CREATED.getStatusCode()) { + throw new CSWebApplicationException(response); + } + } + + if (status == Response.Status.OK.getStatusCode()) { + logger.log(Level.FINE, String.format("Authority of type '%s' with the short ID of '%s' existed already.", + serviceName, authorityInstance.getTitleRef())); + } else if (status == Response.Status.CREATED.getStatusCode()) { + logger.log(Level.INFO, String.format("Created a new authority of type '%s' with the short ID of '%s'.", + serviceName, authorityInstance.getTitleRef())); + } else { + logger.log(Level.WARNING, String.format("Unknown status '%d' encountered when creating or fetching authority of type '%s' with the short ID of '%s'.", + serviceName, authorityInstance.getTitleRef())); + } + + // + // Finally, try to create or verify the authority terms. + // + initializeAuthorityInstanceTerms(authorityResource, client, authoritySpecifier, resourceMap, authorityInstance, serviceName, tenant); } + + private void initializeAuthorityInstanceTerms( + AuthorityResource authorityResource, + AuthorityClient client, + String authoritySpecifier, + ResourceMap resourceMap, + AuthorityInstanceType authorityInstance, + String serviceName, + CSpaceTenant tenant) throws Exception { + + int status = -1; + Response response = null; + TermList termListElement = authorityInstance.getTermList(); + for (Term term : termListElement.getTerm()) { + // + // Check to see if the term already exists + // + try { + String termSpecifier = RefName.shortIdToPath(term.getId()); + authorityResource.getAuthorityItem(null, null, resourceMap, authoritySpecifier, termSpecifier); + status = Response.Status.OK.getStatusCode(); + } catch (CSWebApplicationException e) { + response = e.getResponse(); // If the authority doesn't exist, we expect a 404 error + status = response.getStatus(); + } + + // + // If the term doesn't exist, create it. + // + if (status != Response.Status.OK.getStatusCode()) { + String termShortId = term.getId(); + String termDisplayName = term.getContent().trim(); + String xmlPayload = client.createAuthorityItemInstance(termShortId, termDisplayName); + try { + authorityResource.createAuthorityItem(resourceMap, null, authoritySpecifier, xmlPayload); + logger.log(Level.INFO, String.format("Created a new term '%s:%s' in the authority of type '%s' with the short ID of '%s'.", + termDisplayName, termShortId, serviceName, authorityInstance.getTitleRef())); + } catch (CSWebApplicationException e) { + response = e.getResponse(); + status = response.getStatus(); + if (status != Response.Status.CREATED.getStatusCode()) { + throw new CSWebApplicationException(response); + } + } + } + } + } } diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java index cbfe4f4ad..f1d81fab4 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java @@ -664,7 +664,7 @@ public abstract class AuthorityResource @Path("{csid}/items") public Response createAuthorityItem( @Context ResourceMap resourceMap, - @Context UriInfo uriInfo, + @Context UriInfo uriInfo, @PathParam("csid") String parentIdentifier, // Either a CSID or a URN form -e.g., a8ad38ec-1d7d-4bf2-bd31 or urn:cspace:name(bugsbunny) String xmlPayload) { uriInfo = new UriInfoWrapper(uriInfo); @@ -678,7 +678,7 @@ public abstract class AuthorityResource } catch (Exception e) { throw bigReThrow(e, ServiceMessages.CREATE_FAILED); } - + return result; } diff --git a/services/citation/client/src/main/java/org/collectionspace/services/client/CitationAuthorityClient.java b/services/citation/client/src/main/java/org/collectionspace/services/client/CitationAuthorityClient.java index 6bf858faa..4eb49e814 100644 --- a/services/citation/client/src/main/java/org/collectionspace/services/client/CitationAuthorityClient.java +++ b/services/citation/client/src/main/java/org/collectionspace/services/client/CitationAuthorityClient.java @@ -88,4 +88,16 @@ public class CitationAuthorityClient extends AuthorityClientImpl { * @return the string */ static protected String extractId(Response res) { - MultivaluedMap mvm = res.getMetadata(); - String uri = (String) ((List) mvm.get("Location")).get(0); - if (logger.isDebugEnabled()) { - logger.debug("extractId:uri=" + uri); - } - String[] segments = uri.split("/"); - String id = segments[segments.length - 1]; - if (logger.isDebugEnabled()) { - logger.debug("id=" + id); - } - return id; + return CollectionSpaceClientUtils.extractId(res); } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java index d6b11a094..d3348b029 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java @@ -365,7 +365,8 @@ public abstract class RemoteDocumentModelHandlerImpl } String currentUser = ctx.getUserId(); - if (currentUser.equalsIgnoreCase(AuthN.ANONYMOUS_USER) == false) { + if (currentUser.equalsIgnoreCase(AuthN.ANONYMOUS_USER) == false && + currentUser.equalsIgnoreCase(AuthN.SPRING_ADMIN_USER) == false) { addAccountPermissionsPart(); } } diff --git a/services/concept/client/src/main/java/org/collectionspace/services/client/ConceptAuthorityClient.java b/services/concept/client/src/main/java/org/collectionspace/services/client/ConceptAuthorityClient.java index 7e0643c5a..651a26ab2 100644 --- a/services/concept/client/src/main/java/org/collectionspace/services/client/ConceptAuthorityClient.java +++ b/services/concept/client/src/main/java/org/collectionspace/services/client/ConceptAuthorityClient.java @@ -89,4 +89,16 @@ public class ConceptAuthorityClient extends AuthorityClientImpl - + -