1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_webdav.hxx"
26 #include "DAVSessionFactory.hxx"
27 #include "SerfSession.hxx"
28 #include "SerfUri.hxx"
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 
31 using namespace http_dav_ucp;
32 using namespace com::sun::star;
33 
34 DAVSessionFactory::~DAVSessionFactory()
35 {
36 }
37 
38 rtl::Reference< DAVSession > DAVSessionFactory::createDAVSession(
39 				const ::rtl::OUString & inUri,
40 				const uno::Reference< lang::XMultiServiceFactory > & rxSMgr )
41     throw( DAVException )
42 {
43     m_xMSF = rxSMgr;
44 
45     osl::MutexGuard aGuard( m_aMutex );
46 
47     if ( !m_xProxyDecider.get() )
48         m_xProxyDecider.reset( new ucbhelper::InternetProxyDecider( rxSMgr ) );
49 
50     Map::iterator aIt( m_aMap.begin() );
51     Map::iterator aEnd( m_aMap.end() );
52 
53     while ( aIt != aEnd )
54 	{
55         if ( (*aIt).second->CanUse( inUri ) )
56 			break;
57 
58         ++aIt;
59 	}
60 
61     if ( aIt == aEnd )
62     {
63         SerfUri aURI( inUri );
64 
65         std::auto_ptr< DAVSession > xElement(
66             new SerfSession( this, inUri, *m_xProxyDecider.get() ) );
67 
68         aIt = m_aMap.insert( Map::value_type( inUri, xElement.get() ) ).first;
69         aIt->second->m_aContainerIt = aIt;
70         xElement.release();
71         return aIt->second;
72     }
73     else if ( osl_incrementInterlockedCount( &aIt->second->m_nRefCount ) > 1 )
74     {
75         rtl::Reference< DAVSession > xElement( aIt->second );
76         osl_decrementInterlockedCount( &aIt->second->m_nRefCount );
77         return xElement;
78     }
79     else
80     {
81         osl_decrementInterlockedCount( &aIt->second->m_nRefCount );
82         aIt->second->m_aContainerIt = m_aMap.end();
83 
84         // If URL scheme is different from http or https we definitely
85         // have to use a proxy and therefore can optimize the getProxy
86         // call a little:
87         SerfUri aURI( inUri );
88 
89         aIt->second = new SerfSession( this, inUri, *m_xProxyDecider.get() );
90         aIt->second->m_aContainerIt = aIt;
91         return aIt->second;
92     }
93 }
94 
95 void DAVSessionFactory::releaseElement( DAVSession * pElement ) SAL_THROW(())
96 {
97     OSL_ASSERT( pElement );
98     osl::MutexGuard aGuard( m_aMutex );
99     if ( pElement->m_aContainerIt != m_aMap.end() )
100         m_aMap.erase( pElement->m_aContainerIt );
101 }
102 
103