1 package org.collectionspace.services.common.vocabulary;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
7 import javax.ws.rs.core.Response;
9 import org.collectionspace.services.client.AuthorityClient;
10 import org.collectionspace.services.client.PoxPayloadIn;
11 import org.collectionspace.services.common.ServiceMain;
12 import org.collectionspace.services.common.api.Tools;
13 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
14 import org.collectionspace.services.common.context.ServiceContext;
15 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
16 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
17 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityIdentifierUtils;
18 import org.collectionspace.services.config.service.ServiceBindingType;
19 import org.collectionspace.services.config.tenant.RemoteClientConfig;
20 import org.collectionspace.services.config.tenant.RemoteClientConfigurations;
21 import org.collectionspace.services.config.tenant.TenantBindingType;
22 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
23 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
24 import org.collectionspace.services.common.document.DocumentException;
26 //import org.dom4j.DocumentException;
27 import org.nuxeo.ecm.core.api.DocumentModel;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 @SuppressWarnings("rawtypes")
32 public class AuthorityServiceUtils {
33 private static final Logger logger = LoggerFactory.getLogger(AuthorityServiceUtils.class);
35 // Used to keep track if an authority item's is deprecated
36 public static final String DEFAULT_REMOTECLIENT_CONFIG_NAME = "default";
37 public static final String IS_DEPRECATED_PROPERTY = "IS_DEPRECATED_PROPERTY";
38 public static final Boolean DEPRECATED = true;
39 public static final Boolean NOT_DEPRECATED = !DEPRECATED;
41 // Used to keep track if an authority item's rev number should be updated
42 public static final String SHOULD_UPDATE_REV_PROPERTY = "SHOULD_UPDATE_REV_PROPERTY";
43 public static final boolean UPDATE_REV = true;
44 public static final boolean DONT_UPDATE_REV = !UPDATE_REV;
46 // Used to keep track if an authority item is a locally proposed member of a SAS authority
47 public static final String IS_PROPOSED_PROPERTY = "IS_PROPOSED";
48 public static final Boolean PROPOSED = true;
49 public static final Boolean NOT_PROPOSED = !PROPOSED;
50 public static final Boolean SAS_ITEM = true;
51 public static final Boolean NOT_SAS_ITEM = !SAS_ITEM;
53 public static final Boolean NO_CHANGE = null;
55 // Matches the domain name part of a refname. For example, "core.collectionspace.org" of
56 // urn:cspace:core.collectionspace.org:personauthorities:name(person):item:name(BigBird1461101206103)'Big Bird'.
57 public static final Pattern REFNAME_DOMAIN_PATTERN = Pattern.compile("(urn:cspace:)(([a-z]{1,}\\.?)*)");
60 * Try to find a named remote client configuration in the current tenant bindings. If the value of the incoming param 'remoteClientConfigName' is
61 * blank or null, we'll try to find a name in the authority service's bindings. If we can't find a name there, we'll try using the default name.
63 * If the incoming param 'remoteClientConfigName' is not null, we'll look through all the named remote client configurations in the tenant's binding
64 * to find the configuration. If we can't find the named configuration, we'll throw an exception.
66 * If there are no remote client configurations in the tenant's bindings, we'll throw an exception.
68 public static final RemoteClientConfig getRemoteClientConfig(ServiceContext ctx, String remoteClientConfigName) throws Exception {
69 RemoteClientConfig result = null;
71 TenantBindingType tenantBinding = ServiceMain.getInstance().getTenantBindingConfigReader().getTenantBinding(ctx.getTenantId());
72 RemoteClientConfigurations remoteClientConfigurations = tenantBinding.getRemoteClientConfigurations();
73 if (remoteClientConfigurations != null) {
74 if (Tools.isEmpty(remoteClientConfigName) == true) {
75 // Since the authority instance didn't specify a remote client config name, let's see if the authority type's service bindings specifies one
76 ServiceBindingType serviceBindingType =
77 ServiceMain.getInstance().getTenantBindingConfigReader().getServiceBinding(ctx.getTenantId(), ctx.getServiceName());
78 remoteClientConfigName = serviceBindingType.getRemoteClientConfigName();
81 // If we still don't have a remote client config name, let's use the default value.
83 if (Tools.isEmpty(remoteClientConfigName) == true) {
84 remoteClientConfigName = DEFAULT_REMOTECLIENT_CONFIG_NAME;
87 List<RemoteClientConfig> remoteClientConfigList = remoteClientConfigurations.getRemoteClientConfig();
88 for (RemoteClientConfig config : remoteClientConfigList) {
89 if (config.getName().equalsIgnoreCase(remoteClientConfigName)) {
95 String errMsg = String.format("No remote client configurations could be found in the tenant bindings for tenant named '%s'.",
98 throw new Exception(errMsg);
101 if (result == null) {
102 String errMsg = String.format("Could not find a remote client configuration named '%s' in the tenant bindings for tenant named '%s'",
103 remoteClientConfigName, ctx.getTenantName());
104 logger.error(errMsg);
105 throw new Exception(errMsg);
112 * Make a request to the SAS Server for an authority payload.
116 * @param responseType
120 public static PoxPayloadIn requestPayloadInFromRemoteServer(ServiceContext ctx, String remoteClientConfigName, Specifier specifier, Class responseType) throws Exception {
121 PoxPayloadIn result = null;
123 RemoteClientConfig remoteClientConfig = getRemoteClientConfig(ctx, remoteClientConfigName);
124 AuthorityClient client = (AuthorityClient) ctx.getClient(remoteClientConfig);
126 Response res = client.read(specifier.getURNValue());
128 int statusCode = res.getStatus();
129 if (statusCode == org.apache.commons.httpclient.HttpStatus.SC_OK) {
130 result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!
132 String errMsg = String.format("Could not retrieve authority information for '%s' on remote server '%s'. Server returned status code %d",
133 specifier.getURNValue(), remoteClientConfig.getUrl(), statusCode);
134 if (logger.isDebugEnabled()) {
135 logger.debug(errMsg);
137 throw new DocumentException(statusCode, errMsg);
147 // Makes a call to the remote SAS server for a authority item payload
149 public static PoxPayloadIn requestPayloadInFromRemoteServer(
150 AuthorityItemSpecifier specifier,
151 String remoteClientConfigName,
154 boolean syncHierarchicalRelationships) throws Exception {
155 PoxPayloadIn result = null;
157 ServiceContext authorityCtx = new MultipartServiceContextImpl(serviceName);
158 RemoteClientConfig remoteClientConfig = getRemoteClientConfig(authorityCtx, remoteClientConfigName);
159 AuthorityClient client = (AuthorityClient) authorityCtx.getClient(remoteClientConfig);
160 Response res = client.readItem(specifier.getParentSpecifier().getURNValue(), specifier.getItemSpecifier().getURNValue(),
161 AuthorityClient.INCLUDE_DELETED_ITEMS, syncHierarchicalRelationships);
164 int statusCode = res.getStatus();
165 if (statusCode == org.apache.commons.httpclient.HttpStatus.SC_OK) {
166 result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response.
168 String errMsg = String.format("Could not retrieve authority item information for '%s:%s' on remote server '%s'. Server returned status code %d",
169 specifier.getParentSpecifier().getURNValue(), specifier.getItemSpecifier().getURNValue(), remoteClientConfig.getUrl(), statusCode);
170 if (logger.isDebugEnabled()) {
171 logger.debug(errMsg);
173 throw new DocumentException(statusCode, errMsg);
182 public static boolean setAuthorityItemDeprecated(ServiceContext ctx,
183 DocumentModel docModel, String authorityItemCommonSchemaName, Boolean flag) throws Exception {
184 boolean result = false;
186 docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.DEPRECATED,
188 CoreSessionInterface repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession();
189 repoSession.saveDocument(docModel);
196 * The domain name part of refnames on a remote SAS may not match that of local refnames.
197 * Update all the payload's refnames with the local domain name.
199 public static PoxPayloadIn localizeRefNameDomains(ServiceContext ctx, PoxPayloadIn payload) throws org.dom4j.DocumentException {
200 String localDomain = ctx.getTenantName();
201 Matcher matcher = REFNAME_DOMAIN_PATTERN.matcher(payload.getXmlPayload());
202 StringBuffer localizedXmlBuffer = new StringBuffer();
204 while (matcher.find() == true) {
205 String remoteDomain = matcher.group(2);
207 if (logger.isDebugEnabled()) {
208 logger.debug("Replacing " + remoteDomain + " with " + localDomain);
211 matcher.appendReplacement(localizedXmlBuffer, matcher.group(1) + localDomain);
214 matcher.appendTail(localizedXmlBuffer);
216 if (logger.isTraceEnabled()) {
217 logger.trace(String.format("Updated payload:\n%s", localizedXmlBuffer));
220 return new PoxPayloadIn(localizedXmlBuffer.toString());
224 * Mark the authority item as deprecated.
230 public static boolean markAuthorityItemAsDeprecated(ServiceContext ctx, String authorityItemCommonSchemaName, AuthorityItemSpecifier authorityItemSpecifier) throws Exception {
231 boolean result = false;
234 DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, (CoreSessionInterface)ctx.getCurrentRepositorySession(),
235 authorityItemCommonSchemaName, authorityItemSpecifier);
236 result = setAuthorityItemDeprecated(ctx, docModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED);
237 } catch (Exception e) {
238 logger.warn(String.format("Could not mark item '%s' as deprecated.", authorityItemSpecifier.getItemSpecifier().getURNValue()), e);