https://issues.shibboleth.net/jira/browse/SSPCPP-632
[shibboleth/cpp-sp.git] / shibsp / attribute / XMLAttribute.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * XMLAttribute.cpp
23  *
24  * An Attribute whose values are serialized XML.
25  */
26
27 #include "internal.h"
28 #include "attribute/XMLAttribute.h"
29
30 #include <xercesc/util/Base64.hpp>
31
32 #ifndef SHIBSP_LITE
33 # include <xsec/framework/XSECDefs.hpp>
34 #endif
35
36 using namespace shibsp;
37 using namespace std;
38
39 namespace shibsp {
40     SHIBSP_DLLLOCAL Attribute* XMLAttributeFactory(DDF& in) {
41         return new XMLAttribute(in);
42     }
43 };
44
45 XMLAttribute::XMLAttribute(const vector<string>& ids) : Attribute(ids)
46 {
47 }
48
49 XMLAttribute::XMLAttribute(DDF& in) : Attribute(in)
50 {
51     DDF val = in.first().first();
52     while (val.string()) {
53         m_values.push_back(val.string());
54         val = in.first().next();
55     }
56 }
57
58 XMLAttribute::~XMLAttribute()
59 {
60 }
61
62 vector<string>& XMLAttribute::getValues()
63 {
64     return m_values;
65 }
66
67 const vector<string>& XMLAttribute::getValues() const
68 {
69     return m_values;
70 }
71
72 size_t XMLAttribute::valueCount() const
73 {
74     return m_values.size();
75 }
76
77 void XMLAttribute::clearSerializedValues()
78 {
79     m_serialized.clear();
80 }
81
82 const char* XMLAttribute::getString(size_t index) const
83 {
84     return m_values[index].c_str();
85 }
86
87 void XMLAttribute::removeValue(size_t index)
88 {
89     Attribute::removeValue(index);
90     if (index < m_values.size())
91         m_values.erase(m_values.begin() + index);
92 }
93
94 const vector<string>& XMLAttribute::getSerializedValues() const
95 {
96     xsecsize_t len;
97     XMLByte *pos, *pos2;
98     if (m_serialized.empty()) {
99         for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
100             XMLByte* enc = Base64::encode(reinterpret_cast<const XMLByte*>(i->data()), i->size(), &len);
101             if (enc) {
102                 for (pos=enc, pos2=enc; *pos2; pos2++)
103                     if (isgraph(*pos2))
104                         *pos++=*pos2;
105                 *pos=0;
106                 m_serialized.push_back(reinterpret_cast<char*>(enc));
107 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
108                 XMLString::release(&enc);
109 #else
110                 XMLString::release((char**)&enc);
111 #endif
112             }
113         }
114     }
115     return Attribute::getSerializedValues();
116 }
117
118 DDF XMLAttribute::marshall() const
119 {
120     DDF ddf = Attribute::marshall();
121     ddf.name("XML");
122     DDF vlist = ddf.first();
123     for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i)
124         vlist.add(DDF(nullptr).string(i->c_str()));
125     return ddf;
126 }