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 4 : FTPContentProvider::FTPContentProvider( const Reference< XComponentContext >& rxContext)
44 : : ::ucbhelper::ContentProviderImplHelper(rxContext)
45 : , m_ftpLoaderThread(0)
46 4 : , m_pProxyDecider(0)
47 : {
48 4 : }
49 :
50 :
51 : // virtual
52 12 : FTPContentProvider::~FTPContentProvider()
53 : {
54 4 : delete m_ftpLoaderThread;
55 4 : delete m_pProxyDecider;
56 8 : }
57 :
58 : // XInterface methods.
59 102 : void SAL_CALL FTPContentProvider::acquire()
60 : throw()
61 : {
62 102 : OWeakObject::acquire();
63 102 : }
64 :
65 102 : void SAL_CALL FTPContentProvider::release()
66 : throw()
67 : {
68 102 : OWeakObject::release();
69 102 : }
70 :
71 38 : 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 38 : );
79 38 : 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 2 : 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 2 : if ( !pCollection )
96 : {
97 2 : osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
98 2 : if ( !pCollection )
99 : {
100 : static cppu::OTypeCollection collection(
101 2 : cppu::UnoType<XTypeProvider>::get(),
102 2 : cppu::UnoType<XServiceInfo>::get(),
103 2 : cppu::UnoType<XContentProvider>::get()
104 4 : );
105 2 : pCollection = &collection;
106 2 : }
107 : }
108 2 : 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 10 : OUString FTPContentProvider::getImplementationName_Static()
122 : {
123 10 : 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 0 : css::uno::Sequence< OUString > SAL_CALL FTPContentProvider::getSupportedServiceNames()
133 : throw( css::uno::RuntimeException, std::exception )
134 : {
135 0 : return getSupportedServiceNames_Static();
136 : }
137 :
138 : static css::uno::Reference< css::uno::XInterface > SAL_CALL
139 4 : FTPContentProvider_CreateInstance( const css::uno::Reference<
140 : css::lang::XMultiServiceFactory> & rSMgr )
141 : throw( css::uno::Exception )
142 : {
143 : css::lang::XServiceInfo* pX = (css::lang::XServiceInfo*)
144 4 : new FTPContentProvider( ucbhelper::getComponentContext(rSMgr) );
145 4 : 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 ] = OUString(FTP_CONTENT_PROVIDER_SERVICE_NAME);
152 4 : return aSNS;
153 : }
154 :
155 : // Service factory implementation.
156 :
157 : css::uno::Reference< css::lang::XSingleServiceFactory >
158 4 : 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 4 : FTPContentProvider::getSupportedServiceNames_Static() ) );
168 : }
169 :
170 : // XContentProvider methods.
171 :
172 : // virtual
173 4 : 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 4 : Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
181 4 : if(xContent.is())
182 0 : return xContent;
183 :
184 : // A new content has to be returned:
185 : {
186 : // Initialize
187 4 : osl::MutexGuard aGuard( m_aMutex );
188 4 : if(!m_ftpLoaderThread || !m_pProxyDecider)
189 : {
190 : try {
191 4 : init();
192 0 : } catch( ... ) {
193 0 : throw RuntimeException();
194 : }
195 :
196 4 : if(!m_ftpLoaderThread || !m_pProxyDecider)
197 0 : throw RuntimeException();
198 4 : }
199 : }
200 :
201 : try {
202 4 : FTPURL aURL(xCanonicId->getContentIdentifier(),
203 8 : this);
204 :
205 8 : if(!m_pProxyDecider->shouldUseProxy(
206 : OUString("ftp"),
207 : aURL.host(),
208 8 : aURL.port().toInt32()))
209 : {
210 4 : xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
211 4 : 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 4 : }
221 0 : } catch(const malformed_exception&) {
222 0 : throw IllegalIdentifierException();
223 : }
224 :
225 : // may throw IllegalIdentifierException
226 4 : return xContent;
227 : }
228 :
229 4 : void FTPContentProvider::init()
230 : {
231 4 : m_ftpLoaderThread = new FTPLoaderThread();
232 4 : m_pProxyDecider = new ucbhelper::InternetProxyDecider( m_xContext );
233 4 : }
234 :
235 2 : CURL* FTPContentProvider::handle()
236 : {
237 : // Cannot be zero if called from here;
238 2 : return m_ftpLoaderThread->handle();
239 : }
240 :
241 :
242 2 : bool FTPContentProvider::forHost( const OUString& host,
243 : const OUString& port,
244 : const OUString& username,
245 : OUString& password,
246 : OUString& account)
247 : {
248 2 : osl::MutexGuard aGuard(m_aMutex);
249 2 : for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
250 6 : if(host == m_ServerInfo[i].host &&
251 4 : port == m_ServerInfo[i].port &&
252 2 : username == m_ServerInfo[i].username )
253 : {
254 2 : password = m_ServerInfo[i].password;
255 2 : account = m_ServerInfo[i].account;
256 2 : return true;
257 : }
258 :
259 0 : return false;
260 : }
261 :
262 2 : bool FTPContentProvider::setHost( const OUString& host,
263 : const OUString& port,
264 : const OUString& username,
265 : const OUString& password,
266 : const OUString& account)
267 : {
268 2 : ServerInfo inf;
269 2 : inf.host = host;
270 2 : inf.port = port;
271 2 : inf.username = username;
272 2 : inf.password = password;
273 2 : inf.account = account;
274 :
275 2 : bool present(false);
276 4 : osl::MutexGuard aGuard(m_aMutex);
277 2 : for(unsigned int 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 2 : if(!present)
288 2 : m_ServerInfo.push_back(inf);
289 :
290 4 : 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: */
|