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