Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
************************************************************************
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2011. (c) 2011.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits réservés
*
* NRC disclaims any warranties, Le CNRC dénie toute garantie
* expressed, implied, or énoncée, implicite ou légale,
* statutory, of any kind with de quelque nature que ce
* respect to the software, soit, concernant le logiciel,
* including without limitation y compris sans restriction
* any warranty of merchantability toute garantie de valeur
* or fitness for a particular marchande ou de pertinence
* purpose. NRC shall not be pour un usage particulier.
* liable in any event for any Le CNRC ne pourra en aucun cas
* damages, whether direct or être tenu responsable de tout
* indirect, special or general, dommage, direct ou indirect,
* consequential or incidental, particulier ou général,
* arising from the use of the accessoire ou fortuit, résultant
* software. Neither the name de l'utilisation du logiciel. Ni
* of the National Research le nom du Conseil National de
* Council of Canada nor the Recherches du Canada ni les noms
* names of its contributors may de ses participants ne peuvent
* be used to endorse or promote être utilisés pour approuver ou
* products derived from this promouvoir les produits dérivés
* software without specific prior de ce logiciel sans autorisation
* written permission. préalable et particulière
* par écrit.
*
* This file is part of the Ce fichier fait partie du projet
* OpenCADC project. OpenCADC.
*
* OpenCADC is free software: OpenCADC est un logiciel libre ;
* you can redistribute it and/or vous pouvez le redistribuer ou le
* modify it under the terms of modifier suivant les termes de
* the GNU Affero General Public la “GNU Affero General Public
* License as published by the License” telle que publiée
* Free Software Foundation, par la Free Software Foundation
* either version 3 of the : soit la version 3 de cette
* License, or (at your option) licence, soit (à votre gré)
* any later version. toute version ultérieure.
*
* OpenCADC is distributed in the OpenCADC est distribué
* hope that it will be useful, dans l’espoir qu’il vous
* but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE
* without even the implied GARANTIE : sans même la garantie
* warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ
* or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF
* PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence
* General Public License for Générale Publique GNU Affero
* more details. pour plus de détails.
*
* You should have received Vous devriez avoir reçu une
* a copy of the GNU Affero copie de la Licence Générale
* General Public License along Publique GNU Affero avec
* with OpenCADC. If not, see OpenCADC ; si ce n’est
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
* $Revision: 5 $
*
************************************************************************
*/
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.AC;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.GroupProperty;
import ca.nrc.cadc.ac.PersonalDetails;
import ca.nrc.cadc.ac.PosixDetails;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserDetails;
import ca.nrc.cadc.ac.UserRequest;
import ca.nrc.cadc.ac.WriterException;
Patrick Dowler
committed
import ca.nrc.cadc.auth.AuthenticationUtil;
import ca.nrc.cadc.auth.DNPrincipal;
import ca.nrc.cadc.auth.HttpPrincipal;
import ca.nrc.cadc.auth.IdentityType;
import ca.nrc.cadc.auth.NumericPrincipal;
import ca.nrc.cadc.auth.OpenIdPrincipal;
import ca.nrc.cadc.date.DateUtil;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import javax.security.auth.x500.X500Principal;
import java.io.IOException;
import java.io.Writer;
import java.security.Principal;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.List;
import java.util.Set;
/**
* AbstractReaderWriter TODO describe class
public abstract class AbstractReaderWriter
{
/**
* Write to root Element to a writer.
*
* @param root Root Element to write.
* @param writer Writer to write to.
* @throws IOException if the writer fails to write.
*/
protected void write(Element root, Writer writer)
throws IOException
{
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat());
outputter.output(new Document(root), writer);
}
/**
* Get a User object from a JDOM element.
* @param element The User JDOM element.
* @return A User object.
* @throws ReaderException
*/
protected final User<Principal> getUser(Element element)
throws ReaderException
{
// userID element of the User element
Element userIDElement = element.getChild("userID");
if (userIDElement == null)
String error = "userID element not found in user element";
throw new ReaderException(error);
}
// identity element of the userID element
Element userIDIdentityElement = userIDElement.getChild("identity");
if (userIDIdentityElement == null)
String error = "identity element not found in userID element";
throw new ReaderException(error);
}
Principal userID = getPrincipal(userIDIdentityElement);
User<Principal> user = new User<Principal>(userID);
// identities
Element identitiesElement = element.getChild("identities");
if (identitiesElement != null)
List<Element> identityElements = identitiesElement.getChildren("identity");
for (Element identityElement : identityElements)
user.getIdentities().add(getPrincipal(identityElement));
}
}
// details
Element detailsElement = element.getChild("details");
if (detailsElement != null)
List<Element> userDetailsElements = detailsElement.getChildren("userDetails");
for (Element userDetailsElement : userDetailsElements)
user.details.add(getUserDetails(userDetailsElement));
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}
/**
* Get a UserRequest object from a JDOM element.
*
* @param element The UserRequest JDOM element.
* @return A UserRequest object.
* @throws ReaderException
*/
protected final UserRequest<Principal> getUserRequest(Element element)
throws ReaderException
{
// user element of the UserRequest element
Element userElement = element.getChild("user");
if (userElement == null)
{
String error = "user element not found in userRequest element";
throw new ReaderException(error);
}
User<Principal> user = getUser(userElement);
// password element of the userRequest element
Element passwordElement = element.getChild("password");
if (passwordElement == null)
{
String error = "password element not found in userRequest element";
throw new ReaderException(error);
}
String password = passwordElement.getText();
return new UserRequest<Principal>(user, password.toCharArray());
}
/**
* Get a Principal object from a JDOM element.
* @param element The Principal JDOM element.
* @return A Principal object.
* @throws ReaderException
protected final Principal getPrincipal(Element element)
throws ReaderException
if (element == null)
{
String error = "null identity element";
throw new ReaderException(error);
}
if (!element.getName().equals("identity"))
{
String error = "expected identity element name, found " +
element.getName();
throw new ReaderException(error);
}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
String type = element.getAttributeValue("type");
if (type == null)
{
String error = "type attribute not found in identity element" +
element.getName();
throw new ReaderException(error);
}
String identity = element.getText();
Principal principal;
if (type.equals(IdentityType.OPENID.getValue()))
{
principal = new OpenIdPrincipal(identity);
}
else if (type.equals(IdentityType.CADC.getValue()))
{
Integer cadcID;
try
{
cadcID = Integer.valueOf(identity);
}
catch (NumberFormatException e)
{
String error = "Non-integer cadcID: " + identity;
throw new ReaderException(error);
}
principal = new NumericPrincipal(cadcID);
}
else if (type.equals(IdentityType.USERNAME.getValue()))
{
principal = new HttpPrincipal(identity);
}
else if (type.equals(IdentityType.X500.getValue()))
{
principal = new X500Principal(identity);
}
else if (type.equals(IdentityType.ENTRY_DN.getValue()))
{
principal = new DNPrincipal(identity);
}
else
{
String error = "Unknown type attribute: " + type;
throw new ReaderException(error);
}
return principal;
* Get a UserDetails object from a JDOM element.
* @param element The UserDetails JDOM element.
* @return A UserDetails object.
* @throws ReaderException
*/
protected final UserDetails getUserDetails(Element element)
throws ReaderException
{
throw new ReaderException("null UserDetails");
}
if (!element.getName().equals(UserDetails.NAME))
{
String error = "expected element name userDetails, found " +
element.getName();
throw new ReaderException(error);
}
String type = element.getAttributeValue(UserDetails.TYPE_ATTRIBUTE);
if (type == null)
String error = "userDetails missing required attribute type";
throw new ReaderException(error);
}
if (type.equals(PosixDetails.NAME))
{
return getPosixDetails(element);
}
if (type.equals(PersonalDetails.NAME))
{
return getPersonalDetails(element);
}
String error = "Unknown UserDetails attribute type " + type;
throw new ReaderException(error);
}
/**
* Get a PosixDetails object from a JDOM element.
*
* @param element The PosixDetails JDOM element.
* @return A PosixDetails object.
* @throws ReaderException
*/
protected final PosixDetails getPosixDetails(Element element)
throws ReaderException
{
// uid
Element uidElement = element.getChild(PosixDetails.UID);
if (uidElement == null)
String error = "posixDetails missing required element uid";
throw new ReaderException(error);
}
long uid;
try
{
uid = Long.valueOf(uidElement.getText());
}
catch (NumberFormatException e)
{
String error = "Cannot parse posixDetails uid to a long";
throw new ReaderException(error);
}
// gid
Element gidElement = element.getChild(PosixDetails.GID);
if (gidElement == null)
{
String error = "posixDetails missing required element gid";
throw new ReaderException(error);
}
long gid;
try
{
gid = Long.valueOf(gidElement.getText());
}
catch (NumberFormatException e)
{
String error = "Cannot parse posixDetails gid to a long";
throw new ReaderException(error);
// homeDirectory
Element homeDirElement = element.getChild(PosixDetails.HOME_DIRECTORY);
if (homeDirElement == null)
String error = "posixDetails missing required element homeDirectory";
throw new ReaderException(error);
String homeDirectory = homeDirElement.getText();
return new PosixDetails(uid, gid, homeDirectory);
* Get a PersonalDetails object from a JDOM element.
* @param element The PersonalDetails JDOM element.
* @return A PersonalDetails object.
* @throws ReaderException
protected final PersonalDetails getPersonalDetails(Element element)
throws ReaderException
// firstName
Element firstNameElement = element.getChild(PersonalDetails.FIRSTNAME);
if (firstNameElement == null)
{
String error = "personalDetails missing required element firstName";
throw new ReaderException(error);
}
String firstName = firstNameElement.getText();
// lastName
Element lastNameElement = element.getChild(PersonalDetails.LASTNAME);
if (lastNameElement == null)
{
String error = "personalDetails missing required element lastName";
throw new ReaderException(error);
}
String lastName = lastNameElement.getText();
PersonalDetails details = new PersonalDetails(firstName, lastName);
// email
Element emailElement = element.getChild(PersonalDetails.EMAIL);
if (emailElement != null)
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
details.email = emailElement.getText();
}
// address
Element addressElement = element.getChild(PersonalDetails.ADDRESS);
if (addressElement != null)
{
details.address = addressElement.getText();
}
// institute
Element instituteElement = element.getChild(PersonalDetails.INSTITUTE);
if (instituteElement != null)
{
details.institute = instituteElement.getText();
}
// city
Element cityElement = element.getChild(PersonalDetails.CITY);
if (cityElement != null)
{
details.city = cityElement.getText();
}
// country
Element countryElement = element.getChild(PersonalDetails.COUNTRY);
if (countryElement != null)
{
details.country = countryElement.getText();
}
return details;
}
/**
* Get a UserRequest object from a JDOM element.
*
* @param element The UserRequest JDOM element.
* @return A UserRequest object.
* @throws ReaderException
*/
protected final Group getGroup(Element element)
throws ReaderException
{
String uri = element.getAttributeValue("uri");
if (uri == null)
{
String error = "group missing required uri attribute";
throw new ReaderException(error);
}
// Group groupID
int index = uri.indexOf(AC.GROUP_URI);
if (index == -1)
{
String error = "group uri attribute malformed: " + uri;
throw new ReaderException(error);
}
String groupID = uri.substring(AC.GROUP_URI.length());
// Group owner
User<? extends Principal> user = null;
Element ownerElement = element.getChild("owner");
if (ownerElement != null)
{
// Owner user
Element userElement = ownerElement.getChild("user");
if (userElement == null)
String error = "owner missing required user element";
throw new ReaderException(error);
Group group = new Group(groupID, user);
// description
Element descriptionElement = element.getChild("description");
if (descriptionElement != null)
group.description = descriptionElement.getText();
}
// lastModified
Element lastModifiedElement = element.getChild("lastModified");
if (lastModifiedElement != null)
{
try
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC);
group.lastModified = df.parse(lastModifiedElement.getText());
}
catch (ParseException e)
{
String error = "Unable to parse group lastModified because " + e.getMessage();
throw new ReaderException(error);
}
}
// properties
Element propertiesElement = element.getChild("properties");
if (propertiesElement != null)
{
List<Element> propertyElements = propertiesElement.getChildren("property");
for (Element propertyElement : propertyElements)
{
group.getProperties().add(getGroupProperty(propertyElement));
}
}
// groupMembers
Element groupMembersElement = element.getChild("groupMembers");
if (groupMembersElement != null)
{
List<Element> groupElements = groupMembersElement.getChildren("group");
for (Element groupMember : groupElements)
{
group.getGroupMembers().add(getGroup(groupMember));
}
}
// userMembers
Element userMembersElement = element.getChild("userMembers");
if (userMembersElement != null)
{
List<Element> userElements = userMembersElement.getChildren("user");
for (Element userMember : userElements)
{
group.getUserMembers().add(getUser(userMember));
}
}
// groupAdmins
Element groupAdminsElement = element.getChild("groupAdmins");
if (groupAdminsElement != null)
{
List<Element> groupElements = groupAdminsElement.getChildren("group");
for (Element groupMember : groupElements)
{
group.getGroupAdmins().add(getGroup(groupMember));
}
}
// userAdmins
Element userAdminsElement = element.getChild("userAdmins");
if (userAdminsElement != null)
{
List<Element> userElements = userAdminsElement.getChildren("user");
for (Element userMember : userElements)
{
group.getUserAdmins().add(getUser(userMember));
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
}
/**
* Get a GroupProperty object from a JDOM element.
*
* @param element The GroupProperty JDOM element.
* @return A GroupProperty object.
* @throws ReaderException
*/
protected final GroupProperty getGroupProperty(Element element)
throws ReaderException
{
if (element == null)
{
String error = "null property element";
throw new ReaderException(error);
}
if (!element.getName().equals(GroupProperty.NAME))
{
String error = "expected property element name, found " +
element.getName();
throw new ReaderException(error);
}
String key = element.getAttributeValue(GroupProperty.KEY_ATTRIBUTE);
if (key == null)
{
String error = "required key attribute not found";
throw new ReaderException(error);
}
String type = element.getAttributeValue(GroupProperty.TYPE_ATTRIBUTE);
if (type == null)
{
String error = "required type attribute not found";
throw new ReaderException(error);
}
Object value;
if (type.equals(GroupProperty.STRING_TYPE))
{
value = String.valueOf(element.getText());
}
else
{
if (type.equals(GroupProperty.INTEGER_TYPE))
{
value = Integer.valueOf(element.getText());
}
else
{
String error = "Unsupported GroupProperty type: " + type;
throw new ReaderException(error);
}
}
Boolean readOnly = Boolean.valueOf(element.getAttributeValue(GroupProperty.READONLY_ATTRIBUTE));
return new GroupProperty(key, value, readOnly);
}
/**
* Get a JDOM element from a User object.
* @param user The User.
* @return A JDOM User representation.
* @throws WriterException
*/
protected final Element getElement(User<? extends Principal> user)
throws WriterException
{
// Create the user Element.
Element userElement = new Element("user");
// userID element
Element userIDElement = new Element("userID");
userIDElement.addContent(getElement(user.getUserID()));
userElement.addContent(userIDElement);
// identities
Set<Principal> identities = user.getIdentities();
if (identities.size() > 1) // includes alternate identies
Element identitiesElement = new Element("identities");
for (Principal identity : identities)
{
Patrick Dowler
committed
// userID is in this list, so only include alternate identities
// in the output
if (!AuthenticationUtil.equals(identity, user.getUserID()))
identitiesElement.addContent(getElement(identity));
}
userElement.addContent(identitiesElement);
// details
if (!user.details.isEmpty())
Element detailsElement = new Element("details");
Set<UserDetails> userDetails = user.details;
for (UserDetails userDetail : userDetails)
detailsElement.addContent(getElement(userDetail));
userElement.addContent(detailsElement);
* Get a JDOM element from a UserRequest object.
* @param userRequest The UserRequest.
* @return A JDOM UserRequest representation.
* @throws WriterException
*/
protected final Element getElement(UserRequest<? extends Principal> userRequest)
throws WriterException
{
// Create the userRequest Element.
Element userRequestElement = new Element("userRequest");
// user element
Element userElement = getElement(userRequest.getUser());
userRequestElement.addContent(userElement);
// password element
Element passwordElement = new Element("password");
passwordElement.setText(String.valueOf(userRequest.getPassword()));
userRequestElement.addContent(passwordElement);
return userRequestElement;
* Get a JDOM element from a Principal object.
* @param identity The Principal.
* @return A JDOM UserDetails representation.
* @throws WriterException
protected final Element getElement(Principal identity)
throws WriterException
String error = "null identity";
throw new WriterException(error);
Element identityElement = new Element("identity");
if ((identity instanceof HttpPrincipal))
identityElement.setAttribute("type", IdentityType.USERNAME.getValue());
else if ((identity instanceof NumericPrincipal))
identityElement.setAttribute("type", IdentityType.CADC.getValue());
else if ((identity instanceof OpenIdPrincipal))
identityElement.setAttribute("type", IdentityType.OPENID.getValue());
else if ((identity instanceof X500Principal))
identityElement.setAttribute("type", IdentityType.X500.getValue());
else if ((identity instanceof DNPrincipal))
{
identityElement.setAttribute("type", IdentityType.ENTRY_DN.getValue());
}
String error = "Unsupported Principal type " +
identity.getClass().getSimpleName();
throw new IllegalArgumentException(error);
identityElement.setText(identity.getName());
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
}
/**
* Get a JDOM element from a UserDetails object.
*
* @param details The UserDetails.
* @return A JDOM UserDetails representation.
* @throws WriterException
*/
protected final Element getElement(UserDetails details)
throws WriterException
{
if (details == null)
{
throw new WriterException("null UserDetails");
}
if ((details instanceof PosixDetails))
{
return getElement((PosixDetails) details);
}
if ((details instanceof PersonalDetails))
{
return getElement((PersonalDetails) details);
}
String error = "Unknown UserDetails implementation: " +
details.getClass().getName();
throw new WriterException(error);
}
/**
* Get a JDOM element from a PosixDetails object.
*
* @param details The PosixDetails.
* @return A JDOM PosixDetails representation.
*/
protected final Element getElement(PosixDetails details)
{
Element detailsElement = new Element(UserDetails.NAME);
detailsElement.setAttribute(UserDetails.TYPE_ATTRIBUTE,
PosixDetails.NAME);
Element uidElement = new Element(PosixDetails.UID);
uidElement.setText(String.valueOf(details.getUid()));
detailsElement.addContent(uidElement);
Element gidElement = new Element(PosixDetails.GID);
gidElement.setText(String.valueOf(details.getGid()));
detailsElement.addContent(gidElement);
Element homeDirElement = new Element(PosixDetails.HOME_DIRECTORY);
homeDirElement.setText(details.getHomeDirectory());
detailsElement.addContent(homeDirElement);
return detailsElement;
}
/**
* Get a JDOM element from a PersonalDetails object.
*
* @param details The PersonalDetails.
* @return JDOM PersonalDetails representation.
*/
protected final Element getElement(PersonalDetails details)
{
Element detailsElement = new Element(UserDetails.NAME);
detailsElement.setAttribute(UserDetails.TYPE_ATTRIBUTE,
PersonalDetails.NAME);
Element firstNameElement = new Element(PersonalDetails.FIRSTNAME);
firstNameElement.setText(details.getFirstName());
detailsElement.addContent(firstNameElement);
Element lastNameElement = new Element(PersonalDetails.LASTNAME);
lastNameElement.setText(details.getLastName());
detailsElement.addContent(lastNameElement);
if (details.email != null)
{
Element emailElement = new Element(PersonalDetails.EMAIL);
emailElement.setText(details.email);
detailsElement.addContent(emailElement);
}
if (details.address != null)
{
Element addressElement = new Element(PersonalDetails.ADDRESS);
addressElement.setText(details.address);
detailsElement.addContent(addressElement);
}
if (details.institute != null)
{
Element instituteElement = new Element(PersonalDetails.INSTITUTE);
instituteElement.setText(details.institute);
detailsElement.addContent(instituteElement);
}
if (details.city != null)
{
Element cityElement = new Element(PersonalDetails.CITY);
cityElement.setText(details.city);
detailsElement.addContent(cityElement);
}
if (details.country != null)
{
Element countryElement = new Element(PersonalDetails.COUNTRY);
countryElement.setText(details.country);
detailsElement.addContent(countryElement);
}
return detailsElement;
}
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Get a JDOM element from a Group object.
*
* @param group The UserRequest.
* @return A JDOM Group representation.
* @throws WriterException
*/
protected final Element getElement(Group group)
throws WriterException
{
return getElement(group, true);
}
/**
* Get a JDOM element from a Group object.
*
* @param group The UserRequest.
* @param deepCopy Return all Group elements.
* @return A JDOM Group representation.
* @throws WriterException
*/
protected final Element getElement(Group group, boolean deepCopy)
throws WriterException
{
// Create the root group element.
Element groupElement = new Element("group");
String groupURI = AC.GROUP_URI + group.getID();
groupElement.setAttribute(new Attribute("uri", groupURI));
// Group owner
if (group.getOwner() != null)
{
Element ownerElement = new Element("owner");
Element userElement = getElement(group.getOwner());
ownerElement.addContent(userElement);
groupElement.addContent(ownerElement);
}
if (deepCopy)
{
// Group description
if (group.description != null)
{
Element descriptionElement = new Element("description");
descriptionElement.setText(group.description);
groupElement.addContent(descriptionElement);
}
// lastModified
if (group.lastModified != null)
{
Element lastModifiedElement = new Element("lastModified");
DateFormat df = DateUtil.getDateFormat(DateUtil.IVOA_DATE_FORMAT, DateUtil.UTC);
lastModifiedElement.setText(df.format(group.lastModified));
groupElement.addContent(lastModifiedElement);
}
// Group properties
if (!group.getProperties().isEmpty())
{
Element propertiesElement = new Element("properties");
for (GroupProperty property : group.getProperties())
{
propertiesElement.addContent(getElement(property));
}
groupElement.addContent(propertiesElement);
}
// Group groupMembers.
if ((group.getGroupMembers() != null) && (!group.getGroupMembers().isEmpty()))
{
Element groupMembersElement = new Element("groupMembers");
for (Group groupMember : group.getGroupMembers())
{
groupMembersElement.addContent(getElement(groupMember, false));
}
groupElement.addContent(groupMembersElement);
}
// Group userMembers
if ((group.getUserMembers() != null) && (!group.getUserMembers().isEmpty()))
{
Element userMembersElement = new Element("userMembers");
for (User<? extends Principal> userMember : group.getUserMembers())
{
userMembersElement.addContent(getElement(userMember));
}
groupElement.addContent(userMembersElement);
}
// Group groupAdmins.
if ((group.getGroupAdmins() != null) && (!group.getGroupAdmins().isEmpty()))
{
Element groupAdminsElement = new Element("groupAdmins");
for (Group groupMember : group.getGroupAdmins())
{
groupAdminsElement.addContent(getElement(groupMember, false));
}
groupElement.addContent(groupAdminsElement);
}
// Group userAdmins
if ((group.getUserAdmins() != null) && (!group.getUserAdmins().isEmpty()))
{
Element userAdminsElement = new Element("userAdmins");
for (User<? extends Principal> userMember : group.getUserAdmins())
{
userAdminsElement.addContent(getElement(userMember));
}
groupElement.addContent(userAdminsElement);
}
}
return groupElement;
}
/**
* Get a JDOM element from a GroupProperty object.
*
* @param property The GroupProperty.
* @return A JDOM GroupProperty representation.
* @throws WriterException
*/
protected final Element getElement(GroupProperty property)
throws WriterException
{
if (property == null)
{
throw new WriterException("null GroupProperty");
}