]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d1d10e173f91d823b67b4ade4a26367f41fb01a9
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.common.vocabulary;
2
3 import java.util.List;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
6
7 import javax.ws.rs.core.Response;
8
9 import org.collectionspace.services.client.AuthorityClient;
10 import org.collectionspace.services.client.CollectionSpaceClient;
11 import org.collectionspace.services.client.PoxPayloadIn;
12 import org.collectionspace.services.common.ServiceMain;
13 import org.collectionspace.services.common.api.Tools;
14 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
15 import org.collectionspace.services.common.context.ServiceContext;
16 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
17 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
18 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityIdentifierUtils;
19 import org.collectionspace.services.config.service.ServiceBindingType;
20 import org.collectionspace.services.config.tenant.RemoteClientConfig;
21 import org.collectionspace.services.config.tenant.RemoteClientConfigurations;
22 import org.collectionspace.services.config.tenant.TenantBindingType;
23 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
24 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
25 import org.dom4j.DocumentException;
26 import org.nuxeo.ecm.core.api.DocumentModel;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @SuppressWarnings("rawtypes")
31 public class AuthorityServiceUtils {
32     private static final Logger logger = LoggerFactory.getLogger(AuthorityIdentifierUtils.class);
33     //
34     // Used to keep track if an authority item's is deprecated
35     public static final String DEFAULT_REMOTECLIENT_CONFIG_NAME = "default";
36     public static final String IS_DEPRECATED_PROPERTY = "IS_DEPRECATED_PROPERTY";
37     public static final Boolean DEPRECATED = true;
38     public static final Boolean NOT_DEPRECATED = !DEPRECATED;
39     
40     // Used to keep track if an authority item's rev number should be updated
41     public static final String SHOULD_UPDATE_REV_PROPERTY = "SHOULD_UPDATE_REV_PROPERTY";
42     public static final boolean UPDATE_REV = true;
43     public static final boolean DONT_UPDATE_REV = !UPDATE_REV;
44
45     // Used to keep track if an authority item is a locally proposed member of a SAS authority
46     public static final String IS_PROPOSED_PROPERTY = "IS_PROPOSED";
47     public static final Boolean PROPOSED = true;
48     public static final Boolean NOT_PROPOSED = !PROPOSED;
49     public static final Boolean SAS_ITEM = true;
50     public static final Boolean NOT_SAS_ITEM = !SAS_ITEM;
51
52     public static final Boolean NO_CHANGE = null;
53
54     /*
55      * Try to find a named remote client configuration in the current tenant bindings.  If the value of the incoming param 'remoteClientConfigName' is
56      * 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.
57      * 
58      * If the incoming param 'remoteClientConfigName' is not null, we'll look through all the named remote client configurations in the tenant's binding
59      * to find the configuration.  If we can't find the named configuration, we'll throw an exception.
60      * 
61      * If there are no remote client configurations in the tenant's bindings, we'll throw an exception.
62      */
63         public static final RemoteClientConfig getRemoteClientConfig(ServiceContext ctx, String remoteClientConfigName) throws Exception {
64         RemoteClientConfig result = null;
65         
66         TenantBindingType tenantBinding = ServiceMain.getInstance().getTenantBindingConfigReader().getTenantBinding(ctx.getTenantId());
67         RemoteClientConfigurations remoteClientConfigurations = tenantBinding.getRemoteClientConfigurations();
68         if (remoteClientConfigurations != null) {
69                 if (Tools.isEmpty(remoteClientConfigName) == true) {
70                         // Since the authority instance didn't specify a remote client config name, let's see if the authority type's service bindings specifies one
71                         ServiceBindingType serviceBindingType =
72                                         ServiceMain.getInstance().getTenantBindingConfigReader().getServiceBinding(ctx.getTenantId(), ctx.getServiceName());
73                         remoteClientConfigName = serviceBindingType.getRemoteClientConfigName();
74                 }
75                 //
76                 // If we still don't have a remote client config name, let's use the default value.
77                 //
78                 if (Tools.isEmpty(remoteClientConfigName) == true) {
79                         remoteClientConfigName = DEFAULT_REMOTECLIENT_CONFIG_NAME;
80                 }
81                 
82                 List<RemoteClientConfig> remoteClientConfigList = remoteClientConfigurations.getRemoteClientConfig();
83                 for (RemoteClientConfig config : remoteClientConfigList) {
84                         if (config.getName().equalsIgnoreCase(remoteClientConfigName)) {
85                                 result = config;
86                                 break;
87                         }
88                 }
89         } else {
90                 String errMsg = String.format("No remote client configurations could be found in the tenant bindings for tenant named '%s'.",
91                                 ctx.getTenantName());
92                 logger.error(errMsg);
93                 throw new Exception(errMsg);
94         }
95         
96         if (result == null) {
97                 String errMsg = String.format("Could not find a remote client configuration named '%s' in the tenant bindings for tenant named '%s'",
98                                 remoteClientConfigName, ctx.getTenantName());
99                 logger.error(errMsg);
100                 throw new Exception(errMsg);
101         }
102         
103         return result;
104     }
105     
106     /**
107      * Make a request to the SAS Server for an authority payload.
108      * 
109      * @param ctx
110      * @param specifier
111      * @param responseType
112      * @return
113      * @throws Exception
114      */
115     static public PoxPayloadIn requestPayloadInFromRemoteServer(ServiceContext ctx, String remoteClientConfigName, Specifier specifier, Class responseType) throws Exception {
116         PoxPayloadIn result = null;
117         
118         RemoteClientConfig remoteClientConfig = getRemoteClientConfig(ctx, remoteClientConfigName);
119         AuthorityClient client = (AuthorityClient) ctx.getClient(remoteClientConfig);
120         
121         Response res = client.read(specifier.getURNValue());
122         try {
123                 int statusCode = res.getStatus();
124         
125                 // Check the status code of the response: does it match
126                 // the expected response(s)?
127                 if (logger.isDebugEnabled()) {
128                     logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
129                 }
130                 
131             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
132         } finally {
133                 res.close();
134         }
135         
136         return result;
137     }
138     
139     //
140     // Makes a call to the remote SAS server for a authority item payload
141     //    
142     static public PoxPayloadIn requestPayloadInFromRemoteServer(
143                 AuthorityItemSpecifier specifier, 
144                 String remoteClientConfigName, 
145                 String serviceName, 
146                 Class responseType, 
147                 boolean syncHierarchicalRelationships) throws Exception {
148         PoxPayloadIn result = null;
149         
150         ServiceContext authorityCtx = new MultipartServiceContextImpl(serviceName);
151         RemoteClientConfig remoteClientConfig = getRemoteClientConfig(authorityCtx, remoteClientConfigName);
152         AuthorityClient client = (AuthorityClient) authorityCtx.getClient(remoteClientConfig);
153         Response res = client.readItem(specifier.getParentSpecifier().getURNValue(), specifier.getItemSpecifier().getURNValue(),
154                         AuthorityClient.INCLUDE_DELETED_ITEMS, syncHierarchicalRelationships);
155         
156         try {
157                 int statusCode = res.getStatus();
158         
159                 // Check the status code of the response: does it match
160                 // the expected response(s)?
161                 if (logger.isDebugEnabled()) {
162                     logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
163                 }
164                 
165             result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!                
166         } finally {
167                 res.close();
168         }
169         
170         return result;
171     }
172     
173     static public boolean setAuthorityItemDeprecated(ServiceContext ctx,
174                 DocumentModel docModel, String authorityItemCommonSchemaName, Boolean flag) throws Exception {
175         boolean result = false;
176         
177         docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.DEPRECATED,
178                         new Boolean(flag));
179         CoreSessionInterface repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession();
180         repoSession.saveDocument(docModel);
181         result = true;
182         
183         return result;
184     }
185     
186     /*
187      * The domain name part of refnames on SAS may not match that of local refnames, so we need to update all the payload's
188      * refnames with the correct domain name
189      */
190         static public PoxPayloadIn filterRefnameDomains(ServiceContext ctx,
191                         PoxPayloadIn payload) throws DocumentException {
192                 PoxPayloadIn result = null;
193
194                 
195                 String payloadStr = payload.getXmlPayload();
196                 Pattern p = Pattern.compile("(urn:cspace:)(([a-z]{1,}\\.?)*)"); // matches the domain name part of a RefName.  For example, matches "core.collectionspace.org" of RefName urn:cspace:core.collectionspace.org:personauthorities:name(person):item:name(BigBird1461101206103)'Big Bird'
197                 Matcher m = p.matcher(payloadStr);
198
199                 StringBuffer filteredPayloadStr = new StringBuffer();
200                 while (m.find() == true) {
201                         if (logger.isDebugEnabled()) {
202                                 logger.debug("Replacing: " + m.group(2));
203                         }
204                         m.appendReplacement(filteredPayloadStr, m.group(1) + ctx.getTenantName());
205                 }
206                 m.appendTail(filteredPayloadStr);
207                 result = new PoxPayloadIn(filteredPayloadStr.toString());
208
209                 if (logger.isDebugEnabled()) {
210                         logger.debug(String.format("", filteredPayloadStr));
211                 }
212
213                 return result;
214         }
215     
216     /**
217      * Mark the authority item as deprecated.
218      * 
219      * @param ctx
220      * @param itemInfo
221      * @throws Exception
222      */
223     static public boolean markAuthorityItemAsDeprecated(ServiceContext ctx, String authorityItemCommonSchemaName, AuthorityItemSpecifier authorityItemSpecifier) throws Exception {
224         boolean result = false;
225         
226         try {
227                 DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, (CoreSessionInterface)ctx.getCurrentRepositorySession(),
228                                 authorityItemCommonSchemaName, authorityItemSpecifier);
229                 result = setAuthorityItemDeprecated(ctx, docModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED);
230         } catch (Exception e) {
231                 logger.warn(String.format("Could not mark item '%s' as deprecated.", authorityItemSpecifier.getItemSpecifier().getURNValue()), e);
232                 throw e;
233         }
234         
235         return result;
236     }
237 }