2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * shibsp/attribute/Attribute.cpp
20 * A resolved attribute.
26 # include "attribute/AttributeDecoder.h"
28 #include "attribute/SimpleAttribute.h"
29 #include "attribute/ScopedAttribute.h"
30 #include "attribute/NameIDAttribute.h"
31 #include "attribute/ExtensibleAttribute.h"
32 #include "attribute/XMLAttribute.h"
33 #include "util/SPConstants.h"
35 #include <xercesc/util/Base64.hpp>
36 #include <xercesc/util/XMLUniDefs.hpp>
38 using namespace shibsp;
39 using namespace xmltooling;
44 SHIBSP_DLLLOCAL Attribute* SimpleAttributeFactory(DDF& in) {
45 return new SimpleAttribute(in);
48 SHIBSP_DLLLOCAL Attribute* ScopedAttributeFactory(DDF& in) {
49 return new ScopedAttribute(in);
52 SHIBSP_DLLLOCAL Attribute* NameIDAttributeFactory(DDF& in) {
53 return new NameIDAttribute(in);
56 SHIBSP_DLLLOCAL Attribute* ExtensibleAttributeFactory(DDF& in) {
57 return new ExtensibleAttribute(in);
60 SHIBSP_DLLLOCAL Attribute* XMLAttributeFactory(DDF& in) {
61 return new XMLAttribute(in);
65 SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory StringAttributeDecoderFactory;
66 SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory ScopedAttributeDecoderFactory;
67 SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDAttributeDecoderFactory;
68 SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory NameIDFromScopedAttributeDecoderFactory;
69 SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory KeyInfoAttributeDecoderFactory;
70 SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory DOMAttributeDecoderFactory;
71 SHIBSP_DLLLOCAL PluginManager<AttributeDecoder,xmltooling::QName,const DOMElement*>::Factory XMLAttributeDecoderFactory;
73 static const XMLCh _StringAttributeDecoder[] = UNICODE_LITERAL_22(S,t,r,i,n,g,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
74 static const XMLCh _ScopedAttributeDecoder[] = UNICODE_LITERAL_22(S,c,o,p,e,d,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
75 static const XMLCh _NameIDAttributeDecoder[] = UNICODE_LITERAL_22(N,a,m,e,I,D,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
76 static const XMLCh _NameIDFromScopedAttributeDecoder[] = UNICODE_LITERAL_32(N,a,m,e,I,D,F,r,o,m,S,c,o,p,e,d,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
77 static const XMLCh _KeyInfoAttributeDecoder[] =UNICODE_LITERAL_23(K,e,y,I,n,f,o,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
78 static const XMLCh _DOMAttributeDecoder[] = UNICODE_LITERAL_19(D,O,M,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
79 static const XMLCh _XMLAttributeDecoder[] = UNICODE_LITERAL_19(X,M,L,A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
81 static const XMLCh caseSensitive[] = UNICODE_LITERAL_13(c,a,s,e,S,e,n,s,i,t,i,v,e);
82 static const XMLCh internal[] = UNICODE_LITERAL_8(i,n,t,e,r,n,a,l);
87 xmltooling::QName shibsp::StringAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _StringAttributeDecoder);
88 xmltooling::QName shibsp::ScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _ScopedAttributeDecoder);
89 xmltooling::QName shibsp::NameIDAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDAttributeDecoder);
90 xmltooling::QName shibsp::NameIDFromScopedAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _NameIDFromScopedAttributeDecoder);
91 xmltooling::QName shibsp::KeyInfoAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _KeyInfoAttributeDecoder);
92 xmltooling::QName shibsp::DOMAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _DOMAttributeDecoder);
93 xmltooling::QName shibsp::XMLAttributeDecoderType(shibspconstants::SHIB2ATTRIBUTEMAP_NS, _XMLAttributeDecoder);
95 void shibsp::registerAttributeDecoders()
97 SPConfig& conf = SPConfig::getConfig();
98 conf.AttributeDecoderManager.registerFactory(StringAttributeDecoderType, StringAttributeDecoderFactory);
99 conf.AttributeDecoderManager.registerFactory(ScopedAttributeDecoderType, ScopedAttributeDecoderFactory);
100 conf.AttributeDecoderManager.registerFactory(NameIDAttributeDecoderType, NameIDAttributeDecoderFactory);
101 conf.AttributeDecoderManager.registerFactory(NameIDFromScopedAttributeDecoderType, NameIDFromScopedAttributeDecoderFactory);
102 conf.AttributeDecoderManager.registerFactory(KeyInfoAttributeDecoderType, KeyInfoAttributeDecoderFactory);
103 conf.AttributeDecoderManager.registerFactory(DOMAttributeDecoderType, DOMAttributeDecoderFactory);
104 conf.AttributeDecoderManager.registerFactory(XMLAttributeDecoderType, XMLAttributeDecoderFactory);
107 AttributeDecoder::AttributeDecoder(const DOMElement *e) : m_caseSensitive(true), m_internal(false)
110 const XMLCh* flag = e->getAttributeNS(NULL, caseSensitive);
111 if (flag && (*flag == chLatin_f || *flag == chDigit_0))
112 m_caseSensitive = false;
114 flag = e->getAttributeNS(NULL, internal);
115 if (flag && (*flag == chLatin_t || *flag == chDigit_1))
121 void shibsp::registerAttributeFactories()
123 Attribute::registerFactory("", SimpleAttributeFactory);
124 Attribute::registerFactory("Simple", SimpleAttributeFactory);
125 Attribute::registerFactory("Scoped", ScopedAttributeFactory);
126 Attribute::registerFactory("NameID", NameIDAttributeFactory);
127 Attribute::registerFactory("Extensible", ExtensibleAttributeFactory);
128 Attribute::registerFactory("XML", XMLAttributeFactory);
131 map<string,Attribute::AttributeFactory*> Attribute::m_factoryMap;
133 Attribute::Attribute(DDF& in) : m_caseSensitive(in["case_insensitive"].isnull()), m_internal(!in["internal"].isnull())
135 const char* id = in.first().name();
139 throw AttributeException("No id found in marshalled attribute content.");
140 DDF aliases = in["aliases"];
141 if (aliases.islist()) {
142 DDF alias = aliases.first();
143 while (alias.isstring()) {
144 m_id.push_back(alias.string());
145 alias = aliases.next();
150 DDF Attribute::marshall() const
153 ddf.structure().addmember(m_id.front().c_str()).list();
154 if (!m_caseSensitive)
155 ddf.addmember("case_insensitive");
157 ddf.addmember("internal");
158 if (m_id.size() > 1) {
160 DDF aliases = ddf.addmember("aliases").list();
161 for (std::vector<std::string>::const_iterator a = m_id.begin() + 1; a != m_id.end(); ++a) {
162 alias = DDF(NULL).string(a->c_str());
169 Attribute* Attribute::unmarshall(DDF& in)
171 map<string,AttributeFactory*>::const_iterator i = m_factoryMap.find(in.name() ? in.name() : "");
172 if (i == m_factoryMap.end())
173 throw AttributeException("No registered factory for Attribute of type ($1).", params(1,in.name()));
174 return (i->second)(in);
177 const vector<string>& XMLAttribute::getSerializedValues() const
180 XMLByte *pos, *pos2;
\r
181 if (m_serialized.empty()) {
182 for (vector<string>::const_iterator i=m_values.begin(); i!=m_values.end(); ++i) {
183 XMLByte* enc = Base64::encode(reinterpret_cast<const XMLByte*>(i->data()), i->size(), &len);
185 for (pos=enc, pos2=enc; *pos2; pos2++)
\r
186 if (isgraph(*pos2))
\r
189 m_serialized.push_back(reinterpret_cast<char*>(enc));
190 #ifdef SHIBSP_XERCESC_HAS_XMLBYTE_RELEASE
191 XMLString::release(&enc);
193 XMLString::release((char**)&enc);
198 return Attribute::getSerializedValues();