001: /*
002: * Copyright (C) 2006, 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 12. January 2006 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.converters.extended;
012:
013: import com.thoughtworks.xstream.converters.MarshallingContext;
014: import com.thoughtworks.xstream.converters.UnmarshallingContext;
015: import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
016: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
017: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
018: import com.thoughtworks.xstream.mapper.Mapper;
019:
020: import javax.security.auth.Subject;
021:
022: import java.util.Collections;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Set;
026:
027: /**
028: * Converts a {@link Subject} instance. Note, that this Converter does only convert the contained Principals as
029: * it is done by JDK serialization, but not any credentials. For other behaviour you can derive your own converter,
030: * overload the appropriate methods and register it in the {@link com.thoughtworks.xstream.XStream}.
031: *
032: * @author Jörg Schaible
033: * @since 1.1.3
034: */
035: public class SubjectConverter extends AbstractCollectionConverter {
036:
037: public SubjectConverter(Mapper mapper) {
038: super (mapper);
039: }
040:
041: public boolean canConvert(Class type) {
042: return type.equals(Subject.class);
043: }
044:
045: public void marshal(Object source, HierarchicalStreamWriter writer,
046: MarshallingContext context) {
047: Subject subject = (Subject) source;
048: marshalPrincipals(subject.getPrincipals(), writer, context);
049: marshalPublicCredentials(subject.getPublicCredentials(),
050: writer, context);
051: marshalPrivateCredentials(subject.getPrivateCredentials(),
052: writer, context);
053: marshalReadOnly(subject.isReadOnly(), writer);
054: }
055:
056: protected void marshalPrincipals(Set principals,
057: HierarchicalStreamWriter writer, MarshallingContext context) {
058: writer.startNode("principals");
059: for (final Iterator iter = principals.iterator(); iter
060: .hasNext();) {
061: final Object principal = iter.next(); // pre jdk 1.4 a Principal was also in javax.security
062: writeItem(principal, context, writer);
063: }
064: writer.endNode();
065: };
066:
067: protected void marshalPublicCredentials(Set pubCredentials,
068: HierarchicalStreamWriter writer, MarshallingContext context) {
069: };
070:
071: protected void marshalPrivateCredentials(Set privCredentials,
072: HierarchicalStreamWriter writer, MarshallingContext context) {
073: };
074:
075: protected void marshalReadOnly(boolean readOnly,
076: HierarchicalStreamWriter writer) {
077: writer.startNode("readOnly");
078: writer.setValue(String.valueOf(readOnly));
079: writer.endNode();
080: };
081:
082: public Object unmarshal(HierarchicalStreamReader reader,
083: UnmarshallingContext context) {
084: Set principals = unmarshalPrincipals(reader, context);
085: Set publicCredentials = unmarshalPublicCredentials(reader,
086: context);
087: Set privateCredentials = unmarshalPrivateCredentials(reader,
088: context);
089: boolean readOnly = unmarshalReadOnly(reader);
090: return new Subject(readOnly, principals, publicCredentials,
091: privateCredentials);
092: }
093:
094: protected Set unmarshalPrincipals(HierarchicalStreamReader reader,
095: UnmarshallingContext context) {
096: return populateSet(reader, context);
097: };
098:
099: protected Set unmarshalPublicCredentials(
100: HierarchicalStreamReader reader,
101: UnmarshallingContext context) {
102: return Collections.EMPTY_SET;
103: };
104:
105: protected Set unmarshalPrivateCredentials(
106: HierarchicalStreamReader reader,
107: UnmarshallingContext context) {
108: return Collections.EMPTY_SET;
109: };
110:
111: protected boolean unmarshalReadOnly(HierarchicalStreamReader reader) {
112: reader.moveDown();
113: boolean readOnly = Boolean.getBoolean(reader.getValue());
114: reader.moveUp();
115: return readOnly;
116: };
117:
118: protected Set populateSet(HierarchicalStreamReader reader,
119: UnmarshallingContext context) {
120: Set set = new HashSet();
121: reader.moveDown();
122: while (reader.hasMoreChildren()) {
123: reader.moveDown();
124: Object elementl = readItem(reader, context, set);
125: reader.moveUp();
126: set.add(elementl);
127: }
128: reader.moveUp();
129: return set;
130: }
131: }
|