001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.xml;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceResourceIdentifier;
037: import org.libresource.LibresourceService;
038:
039: import org.libresource.kernel.KernelConstants;
040: import org.libresource.kernel.ejb.model.AclValue;
041: import org.libresource.kernel.interfaces.KernelService;
042:
043: import org.libresource.membership.GroupExportHandler;
044: import org.libresource.membership.MembershipConstants;
045: import org.libresource.membership.ejb.model.ProfileResourceValue;
046: import org.libresource.membership.interfaces.MembershipService;
047:
048: import org.xml.sax.ContentHandler;
049: import org.xml.sax.SAXException;
050:
051: import java.io.ByteArrayInputStream;
052: import java.io.ByteArrayOutputStream;
053: import java.io.ObjectOutputStream;
054:
055: import java.net.URI;
056:
057: import java.util.HashMap;
058: import java.util.Iterator;
059:
060: /**
061: * LibreSource
062: *
063: * @author <a href="mailto:bort@loria.fr">Guillaume Bort</a> - <a
064: * href="http://www.inria.fr">INRIA Lorraine</a>
065: */
066: public class URIExportHandler extends LibresourceExportHandler {
067: private URI uri;
068: private URI rootNode;
069:
070: public URIExportHandler(URI uri, URI rootNode) throws Exception {
071: this .uri = normalizeURI(uri);
072: this .rootNode = normalizeURI(rootNode);
073: }
074:
075: public void export(ContentHandler handler, ImportExportLogger logger)
076: throws Exception {
077: KernelService kernelService = (KernelService) Libresource
078: .getService(KernelConstants.SERVICE);
079: MembershipService membershipService = (MembershipService) Libresource
080: .getService(MembershipConstants.SERVICE);
081:
082: if (!uri.toString().startsWith(rootNode.toString())) {
083: throw new Exception("ARGG !!!!");
084: }
085:
086: XmlDumpAttributes attrs = new XmlDumpAttributes();
087: attrs.put("uri-part", uri.toString().substring(
088: rootNode.toString().length()));
089:
090: URI owner = kernelService.getOwner(uri);
091: attrs.put("owner", (owner == null) ? "" : normalizeURI(owner)
092: .toString());
093: attrs.put("creation-date", Libresource.formatDate(kernelService
094: .getCreationDate(uri)));
095: attrs.put("update-date", Libresource.formatDate(kernelService
096: .getUpdateDate(uri)));
097:
098: XmlDumpHelper.beginElement("kernel", "node", XmlDumpHelper
099: .generateAttributesSet(attrs), handler);
100:
101: // acls
102: try {
103: XmlDumpHelper.beginElement("kernel", "acls-list",
104: XmlDumpHelper.getEmptyAttributesSet(), handler);
105:
106: AclValue[] acls = kernelService.getAcls(uri);
107:
108: for (int i = 0; i < acls.length; i++) {
109: AclValue acl = acls[i];
110:
111: // export only if public
112: if (kernelService.checkSecurity(acl.getOwnerUri(),
113: KernelConstants.SECURITY_READ)) {
114: XmlDumpHelper.beginElement("kernel", "acl",
115: XmlDumpHelper.getEmptyAttributesSet(),
116: handler);
117:
118: XmlDumpAttributes attrsAcl = new XmlDumpAttributes();
119: String type = kernelService.lookup(
120: acl.getOwnerUri()).getResourceType();
121: attrsAcl.put("type", type);
122:
123: if (type.compareTo("Group") == 0) {
124: if (acl.getOwnerUri().getPath().startsWith(
125: rootNode.getPath())) {
126: attrsAcl.put("uri", "."
127: + acl.getOwnerUri().getPath()
128: .substring(
129: rootNode.getPath()
130: .length()));
131: } else {
132: attrsAcl.put("uri", acl.getOwnerUri()
133: .getPath());
134: }
135: }
136:
137: if (type.compareTo("Profile") == 0) {
138: attrsAcl.put("id", kernelService.lookup(
139: acl.getOwnerUri()).getResourceId());
140:
141: if (kernelService
142: .lookup(acl.getOwnerUri())
143: .getResourceId()
144: .compareTo(
145: membershipService
146: .getUnauthentifiedUserId()) != 0) {
147: ProfileResourceValue user = membershipService
148: .getProfile(acl.getOwnerUri());
149: attrsAcl.put("name", user.getFullName());
150: attrsAcl.put("mail", user.getEmail());
151: attrsAcl.put("jabber", user.getJabberId());
152: }
153: }
154:
155: XmlDumpHelper.outputEmptyElement("acl", "owner",
156: XmlDumpHelper
157: .generateAttributesSet(attrsAcl),
158: handler);
159: XmlDumpHelper.outputElementWithContent("acl",
160: "permissions", listPermissions(acl
161: .getPermissions()), XmlDumpHelper
162: .getEmptyAttributesSet(), handler);
163:
164: XmlDumpHelper.endElement("kernel", "acl", handler);
165: }
166: }
167:
168: XmlDumpHelper.endElement("kernel", "acls-list", handler);
169: } catch (Exception e) {
170: throw new SAXException("Can't retrieve ACLs list for uri "
171: + uri, e);
172: }
173:
174: // properties
175: try {
176: XmlDumpHelper.beginElement("kernel", "properties",
177: XmlDumpHelper.getEmptyAttributesSet(), handler);
178:
179: HashMap properties = kernelService
180: .listPropertiesAtNode(uri);
181:
182: for (Iterator i = properties.keySet().iterator(); i
183: .hasNext();) {
184: String key = (String) i.next();
185: XmlDumpAttributes attrsProp = new XmlDumpAttributes();
186: attrsProp.put("key", key);
187:
188: if (properties.get(key) != null) {
189: // Seb allow binary content also in node property
190: if (properties.get(key) instanceof String) {
191: attrsProp.put("type", "txt");
192: XmlDumpHelper
193: .outputElementWithContent(
194: "kernel",
195: "property",
196: (String) properties.get(key),
197: XmlDumpHelper
198: .generateAttributesSet(attrsProp),
199: handler);
200: } else {
201: attrsProp.put("type", "bin");
202: XmlDumpHelper
203: .beginElement(
204: "kernel",
205: "property",
206: XmlDumpHelper
207: .generateAttributesSet(attrsProp),
208: handler);
209:
210: ByteArrayOutputStream outTmp = new ByteArrayOutputStream();
211: ObjectOutputStream objOutTmp = new ObjectOutputStream(
212: outTmp);
213: objOutTmp.writeObject(properties.get(key));
214: objOutTmp.flush();
215: XmlDumpHelper.outputElementWithContent(
216: "property", "bin",
217: new ByteArrayInputStream(outTmp
218: .toByteArray()), XmlDumpHelper
219: .getEmptyAttributesSet(),
220: handler);
221: XmlDumpHelper.endElement("kernel", "property",
222: handler);
223: }
224: }
225: }
226:
227: XmlDumpHelper.endElement("kernel", "properties", handler);
228: } catch (Exception e) {
229: throw new SAXException("Can't retrieve Properties for uri "
230: + uri, e);
231: }
232:
233: LibresourceResourceIdentifier resourceIdentifier = kernelService
234: .lookup(uri);
235:
236: if (resourceIdentifier != null) {
237: XmlDumpAttributes attrs2 = new XmlDumpAttributes();
238: attrs2.put("service", resourceIdentifier.getService());
239: attrs2.put("type", resourceIdentifier.getResourceType());
240: attrs2.put("shortName", kernelService.getResource(uri)
241: .getShortResourceName());
242: XmlDumpHelper.beginElement("kernel", "resource",
243: XmlDumpHelper.generateAttributesSet(attrs2),
244: handler);
245:
246: // deleguate to Resource Export Handler
247: LibresourceService libresourceService = Libresource
248: .getService(resourceIdentifier.getService());
249: LibresourceExportHandler exportHandler = libresourceService
250: .getXmlExportHandlerForResource(uri);
251:
252: // here for groups export
253: if (exportHandler instanceof GroupExportHandler) {
254: ((GroupExportHandler) exportHandler)
255: .setRootNode(rootNode);
256: }
257:
258: exportHandler.export(handler, logger);
259:
260: XmlDumpHelper.endElement("kernel", "resource", handler);
261: }
262:
263: URI[] children = kernelService.listChildren(uri);
264:
265: for (int i = 0; i < children.length; i++) {
266: LibresourceExportHandler exportHandler = new URIExportHandler(
267: children[i], rootNode);
268: exportHandler.export(handler, logger);
269: }
270:
271: XmlDumpHelper.endElement("kernel", "node", handler);
272: }
273:
274: private String listPermissions(String[] permissions) {
275: StringBuffer buffer = new StringBuffer();
276:
277: for (int i = 0; i < permissions.length; i++) {
278: buffer.append(permissions[i]);
279:
280: if (i != (permissions.length - 1)) {
281: buffer.append(",");
282: }
283: }
284:
285: return buffer.toString();
286: }
287:
288: protected static URI normalizeURI(URI uri) throws Exception {
289: String path = uri.normalize().getPath();
290:
291: if (!path.startsWith("/")) {
292: path = "/" + path;
293: }
294:
295: if (path.endsWith("/")) {
296: path = path.substring(0, path.length() - 1);
297: }
298:
299: return new URI(path);
300: }
301: }
|