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>
30 #include <cppuhelper/weak.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 {
102  return "com.sun.star.comp.FTPContentProvider";
103 }
104 
105 sal_Bool SAL_CALL FTPContentProvider::supportsService( const OUString& ServiceName )
106 {
107  return cppu::supportsService( this, ServiceName );
108 }
109 
110 css::uno::Sequence< OUString > SAL_CALL FTPContentProvider::getSupportedServiceNames()
111 {
113 }
114 
115 
116 
117 // XContentProvider methods.
118 
119 // virtual
121  const Reference< XContentIdentifier >& xCanonicId)
122 {
123  // Check, if a content with given id already exists...
125  if(xContent.is())
126  return xContent;
127 
128  // A new content has to be returned:
129  {
130  // Initialize
131  osl::MutexGuard aGuard( m_aMutex );
133  {
134  try {
135  init();
136  } catch (css::uno::Exception const & ex) {
137  css::uno::Any anyEx = cppu::getCaughtException();
138  throw css::lang::WrappedTargetRuntimeException( ex.Message,
139  css::uno::Reference< css::uno::XInterface >(),
140  anyEx );
141  } catch( ... ) {
142  throw RuntimeException();
143  }
144 
146  throw RuntimeException();
147  }
148  }
149 
150  try {
151  FTPURL aURL(xCanonicId->getContentIdentifier(),
152  this);
153 
154  if(!m_pProxyDecider->shouldUseProxy(
155  "ftp",
156  aURL.host(),
157  aURL.port().toInt32()))
158  {
159  xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
160  registerNewContent(xContent);
161  }
162  else {
163  Reference<XContentProvider> xProvider(UniversalContentBroker::create( m_xContext )->queryContentProvider("http:"));
164  if(!xProvider.is())
165  throw RuntimeException();
166  return xProvider->queryContent(xCanonicId);
167  }
168  } catch(const malformed_exception&) {
169  throw IllegalIdentifierException();
170  }
171 
172  // may throw IllegalIdentifierException
173  return xContent;
174 }
175 
177 {
178  m_ftpLoaderThread.reset( new FTPLoaderThread() );
180 }
181 
183 {
184  // Cannot be zero if called from here;
185  return m_ftpLoaderThread->handle();
186 }
187 
188 
189 void FTPContentProvider::forHost( const OUString& host,
190  const OUString& port,
191  const OUString& username,
192  OUString& password,
193  OUString& account)
194 {
195  osl::MutexGuard aGuard(m_aMutex);
196  for(const ServerInfo & i : m_ServerInfo)
197  if(host == i.host &&
198  port == i.port &&
199  username == i.username )
200  {
201  password = i.password;
202  account = i.account;
203  return;
204  }
205 }
206 
207 bool FTPContentProvider::setHost( const OUString& host,
208  const OUString& port,
209  const OUString& username,
210  const OUString& password,
211  const OUString& account)
212 {
213  ServerInfo inf;
214  inf.host = host;
215  inf.port = port;
216  inf.username = username;
217  inf.password = password;
218  inf.account = account;
219 
220  bool present(false);
221  osl::MutexGuard aGuard(m_aMutex);
222  for(ServerInfo & i : m_ServerInfo)
223  if(host == i.host &&
224  port == i.port &&
225  username == i.username)
226  {
227  present = true;
228  i.password = password;
229  i.account = account;
230  }
231 
232  if(!present)
233  m_ServerInfo.push_back(inf);
234 
235  return !present;
236 }
237 
238 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
240  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
241 {
242  return cppu::acquire(static_cast<cppu::OWeakObject*>(new FTPContentProvider(context)));
243 }
244 
245 /* 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
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
std::unique_ptr< ucbhelper::InternetProxyDecider > m_pProxyDecider
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
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
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.
OUString aURL
css::uno::Reference< css::uno::XComponentContext > m_xContext
std::vector< ServerInfo > m_ServerInfo
void registerNewContent(const css::uno::Reference< css::ucb::XContent > &xContent)
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()
uno::Reference< ucb::XContent > xContent
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)
A loaderthread acts as factory for CURL-handles, the key being ( implicit ) the threadid.
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * ucb_ftp_FTPContentProvider_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)