File: SAMLObject.cpp

package info (click to toggle)
opensaml 1.1a-2
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 2,360 kB
  • ctags: 1,771
  • sloc: cpp: 10,614; sh: 8,329; makefile: 199; ansic: 23
file content (266 lines) | stat: -rw-r--r-- 8,254 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
/*
 *  Copyright 2001-2005 Internet2
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* SAMLObject.cpp - base class for all SAML constructs

   Scott Cantor
   2/21/02

   $History:$
*/

#include <xercesc/util/Base64.hpp>
#include <xercesc/util/XMLUTF8Transcoder.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
//#include <xsec/utils/XSECNameSpaceExpander.hpp>
//#include <xsec/canon/XSECC14n20010315.hpp>

#include "internal.h"
using namespace saml;

#include <sstream>
using namespace std;

SAMLObject::SAMLObject() : m_root(NULL), m_document(NULL), m_parent(NULL), m_bDirty(true), m_bOwnStrings(true), m_log(NULL)
{
    RTTI(SAMLObject);
}

SAMLObject::SAMLObject(istream& in) : m_root(NULL), m_document(NULL), m_parent(NULL), m_bDirty(true), m_bOwnStrings(true), m_log(NULL)
{
    RTTI(SAMLObject);
    try {
        XML::Parser p;
        XML::StreamInputSource src(in);
        Wrapper4InputSource dsrc(&src,false);
        m_document=p.parse(dsrc);
    }
    catch (XMLException& e) {
        auto_ptr_char buf(e.getMessage());
        SAML_log.error("caught an XML exception while parsing an istream&:\n"
                     "\tFile: %s\tLine: %u\n\tMessage: %s",
                     e.getSrcFile(),e.getSrcLine(),buf.get());
        throw MalformedException(string("SAMLObject() caught XML exception: ") + buf.get());
    }
}

SAMLObject::SAMLObject(istream& in, int minor)
    : m_root(NULL), m_document(NULL), m_parent(NULL), m_bDirty(true), m_bOwnStrings(true), m_log(NULL)
{
    RTTI(SAMLObject);
    try {
        XML::Parser p(minor);
        XML::StreamInputSource src(in);
        Wrapper4InputSource dsrc(&src,false);
        m_document=p.parse(dsrc);
    }
    catch (XMLException& e) {
        auto_ptr_char buf(e.getMessage());
        SAML_log.error("caught an XML exception while parsing an istream&:\n"
                     "\tFile: %s\tLine: %u\n\tMessage: %s",
                     e.getSrcFile(),e.getSrcLine(),buf.get());
        throw MalformedException(string("SAMLObject() caught XML exception: ") + buf.get());
    }
}

SAMLObject::~SAMLObject()
{
    if (m_document)
        m_document->release();
}

void SAMLObject::_RTTI(const char* classname)
{
    m_classname=classname;
    m_log=&log4cpp::Category::getInstance(string(SAML_LOGCAT) + '.' + m_classname);
}

SAMLObject* SAMLObject::setParent(SAMLObject* parent)
{
    if (m_parent)
        throw SAMLException("SAMLObject::setParent() called on an already-contained object");
    if (!parent)
        throw SAMLException("SAMLObject::setParent() called with null parameter");
    m_parent = parent;
    return this;
}

void SAMLObject::setDirty()
{
    m_bDirty=true;
    if (m_parent)
        m_parent->setDirty();
}

