PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/sip-presence/xdm/server/core/appusage-management/src/main/java/org/mobicents/xdm/server/appusage/AppUsage.java

http://mobicents.googlecode.com/
Java | 429 lines | 173 code | 31 blank | 225 comment | 6 complexity | 90a2601dc884e9121879e3349a16657c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.xdm.server.appusage;
  23. import java.io.IOException;
  24. import javax.xml.transform.dom.DOMSource;
  25. import javax.xml.validation.Validator;
  26. import org.openxdm.xcap.common.error.ConstraintFailureConflictException;
  27. import org.openxdm.xcap.common.error.InternalServerErrorException;
  28. import org.openxdm.xcap.common.error.NotAuthorizedRequestException;
  29. import org.openxdm.xcap.common.error.SchemaValidationErrorConflictException;
  30. import org.openxdm.xcap.common.error.UniquenessFailureConflictException;
  31. import org.openxdm.xcap.common.uri.AttributeSelector;
  32. import org.openxdm.xcap.common.uri.DocumentSelector;
  33. import org.openxdm.xcap.common.uri.ElementSelector;
  34. import org.openxdm.xcap.common.uri.NodeSelector;
  35. import org.w3c.dom.Document;
  36. import org.w3c.dom.Element;
  37. import org.w3c.dom.Node;
  38. import org.xml.sax.SAXException;
  39. /**
  40. * Each XCAP resource on a server is associated with an application. In order
  41. * for an application to use those resources, application specific conventions
  42. * must be specified. Those conventions include the XML schema that defines the
  43. * structure and constraints of the data, well known URIs to bootstrap access to
  44. * the data, and so on. All of those application specific conventions are
  45. * defined by the application usage.
  46. *
  47. * @author Eduardo Martins
  48. *
  49. */
  50. public abstract class AppUsage {
  51. /**
  52. * Each application usage is associated with a name, called an Application
  53. * Unique ID (AUID). This name uniquely identifies the application usage
  54. * within the namespace of application usages, and is different from AUIDs
  55. * used by other applications.
  56. */
  57. private String auid = null;
  58. /**
  59. * All application usages MUST define a namespace URI that represents the
  60. * default document namespace to be used when evaluating URIs. The default
  61. * document namespace does not apply to elements or attributes within the
  62. * documents themselves - it applies only to the evaluation of URIs within
  63. * that application usage. Indeed, the term 'default document namespace' is
  64. * distinct from the term 'default namespace'. The latter has the standard
  65. * meaning within XML documents, and the former refers to the default used
  66. * in evaluation of XCAP URIs. XCAP does not change in any way the
  67. * mechanisms for determining the default namespace within XML documents.
  68. * However, if a document contains a URI representing an XCAP resource, the
  69. * default document namespace defined by the application usage applies to
  70. * that URI as well.
  71. */
  72. private String defaultDocumentNamespace = null;
  73. /**
  74. * The application usage MUST also identify the MIME type for documents
  75. * compliant to that schema.
  76. */
  77. private String mimetype = null;
  78. private Validator uniquenessSchemaValidator = null;
  79. /**
  80. * All application usages MUST describe their document contents using XML
  81. * schema. Here we have an appropriate validator.
  82. */
  83. private Validator schemaValidator = null;
  84. /**
  85. * By default, each user is able to access (read, modify, and delete) all of
  86. * the documents below their home directory, and any user is able to read
  87. * documents within the global directory. However, only trusted users,
  88. * explicitly provisioned into the server, can modify global documents. The
  89. * application usage can specify a different authorization policy that
  90. * applies to all documents associated with that application usage.
  91. */
  92. private AuthorizationPolicy authorizationPolicy;
  93. public AppUsage(String auid, String defaultDocumentNamespace,
  94. String mimetype, Validator schemaValidator,
  95. String authorizedUserDocumentName) {
  96. this.auid = auid;
  97. this.defaultDocumentNamespace = defaultDocumentNamespace;
  98. this.mimetype = mimetype;
  99. this.schemaValidator = schemaValidator;
  100. authorizationPolicy = new DefaultAuthorizationPolicy(
  101. authorizedUserDocumentName);
  102. }
  103. public AppUsage(String auid, String defaultDocumentNamespace,
  104. String mimetype, Validator schemaValidator,
  105. AuthorizationPolicy authorizationPolicy) {
  106. this.auid = auid;
  107. this.defaultDocumentNamespace = defaultDocumentNamespace;
  108. this.mimetype = mimetype;
  109. this.schemaValidator = schemaValidator;
  110. this.authorizationPolicy = authorizationPolicy;
  111. }
  112. public AppUsage(String auid, String defaultDocumentNamespace,
  113. String mimetype, Validator schemaValidator,
  114. Validator uniquenessSchemaValidator,
  115. String authorizedUserDocumentName) {
  116. this.auid = auid;
  117. this.defaultDocumentNamespace = defaultDocumentNamespace;
  118. this.mimetype = mimetype;
  119. this.schemaValidator = schemaValidator;
  120. this.uniquenessSchemaValidator = uniquenessSchemaValidator;
  121. authorizationPolicy = new DefaultAuthorizationPolicy(
  122. authorizedUserDocumentName);
  123. }
  124. public AppUsage(String auid, String defaultDocumentNamespace,
  125. String mimetype, Validator schemaValidator,
  126. Validator uniquenessSchemaValidator,
  127. AuthorizationPolicy authorizationPolicy) {
  128. this.auid = auid;
  129. this.defaultDocumentNamespace = defaultDocumentNamespace;
  130. this.mimetype = mimetype;
  131. this.schemaValidator = schemaValidator;
  132. this.uniquenessSchemaValidator = uniquenessSchemaValidator;
  133. this.authorizationPolicy = authorizationPolicy;
  134. }
  135. public String getDefaultDocumentNamespace() {
  136. return defaultDocumentNamespace;
  137. }
  138. public String getAUID() {
  139. return auid;
  140. }
  141. public String getMimetype() {
  142. return mimetype;
  143. }
  144. public void validateSchema(Document document)
  145. throws SchemaValidationErrorConflictException,
  146. InternalServerErrorException {
  147. // check arg
  148. if (document == null) {
  149. throw new IllegalArgumentException("document can't be null");
  150. }
  151. // validate
  152. try {
  153. if (schemaValidator != null) {
  154. schemaValidator.validate(new DOMSource(document));
  155. }
  156. } catch (SAXException e) {
  157. throw new SchemaValidationErrorConflictException(e.getMessage(), e);
  158. } catch (IOException e) {
  159. throw new InternalServerErrorException(e.getMessage(), e);
  160. }
  161. }
  162. public AuthorizationPolicy getAuthorizationPolicy() {
  163. return authorizationPolicy;
  164. }
  165. /**
  166. * The application usage can specify additional constraints that are not
  167. * possible through XML Schema, e.g. a collection of documents need to have
  168. * the root element with a unique value for attribute "id". In this method
  169. * the app usage implements the checks on constraints for PUT operations. It
  170. * is recommended that the main XML Schema of the app usage doesn't
  171. * implement any of these checks, because in a error use case, the server
  172. * will return a schema validation error, thus this API supports the usage
  173. * of a additional XML Schema just for uniqueness contraints.
  174. *
  175. * Note that this method also throws Not Authorized Exception, this is
  176. * required by some XCAP App usages, which are badly design, and "authorize"
  177. * bad content, instead of the standard Constraint Failure.
  178. *
  179. * @param document
  180. * @param xcapRoot
  181. * @param documentSelector
  182. * @param dataSource
  183. * @throws UniquenessFailureConflictException
  184. * @throws InternalServerErrorException
  185. * @throws ConstraintFailureConflictException
  186. * @throws {@link NotAuthorizedRequestException}
  187. */
  188. public void checkConstraintsOnPut(Document document, String xcapRoot,
  189. DocumentSelector documentSelector, AppUsageDataSource dataSource)
  190. throws UniquenessFailureConflictException,
  191. InternalServerErrorException, ConstraintFailureConflictException,
  192. NotAuthorizedRequestException {
  193. // validate uniqueness schema if exists
  194. if (uniquenessSchemaValidator != null) {
  195. try {
  196. uniquenessSchemaValidator.validate(new DOMSource(document));
  197. } catch (SAXException e) {
  198. throw new UniquenessFailureConflictException();
  199. } catch (IOException e) {
  200. throw new InternalServerErrorException(e.getMessage());
  201. }
  202. }
  203. // default is nothing else to check
  204. }
  205. /**
  206. * The application usage may specify resource interdependencies, like one
  207. * global document being a composition of all user documents, this method is
  208. * where the application usage defines this dependency logic on PUT
  209. * requests.
  210. *
  211. * @param oldDocument
  212. * @param newDocument
  213. * @param documentSelector
  214. * @param requestProcessor
  215. * @throws SchemaValidationErrorConflictException
  216. * @throws UniquenessFailureConflictException
  217. * @throws InternalServerErrorException
  218. * @throws ConstraintFailureConflictException
  219. */
  220. public void processResourceInterdependenciesOnPutDocument(
  221. Document oldDocument, Document newDocument,
  222. DocumentSelector documentSelector, String newETag,
  223. AppUsageRequestProcessor requestProcessor,
  224. AppUsageDataSource dataSource)
  225. throws SchemaValidationErrorConflictException,
  226. UniquenessFailureConflictException, InternalServerErrorException,
  227. ConstraintFailureConflictException {
  228. // default is no resource interdependencies
  229. }
  230. /**
  231. * The application usage may specify resource interdependencies, like one
  232. * global document being a composition of all user documents, this method is
  233. * where the application usage defines this dependency logic on PUT requests
  234. * for an element.
  235. *
  236. * @param oldElement
  237. * @param newElement
  238. * @param document
  239. * @param documentSelector
  240. * @param newETag
  241. * @param nodeSelector
  242. * @param elementSelector
  243. * @param requestProcessor
  244. * @param dataSource
  245. * @throws SchemaValidationErrorConflictException
  246. * @throws UniquenessFailureConflictException
  247. * @throws InternalServerErrorException
  248. * @throws ConstraintFailureConflictException
  249. */
  250. public void processResourceInterdependenciesOnPutElement(
  251. Element oldElement, Element newElement, Document document,
  252. DocumentSelector documentSelector, String newETag,
  253. NodeSelector nodeSelector, ElementSelector elementSelector,
  254. AppUsageRequestProcessor requestProcessor,
  255. AppUsageDataSource dataSource)
  256. throws SchemaValidationErrorConflictException,
  257. UniquenessFailureConflictException, InternalServerErrorException,
  258. ConstraintFailureConflictException {
  259. // default is no resource interdependencies
  260. }
  261. /**
  262. * The application usage may specify resource interdependencies, like one
  263. * global document being a composition of all user documents, this method is
  264. * where the application usage defines this dependency logic on PUT requests
  265. * for an attribute.
  266. *
  267. * @param oldAttrValue
  268. * @param newAttrValue
  269. * @param documentSelector
  270. * @param newETag
  271. * @param nodeSelector
  272. * @param elementSelector
  273. * @param attributeSelector
  274. * @param requestProcessor
  275. * @param dataSource
  276. * @throws SchemaValidationErrorConflictException
  277. * @throws UniquenessFailureConflictException
  278. * @throws InternalServerErrorException
  279. * @throws ConstraintFailureConflictException
  280. */
  281. public void processResourceInterdependenciesOnPutAttribute(
  282. String oldAttrValue, String newAttrValue,
  283. DocumentSelector documentSelector, String newETag,
  284. NodeSelector nodeSelector, ElementSelector elementSelector,
  285. AttributeSelector attributeSelector,
  286. AppUsageRequestProcessor requestProcessor,
  287. AppUsageDataSource dataSource)
  288. throws SchemaValidationErrorConflictException,
  289. UniquenessFailureConflictException, InternalServerErrorException,
  290. ConstraintFailureConflictException {
  291. // default is no resource interdependencies
  292. }
  293. /**
  294. * The application usage can specify additional constraints that are not
  295. * possible through XML Schema, e.g. a collection of documents need to have
  296. * the root element with a unique value for attribute "id". In this method
  297. * the app usage implements the checks on constraints for DELETE operations.
  298. * It is recommended that the main XML Schema of the app usage doesn't
  299. * implement any of these checks, because in a error use case, the server
  300. * will return a schema validation error, thus this API supports the usage
  301. * of a additional XML Schema just for uniqueness contraints.
  302. *
  303. * @param document
  304. * @param xcapRoot
  305. * @param documentSelector
  306. * @param dataSource
  307. * @throws UniquenessFailureConflictException
  308. * @throws InternalServerErrorException
  309. * @throws ConstraintFailureConflictException
  310. */
  311. public void checkConstraintsOnDelete(Document document, String xcapRoot,
  312. DocumentSelector documentSelector, AppUsageDataSource dataSource)
  313. throws UniquenessFailureConflictException,
  314. InternalServerErrorException, ConstraintFailureConflictException {
  315. // default is nothing else to check
  316. }
  317. /**
  318. * The application usage may specify resource interdependencies, like one
  319. * global document being a composition of all user documents, this method is
  320. * where the application usage defines this dependency logic on DELETE
  321. * requests.
  322. *
  323. * @param document
  324. * @param documentSelector
  325. * @param requestProcessor
  326. * @throws SchemaValidationErrorConflictException
  327. * @throws UniquenessFailureConflictException
  328. * @throws InternalServerErrorException
  329. * @throws ConstraintFailureConflictException
  330. */
  331. public void processResourceInterdependenciesOnDeleteDocument(
  332. Document deletedDocument, DocumentSelector documentSelector,
  333. AppUsageRequestProcessor requestProcessor,
  334. AppUsageDataSource dataSource)
  335. throws SchemaValidationErrorConflictException,
  336. UniquenessFailureConflictException, InternalServerErrorException,
  337. ConstraintFailureConflictException {
  338. // default is no resource interdependencies
  339. }
  340. /**
  341. * The application usage may specify resource interdependencies, like one
  342. * global document being a composition of all user documents, this method is
  343. * where the application usage defines this dependency logic on DELETE
  344. * requests.
  345. *
  346. * @param deletedElement
  347. * @param documentSelector
  348. * @param newETag
  349. * @param nodeSelector
  350. * @param elementSelector
  351. * @param requestProcessor
  352. * @param dataSource
  353. * @throws SchemaValidationErrorConflictException
  354. * @throws UniquenessFailureConflictException
  355. * @throws InternalServerErrorException
  356. * @throws ConstraintFailureConflictException
  357. */
  358. public void processResourceInterdependenciesOnDeleteElement(
  359. Node deletedElement, DocumentSelector documentSelector,
  360. String newETag, NodeSelector nodeSelector,
  361. ElementSelector elementSelector,
  362. AppUsageRequestProcessor requestProcessor,
  363. AppUsageDataSource dataSource)
  364. throws SchemaValidationErrorConflictException,
  365. UniquenessFailureConflictException, InternalServerErrorException,
  366. ConstraintFailureConflictException {
  367. // default is no resource interdependencies
  368. }
  369. /**
  370. * The application usage may specify resource interdependencies, like one
  371. * global document being a composition of all user documents, this method is
  372. * where the application usage defines this dependency logic on DELETE
  373. * requests.
  374. *
  375. * @param documentSelector
  376. * @param newETag
  377. * @param nodeSelector
  378. * @param elementSelector
  379. * @param attributeSelector
  380. * @param requestProcessor
  381. * @param dataSource
  382. * @throws SchemaValidationErrorConflictException
  383. * @throws UniquenessFailureConflictException
  384. * @throws InternalServerErrorException
  385. * @throws ConstraintFailureConflictException
  386. */
  387. public void processResourceInterdependenciesOnDeleteAttribute(
  388. DocumentSelector documentSelector, String newETag,
  389. NodeSelector nodeSelector, ElementSelector elementSelector,
  390. AttributeSelector attributeSelector,
  391. AppUsageRequestProcessor requestProcessor,
  392. AppUsageDataSource dataSource)
  393. throws SchemaValidationErrorConflictException,
  394. UniquenessFailureConflictException, InternalServerErrorException,
  395. ConstraintFailureConflictException {
  396. // default is no resource interdependencies
  397. }
  398. }