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.LibresourceService;
037:
038: import org.libresource.kernel.KernelConstants;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import org.libresource.membership.GroupImportHandler;
042: import org.libresource.membership.MembershipConstants;
043: import org.libresource.membership.interfaces.MembershipService;
044:
045: import org.xml.sax.Attributes;
046:
047: import sun.misc.BASE64Decoder;
048:
049: import java.io.ByteArrayInputStream;
050: import java.io.ObjectInputStream;
051:
052: import java.net.URI;
053:
054: import java.util.Hashtable;
055:
056: /**
057: * LibreSource
058: *
059: * @author <a href="mailto:bort@loria.fr">Guillaume Bort</a> - <a
060: * href="http://www.inria.fr">INRIA Lorraine</a>
061: */
062: public class URIImportHandler extends LibresourceImportHandler {
063: private LibresourceImportHandler deleguatedHandler;
064: private Hashtable resourceHandlerList;
065: private URI originalRoot;
066: private String originalHost;
067: private URI rootNode;
068: private URI userAclOwner;
069: private URI groupAclOwner;
070: private String[] aclPermissions;
071: private KernelService kernelService;
072: private MembershipService membershipService;
073: private StringBuffer buffer;
074: private String tmpPropertyKey;
075: private String tmpPropertyType;
076: private SecurityImportHandler securityImportHandler;
077:
078: //
079: private URI currentNode;
080:
081: public URIImportHandler(SecurityImportHandler aclImportHandler) {
082: resourceHandlerList = new Hashtable();
083: this .securityImportHandler = aclImportHandler;
084:
085: try {
086: kernelService = (KernelService) Libresource
087: .getService(KernelConstants.SERVICE);
088: membershipService = (MembershipService) Libresource
089: .getService(MembershipConstants.SERVICE);
090: } catch (Exception e) {
091: }
092: }
093:
094: public void handleBeginElement(String name, Attributes attributes,
095: ImportExportLogger logger) throws Exception {
096: buffer = null;
097:
098: if (deleguatedHandler != null) {
099: deleguatedHandler.handleBeginElement(name, attributes,
100: logger);
101:
102: return;
103: }
104:
105: if (name.equals("libresource:libresource-dump")) {
106: // begin
107: originalRoot = new URI(attributes.getValue("original-root"));
108: originalHost = attributes.getValue("original-host");
109: securityImportHandler.init(logger);
110:
111: return;
112: }
113:
114: if (name.equals("kernel:node")) {
115: currentNode = new URI(rootNode.getPath()
116: + attributes.getValue("uri-part"));
117: kernelService.createURI(currentNode);
118: kernelService.resetAcls(currentNode);
119:
120: try {
121: kernelService.setCreationDate(currentNode,
122: Libresource.parseDate(attributes
123: .getValue("creation-date")));
124: } catch (Exception e) {
125: //
126: }
127:
128: try {
129: kernelService.setUpdateDate(currentNode, Libresource
130: .parseDate(attributes.getValue("update-date")));
131: } catch (Exception e) {
132: //
133: }
134:
135: return;
136: }
137:
138: if (name.equals("kernel:acls-list")) {
139: return;
140: }
141:
142: if (name.equals("kernel:properties")) {
143: return;
144: }
145:
146: if (name.equals("kernel:property")) {
147: tmpPropertyKey = attributes.getValue("key");
148: tmpPropertyType = attributes.getValue("type");
149:
150: if (tmpPropertyType == null) {
151: tmpPropertyType = "txt";
152: }
153:
154: buffer = new StringBuffer();
155:
156: return;
157: }
158:
159: if (name.equals("property:bin")) {
160: buffer = new StringBuffer();
161:
162: return;
163: }
164:
165: if (name.equals("kernel:acl")) {
166: return;
167: }
168:
169: if (name.equals("acl:owner")) {
170: String type = attributes.getValue("type");
171:
172: if ((type != null) && (type.compareTo("Profile") == 0)) {
173: URI user = new URI(membershipService.getUsersRootURI()
174: + "/" + attributes.getValue("id"));
175:
176: try {
177: securityImportHandler.createProfile(attributes
178: .getValue("id"), attributes
179: .getValue("name"), attributes
180: .getValue("mail"), attributes
181: .getValue("jabber"));
182: userAclOwner = user;
183: } catch (Exception e) {
184: userAclOwner = null;
185: }
186: }
187:
188: if ((type != null) && (type.compareTo("Group") == 0)) {
189: URI group = new URI(attributes.getValue("uri"));
190:
191: if (group.getPath().startsWith(".")) {
192: group = new URI(rootNode.getPath() + "/"
193: + group.getPath());
194: }
195:
196: groupAclOwner = group;
197: }
198:
199: return;
200: }
201:
202: if (name.equals("acl:permissions")) {
203: buffer = new StringBuffer();
204:
205: return;
206: }
207:
208: if (name.equals("kernel:resource")) {
209: LibresourceService libresourceService = Libresource
210: .getService(attributes.getValue("service"));
211: deleguatedHandler = libresourceService
212: .getXmlImportHandler(attributes.getValue("type"));
213: logger.log(ImportExportLogger.MESSAGE, "Importing "
214: + attributes.getValue("type") + " \""
215: + attributes.getValue("shortName") + "\" at node "
216: + currentNode);
217:
218: if (deleguatedHandler != null) {
219: if (deleguatedHandler instanceof GroupImportHandler) {
220: ((GroupImportHandler) deleguatedHandler)
221: .setRootNode(rootNode);
222: ((GroupImportHandler) deleguatedHandler)
223: .setSecurityImportHandler(securityImportHandler);
224: }
225:
226: deleguatedHandler.init(currentNode, logger);
227: }
228:
229: return;
230: }
231: }
232:
233: /*
234: * return true if this handler has terminated
235: */
236: public boolean handleEndElement(String name,
237: ImportExportLogger logger) throws Exception {
238: if (deleguatedHandler != null) {
239: boolean finished = deleguatedHandler.handleEndElement(name,
240: logger);
241:
242: if (finished) {
243: deleguatedHandler = null;
244: }
245:
246: return false;
247: }
248:
249: if (name.equals("libresource:libresource-dump")) {
250: securityImportHandler.destroy();
251:
252: // end
253: return true;
254: }
255:
256: if (name.equals("kernel:node")) {
257: // add properties
258: return false;
259: }
260:
261: if (name.equals("kernel:property")) {
262: String propertyValue = buffer.toString().trim();
263:
264: if (tmpPropertyType.equals("txt")) {
265: kernelService.setProperty(currentNode, tmpPropertyKey,
266: propertyValue);
267: }
268:
269: return false;
270: }
271:
272: if (name.equals("property:bin")) {
273: String propertyValue = buffer.toString().trim();
274:
275: // binary part
276: System.out.println("Try to save " + currentNode
277: + " property");
278:
279: ObjectInputStream stream = new ObjectInputStream(
280: new ByteArrayInputStream(new BASE64Decoder()
281: .decodeBuffer(propertyValue)));
282: Object toSave = stream.readObject();
283:
284: System.out.println(toSave.toString());
285: kernelService.setProperty(currentNode, tmpPropertyKey,
286: toSave);
287: stream.close();
288:
289: return false;
290: }
291:
292: if (name.equals("kernel:acls-list")) {
293: return false;
294: }
295:
296: if (name.equals("kernel:acl")) {
297: if (userAclOwner != null) {
298: securityImportHandler.handleUserAcl(currentNode,
299: userAclOwner, aclPermissions);
300: }
301:
302: if (groupAclOwner != null) {
303: securityImportHandler.handleGroupAcl(currentNode,
304: groupAclOwner, aclPermissions);
305: }
306:
307: userAclOwner = null;
308: groupAclOwner = null;
309: aclPermissions = null;
310:
311: return false;
312: }
313:
314: if (name.equals("acl:owner")) {
315: return false;
316: }
317:
318: if (name.equals("acl:permissions")) {
319: aclPermissions = buffer.toString().trim().split(",");
320:
321: return false;
322: }
323:
324: if (name.equals("kernel:resource")) {
325: deleguatedHandler = null;
326:
327: return false;
328: }
329:
330: return false;
331: }
332:
333: public void handleContent(String content, ImportExportLogger logger)
334: throws Exception {
335: if (deleguatedHandler != null) {
336: deleguatedHandler.handleContent(content, logger);
337: }
338:
339: if (buffer != null) {
340: buffer.append(content);
341: }
342: }
343:
344: public void init(URI node, ImportExportLogger logger) {
345: rootNode = node;
346: currentNode = node;
347: deleguatedHandler = null;
348: buffer = null;
349: }
350: }
|