DOMNode* SAMLObject::toDOM(DOMDocument* doc, bool xmlns) const
{
    checkValidity();
    
    // Make sure we have a document to use.
    if (!doc) {
        if (m_root)
            doc=m_root->getOwnerDocument();     // favor an existing DOM
        else if (m_document)
            doc=m_document;                     // partial success earlier?
        else {                                  // go ahead and build a new one
            DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
            m_document=doc=impl->createDocument();
        }
    }
    
    // Are we built?
    if (m_root) {
        // Can we use what we have?
        if (!m_bDirty) {
            // If the DOM tree is already generated, compare the Documents.
            if (m_root->getOwnerDocument() != doc) {
                // We already built a tree. Just adopt it into the new document.
                // XXX: this won't actually work, Xerces-C doesn't support adoptNode
                // But there's no way to safely import because child objects won't get
                // notified and be able to update their roots.
                m_root = doc->adoptNode(m_root);
                
                // Release anything we have now.
                if (m_document) {
                    m_document->release();
                    m_document = NULL;
                }
            }
            return m_root;
        }
    }
    
    // This means we needed a new DOM, for whatever reason. We start by
    // letting each subtype construct its own root element.
    // The key here for dirty DOMs is to control Node memory mgmt sole via
    // the document owner. We count on the rest to be garbage collected
    // at that point, so if we end up orphaning part of a DOM we built
    // earlier, it should get cleaned up once the document is released.
    return m_root = buildRoot(doc,xmlns);
}

void SAMLObject::fromDOM(DOMElement* e)
{
    if (!e)
        throw MalformedException("SAMLObject::fromDOM() given an empty DOM");
    m_root = e;
    m_bOwnStrings = false;
    setClean();    // we have to be clean if we're starting from existing XML
}

DOMNode* SAMLObject::plantRoot()
{
    if (m_root)
    {
        DOMNode* domroot=m_root;
        while (domroot->getParentNode() && domroot->getParentNode()->getNodeType()!=DOMNode::DOCUMENT_NODE)
            domroot=domroot->getParentNode();

        DOMElement* e=m_root->getOwnerDocument()->getDocumentElement();
        if (e && e!=domroot)
            m_root->getOwnerDocument()->replaceChild(domroot,e);
        else if (!e)
            m_root->getOwnerDocument()->appendChild(domroot);
    }
    return m_root;
}

XMLByte* SAMLObject::toBase64(unsigned int* outputLength) const
{
    ostringstream os;
    os << *this;
    string osstr=os.str();
    return Base64::encode(reinterpret_cast<const XMLByte*>(osstr.data()),osstr.size(),outputLength);
}

namespace {
    static XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
}

ostream& saml::xmlout(ostream& target, const XMLCh* s)
{
    if (!s)
        return target;
    char* p=toUTF8(s);
    target << p;
    delete[] p;
    return target;
}

ostream& saml::operator<<(ostream& ostr, const DOMNode& node)
{
    static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
    DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
    DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
    serializer->setEncoding(UTF8);
    try
    {
        MemBufFormatTarget target;
        if (!serializer->writeNode(&target,node))
            throw SAMLException("operator <<: unable to serialize XML instance");
        unsigned int len=target.getLen();
        const XMLByte* byteptr=target.getRawBuffer();
        while (len--)
            ostr << (char)*(byteptr++);
    }
    catch (...)
    {
        serializer->release();
        throw;
    }
    serializer->release();
    return ostr;
}

ostream& saml::operator<<(ostream& ostr, const SAMLObject& obj)
{
    DOMNode* n=obj.toDOM();
    if (n)
        ostr << *n;
    else
        ostr << "{null SAML object}" << endl;
    return ostr;
}

char* saml::toUTF8(const XMLCh* src)
{
    unsigned int eaten;
    unsigned int srclen=XMLString::stringLen(src);
    XMLUTF8Transcoder t(UTF8, srclen*4 + 1);
    char* buf=new char[srclen*4 + 1];
    memset(buf,0,srclen*4 + 1);
    t.transcodeTo(
        src,srclen,
        reinterpret_cast<XMLByte*>(buf),srclen*4,
        eaten,XMLTranscoder::UnRep_RepChar);
    return buf;
}

XMLCh* saml::fromUTF8(const char* src)
{
    unsigned int eaten;
    unsigned int srclen=strlen(src);
    XMLUTF8Transcoder t(UTF8, srclen + 1);
    XMLCh* buf=new XMLCh[srclen + 1];
    unsigned char* sizes=new unsigned char[srclen];
    memset(buf,0,(srclen+1)*sizeof(XMLCh));
    t.transcodeFrom(
        reinterpret_cast<const XMLByte*>(src),srclen,
        buf,srclen,
        eaten,sizes);
    delete[] sizes;
    return buf;
}