Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : /**************************************************************************
22 : TODO
23 : **************************************************************************
24 :
25 : *************************************************************************/
26 :
27 : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
28 : #include <comphelper/processfactory.hxx>
29 : #include <osl/socket.hxx>
30 : #include "ftpcontentprovider.hxx"
31 : #include "ftpcontent.hxx"
32 : #include "ftploaderthread.hxx"
33 :
34 : using namespace ftp;
35 : using namespace com::sun::star::lang;
36 : using namespace com::sun::star::container;
37 : using namespace com::sun::star::uno;
38 : using namespace com::sun::star::ucb;
39 : using namespace com::sun::star::beans;
40 :
41 : // ContentProvider Implementation.
42 :
43 3 : FTPContentProvider::FTPContentProvider( const Reference< XComponentContext >& rxContext)
44 : : ::ucbhelper::ContentProviderImplHelper(rxContext)
45 : , m_ftpLoaderThread(0)
46 3 : , m_pProxyDecider(0)
47 : {
48 3 : }
49 :
50 :
51 : // virtual
52 9 : FTPContentProvider::~FTPContentProvider()
53 : {
54 3 : delete m_ftpLoaderThread;
55 3 : delete m_pProxyDecider;
56 6 : }
57 :
58 : // XInterface methods.
59 59 : void SAL_CALL FTPContentProvider::acquire()
60 : throw()
61 : {
62 59 : OWeakObject::acquire();
63 59 : }
64 :
65 59 : void SAL_CALL FTPContentProvider::release()
66 : throw()
67 : {
68 59 : OWeakObject::release();
69 59 : }
70 :
71 23 : css::uno::Any SAL_CALL FTPContentProvider::queryInterface( const css::uno::Type & rType )
72 : throw( css::uno::RuntimeException, std::exception )
73 : {
74 : css::uno::Any aRet = cppu::queryInterface( rType,
75 : (static_cast< XTypeProvider* >(this)),
76 : (static_cast< XServiceInfo* >(this)),
77 : (static_cast< XContentProvider* >(this))
78 23 : );
79 23 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
80 : }
81 :
82 : // XTypeProvider methods.
83 0 : css::uno::Sequence< sal_Int8 > SAL_CALL FTPContentProvider::getImplementationId()
84 : throw( css::uno::RuntimeException,
85 : std::exception )
86 : {
87 0 : return css::uno::Sequence<sal_Int8>();
88 : }
89 :
90 1 : css::uno::Sequence< css::uno::Type > SAL_CALL FTPContentProvider::getTypes()
91 : throw( css::uno::RuntimeException,
92 : std::exception )
93 : {
94 : static cppu::OTypeCollection* pCollection = NULL;
95 1 : if ( !pCollection )
96 : {
97 1 : osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
98 1 : if ( !pCollection )
99 : {
100 : static cppu::OTypeCollection collection(
101 1 : cppu::UnoType<XTypeProvider>::get(),
102 1 : cppu::UnoType<XServiceInfo>::get(),
103 1 : cppu::UnoType<XContentProvider>::get()
104 2 : );
105 1 : pCollection = &collection;
106 1 : }
107 : }
108 1 : return (*pCollection).getTypes();
109 : }
110 :
111 :
112 :
113 : // XServiceInfo methods.
114 :
115 2 : OUString SAL_CALL FTPContentProvider::getImplementationName()
116 : throw( css::uno::RuntimeException, std::exception )
117 : {
118 2 : return getImplementationName_Static();
119 : }
120 :
121 8 : OUString FTPContentProvider::getImplementationName_Static()
122 : {
123 8 : return OUString("com.sun.star.comp.FTPContentProvider");
124 : }
125 :
126 0 : sal_Bool SAL_CALL FTPContentProvider::supportsService( const OUString& ServiceName )
127 : throw( css::uno::RuntimeException, std::exception )
128 : {
129 0 : return cppu::supportsService( this, ServiceName );
130 : }
131 :
132 1 : css::uno::Sequence< OUString > SAL_CALL FTPContentProvider::getSupportedServiceNames()
133 : throw( css::uno::RuntimeException, std::exception )
134 : {
135 1 : return getSupportedServiceNames_Static();
136 : }
137 :
138 : static css::uno::Reference< css::uno::XInterface > SAL_CALL
139 3 : FTPContentProvider_CreateInstance( const css::uno::Reference<
140 : css::lang::XMultiServiceFactory> & rSMgr )
141 : throw( css::uno::Exception )
142 : {
143 : css::lang::XServiceInfo* pX = static_cast<css::lang::XServiceInfo*>(
144 3 : new FTPContentProvider( ucbhelper::getComponentContext(rSMgr) ));
145 3 : return css::uno::Reference< css::uno::XInterface >::query( pX );
146 : }
147 :
148 4 : css::uno::Sequence< OUString > FTPContentProvider::getSupportedServiceNames_Static()
149 : {
150 4 : css::uno::Sequence< OUString > aSNS( 1 );
151 4 : aSNS.getArray()[ 0 ] = FTP_CONTENT_PROVIDER_SERVICE_NAME;
152 4 : return aSNS;
153 : }
154 :
155 : // Service factory implementation.
156 :
157 : css::uno::Reference< css::lang::XSingleServiceFactory >
158 3 : FTPContentProvider::createServiceFactory( const css::uno::Reference<
159 : css::lang::XMultiServiceFactory >& rxServiceMgr )
160 : {
161 : return css::uno::Reference<
162 : css::lang::XSingleServiceFactory >(
163 : cppu::createOneInstanceFactory(
164 : rxServiceMgr,
165 : FTPContentProvider::getImplementationName_Static(),
166 : FTPContentProvider_CreateInstance,
167 3 : FTPContentProvider::getSupportedServiceNames_Static() ) );
168 : }
169 :
170 : // XContentProvider methods.
171 :
172 : // virtual
173 2 : Reference<XContent> SAL_CALL FTPContentProvider::queryContent(
174 : const Reference< XContentIdentifier >& xCanonicId)
175 : throw( IllegalIdentifierException,
176 : RuntimeException,
177 : std::exception)
178 : {
179 : // Check, if a content with given id already exists...
180 2 : Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
181 2 : if(xContent.is())
182 0 : return xContent;
183 :
184 : // A new content has to be returned:
185 : {
186 : // Initialize
187 2 : osl::MutexGuard aGuard( m_aMutex );
188 2 : if(!m_ftpLoaderThread || !m_pProxyDecider)
189 : {
190 : try {
191 2 : init();
192 0 : } catch( ... ) {
193 0 : throw RuntimeException();
194 : }
195 :
196 2 : if(!m_ftpLoaderThread || !m_pProxyDecider)
197 0 : throw RuntimeException();
198 2 : }
199 : }
200 :
201 : try {
202 2 : FTPURL aURL(xCanonicId->getContentIdentifier(),
203 4 : this);
204 :
205 4 : if(!m_pProxyDecider->shouldUseProxy(
206 : OUString("ftp"),
207 : aURL.host(),
208 4 : aURL.port().toInt32()))
209 : {
210 2 : xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
211 2 : registerNewContent(xContent);
212 : }
213 : else {
214 : Reference<XContentProvider>
215 0 : xProvider(getHttpProvider());
216 0 : if(xProvider.is())
217 0 : return xProvider->queryContent(xCanonicId);
218 : else
219 0 : throw RuntimeException();
220 2 : }
221 0 : } catch(const malformed_exception&) {
222 0 : throw IllegalIdentifierException();
223 : }
224 :
225 : // may throw IllegalIdentifierException
226 2 : return xContent;
227 : }
228 :
229 2 : void FTPContentProvider::init()
230 : {
231 2 : m_ftpLoaderThread = new FTPLoaderThread();
232 2 : m_pProxyDecider = new ucbhelper::InternetProxyDecider( m_xContext );
233 2 : }
234 :
235 1 : CURL* FTPContentProvider::handle()
236 : {
237 : // Cannot be zero if called from here;
238 1 : return m_ftpLoaderThread->handle();
239 : }
240 :
241 :
242 1 : bool FTPContentProvider::forHost( const OUString& host,
243 : const OUString& port,
244 : const OUString& username,
245 : OUString& password,
246 : OUString& account)
247 : {
248 1 : osl::MutexGuard aGuard(m_aMutex);
249 1 : for(size_t i = 0; i < m_ServerInfo.size(); ++i)
250 3 : if(host == m_ServerInfo[i].host &&
251 2 : port == m_ServerInfo[i].port &&
252 1 : username == m_ServerInfo[i].username )
253 : {
254 1 : password = m_ServerInfo[i].password;
255 1 : account = m_ServerInfo[i].account;
256 1 : return true;
257 : }
258 :
259 0 : return false;
260 : }
261 :
262 1 : bool FTPContentProvider::setHost( const OUString& host,
263 : const OUString& port,
264 : const OUString& username,
265 : const OUString& password,
266 : const OUString& account)
267 : {
268 1 : ServerInfo inf;
269 1 : inf.host = host;
270 1 : inf.port = port;
271 1 : inf.username = username;
272 1 : inf.password = password;
273 1 : inf.account = account;
274 :
275 1 : bool present(false);
276 2 : osl::MutexGuard aGuard(m_aMutex);
277 1 : for(size_t i = 0; i < m_ServerInfo.size(); ++i)
278 0 : if(host == m_ServerInfo[i].host &&
279 0 : port == m_ServerInfo[i].port &&
280 0 : username == m_ServerInfo[i].username)
281 : {
282 0 : present = true;
283 0 : m_ServerInfo[i].password = password;
284 0 : m_ServerInfo[i].account = account;
285 : }
286 :
287 1 : if(!present)
288 1 : m_ServerInfo.push_back(inf);
289 :
290 2 : return !present;
291 : }
292 :
293 :
294 :
295 0 : Reference<XContentProvider> FTPContentProvider::getHttpProvider()
296 : throw(RuntimeException)
297 : {
298 : // used for access to ftp-proxy
299 0 : return UniversalContentBroker::create( m_xContext )->queryContentProvider("http:");
300 : }
301 :
302 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|