update dev300-m58
[ooovba.git] / extensions / source / config / ldap / ldapuserprofilelayer.cxx
blob4f203ed89f69bfd1ea1c9d972fe02de1be1e4783
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ldapuserprofilelayer.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "ldapuserprofilelayer.hxx"
34 #include <com/sun/star/configuration/backend/PropertyInfo.hpp>
35 #include <com/sun/star/configuration/backend/ConnectionLostException.hpp>
36 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBUTE_HPP_
37 // on windows this is defined indirectly by <ldap.h>
38 #undef OPTIONAL
39 #include <com/sun/star/beans/PropertyAttribute.hpp>
40 #endif
41 #include <rtl/ustrbuf.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
44 //==============================================================================
45 namespace extensions { namespace config { namespace ldap {
47 static const sal_Unicode kPathSeparator = '/' ;
49 static
50 uno::Reference<backend::XLayerContentDescriber>
51 newLayerDescriber(const uno::Reference<lang::XMultiServiceFactory>& xFactory)
53 typedef uno::Reference<backend::XLayerContentDescriber> LayerDescriber;
55 rtl::OUString const k_sLayerDescriberService (
56 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.configuration.backend.LayerDescriber"));
58 LayerDescriber xResult(xFactory->createInstance(k_sLayerDescriberService), uno::UNO_QUERY_THROW);
59 return xResult;
61 //------------------------------------------------------------------------------
63 void LdapUserProfileSource::getUserProfile(const rtl::OUString & aUser, LdapUserProfile & aProfile)
65 mConnection.getUserProfile(aUser,
66 mProfileMap,
67 aProfile);
70 rtl::OUString LdapUserProfileSource::getComponentName() const
72 return mProfileMap.getComponentName();
75 rtl::OUString LdapUserProfileSource::getConfigurationBasePath() const
77 rtl::OUStringBuffer sComponentNameBuffer(mProfileMap.getComponentName());
78 sComponentNameBuffer.append(kPathSeparator);
79 sComponentNameBuffer.append (mProfileMap.getGroupName());
80 sComponentNameBuffer.append(kPathSeparator);
82 return sComponentNameBuffer.makeStringAndClear();
84 //------------------------------------------------------------------------------
86 struct LdapUserProfileLayer::ProfileData
88 LdapUserProfile mProfile;
89 rtl::OUString mBasePath;
91 explicit ProfileData(LdapUserProfileSource & aSource, const rtl::OUString & aUser)
93 aSource.getUserProfile(aUser, mProfile);
94 mBasePath = aSource.getConfigurationBasePath();
97 //------------------------------------------------------------------------------
99 LdapUserProfileLayer::LdapUserProfileLayer(
100 const uno::Reference<lang::XMultiServiceFactory>& xFactory,
101 const rtl::OUString& aUser,
102 const LdapUserProfileSourceRef & aUserProfileSource,
103 const rtl::OUString& aTimestamp)
104 : mLayerDescriber( newLayerDescriber(xFactory) )
105 , mSource( aUserProfileSource )
106 , mUser(aUser)
107 , mTimestamp(aTimestamp)
108 , mProfile( 0 )
110 OSL_ASSERT(mSource.is());
113 //------------------------------------------------------------------------------
114 LdapUserProfileLayer::~LdapUserProfileLayer()
116 delete mProfile;
118 //------------------------------------------------------------------------------
120 #define PROPNAME( name ) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( name ) )
121 #define PROPTYPE( type ) getCppuType( static_cast< type const *>( 0 ) )
123 const sal_Int32 LAYER_PROPERTY_URL = 1;
125 cppu::IPropertyArrayHelper * SAL_CALL LdapUserProfileLayer::newInfoHelper()
127 using com::sun::star::beans::Property;
128 using namespace com::sun::star::beans::PropertyAttribute;
130 Property properties[] =
132 Property(PROPNAME("URL"), LAYER_PROPERTY_URL, PROPTYPE(rtl::OUString), READONLY)
135 return new cppu::OPropertyArrayHelper(properties, sizeof(properties)/sizeof(properties[0]));
137 //------------------------------------------------------------------------------
139 void SAL_CALL LdapUserProfileLayer::getFastPropertyValue( uno::Any& rValue, sal_Int32 nHandle ) const
141 switch (nHandle)
143 case LAYER_PROPERTY_URL:
145 rtl::OUStringBuffer aURL;
146 aURL.appendAscii("ldap-user-profile:");
147 aURL.append(mUser);
148 aURL.append(sal_Unicode('@'));
149 if (mSource.is())
150 aURL.append(mSource->getComponentName());
151 else
152 aURL.appendAscii("<NULL>");
154 rValue <<= aURL.makeStringAndClear();
156 break;
158 default:
159 OSL_ENSURE(false, "Error: trying to get an UNKNOWN property");
160 break;
163 //------------------------------------------------------------------------------
165 bool LdapUserProfileLayer::readProfile()
167 if (mSource.is())
170 OSL_ASSERT(!mProfile);
171 mProfile = new ProfileData(*mSource,mUser);
173 mSource.clear();
175 catch (ldap::LdapConnectionException & e)
177 // without existing Ldap Connection we should never have gotten a timestamp
178 OSL_ENSURE(false, "Unexpected: Have Ldap Backedn Layer but no vaild LDAP connection ?!");
179 throw backend::ConnectionLostException(e.Message, *this, uno::makeAny(e) );
181 catch (ldap::LdapGenericException & e)
183 throw backend::BackendAccessException(e.Message, *this, uno::makeAny(e) );
185 OSL_ASSERT( !mSource.is() );
186 OSL_ASSERT( mProfile != 0 );
187 return mProfile != 0;
189 //------------------------------------------------------------------------------
191 void SAL_CALL LdapUserProfileLayer::readData(
192 const uno::Reference<backend::XLayerHandler>& xHandler)
193 throw ( backend::MalformedDataException,
194 lang::NullPointerException,
195 lang::WrappedTargetException,
196 uno::RuntimeException)
198 std::vector<backend::PropertyInfo> aPropList;
199 #ifdef SUPPRESS_BACKEND_ERRORS
201 #endif
202 if ( readProfile() )
204 // initialize PropInfo members that are the same for all settings
205 const rtl::OUString k_sTypeString(RTL_CONSTASCII_USTRINGPARAM("string"));
207 backend::PropertyInfo aPropInfo;
208 aPropInfo.Type = k_sTypeString;
209 aPropInfo.Protected = sal_False;
211 LdapUserProfile * pProfile = &mProfile->mProfile;
212 aPropList.reserve(pProfile->mProfile.size());
214 for (LdapUserProfile::Iterator entry = pProfile->mProfile.begin() ;
215 entry != pProfile->mProfile.end() ; ++ entry)
217 if (entry->mAttribute.getLength()==0) { continue ; }
218 if (entry->mValue.getLength()==0) { continue ; }
220 aPropInfo.Name = mProfile->mBasePath + entry->mAttribute;
221 aPropInfo.Value <<= entry->mValue;
223 aPropList.push_back(aPropInfo);
226 #ifdef SUPPRESS_BACKEND_ERRORS
227 catch (uno::Exception & e)
229 OSL_TRACE("LDAP Backend - Reading data from LDAP failed: %s\n",
230 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
232 #endif
234 if ( !aPropList.empty())
236 //Describe UserProfileLayer (the list of properties) to the XHandler
237 //Object using com.sun.star.comp.backend.LayerContentDescriber Service
238 uno::Sequence<backend::PropertyInfo> aPropInfoList(&aPropList.front(),aPropList.size());
240 mLayerDescriber->describeLayer(xHandler, aPropInfoList);
242 // else { check handler not NULL; xHandler->startLayer(); xHandler->endLayer(); }
244 //------------------------------------------------------------------------------
246 //------------------------------------------------------------------------------