xref: /AOO41X/main/ucb/source/ucp/ftp/ftpcontentprovider.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucb.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /**************************************************************************
32*cdf0e10cSrcweir 								TODO
33*cdf0e10cSrcweir  **************************************************************************
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir  *************************************************************************/
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
38*cdf0e10cSrcweir #include <osl/socket.hxx>
39*cdf0e10cSrcweir #include "ftpcontentprovider.hxx"
40*cdf0e10cSrcweir #include "ftpcontent.hxx"
41*cdf0e10cSrcweir #include "ftploaderthread.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using namespace ftp;
45*cdf0e10cSrcweir using namespace com::sun::star::lang;
46*cdf0e10cSrcweir using namespace com::sun::star::container;
47*cdf0e10cSrcweir using namespace com::sun::star::uno;
48*cdf0e10cSrcweir using namespace com::sun::star::ucb;
49*cdf0e10cSrcweir using namespace com::sun::star::beans;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir //=========================================================================
54*cdf0e10cSrcweir //=========================================================================
55*cdf0e10cSrcweir //
56*cdf0e10cSrcweir // ContentProvider Implementation.
57*cdf0e10cSrcweir //
58*cdf0e10cSrcweir //=========================================================================
59*cdf0e10cSrcweir //=========================================================================
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir FTPContentProvider::FTPContentProvider(
62*cdf0e10cSrcweir     const Reference< XMultiServiceFactory >& rSMgr)
63*cdf0e10cSrcweir : ::ucbhelper::ContentProviderImplHelper(rSMgr),
64*cdf0e10cSrcweir   m_ftpLoaderThread(0),
65*cdf0e10cSrcweir   m_pProxyDecider(0)
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir //=========================================================================
70*cdf0e10cSrcweir // virtual
71*cdf0e10cSrcweir FTPContentProvider::~FTPContentProvider()
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	delete m_ftpLoaderThread;
74*cdf0e10cSrcweir 	delete m_pProxyDecider;
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir //=========================================================================
78*cdf0e10cSrcweir //
79*cdf0e10cSrcweir // XInterface methods.
80*cdf0e10cSrcweir //
81*cdf0e10cSrcweir //=========================================================================
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir XINTERFACE_IMPL_3(FTPContentProvider,
84*cdf0e10cSrcweir 				  XTypeProvider,
85*cdf0e10cSrcweir 				  XServiceInfo,
86*cdf0e10cSrcweir 				  XContentProvider)
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir //=========================================================================
89*cdf0e10cSrcweir //
90*cdf0e10cSrcweir // XTypeProvider methods.
91*cdf0e10cSrcweir //
92*cdf0e10cSrcweir //=========================================================================
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3(FTPContentProvider,
95*cdf0e10cSrcweir 					 XTypeProvider,
96*cdf0e10cSrcweir 					 XServiceInfo,
97*cdf0e10cSrcweir 					 XContentProvider)
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir //=========================================================================
100*cdf0e10cSrcweir //
101*cdf0e10cSrcweir // XServiceInfo methods.
102*cdf0e10cSrcweir //
103*cdf0e10cSrcweir //=========================================================================
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir XSERVICEINFO_IMPL_1(
106*cdf0e10cSrcweir     FTPContentProvider,
107*cdf0e10cSrcweir     rtl::OUString::createFromAscii("com.sun.star.comp.FTPContentProvider"),
108*cdf0e10cSrcweir     rtl::OUString::createFromAscii(FTP_CONTENT_PROVIDER_SERVICE_NAME));
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir //=========================================================================
111*cdf0e10cSrcweir //
112*cdf0e10cSrcweir // Service factory implementation.
113*cdf0e10cSrcweir //
114*cdf0e10cSrcweir //=========================================================================
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir //=========================================================================
120*cdf0e10cSrcweir //
121*cdf0e10cSrcweir // XContentProvider methods.
122*cdf0e10cSrcweir //
123*cdf0e10cSrcweir //=========================================================================
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir // virtual
126*cdf0e10cSrcweir Reference<XContent> SAL_CALL
127*cdf0e10cSrcweir FTPContentProvider::queryContent(
128*cdf0e10cSrcweir 	const Reference< XContentIdentifier >& xCanonicId
129*cdf0e10cSrcweir )
130*cdf0e10cSrcweir     throw(
131*cdf0e10cSrcweir 		IllegalIdentifierException,
132*cdf0e10cSrcweir 		RuntimeException
133*cdf0e10cSrcweir 	)
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir     // Check, if a content with given id already exists...
136*cdf0e10cSrcweir     Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
137*cdf0e10cSrcweir     if(xContent.is())
138*cdf0e10cSrcweir         return xContent;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	// A new content has to be returned:
141*cdf0e10cSrcweir 	{
142*cdf0e10cSrcweir 		// Initialize
143*cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
144*cdf0e10cSrcweir         if(!m_ftpLoaderThread || !m_pProxyDecider)
145*cdf0e10cSrcweir 		{
146*cdf0e10cSrcweir 			try {
147*cdf0e10cSrcweir 				init();
148*cdf0e10cSrcweir 			} catch( ... ) {
149*cdf0e10cSrcweir 				throw RuntimeException();
150*cdf0e10cSrcweir 			}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 			if(!m_ftpLoaderThread || !m_pProxyDecider)
153*cdf0e10cSrcweir 				throw RuntimeException();
154*cdf0e10cSrcweir 		}
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 	try {
158*cdf0e10cSrcweir 		FTPURL aURL(xCanonicId->getContentIdentifier(),
159*cdf0e10cSrcweir 					this);
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		if(!m_pProxyDecider->shouldUseProxy(
162*cdf0e10cSrcweir 			rtl::OUString::createFromAscii("ftp"),
163*cdf0e10cSrcweir 			aURL.host(),
164*cdf0e10cSrcweir 			aURL.port().toInt32()))
165*cdf0e10cSrcweir         {
166*cdf0e10cSrcweir 			xContent = new FTPContent(m_xSMgr,this,xCanonicId,aURL);
167*cdf0e10cSrcweir             registerNewContent(xContent);
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir 		else {
170*cdf0e10cSrcweir 			Reference<XContentProvider>
171*cdf0e10cSrcweir 				xProvider(getHttpProvider());
172*cdf0e10cSrcweir 			if(xProvider.is())
173*cdf0e10cSrcweir 				return xProvider->queryContent(xCanonicId);
174*cdf0e10cSrcweir 			else
175*cdf0e10cSrcweir 				throw RuntimeException();
176*cdf0e10cSrcweir 		}
177*cdf0e10cSrcweir 	} catch(const malformed_exception&) {
178*cdf0e10cSrcweir 		throw IllegalIdentifierException();
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	// may throw IllegalIdentifierException
182*cdf0e10cSrcweir     return xContent;
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir void FTPContentProvider::init() {
189*cdf0e10cSrcweir 	m_ftpLoaderThread = new FTPLoaderThread();
190*cdf0e10cSrcweir 	m_pProxyDecider = new ucbhelper::InternetProxyDecider(m_xSMgr);
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir CURL* FTPContentProvider::handle() {
196*cdf0e10cSrcweir 	// Cannot be zero if called from here;
197*cdf0e10cSrcweir 	return m_ftpLoaderThread->handle();
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir bool FTPContentProvider::forHost(
202*cdf0e10cSrcweir     const rtl::OUString& host,
203*cdf0e10cSrcweir     const rtl::OUString& port,
204*cdf0e10cSrcweir     const rtl::OUString& username,
205*cdf0e10cSrcweir     rtl::OUString& password,
206*cdf0e10cSrcweir     rtl::OUString& account)
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
209*cdf0e10cSrcweir     for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
210*cdf0e10cSrcweir         if(host == m_ServerInfo[i].host &&
211*cdf0e10cSrcweir            port == m_ServerInfo[i].port &&
212*cdf0e10cSrcweir 		   username == m_ServerInfo[i].username )
213*cdf0e10cSrcweir 		{
214*cdf0e10cSrcweir             password = m_ServerInfo[i].password;
215*cdf0e10cSrcweir             account = m_ServerInfo[i].account;
216*cdf0e10cSrcweir             return true;
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     return false;
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir bool  FTPContentProvider::setHost(
224*cdf0e10cSrcweir     const rtl::OUString& host,
225*cdf0e10cSrcweir     const rtl::OUString& port,
226*cdf0e10cSrcweir     const rtl::OUString& username,
227*cdf0e10cSrcweir     const rtl::OUString& password,
228*cdf0e10cSrcweir     const rtl::OUString& account)
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir     ServerInfo inf;
231*cdf0e10cSrcweir     inf.host = host;
232*cdf0e10cSrcweir     inf.port = port;
233*cdf0e10cSrcweir     inf.username = username;
234*cdf0e10cSrcweir     inf.password = password;
235*cdf0e10cSrcweir     inf.account = account;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     bool present(false);
238*cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
239*cdf0e10cSrcweir     for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
240*cdf0e10cSrcweir         if(host == m_ServerInfo[i].host &&
241*cdf0e10cSrcweir            port == m_ServerInfo[i].port &&
242*cdf0e10cSrcweir 		   username == m_ServerInfo[i].username)
243*cdf0e10cSrcweir 		{
244*cdf0e10cSrcweir 			present = true;
245*cdf0e10cSrcweir 			m_ServerInfo[i].password = password;
246*cdf0e10cSrcweir 			m_ServerInfo[i].account = account;
247*cdf0e10cSrcweir 		}
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     if(!present)
250*cdf0e10cSrcweir         m_ServerInfo.push_back(inf);
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir     return !present;
253*cdf0e10cSrcweir }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir Reference<XContentProvider>
258*cdf0e10cSrcweir FTPContentProvider::getHttpProvider()
259*cdf0e10cSrcweir     throw(RuntimeException)
260*cdf0e10cSrcweir {
261*cdf0e10cSrcweir 	// used for access to ftp-proxy
262*cdf0e10cSrcweir 	ucbhelper::ContentBroker *pBroker = ucbhelper::ContentBroker::get();
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 	if(pBroker) {
265*cdf0e10cSrcweir 		Reference<XContentProviderManager > xManager(
266*cdf0e10cSrcweir 			pBroker->getContentProviderManagerInterface());
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 		if(xManager.is())
269*cdf0e10cSrcweir 			return
270*cdf0e10cSrcweir 				xManager->queryContentProvider(
271*cdf0e10cSrcweir 					rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("http:")));
272*cdf0e10cSrcweir 		else
273*cdf0e10cSrcweir 			throw RuntimeException(
274*cdf0e10cSrcweir 				rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
275*cdf0e10cSrcweir 					"bad ucbhelper::ContentBroker")),
276*cdf0e10cSrcweir 				*this);
277*cdf0e10cSrcweir 	} else
278*cdf0e10cSrcweir 		return 0;
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir }
281