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