1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ftp.hxx"
26
27 /**************************************************************************
28 TODO
29 **************************************************************************
30
31 *************************************************************************/
32
33 #include <ucbhelper/contentbroker.hxx>
34 #include <osl/socket.hxx>
35 #include "ftpcontentprovider.hxx"
36 #include "ftpcontent.hxx"
37 #include "ftploaderthread.hxx"
38
39
40 using namespace ftp;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::container;
43 using namespace com::sun::star::uno;
44 using namespace com::sun::star::ucb;
45 using namespace com::sun::star::beans;
46
47
48
49 //=========================================================================
50 //=========================================================================
51 //
52 // ContentProvider Implementation.
53 //
54 //=========================================================================
55 //=========================================================================
56
FTPContentProvider(const Reference<XMultiServiceFactory> & rSMgr)57 FTPContentProvider::FTPContentProvider(
58 const Reference< XMultiServiceFactory >& rSMgr)
59 : ::ucbhelper::ContentProviderImplHelper(rSMgr),
60 m_ftpLoaderThread(0),
61 m_pProxyDecider(0)
62 {
63 }
64
65 //=========================================================================
66 // virtual
~FTPContentProvider()67 FTPContentProvider::~FTPContentProvider()
68 {
69 delete m_ftpLoaderThread;
70 delete m_pProxyDecider;
71 }
72
73 //=========================================================================
74 //
75 // XInterface methods.
76 //
77 //=========================================================================
78
79 XINTERFACE_IMPL_3(FTPContentProvider,
80 XTypeProvider,
81 XServiceInfo,
82 XContentProvider)
83
84 //=========================================================================
85 //
86 // XTypeProvider methods.
87 //
88 //=========================================================================
89
90 XTYPEPROVIDER_IMPL_3(FTPContentProvider,
91 XTypeProvider,
92 XServiceInfo,
93 XContentProvider)
94
95 //=========================================================================
96 //
97 // XServiceInfo methods.
98 //
99 //=========================================================================
100
101 XSERVICEINFO_IMPL_1(
102 FTPContentProvider,
103 rtl::OUString::createFromAscii("com.sun.star.comp.FTPContentProvider"),
104 rtl::OUString::createFromAscii(FTP_CONTENT_PROVIDER_SERVICE_NAME));
105
106 //=========================================================================
107 //
108 // Service factory implementation.
109 //
110 //=========================================================================
111
112 ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
113
114
115 //=========================================================================
116 //
117 // XContentProvider methods.
118 //
119 //=========================================================================
120
121 // virtual
122 Reference<XContent> SAL_CALL
queryContent(const Reference<XContentIdentifier> & xCanonicId)123 FTPContentProvider::queryContent(
124 const Reference< XContentIdentifier >& xCanonicId
125 )
126 {
127 // Check, if a content with given id already exists...
128 Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
129 if(xContent.is())
130 return xContent;
131
132 // A new content has to be returned:
133 {
134 // Initialize
135 osl::MutexGuard aGuard( m_aMutex );
136 if(!m_ftpLoaderThread || !m_pProxyDecider)
137 {
138 try {
139 init();
140 } catch( ... ) {
141 throw RuntimeException();
142 }
143
144 if(!m_ftpLoaderThread || !m_pProxyDecider)
145 throw RuntimeException();
146 }
147 }
148
149 try {
150 FTPURL aURL(xCanonicId->getContentIdentifier(),
151 this);
152
153 if(!m_pProxyDecider->shouldUseProxy(
154 rtl::OUString::createFromAscii("ftp"),
155 aURL.host(),
156 aURL.port().toInt32()))
157 {
158 xContent = new FTPContent(m_xSMgr,this,xCanonicId,aURL);
159 registerNewContent(xContent);
160 }
161 else {
162 Reference<XContentProvider>
163 xProvider(getHttpProvider());
164 if(xProvider.is())
165 return xProvider->queryContent(xCanonicId);
166 else
167 throw RuntimeException();
168 }
169 } catch(const malformed_exception&) {
170 throw IllegalIdentifierException();
171 }
172
173 // may throw IllegalIdentifierException
174 return xContent;
175 }
176
177
178
179
init()180 void FTPContentProvider::init() {
181 m_ftpLoaderThread = new FTPLoaderThread();
182 m_pProxyDecider = new ucbhelper::InternetProxyDecider(m_xSMgr);
183 }
184
185
186
handle()187 CURL* FTPContentProvider::handle() {
188 // Cannot be zero if called from here;
189 return m_ftpLoaderThread->handle();
190 }
191
192
forHost(const rtl::OUString & host,const rtl::OUString & port,const rtl::OUString & username,rtl::OUString & password,rtl::OUString & account)193 bool FTPContentProvider::forHost(
194 const rtl::OUString& host,
195 const rtl::OUString& port,
196 const rtl::OUString& username,
197 rtl::OUString& password,
198 rtl::OUString& account)
199 {
200 osl::MutexGuard aGuard(m_aMutex);
201 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
202 if(host == m_ServerInfo[i].host &&
203 port == m_ServerInfo[i].port &&
204 username == m_ServerInfo[i].username )
205 {
206 password = m_ServerInfo[i].password;
207 account = m_ServerInfo[i].account;
208 return true;
209 }
210
211 return false;
212 }
213
214
setHost(const rtl::OUString & host,const rtl::OUString & port,const rtl::OUString & username,const rtl::OUString & password,const rtl::OUString & account)215 bool FTPContentProvider::setHost(
216 const rtl::OUString& host,
217 const rtl::OUString& port,
218 const rtl::OUString& username,
219 const rtl::OUString& password,
220 const rtl::OUString& account)
221 {
222 ServerInfo inf;
223 inf.host = host;
224 inf.port = port;
225 inf.username = username;
226 inf.password = password;
227 inf.account = account;
228
229 bool present(false);
230 osl::MutexGuard aGuard(m_aMutex);
231 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
232 if(host == m_ServerInfo[i].host &&
233 port == m_ServerInfo[i].port &&
234 username == m_ServerInfo[i].username)
235 {
236 present = true;
237 m_ServerInfo[i].password = password;
238 m_ServerInfo[i].account = account;
239 }
240
241 if(!present)
242 m_ServerInfo.push_back(inf);
243
244 return !present;
245 }
246
247
248
249 Reference<XContentProvider>
getHttpProvider()250 FTPContentProvider::getHttpProvider()
251 {
252 // used for access to ftp-proxy
253 ucbhelper::ContentBroker *pBroker = ucbhelper::ContentBroker::get();
254
255 if(pBroker) {
256 Reference<XContentProviderManager > xManager(
257 pBroker->getContentProviderManagerInterface());
258
259 if(xManager.is())
260 return
261 xManager->queryContentProvider(
262 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("http:")));
263 else
264 throw RuntimeException(
265 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
266 "bad ucbhelper::ContentBroker")),
267 *this);
268 } else
269 return 0;
270
271 }
272