LibreOffice Module ucb (master)  1
ftpcontentprovider.cxx
Go to the documentation of this file.
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 #include <sal/config.h>
21 
22 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
23 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
24 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
25 #include <cppuhelper/exc_hlp.hxx>
29 #include <cppuhelper/factory.hxx>
31 #include "ftpcontentprovider.hxx"
32 #include "ftpcontent.hxx"
33 #include "ftploaderthread.hxx"
34 
35 using namespace ftp;
36 using namespace com::sun::star::lang;
37 using namespace com::sun::star::container;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::ucb;
40 using namespace com::sun::star::beans;
41 
42 // ContentProvider Implementation.
43 
45  : ::ucbhelper::ContentProviderImplHelper(rxContext)
46 {
47 }
48 
49 
50 // virtual
52 {
53  m_ftpLoaderThread.reset();
54  m_pProxyDecider.reset();
55 }
56 
57 // XInterface methods.
59  throw()
60 {
61  OWeakObject::acquire();
62 }
63 
65  throw()
66 {
67  OWeakObject::release();
68 }
69 
70 css::uno::Any SAL_CALL FTPContentProvider::queryInterface( const css::uno::Type & rType )
71 {
72  css::uno::Any aRet = cppu::queryInterface( rType,
73  static_cast< XTypeProvider* >(this),
74  static_cast< XServiceInfo* >(this),
75  static_cast< XContentProvider* >(this)
76  );
77  return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
78 }
79 
80 // XTypeProvider methods.
81 css::uno::Sequence< sal_Int8 > SAL_CALL FTPContentProvider::getImplementationId()
82 {
83  return css::uno::Sequence<sal_Int8>();
84 }
85 
86 css::uno::Sequence< css::uno::Type > SAL_CALL FTPContentProvider::getTypes()
87 {
88  static cppu::OTypeCollection s_aCollection(
92  );
93 
94  return s_aCollection.getTypes();
95 }
96 
97 
98 // XServiceInfo methods.
99 
101 {
103 }
104 
106 {
107  return "com.sun.star.comp.FTPContentProvider";
108 }
109 
110 sal_Bool SAL_CALL FTPContentProvider::supportsService( const OUString& ServiceName )
111 {
112  return cppu::supportsService( this, ServiceName );
113 }
114 
115 css::uno::Sequence< OUString > SAL_CALL FTPContentProvider::getSupportedServiceNames()
116 {
118 }
119 
121 static css::uno::Reference< css::uno::XInterface >
122 FTPContentProvider_CreateInstance( const css::uno::Reference<
123  css::lang::XMultiServiceFactory> & rSMgr )
124 {
125  css::lang::XServiceInfo* pX = new FTPContentProvider( ucbhelper::getComponentContext(rSMgr) );
126  return css::uno::Reference< css::uno::XInterface >::query( pX );
127 }
128 
130 {
131  css::uno::Sequence<OUString> aSNS { FTP_CONTENT_PROVIDER_SERVICE_NAME };
132  return aSNS;
133 }
134 
135 // Service factory implementation.
136 
137 css::uno::Reference< css::lang::XSingleServiceFactory >
138 FTPContentProvider::createServiceFactory( const css::uno::Reference<
139  css::lang::XMultiServiceFactory >& rxServiceMgr )
140 {
142  rxServiceMgr,
146 }
147 
148 // XContentProvider methods.
149 
150 // virtual
152  const Reference< XContentIdentifier >& xCanonicId)
153 {
154  // Check, if a content with given id already exists...
155  Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
156  if(xContent.is())
157  return xContent;
158 
159  // A new content has to be returned:
160  {
161  // Initialize
162  osl::MutexGuard aGuard( m_aMutex );
164  {
165  try {
166  init();
167  } catch (css::uno::Exception const & ex) {
168  css::uno::Any anyEx = cppu::getCaughtException();
169  throw css::lang::WrappedTargetRuntimeException( ex.Message,
170  css::uno::Reference< css::uno::XInterface >(),
171  anyEx );
172  } catch( ... ) {
173  throw RuntimeException();
174  }
175 
177  throw RuntimeException();
178  }
179  }
180 
181  try {
182  FTPURL aURL(xCanonicId->getContentIdentifier(),
183  this);
184 
185  if(!m_pProxyDecider->shouldUseProxy(
186  "ftp",
187  aURL.host(),
188  aURL.port().toInt32()))
189  {
190  xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
191  registerNewContent(xContent);
192  }
193  else {
194  Reference<XContentProvider> xProvider(UniversalContentBroker::create( m_xContext )->queryContentProvider("http:"));
195  if(!xProvider.is())
196  throw RuntimeException();
197  return xProvider->queryContent(xCanonicId);
198  }
199  } catch(const malformed_exception&) {
200  throw IllegalIdentifierException();
201  }
202 
203  // may throw IllegalIdentifierException
204  return xContent;
205 }
206 
208 {
209  m_ftpLoaderThread.reset( new FTPLoaderThread() );
211 }
212 
214 {
215  // Cannot be zero if called from here;
216  return m_ftpLoaderThread->handle();
217 }
218 
219 
220 void FTPContentProvider::forHost( const OUString& host,
221  const OUString& port,
222  const OUString& username,
223  OUString& password,
224  OUString& account)
225 {
226  osl::MutexGuard aGuard(m_aMutex);
227  for(const ServerInfo & i : m_ServerInfo)
228  if(host == i.host &&
229  port == i.port &&
230  username == i.username )
231  {
232  password = i.password;
233  account = i.account;
234  return;
235  }
236 }
237 
238 bool FTPContentProvider::setHost( const OUString& host,
239  const OUString& port,
240  const OUString& username,
241  const OUString& password,
242  const OUString& account)
243 {
244  ServerInfo inf;
245  inf.host = host;
246  inf.port = port;
247  inf.username = username;
248  inf.password = password;
249  inf.account = account;
250 
251  bool present(false);
252  osl::MutexGuard aGuard(m_aMutex);
253  for(ServerInfo & i : m_ServerInfo)
254  if(host == i.host &&
255  port == i.port &&
256  username == i.username)
257  {
258  present = true;
259  i.password = password;
260  i.account = account;
261  }
262 
263  if(!present)
264  m_ServerInfo.push_back(inf);
265 
266  return !present;
267 }
268 
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
virtual OUString SAL_CALL getImplementationName() override
bool setHost(const OUString &host, const OUString &port, const OUString &username, const OUString &password, const OUString &account)
#define FTP_CONTENT_PROVIDER_SERVICE_NAME
static css::uno::Sequence< OUString > getSupportedServiceNames_Static()
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
UCBHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > getComponentContext(css::uno::Reference< css::lang::XMultiServiceFactory > const &factory)
std::unique_ptr< ucbhelper::InternetProxyDecider > m_pProxyDecider
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
static OUString getImplementationName_Static()
Any SAL_CALL getCaughtException()
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual void SAL_CALL acquire() override
static css::uno::Reference< css::lang::XSingleServiceFactory > createServiceFactory(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxServiceMgr)
int i
virtual ~FTPContentProvider() override
unsigned char sal_Bool
rtl::Reference< ContentImplHelper > queryExistingContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
FTPContentProvider(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual void SAL_CALL release() override
Definition of ftpcontentprovider.
css::uno::Reference< css::uno::XComponentContext > m_xContext
std::vector< ServerInfo > m_ServerInfo
void registerNewContent(const css::uno::Reference< css::ucb::XContent > &xContent)
Reference< XSingleServiceFactory > SAL_CALL createOneInstanceFactory(const Reference< XMultiServiceFactory > &rServiceManager, const OUString &rImplementationName, ComponentInstantiation pCreateFunction, const Sequence< OUString > &rServiceNames, rtl_ModuleCount *)
void forHost(const OUString &host, const OUString &port, const OUString &username, OUString &password, OUString &account)
host is in the form host:port.
css::uno::Sequence< css::uno::Type > SAL_CALL getTypes()
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
std::unique_ptr< FTPLoaderThread > m_ftpLoaderThread
virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier) override
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
static css::uno::Reference< css::uno::XInterface > FTPContentProvider_CreateInstance(const css::uno::Reference< css::lang::XMultiServiceFactory > &rSMgr)
A loaderthread acts as factory for CURL-handles, the key being ( implicit ) the threadid.