1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2f86921cSAndrew Rist  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2f86921cSAndrew Rist  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19*2f86921cSAndrew Rist  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**************************************************************************
28cdf0e10cSrcweir 								TODO
29cdf0e10cSrcweir  **************************************************************************
30cdf0e10cSrcweir 
31cdf0e10cSrcweir  *************************************************************************/
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
34cdf0e10cSrcweir #include <osl/socket.hxx>
35cdf0e10cSrcweir #include "ftpcontentprovider.hxx"
36cdf0e10cSrcweir #include "ftpcontent.hxx"
37cdf0e10cSrcweir #include "ftploaderthread.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ftp;
41cdf0e10cSrcweir using namespace com::sun::star::lang;
42cdf0e10cSrcweir using namespace com::sun::star::container;
43cdf0e10cSrcweir using namespace com::sun::star::uno;
44cdf0e10cSrcweir using namespace com::sun::star::ucb;
45cdf0e10cSrcweir using namespace com::sun::star::beans;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //=========================================================================
50cdf0e10cSrcweir //=========================================================================
51cdf0e10cSrcweir //
52cdf0e10cSrcweir // ContentProvider Implementation.
53cdf0e10cSrcweir //
54cdf0e10cSrcweir //=========================================================================
55cdf0e10cSrcweir //=========================================================================
56cdf0e10cSrcweir 
FTPContentProvider(const Reference<XMultiServiceFactory> & rSMgr)57cdf0e10cSrcweir FTPContentProvider::FTPContentProvider(
58cdf0e10cSrcweir     const Reference< XMultiServiceFactory >& rSMgr)
59cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper(rSMgr),
60cdf0e10cSrcweir   m_ftpLoaderThread(0),
61cdf0e10cSrcweir   m_pProxyDecider(0)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir //=========================================================================
66cdf0e10cSrcweir // virtual
~FTPContentProvider()67cdf0e10cSrcweir FTPContentProvider::~FTPContentProvider()
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	delete m_ftpLoaderThread;
70cdf0e10cSrcweir 	delete m_pProxyDecider;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir //=========================================================================
74cdf0e10cSrcweir //
75cdf0e10cSrcweir // XInterface methods.
76cdf0e10cSrcweir //
77cdf0e10cSrcweir //=========================================================================
78cdf0e10cSrcweir 
79cdf0e10cSrcweir XINTERFACE_IMPL_3(FTPContentProvider,
80cdf0e10cSrcweir 				  XTypeProvider,
81cdf0e10cSrcweir 				  XServiceInfo,
82cdf0e10cSrcweir 				  XContentProvider)
83cdf0e10cSrcweir 
84cdf0e10cSrcweir //=========================================================================
85cdf0e10cSrcweir //
86cdf0e10cSrcweir // XTypeProvider methods.
87cdf0e10cSrcweir //
88cdf0e10cSrcweir //=========================================================================
89cdf0e10cSrcweir 
90cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3(FTPContentProvider,
91cdf0e10cSrcweir 					 XTypeProvider,
92cdf0e10cSrcweir 					 XServiceInfo,
93cdf0e10cSrcweir 					 XContentProvider)
94cdf0e10cSrcweir 
95cdf0e10cSrcweir //=========================================================================
96cdf0e10cSrcweir //
97cdf0e10cSrcweir // XServiceInfo methods.
98cdf0e10cSrcweir //
99cdf0e10cSrcweir //=========================================================================
100cdf0e10cSrcweir 
101cdf0e10cSrcweir XSERVICEINFO_IMPL_1(
102cdf0e10cSrcweir     FTPContentProvider,
103cdf0e10cSrcweir     rtl::OUString::createFromAscii("com.sun.star.comp.FTPContentProvider"),
104cdf0e10cSrcweir     rtl::OUString::createFromAscii(FTP_CONTENT_PROVIDER_SERVICE_NAME));
105cdf0e10cSrcweir 
106cdf0e10cSrcweir //=========================================================================
107cdf0e10cSrcweir //
108cdf0e10cSrcweir // Service factory implementation.
109cdf0e10cSrcweir //
110cdf0e10cSrcweir //=========================================================================
111cdf0e10cSrcweir 
112cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 
115cdf0e10cSrcweir //=========================================================================
116cdf0e10cSrcweir //
117cdf0e10cSrcweir // XContentProvider methods.
118cdf0e10cSrcweir //
119cdf0e10cSrcweir //=========================================================================
120cdf0e10cSrcweir 
121cdf0e10cSrcweir // virtual
122cdf0e10cSrcweir Reference<XContent> SAL_CALL
queryContent(const Reference<XContentIdentifier> & xCanonicId)123cdf0e10cSrcweir FTPContentProvider::queryContent(
124cdf0e10cSrcweir 	const Reference< XContentIdentifier >& xCanonicId
125cdf0e10cSrcweir )
126cdf0e10cSrcweir     throw(
127cdf0e10cSrcweir 		IllegalIdentifierException,
128cdf0e10cSrcweir 		RuntimeException
129cdf0e10cSrcweir 	)
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     // Check, if a content with given id already exists...
132cdf0e10cSrcweir     Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
133cdf0e10cSrcweir     if(xContent.is())
134cdf0e10cSrcweir         return xContent;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	// A new content has to be returned:
137cdf0e10cSrcweir 	{
138cdf0e10cSrcweir 		// Initialize
139cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
140cdf0e10cSrcweir         if(!m_ftpLoaderThread || !m_pProxyDecider)
141cdf0e10cSrcweir 		{
142cdf0e10cSrcweir 			try {
143cdf0e10cSrcweir 				init();
144cdf0e10cSrcweir 			} catch( ... ) {
145cdf0e10cSrcweir 				throw RuntimeException();
146cdf0e10cSrcweir 			}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 			if(!m_ftpLoaderThread || !m_pProxyDecider)
149cdf0e10cSrcweir 				throw RuntimeException();
150cdf0e10cSrcweir 		}
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	try {
154cdf0e10cSrcweir 		FTPURL aURL(xCanonicId->getContentIdentifier(),
155cdf0e10cSrcweir 					this);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 		if(!m_pProxyDecider->shouldUseProxy(
158cdf0e10cSrcweir 			rtl::OUString::createFromAscii("ftp"),
159cdf0e10cSrcweir 			aURL.host(),
160cdf0e10cSrcweir 			aURL.port().toInt32()))
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir 			xContent = new FTPContent(m_xSMgr,this,xCanonicId,aURL);
163cdf0e10cSrcweir             registerNewContent(xContent);
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir 		else {
166cdf0e10cSrcweir 			Reference<XContentProvider>
167cdf0e10cSrcweir 				xProvider(getHttpProvider());
168cdf0e10cSrcweir 			if(xProvider.is())
169cdf0e10cSrcweir 				return xProvider->queryContent(xCanonicId);
170cdf0e10cSrcweir 			else
171cdf0e10cSrcweir 				throw RuntimeException();
172cdf0e10cSrcweir 		}
173cdf0e10cSrcweir 	} catch(const malformed_exception&) {
174cdf0e10cSrcweir 		throw IllegalIdentifierException();
175cdf0e10cSrcweir 	}
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	// may throw IllegalIdentifierException
178cdf0e10cSrcweir     return xContent;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 
init()184cdf0e10cSrcweir void FTPContentProvider::init() {
185cdf0e10cSrcweir 	m_ftpLoaderThread = new FTPLoaderThread();
186cdf0e10cSrcweir 	m_pProxyDecider = new ucbhelper::InternetProxyDecider(m_xSMgr);
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 
handle()191cdf0e10cSrcweir CURL* FTPContentProvider::handle() {
192cdf0e10cSrcweir 	// Cannot be zero if called from here;
193cdf0e10cSrcweir 	return m_ftpLoaderThread->handle();
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
forHost(const rtl::OUString & host,const rtl::OUString & port,const rtl::OUString & username,rtl::OUString & password,rtl::OUString & account)197cdf0e10cSrcweir bool FTPContentProvider::forHost(
198cdf0e10cSrcweir     const rtl::OUString& host,
199cdf0e10cSrcweir     const rtl::OUString& port,
200cdf0e10cSrcweir     const rtl::OUString& username,
201cdf0e10cSrcweir     rtl::OUString& password,
202cdf0e10cSrcweir     rtl::OUString& account)
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
205cdf0e10cSrcweir     for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
206cdf0e10cSrcweir         if(host == m_ServerInfo[i].host &&
207cdf0e10cSrcweir            port == m_ServerInfo[i].port &&
208cdf0e10cSrcweir 		   username == m_ServerInfo[i].username )
209cdf0e10cSrcweir 		{
210cdf0e10cSrcweir             password = m_ServerInfo[i].password;
211cdf0e10cSrcweir             account = m_ServerInfo[i].account;
212cdf0e10cSrcweir             return true;
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     return false;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 
setHost(const rtl::OUString & host,const rtl::OUString & port,const rtl::OUString & username,const rtl::OUString & password,const rtl::OUString & account)219cdf0e10cSrcweir bool  FTPContentProvider::setHost(
220cdf0e10cSrcweir     const rtl::OUString& host,
221cdf0e10cSrcweir     const rtl::OUString& port,
222cdf0e10cSrcweir     const rtl::OUString& username,
223cdf0e10cSrcweir     const rtl::OUString& password,
224cdf0e10cSrcweir     const rtl::OUString& account)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir     ServerInfo inf;
227cdf0e10cSrcweir     inf.host = host;
228cdf0e10cSrcweir     inf.port = port;
229cdf0e10cSrcweir     inf.username = username;
230cdf0e10cSrcweir     inf.password = password;
231cdf0e10cSrcweir     inf.account = account;
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     bool present(false);
234cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
235cdf0e10cSrcweir     for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
236cdf0e10cSrcweir         if(host == m_ServerInfo[i].host &&
237cdf0e10cSrcweir            port == m_ServerInfo[i].port &&
238cdf0e10cSrcweir 		   username == m_ServerInfo[i].username)
239cdf0e10cSrcweir 		{
240cdf0e10cSrcweir 			present = true;
241cdf0e10cSrcweir 			m_ServerInfo[i].password = password;
242cdf0e10cSrcweir 			m_ServerInfo[i].account = account;
243cdf0e10cSrcweir 		}
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     if(!present)
246cdf0e10cSrcweir         m_ServerInfo.push_back(inf);
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     return !present;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 
253cdf0e10cSrcweir Reference<XContentProvider>
getHttpProvider()254cdf0e10cSrcweir FTPContentProvider::getHttpProvider()
255cdf0e10cSrcweir     throw(RuntimeException)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir 	// used for access to ftp-proxy
258cdf0e10cSrcweir 	ucbhelper::ContentBroker *pBroker = ucbhelper::ContentBroker::get();
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	if(pBroker) {
261cdf0e10cSrcweir 		Reference<XContentProviderManager > xManager(
262cdf0e10cSrcweir 			pBroker->getContentProviderManagerInterface());
263cdf0e10cSrcweir 
264cdf0e10cSrcweir 		if(xManager.is())
265cdf0e10cSrcweir 			return
266cdf0e10cSrcweir 				xManager->queryContentProvider(
267cdf0e10cSrcweir 					rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("http:")));
268cdf0e10cSrcweir 		else
269cdf0e10cSrcweir 			throw RuntimeException(
270cdf0e10cSrcweir 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
271cdf0e10cSrcweir 					"bad ucbhelper::ContentBroker")),
272cdf0e10cSrcweir 				*this);
273cdf0e10cSrcweir 	} else
274cdf0e10cSrcweir 		return 0;
275cdf0e10cSrcweir 
276cdf0e10cSrcweir }
277