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>
26 #include <cppuhelper/exc_hlp.hxx>
30 #include <cppuhelper/factory.hxx>
32 #include <osl/socket.hxx>
33 #include "ftpcontentprovider.hxx"
34 #include "ftpcontent.hxx"
35 #include "ftploaderthread.hxx"
36 
37 using namespace ftp;
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::container;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::ucb;
42 using namespace com::sun::star::beans;
43 
44 // ContentProvider Implementation.
45 
47  : ::ucbhelper::ContentProviderImplHelper(rxContext)
48 {
49 }
50 
51 
52 // virtual
54 {
55  m_ftpLoaderThread.reset();
56  m_pProxyDecider.reset();
57 }
58 
59 // XInterface methods.
61  throw()
62 {
63  OWeakObject::acquire();
64 }
65 
67  throw()
68 {
69  OWeakObject::release();
70 }
71 
72 css::uno::Any SAL_CALL FTPContentProvider::queryInterface( const css::uno::Type & rType )
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  );
79  return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
80 }
81 
82 // XTypeProvider methods.
83 css::uno::Sequence< sal_Int8 > SAL_CALL FTPContentProvider::getImplementationId()
84 {
85  return css::uno::Sequence<sal_Int8>();
86 }
87 
88 css::uno::Sequence< css::uno::Type > SAL_CALL FTPContentProvider::getTypes()
89 {
90  static cppu::OTypeCollection s_aCollection(
94  );
95 
96  return s_aCollection.getTypes();
97 }
98 
99 
100 // XServiceInfo methods.
101 
103 {
105 }
106 
108 {
109  return "com.sun.star.comp.FTPContentProvider";
110 }
111 
112 sal_Bool SAL_CALL FTPContentProvider::supportsService( const OUString& ServiceName )
113 {
114  return cppu::supportsService( this, ServiceName );
115 }
116 
117 css::uno::Sequence< OUString > SAL_CALL FTPContentProvider::getSupportedServiceNames()
118 {
120 }
121 
123 static css::uno::Reference< css::uno::XInterface >
124 FTPContentProvider_CreateInstance( const css::uno::Reference<
125  css::lang::XMultiServiceFactory> & rSMgr )
126 {
127  css::lang::XServiceInfo* pX = static_cast<css::lang::XServiceInfo*>(
129  return css::uno::Reference< css::uno::XInterface >::query( pX );
130 }
131 
133 {
134  css::uno::Sequence<OUString> aSNS { FTP_CONTENT_PROVIDER_SERVICE_NAME };
135  return aSNS;
136 }
137 
138 // Service factory implementation.
139 
140 css::uno::Reference< css::lang::XSingleServiceFactory >
141 FTPContentProvider::createServiceFactory( const css::uno::Reference<
142  css::lang::XMultiServiceFactory >& rxServiceMgr )
143 {
145  rxServiceMgr,
149 }
150 
151 // XContentProvider methods.
152 
153 // virtual
155  const Reference< XContentIdentifier >& xCanonicId)
156 {
157  // Check, if a content with given id already exists...
158  Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
159  if(xContent.is())
160  return xContent;
161 
162  // A new content has to be returned:
163  {
164  // Initialize
165  osl::MutexGuard aGuard( m_aMutex );
167  {
168  try {
169  init();
170  } catch (css::uno::Exception const & ex) {
171  css::uno::Any anyEx = cppu::getCaughtException();
172  throw css::lang::WrappedTargetRuntimeException( ex.Message,
173  css::uno::Reference< css::uno::XInterface >(),
174  anyEx );
175  } catch( ... ) {
176  throw RuntimeException();
177  }
178 
180  throw RuntimeException();
181  }
182  }
183 
184  try {
185  FTPURL aURL(xCanonicId->getContentIdentifier(),
186  this);
187 
188  if(!m_pProxyDecider->shouldUseProxy(
189  "ftp",
190  aURL.host(),
191  aURL.port().toInt32()))
192  {
193  xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
194  registerNewContent(xContent);
195  }
196  else {
197  Reference<XContentProvider> xProvider(UniversalContentBroker::create( m_xContext )->queryContentProvider("http:"));
198  if(!xProvider.is())
199  throw RuntimeException();
200  return xProvider->queryContent(xCanonicId);
201  }
202  } catch(const malformed_exception&) {
203  throw IllegalIdentifierException();
204  }
205 
206  // may throw IllegalIdentifierException
207  return xContent;
208 }
209 
211 {
212  m_ftpLoaderThread.reset( new FTPLoaderThread() );
214 }
215 
217 {
218  // Cannot be zero if called from here;
219  return m_ftpLoaderThread->handle();
220 }
221 
222 
223 void FTPContentProvider::forHost( const OUString& host,
224  const OUString& port,
225  const OUString& username,
226  OUString& password,
227  OUString& account)
228 {
229  osl::MutexGuard aGuard(m_aMutex);
230  for(ServerInfo & i : m_ServerInfo)
231  if(host == i.host &&
232  port == i.port &&
233  username == i.username )
234  {
235  password = i.password;
236  account = i.account;
237  return;
238  }
239 }
240 
241 bool FTPContentProvider::setHost( const OUString& host,
242  const OUString& port,
243  const OUString& username,
244  const OUString& password,
245  const OUString& account)
246 {
247  ServerInfo inf;
248  inf.host = host;
249  inf.port = port;
250  inf.username = username;
251  inf.password = password;
252  inf.account = account;
253 
254  bool present(false);
255  osl::MutexGuard aGuard(m_aMutex);
256  for(ServerInfo & i : m_ServerInfo)
257  if(host == i.host &&
258  port == i.port &&
259  username == i.username)
260  {
261  present = true;
262  i.password = password;
263  i.account = account;
264  }
265 
266  if(!present)
267  m_ServerInfo.push_back(inf);
268 
269  return !present;
270 }
271 
272 /* 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.