1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.serializer;
18
19 import java.security.Principal;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.prefs.Preferences;
27
28 import javax.security.auth.Subject;
29
30 import javolution.xml.XMLBinding;
31
32 import org.apache.jetspeed.capabilities.Capabilities;
33 import org.apache.jetspeed.capabilities.Capability;
34 import org.apache.jetspeed.capabilities.Client;
35 import org.apache.jetspeed.capabilities.MediaType;
36 import org.apache.jetspeed.capabilities.MimeType;
37 import org.apache.jetspeed.components.ComponentManager;
38 import org.apache.jetspeed.profiler.Profiler;
39 import org.apache.jetspeed.profiler.ProfilerException;
40 import org.apache.jetspeed.profiler.rules.PrincipalRule;
41 import org.apache.jetspeed.profiler.rules.ProfilingRule;
42 import org.apache.jetspeed.profiler.rules.RuleCriterion;
43 import org.apache.jetspeed.security.BasePrincipal;
44 import org.apache.jetspeed.security.Group;
45 import org.apache.jetspeed.security.GroupManager;
46 import org.apache.jetspeed.security.PasswordCredential;
47 import org.apache.jetspeed.security.PermissionManager;
48 import org.apache.jetspeed.security.PortalResourcePermission;
49 import org.apache.jetspeed.security.Role;
50 import org.apache.jetspeed.security.RoleManager;
51 import org.apache.jetspeed.security.User;
52 import org.apache.jetspeed.security.UserManager;
53 import org.apache.jetspeed.security.om.InternalPermission;
54 import org.apache.jetspeed.security.om.InternalPrincipal;
55 import org.apache.jetspeed.security.spi.PasswordCredentialProvider;
56 import org.apache.jetspeed.serializer.objects.JSCapabilities;
57 import org.apache.jetspeed.serializer.objects.JSCapability;
58 import org.apache.jetspeed.serializer.objects.JSClient;
59 import org.apache.jetspeed.serializer.objects.JSClientCapabilities;
60 import org.apache.jetspeed.serializer.objects.JSClientMimeTypes;
61 import org.apache.jetspeed.serializer.objects.JSClients;
62 import org.apache.jetspeed.serializer.objects.JSGroup;
63 import org.apache.jetspeed.serializer.objects.JSGroups;
64 import org.apache.jetspeed.serializer.objects.JSMediaType;
65 import org.apache.jetspeed.serializer.objects.JSMediaTypes;
66 import org.apache.jetspeed.serializer.objects.JSMimeType;
67 import org.apache.jetspeed.serializer.objects.JSMimeTypes;
68 import org.apache.jetspeed.serializer.objects.JSNVPElements;
69 import org.apache.jetspeed.serializer.objects.JSPWAttributes;
70 import org.apache.jetspeed.serializer.objects.JSPermission;
71 import org.apache.jetspeed.serializer.objects.JSPermissions;
72 import org.apache.jetspeed.serializer.objects.JSPrincipalRule;
73 import org.apache.jetspeed.serializer.objects.JSPrincipalRules;
74 import org.apache.jetspeed.serializer.objects.JSProfilingRule;
75 import org.apache.jetspeed.serializer.objects.JSProfilingRules;
76 import org.apache.jetspeed.serializer.objects.JSRole;
77 import org.apache.jetspeed.serializer.objects.JSRoles;
78 import org.apache.jetspeed.serializer.objects.JSRuleCriterion;
79 import org.apache.jetspeed.serializer.objects.JSRuleCriterions;
80 import org.apache.jetspeed.serializer.objects.JSSeedData;
81 import org.apache.jetspeed.serializer.objects.JSSnapshot;
82 import org.apache.jetspeed.serializer.objects.JSUser;
83 import org.apache.jetspeed.serializer.objects.JSUserAttributes;
84 import org.apache.jetspeed.serializer.objects.JSUserGroups;
85 import org.apache.jetspeed.serializer.objects.JSUserRoles;
86 import org.apache.jetspeed.serializer.objects.JSUserUsers;
87 import org.apache.jetspeed.serializer.objects.JSUsers;
88
89 /***
90 * Jetspeed Serializer
91 * <p>
92 * The Serializer is capable of reading and writing the current content of the
93 * Jetspeed environment to and from XML files. The component can be used from a
94 * standalone java application for seeding a new database or from a running
95 * portal as an administrative backup/restore function.
96 * <p>
97 * The XML file needs to indicate whether passwords used in credentials
98 * are plain text or whether they are encoded. The import algoritm can determine -
99 * prior to reading users - which encode/decode scheme was used and if <none> or
100 * <implements PasswordEncodingService> then we store plain passwords (Note that
101 * that alone requires the resulting XML to be encoded!!!!!)
102 *
103 * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
104 * @version $Id: $
105 */
106 public class JetspeedSerializerImpl extends JetspeedSerializerBase implements JetspeedSerializer
107 {
108
109
110 private HashMap roleMap = new HashMap();
111
112 private HashMap groupMap = new HashMap();
113
114 private HashMap userMap = new HashMap();
115
116 private HashMap mimeMap = new HashMap();
117 private HashMap mimeMapInt = new HashMap();
118
119 private HashMap mediaMap = new HashMap();
120
121 private HashMap capabilityMap = new HashMap();
122 private HashMap capabilityMapInt = new HashMap();
123
124 private HashMap clientMap = new HashMap();
125
126 private HashMap permissionMap = new HashMap();
127
128 private HashMap rulesMap = new HashMap();
129
130 int refCouter = 0;
131
132
133 private static String ENCODING_STRING = "JETSPEED 2.1 - 2006";
134 private static String JETSPEED = "JETSPEED";
135
136
137 protected Class getSerializerDataClass()
138 {
139 return JSSeedData.class;
140 }
141
142 protected String getSerializerDataTag()
143 {
144 return TAG_SNAPSHOT;
145 }
146
147 public JetspeedSerializerImpl()
148 {
149 super();
150 }
151
152 /***
153 * hand over existing component manager
154 *
155 * @param cm
156 */
157 public JetspeedSerializerImpl(ComponentManager cm)
158 {
159 super(cm);
160 }
161
162 /***
163 * This constructor takes the application root, the search path for the boot
164 * component configuration files and the search path for the application
165 * component configuration files.
166 * <p>
167 * For example: new JetspeedSerializerImpl("./", "assembly/boot/*.xml",
168 * "assembly/*.xml") will establish the current directory as the root,
169 * process all xml files in the assembly/boot directory before processing
170 * all xml files in the assembly directory itself.
171 *
172 * @param appRoot
173 * working directory
174 * @param bootConfig
175 * boot (primary) file or files (wildcards are allowed)
176 * @param appConfig
177 * application (secondary) file or files (wildcards are allowed)
178 */
179 public JetspeedSerializerImpl(String appRoot, String[] bootConfig,
180 String[] appConfig) throws SerializerException
181 {
182 super(appRoot,bootConfig,appConfig);
183 }
184
185
186
187
188
189
190
191
192 /***
193 * reset instruction flags to default settings (all true)
194 *
195 */
196 protected void resetSettings()
197 {
198 setSetting(JetspeedSerializer.KEY_PROCESS_USERS, true);
199 setSetting(JetspeedSerializer.KEY_PROCESS_CAPABILITIES, true);
200 setSetting(JetspeedSerializer.KEY_PROCESS_PROFILER, true);
201 setSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING, true);
202 setSetting(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS, true);
203 }
204
205
206 /***
207 * On import, get the basic SnapShot data
208 *
209 */
210 protected void getSnapshotData()
211 {
212 logMe("date created : " + getSnapshot().getDateCreated());
213 logMe("software Version : " + getSnapshot().getSavedVersion());
214 logMe("software SUbVersion : " + getSnapshot().getSavedSubversion());
215 }
216
217
218
219 private void recreateCapabilities (Capabilities caps) throws SerializerException
220 {
221 logMe("recreateCapabilities - processing");
222 JSCapabilities capabilities = ((JSSeedData)getSnapshot()).getCapabilities();
223 if ((capabilities != null) && (capabilities.size() > 0))
224 {
225 Iterator _it = capabilities.iterator();
226 while (_it.hasNext())
227 {
228 JSCapability _c = (JSCapability)_it.next();
229
230 try
231 {
232 Capability capability = caps.createCapability(_c.getName());
233 /*** THE KEY_OVERWRITE_EXISTING test is not required for capabilites, since they carry no other information than the name
234 * Used here for consistency, though
235 */
236 if ((this.getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING)) || (capability.getCapabilityId() == 0))
237 {
238 caps.storeCapability(capability);
239 }
240 this.capabilityMap.put(_c.getName(), capability);
241 }
242 catch (Exception e)
243 {
244 throw new SerializerException(
245 SerializerException.CREATE_OBJECT_FAILED
246 .create("org.apache.jetspeed.capabilities.Capabilities",e.getLocalizedMessage()));
247 }
248 }
249 }
250 else
251 logMe("NO CAPABILITES?????");
252 logMe("recreateCapabilities - done");
253 }
254 private void recreateMimeTypes (Capabilities caps) throws SerializerException
255 {
256 logMe("recreateMimeTypes - processing");
257 JSMimeTypes mimeTypes = ((JSSeedData)getSnapshot()).getMimeTypes();
258 if ((mimeTypes != null) && (mimeTypes.size() > 0))
259 {
260 Iterator _it = mimeTypes.iterator();
261 while (_it.hasNext())
262 {
263 JSMimeType _c = (JSMimeType)_it.next();
264
265 try
266 {
267 MimeType mimeType = caps.createMimeType(_c.getName());
268 /*** THE KEY_OVERWRITE_EXISTING test is not required for mime types, since they carry no other information than the name
269 * Used here for consistency, though
270 */
271 if ((this.getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING)) || (mimeType.getMimetypeId() == 0))
272 {
273 caps.storeMimeType(mimeType);
274 }
275 this.mimeMap.put(_c.getName(), mimeType);
276
277 }
278 catch (Exception e)
279 {
280 throw new SerializerException(
281 SerializerException.CREATE_OBJECT_FAILED
282 .create("org.apache.jetspeed.capabilities.MimeType",e.getLocalizedMessage()));
283 }
284 }
285 }
286 else
287 logMe("NO MIME TYPES?????");
288 logMe("recreateMimeTypes - done");
289 }
290
291 private void recreateMediaTypes (Capabilities caps) throws SerializerException
292 {
293 String _line;
294
295 logMe("recreateMediaTypes - processing");
296 JSMediaTypes mediaTypes = ((JSSeedData)getSnapshot()).getMediaTypes();
297 if ((mediaTypes != null) && (mediaTypes.size() > 0))
298 {
299 Iterator _it = mediaTypes.iterator();
300 while (_it.hasNext())
301 {
302 JSMediaType _c = (JSMediaType)_it.next();
303
304 try
305 {
306 MediaType mediaType = caps.createMediaType(_c.getName());
307 /*** THE KEY_OVERWRITE_EXISTING test IS required for media types, since they carry no other information than the name
308 * Used here for consistency, though
309 */
310 if ((this.getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING)) || (mediaType.getMediatypeId() == 0))
311 {
312
313 mediaType.setCharacterSet(_c.getCharacterSet());
314 mediaType.setTitle(_c.getTitle());
315 mediaType.setDescription(_c.getDescription());
316
317 try
318 {
319 _line = _c.getMimeTypesString().toString();
320 ArrayList list = this.getTokens(_line);
321 if ((list != null) && (list.size()>0))
322 {
323 Iterator _it1 = list.iterator();
324 int added = 0;
325 while (_it1.hasNext())
326 {
327 MimeType _mt = caps.createMimeType((String)_it1.next());
328 if (_mt != null)
329 mediaType.addMimetype(_mt);
330 added++;
331 }
332 }
333 }
334 catch (Exception e1)
335 {
336 e1.printStackTrace();
337 }
338 try
339 {
340 _line = _c.getCapabilitiesString().toString();
341 ArrayList list = this.getTokens(_line);
342 if ((list != null) && (list.size()>0))
343 {
344 Iterator _it1 = list.iterator();
345 if ((list != null) && (list.size()>0))
346 {
347 int added = 0;
348 while (_it1.hasNext())
349 {
350 Capability _ct = caps.createCapability((String)_it1.next());
351 if (_ct != null)
352 mediaType.addCapability(_ct);
353 added++;
354 }
355 }
356 }
357 }
358 catch (Exception e1)
359 {
360 e1.printStackTrace();
361 }
362 caps.storeMediaType(mediaType);
363 }
364 this.mediaMap.put(_c.getName(), mediaType);
365 }
366 catch (Exception e)
367 {
368 throw new SerializerException(
369 SerializerException.CREATE_OBJECT_FAILED
370 .create("org.apache.jetspeed.capabilities.MediaType",e.getLocalizedMessage()));
371 }
372 }
373 }
374 else
375 logMe("NO MEDIA TYPES?????");
376 logMe("recreateMediaTypes - done");
377 }
378
379
380 private void recreateClients (Capabilities caps) throws SerializerException
381 {
382 String _line;
383
384 logMe("recreateClients - processing");
385 JSClients clients = ((JSSeedData)getSnapshot()).getClients();
386 if ((clients != null) && (clients.size() > 0))
387 {
388 Iterator _it = clients.iterator();
389 while (_it.hasNext())
390 {
391 JSClient _c = (JSClient)_it.next();
392
393 try
394 {
395 Client client = caps.createClient(_c.getName());
396 /*** THE KEY_OVERWRITE_EXISTING test IS required for media types, since they carry no other information than the name
397 * Used here for consistency, though
398 */
399 if ((this.getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING)) || (client.getClientId() == 0))
400 {
401
402 client.setUserAgentPattern(_c.getUserAgentPattern());
403 client.setManufacturer(_c.getManufacturer());
404 client.setModel(_c.getModel());
405 client.setEvalOrder(_c.getEvalOrder());
406 String myPrefMimeType = _c.getPreferredMimeTypeID();
407 client.setVersion(_c.getVersion());
408 try
409 {
410 _line = _c.getMimeTypesString().toString();
411 ArrayList list = this.getTokens(_line);
412 if ((list != null) && (list.size()>0))
413 {
414 Iterator _it1 = list.iterator();
415 int added = 0;
416 while (_it1.hasNext())
417 {
418 MimeType _mt = caps.createMimeType((String)_it1.next());
419 if (_mt != null)
420 {
421 client.getMimetypes().add(_mt);
422 if (_mt.getMimetypeId() == 0)
423 {
424 caps.storeMimeType(_mt);
425 }
426 if (myPrefMimeType.equalsIgnoreCase(_mt.getName()))
427 client.setPreferredMimeTypeId(_mt.getMimetypeId());
428
429 }
430 added++;
431 }
432 }
433 }
434 catch (Exception e1)
435 {
436 e1.printStackTrace();
437 }
438 try
439 {
440 _line = _c.getCapabilitiesString().toString();
441 ArrayList list = this.getTokens(_line);
442 if ((list != null) && (list.size()>0))
443 {
444 Iterator _it1 = list.iterator();
445 if ((list != null) && (list.size()>0))
446 {
447 int added = 0;
448 while (_it1.hasNext())
449 {
450 Capability _ct = caps.createCapability((String)_it1.next());
451 if (_ct != null)
452 client.getCapabilities().add(_ct);
453 added++;
454 }
455 }
456 }
457 }
458 catch (Exception e1)
459 {
460 e1.printStackTrace();
461 }
462 caps.storeClient(client);
463 }
464 this.clientMap.put(_c.getName(), client);
465 }
466 catch (Exception e)
467 {
468 throw new SerializerException(
469 SerializerException.CREATE_OBJECT_FAILED
470 .create("org.apache.jetspeed.capabilities.Client",e.getLocalizedMessage()));
471 }
472 }
473 }
474 else
475 logMe("NO MEDIA TYPES?????");
476 logMe("recreateClients - done");
477 }
478
479
480 private void importCapabilitiesInfrastructure() throws SerializerException
481 {
482 logMe("importCapabilitiesInfrastructure - processing");
483 Capabilities caps = (Capabilities) getCM()
484 .getComponent("org.apache.jetspeed.capabilities.Capabilities");
485 if (caps == null)
486 throw new SerializerException(
487 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
488 .create("org.apache.jetspeed.capabilities.Capabilities"));
489
490 recreateCapabilities(caps);
491 recreateMimeTypes(caps);
492 recreateMediaTypes(caps);
493 recreateClients(caps);
494
495
496 logMe("importCapabilitiesInfrastructure - processing done");
497 }
498
499
500 /***
501 * import the groups, roles and finally the users to the current environment
502 *
503 * @throws SerializerException
504 */
505 private void recreateRolesGroupsUsers() throws SerializerException
506 {
507 logMe("recreateRolesGroupsUsers");
508 GroupManager groupManager = (GroupManager) getCM()
509 .getComponent("org.apache.jetspeed.security.GroupManager");
510 if (groupManager == null)
511 throw new SerializerException(
512 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
513 .create("org.apache.jetspeed.security.GroupManager"));
514 RoleManager roleManager = (RoleManager) getCM()
515 .getComponent("org.apache.jetspeed.security.RoleManager");
516 if (roleManager == null)
517 throw new SerializerException(
518 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
519 .create("org.apache.jetspeed.security.RoleManager"));
520 UserManager userManager = (UserManager) getCM()
521 .getComponent("org.apache.jetspeed.security.UserManager");
522 if (userManager == null)
523 throw new SerializerException(
524 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
525 .create("org.apache.jetspeed.security.UserManager"));
526
527
528
529
530 JSGroups groups = null;
531 JSRoles roles = null;
532
533 groups = ((JSSeedData)getSnapshot()).getGroups();
534
535 Iterator _it = groups.iterator();
536 while (_it.hasNext())
537 {
538 String name = ((JSGroup)_it.next()).getName();
539
540 try
541 {
542 if (!(groupManager.groupExists(name)))
543 groupManager.addGroup(name);
544 Group group = groupManager.getGroup(name);
545 this.groupMap.put(name, group.getPrincipal());
546 } catch (Exception e)
547 {
548 throw new SerializerException(
549 SerializerException.CREATE_OBJECT_FAILED
550 .create(new String[]
551 { "Group", e.getMessage()}));
552 }
553 }
554 logMe("recreateGroups - done");
555 logMe("processing roles");
556
557 roles = ((JSSeedData)getSnapshot()).getRoles();
558
559 _it = roles.iterator();
560 while (_it.hasNext())
561 {
562 String name = ((JSRole)_it.next()).getName();
563
564 try
565 {
566 if (!(roleManager.roleExists(name)))
567 roleManager.addRole(name);
568 Role role = roleManager.getRole(name);
569 this.roleMap.put(name, role.getPrincipal());
570 } catch (Exception e)
571 {
572 throw new SerializerException(
573 SerializerException.CREATE_OBJECT_FAILED
574 .create(new String[]
575 { "Role", e.getMessage()}));
576 }
577 }
578 logMe("recreateRoles - done");
579 logMe("processing users");
580
581 /*** determine whether passwords can be reconstructed or not */
582 int passwordEncoding = compareCurrentSecurityProvider((JSSeedData)getSnapshot());
583 JSUsers users = null;
584 users = ((JSSeedData)getSnapshot()).getUsers();
585
586 _it = users.iterator();
587 while (_it.hasNext())
588 {
589
590 JSUser jsuser = (JSUser)_it.next();
591
592 try
593 {
594 User user = null;
595 if (userManager.userExists(jsuser.getName()))
596 {
597 user = userManager.getUser(jsuser.getName());
598 }
599 if ((this.getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING)) || (user == null))
600 {
601 if (user == null)
602 {
603 String password = recreatePassword(jsuser.getPassword());
604 logMe("add User "+ jsuser.getName() + " with password " + password);
605 userManager.importUser(jsuser.getName(), password,(passwordEncoding == PASSTHRU_REQUIRED));
606 logMe("add User done ");
607 user = userManager.getUser(jsuser.getName());
608 }
609 try
610 {
611 userManager.setPasswordEnabled(jsuser.getName(), jsuser.getPwEnabled());
612 userManager.setPasswordUpdateRequired(jsuser.getName(), jsuser.getPwRequiredUpdate());
613 java.sql.Date d = jsuser.getPwExpirationDate();
614 if (d != null)
615 userManager.setPasswordExpiration(jsuser.getName(), d);
616 }
617 catch (Exception e)
618 {
619
620 logMe("setting userinfo for "+ jsuser.getName() + " failed because of " + e.getLocalizedMessage());
621 }
622
623
624 Subject subject = user.getSubject();
625
626 ArrayList listTemp = jsuser.getPrivateCredentials();
627 if ((listTemp != null) && (listTemp.size()>0))
628 {
629 Iterator _itTemp = listTemp.iterator();
630 while (_itTemp.hasNext())
631 {
632 subject.getPrivateCredentials().add(_itTemp.next());
633 }
634 }
635 listTemp = jsuser.getPublicCredentials();
636 if ((listTemp != null) && (listTemp.size()>0))
637 {
638 Iterator _itTemp = listTemp.iterator();
639 while (_itTemp.hasNext())
640 {
641 subject.getPublicCredentials().add(_itTemp.next());
642 }
643 }
644 JSUserGroups jsUserGroups = jsuser.getGroupString();
645 if (jsUserGroups != null)
646 listTemp = this.getTokens(jsUserGroups.toString());
647 else
648 listTemp = null;
649 if ((listTemp != null) && (listTemp.size()>0))
650 {
651 Iterator _itTemp = listTemp.iterator();
652 while (_itTemp.hasNext())
653 {
654 groupManager.addUserToGroup(jsuser.getName(), (String)_itTemp.next());
655 }
656 }
657 JSUserRoles jsUserRoles = jsuser.getRoleString();
658 if (jsUserRoles != null)
659 listTemp = this.getTokens(jsUserRoles.toString());
660 else
661 listTemp = null;
662 if ((listTemp != null) && (listTemp.size()>0))
663 {
664 Iterator _itTemp = listTemp.iterator();
665 while (_itTemp.hasNext())
666 {
667 roleManager.addRoleToUser(jsuser.getName(), (String)_itTemp.next());
668 }
669 }
670 JSUserAttributes attributes = jsuser.getUserInfo();
671 if (attributes != null)
672 {
673 Preferences userAttributes = user.getUserAttributes();
674 HashMap map = attributes.getMyMap();
675 if (map != null)
676 {
677 Iterator _itTemp = map.keySet().iterator();
678 while (_itTemp.hasNext())
679 {
680 String userAttrName = (String)_itTemp.next();
681
682 String userAttrValue = (String)map.get(userAttrName);
683 userAttributes.put(userAttrName, userAttrValue);
684 }
685 }
686
687 }
688
689 JSNVPElements jsNVP = jsuser.getPreferences();
690 if ((jsNVP != null) && (jsNVP.getMyMap() != null))
691 {
692 Preferences preferences = user.getPreferences();
693 Iterator _itTemp = jsNVP.getMyMap().keySet().iterator();
694 while (_itTemp.hasNext())
695 {
696 String prefKey = (String)_itTemp.next();
697 String prefValue = (String)(jsNVP.getMyMap().get(prefKey));
698 preferences.put(prefKey,prefValue);
699 }
700 }
701
702 this.userMap.put(jsuser.getName(), getUserPrincipal(user));
703
704 }
705 } catch (Exception e)
706 {
707 e.printStackTrace();
708 throw new SerializerException(
709 SerializerException.CREATE_OBJECT_FAILED
710 .create(new String[]
711 { "User", e.getMessage()}));
712 }
713 }
714 logMe("recreateUsers - done");
715 recreatePermissions();
716 return;
717 }
718
719 /***
720 * called only after users have been established
721 * @throws SerializerException
722 */
723 private void recreateUserPrincipalRules() throws SerializerException
724 {
725 logMe("recreateUserPrincipalRules - started");
726
727 Profiler pm = (Profiler) getCM()
728 .getComponent("org.apache.jetspeed.profiler.Profiler");
729 if (pm == null)
730
731 throw new SerializerException(
732 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
733 .create("org.apache.jetspeed.profiler.Profiler"));
734 UserManager userManager = (UserManager) getCM()
735 .getComponent("org.apache.jetspeed.security.UserManager");
736 if (userManager == null)
737 throw new SerializerException(
738 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
739 .create("org.apache.jetspeed.security.UserManager"));
740
741
742
743 Iterator _itUsers = ((JSSeedData)getSnapshot()).getUsers().iterator();
744 while (_itUsers.hasNext())
745 {
746 JSUser _user = (JSUser) _itUsers.next();
747 JSPrincipalRules jsRules = _user.getRules();
748 try
749 {
750 User user = userManager.getUser(_user.getName());
751 Principal principal = getUserPrincipal(user);
752 if (jsRules != null)
753 {
754 Iterator _itRoles = jsRules.iterator();
755 while (_itRoles.hasNext())
756 {
757 JSPrincipalRule pr = (JSPrincipalRule) _itRoles.next();
758 ProfilingRule pRule = pm.getRule(pr.getRule());
759
760 try
761 {
762 PrincipalRule p1 = pm.createPrincipalRule();
763 p1.setLocatorName(pr.getLocator());
764 p1.setProfilingRule(pRule);
765 p1.setPrincipalName(principal.getName());
766 pm.storePrincipalRule(p1);
767 }
768 catch (Exception eRole)
769 {
770 eRole.printStackTrace();
771 }
772 }
773 }
774 }
775 catch (Exception eUser)
776 {
777 eUser.printStackTrace();
778 }
779 }
780 logMe("recreateUserPrincipalRules - done");
781
782 }
783 /***
784 * recreates all permissions from the current snapshot
785 *
786 * @throws SerializerException
787 */
788 private void recreatePermissions() throws SerializerException
789 {
790 logMe("recreatePermissions - started");
791 PermissionManager pm = (PermissionManager) getCM()
792 .getComponent("org.apache.jetspeed.security.PermissionManager");
793 if (pm == null)
794 throw new SerializerException(
795 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
796 .create("org.apache.jetspeed.security.PermissionManager"));
797
798 Iterator list = null;
799 try
800 {
801 list = ((JSSeedData)getSnapshot()).getPermissions().iterator();
802 } catch (Exception e)
803 {
804 throw new SerializerException(
805 SerializerException.GET_EXISTING_OBJECTS
806 .create(new String[]
807 { "Permissions", e.getMessage()}));
808 }
809
810 while (list.hasNext())
811 {
812 JSPermission _js = (JSPermission)list.next();
813 PortalResourcePermission perm = _js.getPermissionForType();
814 if ((perm != null) && (perm instanceof PortalResourcePermission))
815 {
816 try
817 {
818 pm.addPermission(perm);
819 ArrayList listTemp = null;
820 JSUserGroups jsUserGroups = _js.getGroupString();
821 if (jsUserGroups != null)
822 listTemp = this.getTokens(jsUserGroups.toString());
823 else
824 listTemp = null;
825 if ((listTemp != null) && (listTemp.size()>0))
826 {
827 Iterator _itTemp = listTemp.iterator();
828 while (_itTemp.hasNext())
829 {
830 Principal p = (Principal)this.groupMap.get((String)_itTemp.next());
831 if (p != null)
832 pm.grantPermission(p, perm);
833 }
834 }
835 JSUserRoles jsUserRoles = _js.getRoleString();
836 if (jsUserRoles != null)
837 listTemp = this.getTokens(jsUserRoles.toString());
838 else
839 listTemp = null;
840 if ((listTemp != null) && (listTemp.size()>0))
841 {
842 Iterator _itTemp = listTemp.iterator();
843 while (_itTemp.hasNext())
844 {
845 Principal p = (Principal)this.roleMap.get((String)_itTemp.next());
846 if (p != null)
847 pm.grantPermission(p, perm);
848 }
849 }
850 JSUserUsers jsUserUsers = _js.getUserString();
851 if (jsUserUsers != null)
852 listTemp = this.getTokens(jsUserUsers.toString());
853 else
854 listTemp = null;
855 if ((listTemp != null) && (listTemp.size()>0))
856 {
857 Iterator _itTemp = listTemp.iterator();
858 while (_itTemp.hasNext())
859 {
860 Principal p = (Principal)this.userMap.get((String)_itTemp.next());
861 if (p != null)
862 pm.grantPermission(p, perm);
863 }
864 }
865
866 }
867 catch (Exception e)
868 {
869 throw new SerializerException(
870 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
871 .create(new String[]
872 { "Permissions", e.getMessage()}));
873 }
874 }
875 }
876 logMe("recreatePermissions - done");
877 }
878
879 private Principal getUserPrincipal(User user)
880 {
881 Subject subject = user.getSubject();
882
883 Set principals = subject.getPrincipals();
884 Iterator list = principals.iterator();
885 while (list.hasNext())
886 {
887 BasePrincipal principal = (BasePrincipal) list.next();
888 String path = principal.getFullPath();
889 if (path.startsWith("/user/"))
890 return principal;
891 }
892 return null;
893
894 }
895
896 private void importProfiler()
897 {
898 System.out.println("importProfiler - processing");
899 try
900 {
901 recreateProfilingRules();
902 }
903 catch (Exception e)
904 {
905 e.printStackTrace();
906 }
907
908 try
909 {
910 this.recreateRolesGroupsUsers();
911 recreateUserPrincipalRules();
912
913 }
914 catch (Exception e)
915 {
916 e.printStackTrace();
917 }
918 }
919
920
921 /***
922 * The workhorse for importing data
923 *
924 * @param binding
925 * established XML binding
926 * @return
927 * @throws SerializerException
928 */
929 protected void processImport() throws SerializerException
930 {
931 this.logMe("*********reinstalling data*********");
932
933
934
935 if (this.getSetting(JetspeedSerializer.KEY_PROCESS_CAPABILITIES))
936 {
937 logMe("creating clients, mediatypes and mimetypes");
938 importCapabilitiesInfrastructure();
939 }
940
941 /***
942 * the order is important, since profiling rules are referenced by the user
943 *
944 */
945
946 if (this.getSetting(JetspeedSerializer.KEY_PROCESS_PROFILER))
947 {
948 logMe("collecting permissions, profiling rules and users/proups etc. etc.");
949 importProfiler();
950 } else
951 {
952 logMe("permissions, rules etc. skipped ");
953
954 if (this.getSetting(JetspeedSerializer.KEY_PROCESS_USERS))
955 {
956 logMe("creating users/roles/groups");
957 this.recreateRolesGroupsUsers();
958 }
959 else
960 {
961 logMe("users skipped - ensure we have valid users to work with");
962 exportUsers();
963 }
964 }
965 }
966
967 /***
968 * The workhorse for exporting data
969 *
970 * @param binding
971 * established XML binding
972 * @return
973 * @throws SerializerException
974 */
975 protected void processExport(String name, XMLBinding binding)
976 throws SerializerException
977 {
978 this.logMe("*********collecting data*********");
979 /*** first create the snapshot file */
980
981 setSnapshot(new JSSeedData(name));
982
983 setSnapshotData();
984
985 if (this.getSetting(JetspeedSerializer.KEY_PROCESS_CAPABILITIES))
986 {
987 logMe("collecting clients, mediatypes and mimetypes");
988 exportCapabilitiesInfrastructure();
989 } else
990 logMe("capabilities skipped");
991
992 if (this.getSetting(JetspeedSerializer.KEY_PROCESS_USERS))
993 {
994 logMe("collecting users");
995 exportUsers();
996
997 /*** permissions require users - hence inside this scope */
998 if (this.getSetting(JetspeedSerializer.KEY_PROCESS_PROFILER))
999 {
1000 logMe("collecting permissions, profiling rules etc.");
1001 this.getProfilingRules();
1002 } else
1003 logMe(" profiling rules etc. skipped");
1004
1005 } else
1006 logMe("users skipped");
1007
1008 }
1009
1010 /***
1011 * Setup the binding for the different classes, mapping each extracted class
1012 * to a unique tag name in the XML
1013 *
1014 * @param binding
1015 */
1016 protected void setupAliases(XMLBinding binding)
1017 {
1018 binding.setAlias(JSRole.class, "Role");
1019 binding.setAlias(JSRoles.class, "Roles");
1020 binding.setAlias(JSGroup.class, "Group");
1021 binding.setAlias(JSGroups.class, "Groups");
1022 binding.setAlias(JSUser.class, "User");
1023 binding.setAlias(JSUsers.class, "Users");
1024 binding.setAlias(JSNVPElements.class, "preferences");
1025 binding.setAlias(JSUserAttributes.class, "userinfo");
1026 binding.setAlias(JSSnapshot.class, "snapshot");
1027 binding.setAlias(JSUserRoles.class, "roles");
1028 binding.setAlias(JSUserGroups.class, "groups");
1029 binding.setAlias(JSClient.class, "Client");
1030 binding.setAlias(JSClients.class, "Clients");
1031 binding.setAlias(JSClientCapabilities.class, "capabilities");
1032 binding.setAlias(JSClientMimeTypes.class, "mimeTypes");
1033 binding.setAlias(JSMimeTypes.class, "MimeTypes");
1034 binding.setAlias(JSMimeType.class, "MimeType");
1035 binding.setAlias(JSCapabilities.class, "Capabilities");
1036 binding.setAlias(JSCapability.class, "Capability");
1037 binding.setAlias(JSMediaTypes.class, "MediaTypes");
1038 binding.setAlias(JSMediaType.class, "MediaType");
1039 binding.setAlias(JSUserUsers.class, "users");
1040
1041 binding.setAlias(JSPermissions.class, "Permissions");
1042 binding.setAlias(JSPermission.class, "Permission");
1043 binding.setAlias(JSProfilingRules.class, "ProfilingRules");
1044 binding.setAlias(JSProfilingRule.class, "ProfilingRule");
1045 binding.setAlias(JSRuleCriterions.class, "Criteria");
1046 binding.setAlias(JSRuleCriterion.class, "Criterion");
1047
1048 binding.setAlias(JSPrincipalRule.class, "Rule");
1049 binding.setAlias(JSPrincipalRules.class, "Rules");
1050
1051 binding.setAlias(String.class, "String");
1052 binding.setAlias(Integer.class, "int");
1053
1054 binding.setAlias(JSPWAttributes.class,"credentials");
1055
1056 binding.setClassAttribute(null);
1057
1058 }
1059
1060 /***
1061 * simple lookup for principal object from a map
1062 * @param map
1063 * @param _fullPath
1064 * @return
1065 */
1066
1067 private Object getObjectBehindPrinicpal(Map map, BasePrincipal _principal)
1068 {
1069 return getObjectBehindPath(map, _principal.getFullPath());
1070 }
1071
1072
1073
1074 /***
1075 * create a serializable wrapper for role
1076 *
1077 * @param role
1078 * @return
1079 */
1080 private JSRole createJSRole(Role role)
1081 {
1082 JSRole _role = new JSRole();
1083 _role.setName(role.getPrincipal().getName());
1084 return _role;
1085 }
1086
1087 /***
1088 * export roles
1089 *
1090 * @return
1091 */
1092 private void exportRoles() throws SerializerException
1093 {
1094 RoleManager roleManager = (RoleManager) getCM()
1095 .getComponent("org.apache.jetspeed.security.RoleManager");
1096 if (roleManager == null)
1097 throw new SerializerException(
1098 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1099 .create("org.apache.jetspeed.security.RoleManager"));
1100
1101 Iterator list = null;
1102 try
1103 {
1104 list = roleManager.getRoles("");
1105 } catch (Exception e)
1106 {
1107 throw new SerializerException(
1108 SerializerException.GET_EXISTING_OBJECTS
1109 .create(new String[]
1110 { "Role", e.getMessage()}));
1111 }
1112 while (list.hasNext())
1113 {
1114 try
1115 {
1116 Role role = (Role) list.next();
1117 JSRole _tempRole = (JSRole) getObjectBehindPrinicpal(roleMap,
1118 (BasePrincipal) (role.getPrincipal()));
1119 if (_tempRole == null)
1120 {
1121 _tempRole = createJSRole(role);
1122 roleMap.put(_tempRole.getName(), _tempRole);
1123 ((JSSeedData)getSnapshot()).getRoles().add(_tempRole);
1124 }
1125
1126 } catch (Exception e)
1127 {
1128 throw new SerializerException(
1129 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1130 .create(new String[]
1131 { "Role", e.getMessage()}));
1132 }
1133 }
1134 return;
1135 }
1136
1137 /*** Groups -------------------------- */
1138 /***
1139 * create a wrapper JSGroup object
1140 */
1141 private JSGroup createJSGroup(Group group)
1142 {
1143 JSGroup _group = new JSGroup();
1144 _group.setName(group.getPrincipal().getName());
1145 return _group;
1146 }
1147
1148 /***
1149 * extract the groups from the current environment
1150 *
1151 * @throws SerializerException
1152 */
1153 private void exportGroups() throws SerializerException
1154 {
1155 GroupManager groupManager = (GroupManager) getCM()
1156 .getComponent("org.apache.jetspeed.security.GroupManager");
1157 if (groupManager == null)
1158 throw new SerializerException(
1159 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1160 .create("org.apache.jetspeed.security.GroupManager"));
1161 Iterator list = null;
1162 try
1163 {
1164 list = groupManager.getGroups("");
1165 } catch (Exception e)
1166 {
1167 throw new SerializerException(
1168 SerializerException.GET_EXISTING_OBJECTS
1169 .create(new String[]
1170 { "Group", e.getMessage()}));
1171 }
1172 while (list.hasNext())
1173 {
1174
1175 try
1176 {
1177 Group group = (Group) list.next();
1178 JSGroup _tempGroup = (JSGroup) getObjectBehindPrinicpal(
1179 groupMap, (BasePrincipal) (group.getPrincipal()));
1180 if (_tempGroup == null)
1181 {
1182 _tempGroup = createJSGroup(group);
1183 groupMap.put(_tempGroup.getName(), _tempGroup);
1184 ((JSSeedData)getSnapshot()).getGroups().add(_tempGroup);
1185 }
1186
1187 } catch (Exception e)
1188 {
1189 throw new SerializerException(
1190 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1191 .create(new String[]
1192 { "Group", e.getMessage()}));
1193 }
1194 }
1195 return;
1196 }
1197
1198 /***
1199 * Add the credentials to the JSUser object.
1200 * <p>
1201 * If the credential provided is a PasswordCredential, userid and password
1202 * are extracted and set explcitely
1203 *
1204 * @param isPublic
1205 * public or private credential
1206 * @param newUser
1207 * the JS user object reference
1208 * @param credential
1209 * the credential object
1210 */
1211
1212 private void addJSUserCredentials(boolean isPublic, JSUser newUser,
1213 Object credential)
1214 {
1215 if (credential == null) return;
1216 if (credential instanceof PasswordCredential)
1217 {
1218 PasswordCredential pw = (PasswordCredential) credential;
1219 newUser.setUserCredential(pw.getUserName(), pw.getPassword(),pw.getExpirationDate(),pw.isEnabled(), pw.isExpired(), pw.isUpdateRequired());
1220 return;
1221 } else if (isPublic)
1222 newUser.addPublicCredential(credential);
1223 else
1224 newUser.addPrivateCredential(credential);
1225 }
1226
1227 /***
1228 * create a new JSUser object
1229 *
1230 * @param user
1231 * @return a new JSUser object
1232 */
1233 private JSUser createJSUser(User user)
1234 {
1235 JSUser _newUser = new JSUser();
1236
1237 Subject subject = user.getSubject();
1238
1239 Set principals = subject.getPrincipals();
1240 Iterator list = principals.iterator();
1241 while (list.hasNext())
1242 {
1243 BasePrincipal principal = (BasePrincipal) list.next();
1244 String path = principal.getFullPath();
1245 if (path.startsWith("/role/"))
1246 {
1247 JSRole _tempRole = (JSRole) this.getObjectBehindPath(roleMap,
1248 principal.getName());
1249 if (_tempRole != null)
1250 {
1251 _newUser.addRole(_tempRole);
1252 }
1253
1254 } else
1255 {
1256 if (path.startsWith("/group/"))
1257 {
1258 JSGroup _tempGroup = (JSGroup) this.getObjectBehindPath(
1259 groupMap, principal.getName());
1260 if (_tempGroup != null)
1261 {
1262 _newUser.addGroup(_tempGroup);
1263 }
1264
1265 } else if (path.startsWith("/user/"))
1266 _newUser.setPrincipal(principal);
1267
1268 }
1269
1270 }
1271
1272 Set credentials = subject.getPublicCredentials();
1273 list = credentials.iterator();
1274 while (list.hasNext())
1275 {
1276 Object credential = list.next();
1277 addJSUserCredentials(true, _newUser, credential);
1278 }
1279
1280 credentials = subject.getPrivateCredentials();
1281 list = credentials.iterator();
1282 while (list.hasNext())
1283 {
1284 Object credential = list.next();
1285 addJSUserCredentials(false, _newUser, credential);
1286 }
1287
1288 Preferences preferences = user.getPreferences();
1289 _newUser.setPreferences(preferences);
1290 preferences = user.getUserAttributes();
1291 _newUser.setUserInfo(preferences);
1292
1293 return _newUser;
1294 }
1295
1296
1297 /***
1298 * Collect all the roles, groups and users from the current environment.
1299 * Include the current SecurityProvider to understand, whether the password
1300 * collected can be used upon import
1301 *
1302 * @throws SerializerException
1303 */
1304
1305 private void exportUsers() throws SerializerException
1306 {
1307 /*** set the security provider info in the snapshot file */
1308 ((JSSeedData)getSnapshot()).setEncryption(getEncryptionString());
1309 /*** get the roles */
1310 exportRoles();
1311 /*** get the groups */
1312 exportGroups();
1313
1314 /*** users */
1315 UserManager userManager = (UserManager) getCM()
1316 .getComponent("org.apache.jetspeed.security.UserManager");
1317 if (userManager == null)
1318 throw new SerializerException(
1319 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1320 .create("org.apache.jetspeed.security.UserManager"));
1321 Iterator list = null;
1322 try
1323 {
1324 list = userManager.getUsers("");
1325 } catch (Exception e)
1326 {
1327 throw new SerializerException(
1328 SerializerException.GET_EXISTING_OBJECTS
1329 .create(new String[]
1330 { "User", e.getMessage()}));
1331 }
1332 while (list.hasNext())
1333 {
1334
1335 try
1336 {
1337 User _user = (User) list.next();
1338 JSUser _tempUser = createJSUser(_user);
1339 userMap.put(_tempUser.getName(), _tempUser);
1340 ((JSSeedData)getSnapshot()).getUsers().add(_tempUser);
1341 } catch (Exception e)
1342 {
1343 throw new SerializerException(
1344 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1345 .create(new String[]
1346 { "User", e.getMessage()}));
1347 }
1348
1349 }
1350 return;
1351
1352 }
1353
1354 /***
1355 * extract the capabilities and save in snapshot file
1356 *
1357 * @param caps
1358 * capability-container
1359 * @throws SerializerException
1360 */
1361 private void exportCapabilites(Capabilities caps)
1362 throws SerializerException
1363 {
1364
1365 Iterator list = caps.getCapabilities();
1366
1367 while (list.hasNext())
1368 {
1369 try
1370 {
1371 Capability _cp = (Capability) list.next();
1372 JSCapability _jsC = new JSCapability();
1373 _jsC.setName(_cp.getName());
1374 this.capabilityMap.put(_jsC.getName(), _jsC);
1375 this.capabilityMapInt.put(new Integer(_cp.getCapabilityId()), _jsC);
1376 ((JSSeedData)getSnapshot()).getCapabilities().add(_jsC);
1377 } catch (Exception e)
1378 {
1379 throw new SerializerException(
1380 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1381 .create(new String[]
1382 { "C", e.getMessage()}));
1383 }
1384 }
1385 return;
1386
1387 }
1388
1389 /***
1390 * exstract the mime types anmd save in snapshot file
1391 *
1392 * @param caps
1393 * capability container
1394 * @throws SerializerException
1395 */
1396 private void exportMimeTypes(Capabilities caps) throws SerializerException
1397 {
1398
1399 Iterator list = caps.getMimeTypes();
1400
1401 while (list.hasNext())
1402 {
1403 try
1404 {
1405 MimeType _mt = (MimeType) list.next();
1406 JSMimeType _jsM = new JSMimeType();
1407 _jsM.setName(_mt.getName());
1408 this.mimeMap.put(_jsM.getName(), _jsM);
1409 this.mimeMapInt.put(new Integer(_mt.getMimetypeId()), _jsM);
1410
1411 ((JSSeedData)getSnapshot()).getMimeTypes().add(_jsM);
1412 } catch (Exception e)
1413 {
1414 throw new SerializerException(
1415 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1416 .create(new String[]
1417 { "MimeType", e.getMessage()}));
1418 }
1419 }
1420 return;
1421
1422 }
1423
1424 /***
1425 * create a JS CLient
1426 *
1427 * @param c
1428 * the existing Client object
1429 * @return
1430 * @throws SerializerException
1431 */
1432
1433 private JSClient createJSClient(Client c) throws SerializerException
1434 {
1435 try
1436 {
1437 JSClient jsC = new JSClient(c);
1438
1439 Iterator _itM = c.getMimetypes().iterator();
1440 while (_itM.hasNext())
1441 {
1442 MimeType _m = (MimeType) _itM.next();
1443 JSMimeType _mt = (JSMimeType) mimeMap.get(_m.getName());
1444 if (_mt != null) jsC.getMimeTypes().add(_mt);
1445 }
1446
1447 Integer id = new Integer(c.getPreferredMimeTypeId());
1448 JSMimeType _mt = (JSMimeType) mimeMapInt.get(id);
1449 if (_mt != null)
1450 jsC.setPreferredMimeTypeID(_mt.getName());
1451 else
1452 jsC.setPreferredMimeTypeID("???");
1453
1454
1455 Iterator _itC = c.getCapabilities().iterator();
1456 while (_itC.hasNext())
1457 {
1458 Capability _c = (Capability) _itC.next();
1459 JSCapability _ct = (JSCapability) capabilityMap.get(_c
1460 .getName());
1461 if (_ct != null) jsC.getCapabilities().add(_ct);
1462 }
1463
1464 return jsC;
1465 } catch (Exception e)
1466 {
1467 throw new SerializerException(
1468 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1469 .create(new String[]
1470 { "Client", e.getMessage()}));
1471 }
1472
1473 }
1474
1475 /***
1476 * extract the current clients and save in the snapshot file
1477 *
1478 * @param list
1479 * @param caps
1480 * @return
1481 * @throws SerializerException
1482 */
1483 private void exportClients(Capabilities caps) throws SerializerException
1484 {
1485
1486 /*** first the the mime types */
1487 exportMimeTypes(caps);
1488
1489 /*** second get the capabilities */
1490 this.exportCapabilites(caps);
1491
1492 /*** now get the clients */
1493 Iterator _it = caps.getClients();
1494 while (_it.hasNext())
1495 {
1496 Client c = (Client) _it.next();
1497 JSClient jsC = createJSClient(c);
1498 if (jsC == null)
1499 throw new SerializerException(
1500 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1501 .create(new String[]
1502 { "Client", "createClient returned NULL"}));
1503 this.clientMap.put(jsC.getName(), jsC);
1504 ((JSSeedData)getSnapshot()).getClients().add(jsC);
1505 }
1506 return;
1507 }
1508
1509 /***
1510 * extract the media types and save in snapshot file
1511 *
1512 * @param caps
1513 * capabilit container
1514 * @throws SerializerException
1515 */
1516 private void exportMediaTypes(Capabilities caps) throws SerializerException
1517 {
1518 Iterator list = caps.getMediaTypes();
1519
1520 while (list.hasNext())
1521 {
1522 try
1523 {
1524 MediaType _mt = (MediaType) list.next();
1525 JSMediaType _jsM = new JSMediaType(_mt);
1526
1527 Iterator _itM = _mt.getMimetypes().iterator();
1528 while (_itM.hasNext())
1529 {
1530 MimeType _m = (MimeType) _itM.next();
1531 JSMimeType _mttype = (JSMimeType) mimeMap.get(_m.getName());
1532 if (_mttype != null) _jsM.getMimeTypes().add(_mttype);
1533 }
1534
1535 Iterator _itC = _mt.getCapabilities().iterator();
1536 while (_itC.hasNext())
1537 {
1538 Capability _c = (Capability) _itC.next();
1539 JSCapability _ct = (JSCapability) capabilityMap.get(_c
1540 .getName());
1541 if (_ct != null) _jsM.getCapabilities().add(_ct);
1542 }
1543 this.mediaMap.put(_jsM.getName(), _jsM);
1544 ((JSSeedData)getSnapshot()).getMediaTypes().add(_jsM);
1545 } catch (Exception e)
1546 {
1547
1548 throw new SerializerException(
1549 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1550 .create(new String[]
1551 { "MediaType", e.getMessage()}));
1552 }
1553 }
1554 return;
1555 }
1556
1557 /***
1558 * Extracts all capability related objects (clients, mimetypes and
1559 * mediatypes) and their relationships
1560 *
1561 * @return
1562 */
1563
1564 private void exportCapabilitiesInfrastructure() throws SerializerException
1565 {
1566 Capabilities caps = (Capabilities) getCM()
1567 .getComponent("org.apache.jetspeed.capabilities.Capabilities");
1568 if (caps == null)
1569 throw new SerializerException(
1570 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1571 .create("org.apache.jetspeed.capabilities.Capabilities"));
1572
1573 /***
1574 * get the clients (which in turn will get the mime types and
1575 * capailities)
1576 */
1577
1578 exportClients(caps);
1579
1580
1581 exportMediaTypes(caps);
1582
1583 }
1584
1585 /***
1586 * extract all permissions from the current environment
1587 *
1588 * @throws SerializerException
1589 */
1590 private void getPermissions() throws SerializerException
1591 {
1592 Object o = null;
1593 PermissionManager pm = (PermissionManager) getCM()
1594 .getComponent("org.apache.jetspeed.security.PermissionManager");
1595 if (pm == null)
1596 throw new SerializerException(
1597 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1598 .create("org.apache.jetspeed.security.PermissionManager"));
1599
1600 Iterator list = null;
1601 try
1602 {
1603 list = pm.getPermissions().iterator();
1604 } catch (Exception e)
1605 {
1606 throw new SerializerException(
1607 SerializerException.GET_EXISTING_OBJECTS
1608 .create(new String[]
1609 { "Permissions", e.getMessage()}));
1610 }
1611
1612 while (list.hasNext())
1613 {
1614 try
1615 {
1616 JSPermission _js = new JSPermission();
1617
1618 InternalPermission p = (InternalPermission) list.next();
1619 _js.setResource(p.getName());
1620 _js.setActions(p.getActions());
1621 _js.setId(p.getPermissionId());
1622 _js.setType(_js.getTypeForClass(p.getClassname()));
1623
1624 Iterator list2 = p.getPrincipals().iterator();
1625 while (list2.hasNext())
1626 {
1627 o = list2.next();
1628 InternalPrincipal principal = (InternalPrincipal) o;
1629 String path = principal.getFullPath();
1630 if (path.startsWith("/role/"))
1631 {
1632 JSRole _tempRole = (JSRole) this.getObjectBehindPath(
1633 roleMap, removeFromString(path, "/role/"));
1634 if (_tempRole != null)
1635 {
1636 _js.addRole(_tempRole);
1637 }
1638
1639 } else
1640 {
1641 if (path.startsWith("/group/"))
1642 {
1643 JSGroup _tempGroup = (JSGroup) this
1644 .getObjectBehindPath(groupMap,
1645 removeFromString(path, "/group/"));
1646 if (_tempGroup != null)
1647 {
1648 _js.addGroup(_tempGroup);
1649 }
1650
1651 } else
1652 {
1653 if (path.startsWith("/user/"))
1654 {
1655 JSUser _tempUser = (JSUser) this
1656 .getObjectBehindPath(
1657 userMap,
1658 removeFromString(path, "/user/"));
1659 if (_tempUser != null)
1660 {
1661 _js.addUser(_tempUser);
1662 }
1663
1664 }
1665
1666 }
1667
1668 }
1669 }
1670 this.permissionMap.put(_js.getType(), _js);
1671 ((JSSeedData)getSnapshot()).getPermissions().add(_js);
1672
1673 } catch (Exception e)
1674 {
1675 throw new SerializerException(
1676 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1677 .create(new String[]
1678 { "Permissions", e.getMessage()}));
1679 }
1680 }
1681 return;
1682
1683 }
1684
1685 /***
1686 * Create the Profiling Rule Wrapper
1687 *
1688 * @param p
1689 * @return
1690 */
1691 private JSProfilingRule createProfilingRule(ProfilingRule p, boolean standard)
1692 {
1693 JSProfilingRule rule = new JSProfilingRule();
1694
1695
1696
1697 rule.setStandardRule(standard);
1698 rule.setDescription(p.getTitle());
1699 rule.setId(p.getId());
1700
1701 Collection col = p.getRuleCriteria();
1702 Iterator keys = col.iterator();
1703 while (keys.hasNext())
1704 {
1705 RuleCriterion rc = (RuleCriterion) keys.next();
1706 rule.getCriterions().add(new JSRuleCriterion(rc));
1707 }
1708 return rule;
1709
1710 }
1711
1712 /***
1713 * read the permissions and then the profiling rules.
1714 * <p>
1715 * after that update the cross reference with the users
1716 *
1717 * @throws SerializerException
1718 */
1719 private void getProfilingRules() throws SerializerException
1720 {
1721 getPermissions();
1722 Profiler pm = (Profiler) getCM()
1723 .getComponent("org.apache.jetspeed.profiler.Profiler");
1724 if (pm == null)
1725
1726 throw new SerializerException(
1727 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1728 .create("org.apache.jetspeed.profiler.Profiler"));
1729 Class standardRuleClass = null;
1730 try
1731 {
1732 ProfilingRule tempStandardRule = pm.createProfilingRule(true);
1733 standardRuleClass = tempStandardRule.getClass();
1734 }
1735 catch (Exception e)
1736 {
1737 throw new SerializerException(
1738 SerializerException.CREATE_OBJECT_FAILED
1739 .create(new String[]
1740 { "Standard Rule", e.getMessage()}));
1741 }
1742
1743 Iterator list = null;
1744 try
1745 {
1746 list = pm.getRules().iterator();
1747 } catch (Exception e)
1748 {
1749 throw new SerializerException(
1750 SerializerException.GET_EXISTING_OBJECTS
1751 .create(new String[]
1752 { "ProfilingRules", e.getMessage()}));
1753 }
1754 while (list.hasNext())
1755 {
1756 try
1757 {
1758 ProfilingRule p = (ProfilingRule) list.next();
1759 if (!(this.rulesMap.containsKey(p.getId())))
1760 {
1761 JSProfilingRule rule = createProfilingRule(p, (standardRuleClass == p.getClass()));
1762 rulesMap.put(rule.getId(), rule);
1763 ((JSSeedData)getSnapshot()).getRules().add(rule);
1764
1765 }
1766 } catch (Exception e)
1767 {
1768 throw new SerializerException(
1769 SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1770 .create(new String[]
1771 { "ProfilingRules", e.getMessage()}));
1772 }
1773 }
1774
1775
1776 ProfilingRule defaultRule = pm.getDefaultRule();
1777 if (defaultRule != null)
1778 ((JSSeedData)getSnapshot()).setDefaultRule(defaultRule.getId());
1779
1780
1781
1782 Iterator _itUsers = this.userMap.values().iterator();
1783 while (_itUsers.hasNext())
1784 {
1785 JSUser _user = (JSUser) _itUsers.next();
1786 Principal principal = _user.getPrincipal();
1787 if (principal != null)
1788 {
1789 Collection col = pm.getRulesForPrincipal(principal);
1790 Iterator _itCol = col.iterator();
1791 while (_itCol.hasNext())
1792 {
1793 PrincipalRule p1 = (PrincipalRule) _itCol.next();
1794 JSPrincipalRule pr = new JSPrincipalRule(p1
1795 .getLocatorName(), p1.getProfilingRule().getId());
1796 _user.getRules().add(pr);
1797 }
1798 }
1799 }
1800
1801 return;
1802
1803 }
1804
1805 /***
1806 * Establish whether incoming passwords are "clear" text or whether they are
1807 * to be decoded. That however depends on whether the passwords were encoded
1808 * with the current active provider or not.
1809 *
1810 * @return
1811 */
1812 protected int compareCurrentSecurityProvider(JSSeedData file)
1813 {
1814 String _fileEncryption = file.getEncryption();
1815 if ((_fileEncryption == null) || (_fileEncryption.length() == 0))
1816 return NO_DECODING;
1817
1818 if (_fileEncryption.equals(getEncryptionString()))
1819 return PASSTHRU_REQUIRED;
1820 else
1821 return NO_DECODING;
1822 }
1823
1824 private String getEncryptionString()
1825 {
1826 PasswordCredentialProvider provider = (PasswordCredentialProvider) getCM()
1827 .getComponent("org.apache.jetspeed.security.spi.PasswordCredentialProvider");
1828 if (provider == null)
1829 {
1830 System.err
1831 .println("Error!!! PasswordCredentialProvider not available");
1832 return ENCODING_STRING;
1833 }
1834 try
1835 {
1836 PasswordCredential credential = provider.create(JETSPEED,ENCODING_STRING);
1837 if ((credential != null) && (credential.getPassword() != null))
1838 return new String(credential.getPassword());
1839 else
1840 return ENCODING_STRING;
1841 }
1842 catch (Exception e)
1843 {
1844 e.printStackTrace();
1845 return ENCODING_STRING;
1846 }
1847 }
1848
1849 /***
1850 * ++++++++++++++++++++++++++++++HELPERS
1851 * +++++++++++++++++++++++++++++++++++++++++++++
1852 */
1853
1854
1855
1856
1857 /***
1858 * recreate a rule criterion object from the deserialized wrapper
1859 * @param profiler established profile manager
1860 * @param jsr deserialized object
1861 * @return new RuleCriterion with content set to deserialized wrapepr
1862 * @throws SerializerException
1863 */
1864 protected RuleCriterion recreateRuleCriterion(Profiler profiler, JSRuleCriterion jsr, ProfilingRule rule)
1865 throws SerializerException, ClassNotFoundException
1866
1867 {
1868 try
1869 {
1870
1871 RuleCriterion c = profiler.createRuleCriterion();
1872 if (c == null)
1873 throw new SerializerException(
1874 SerializerException.CREATE_OBJECT_FAILED
1875 .create("org.apache.jetspeed.profiler.rules.RuleCriterion","returned null"));
1876 c.setFallbackOrder(jsr.getFallBackOrder());
1877 c.setFallbackType(jsr.getFallBackType());
1878 c.setName(jsr.getName());
1879 c.setType(jsr.getType());
1880 c.setValue(jsr.getValue());
1881 c.setRuleId(rule.getId());
1882 return c;
1883 }
1884 catch (Exception e)
1885 {
1886 SerializerException.CREATE_OBJECT_FAILED
1887 .create("org.apache.jetspeed.profiler.rules.RuleCriterion",e.getLocalizedMessage());
1888 return null;
1889 }
1890 }
1891
1892 /***
1893 * recreate a profiling rule object from the deserialized wrapper and store it
1894 * @param profiler established profile manager
1895 * @param jsp deserialized object
1896 * @
1897 * @throws SerializerException, ClassNotFoundException, ProfilerException
1898 */
1899 protected ProfilingRule recreateRule(Profiler profiler, ProfilingRule existingRule, JSProfilingRule jsp) throws SerializerException, ClassNotFoundException, ProfilerException
1900 {
1901 ProfilingRule rule = null;
1902 boolean existing = false;
1903
1904 if (existingRule == null)
1905 {
1906 rule = profiler.getRule(jsp.getId());
1907 if (jsp.isStandardRule())
1908 rule = profiler.createProfilingRule(true);
1909 else
1910 rule = profiler.createProfilingRule(false);
1911 rule.setId(jsp.getId());
1912 }
1913 else
1914 {
1915 rule = existingRule;
1916 existing = true;
1917 }
1918
1919 rule.setTitle(jsp.getDescription());
1920
1921 JSRuleCriterions col = jsp.getCriterions();
1922
1923 Iterator _it = col.iterator();
1924 while (_it.hasNext())
1925 {
1926 RuleCriterion c = recreateRuleCriterion(profiler, (JSRuleCriterion) _it.next(),rule);
1927 if (c != null)
1928 {
1929 Collection cHelp = rule.getRuleCriteria();
1930 if ((existing) && (cHelp.contains(c)))
1931 cHelp.remove(c);
1932 cHelp.add(c);
1933 }
1934 }
1935 return rule;
1936
1937 }
1938
1939
1940
1941 private void recreateProfilingRules () throws SerializerException
1942 {
1943 logMe("recreateProfilingRules - processing");
1944 Profiler pm = (Profiler) getCM()
1945 .getComponent("org.apache.jetspeed.profiler.Profiler");
1946 if (pm == null)
1947 throw new SerializerException(
1948 SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1949 .create("org.apache.jetspeed.profiler.Profiler"));
1950 JSProfilingRules rules = ((JSSeedData)getSnapshot()).getRules();
1951 if ((rules != null) && (rules.size() > 0))
1952 {
1953 Iterator _it = rules.iterator();
1954 while (_it.hasNext())
1955 {
1956 JSProfilingRule _c = (JSProfilingRule)_it.next();
1957
1958 try
1959 {
1960 ProfilingRule rule = null;
1961
1962 rule = pm.getRule(_c.getId());
1963 if ((rule == null) || (this.getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING)))
1964 {
1965 rule = recreateRule(pm,rule, _c);
1966 pm.storeProfilingRule(rule);
1967 }
1968 }
1969 catch (Exception e)
1970 {
1971 throw new SerializerException(
1972 SerializerException.CREATE_OBJECT_FAILED
1973 .create("org.apache.jetspeed.capabilities.Capabilities",e.getLocalizedMessage()));
1974 }
1975 }
1976 /*** reset the default profiling rule */
1977 String defaultRuleID = ((JSSeedData)getSnapshot()).getDefaultRule();
1978 if (defaultRuleID != null)
1979 {
1980 ProfilingRule defaultRule = pm.getRule(defaultRuleID);
1981 if (defaultRule != null)
1982 pm.setDefaultRule(defaultRuleID);
1983 }
1984 }
1985 else
1986 logMe("NO PROFILING RULES?????");
1987 logMe("recreateProfilingRules - done");
1988 }
1989
1990
1991
1992 }