1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*6d739b60SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*6d739b60SAndrew Rist * distributed with this work for additional information
6*6d739b60SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*6d739b60SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist * with the License. You may obtain a copy of the License at
10*6d739b60SAndrew Rist *
11*6d739b60SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist *
13*6d739b60SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist * KIND, either express or implied. See the License for the
17*6d739b60SAndrew Rist * specific language governing permissions and limitations
18*6d739b60SAndrew Rist * under the License.
19*6d739b60SAndrew Rist *
20*6d739b60SAndrew Rist *************************************************************/
21*6d739b60SAndrew Rist
22*6d739b60SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir //_________________________________________________________________________________________________________________
28cdf0e10cSrcweir // my own includes
29cdf0e10cSrcweir //_________________________________________________________________________________________________________________
30cdf0e10cSrcweir #include <services/urltransformer.hxx>
31cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
32cdf0e10cSrcweir #include <macros/debug.hxx>
33cdf0e10cSrcweir #include <services.h>
34cdf0e10cSrcweir
35cdf0e10cSrcweir //_________________________________________________________________________________________________________________
36cdf0e10cSrcweir // interface includes
37cdf0e10cSrcweir //_________________________________________________________________________________________________________________
38cdf0e10cSrcweir
39cdf0e10cSrcweir //_________________________________________________________________________________________________________________
40cdf0e10cSrcweir // includes of other projects
41cdf0e10cSrcweir //_________________________________________________________________________________________________________________
42cdf0e10cSrcweir #include <tools/urlobj.hxx>
43cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
44cdf0e10cSrcweir #include <vcl/svapp.hxx>
45cdf0e10cSrcweir
46cdf0e10cSrcweir //_________________________________________________________________________________________________________________
47cdf0e10cSrcweir // namespace
48cdf0e10cSrcweir //_________________________________________________________________________________________________________________
49cdf0e10cSrcweir
50cdf0e10cSrcweir namespace framework{
51cdf0e10cSrcweir
52cdf0e10cSrcweir using namespace ::osl ;
53cdf0e10cSrcweir using namespace ::cppu ;
54cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
55cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
56cdf0e10cSrcweir using namespace ::com::sun::star::util ;
57cdf0e10cSrcweir
58cdf0e10cSrcweir //_________________________________________________________________________________________________________________
59cdf0e10cSrcweir // non exported const
60cdf0e10cSrcweir //_________________________________________________________________________________________________________________
61cdf0e10cSrcweir
62cdf0e10cSrcweir //_________________________________________________________________________________________________________________
63cdf0e10cSrcweir // non exported definitions
64cdf0e10cSrcweir //_________________________________________________________________________________________________________________
65cdf0e10cSrcweir
66cdf0e10cSrcweir //_________________________________________________________________________________________________________________
67cdf0e10cSrcweir // declarations
68cdf0e10cSrcweir //_________________________________________________________________________________________________________________
69cdf0e10cSrcweir
70cdf0e10cSrcweir //*****************************************************************************************************************
71cdf0e10cSrcweir // constructor
72cdf0e10cSrcweir //*****************************************************************************************************************
URLTransformer(const Reference<XMultiServiceFactory> &)73cdf0e10cSrcweir URLTransformer::URLTransformer( const Reference< XMultiServiceFactory >& /*xFactory*/ )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir // Safe impossible cases.
76cdf0e10cSrcweir // Method not defined for all incoming parameter.
77cdf0e10cSrcweir //LOG_ASSERT( xFactory.is(), "URLTransformer::URLTransformer()\nInvalid parameter detected!\n" )
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
80cdf0e10cSrcweir //*****************************************************************************************************************
81cdf0e10cSrcweir // destructor
82cdf0e10cSrcweir //*****************************************************************************************************************
~URLTransformer()83cdf0e10cSrcweir URLTransformer::~URLTransformer()
84cdf0e10cSrcweir {
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
87cdf0e10cSrcweir //*****************************************************************************************************************
88cdf0e10cSrcweir // XInterface, XTypeProvider, XServiceInfo
89cdf0e10cSrcweir //*****************************************************************************************************************
90cdf0e10cSrcweir
91cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE ( URLTransformer ,
92cdf0e10cSrcweir OWeakObject ,
93cdf0e10cSrcweir SERVICENAME_URLTRANSFORMER ,
94cdf0e10cSrcweir IMPLEMENTATIONNAME_URLTRANSFORMER
95cdf0e10cSrcweir )
96cdf0e10cSrcweir
97cdf0e10cSrcweir DEFINE_INIT_SERVICE ( URLTransformer,
98cdf0e10cSrcweir {
99cdf0e10cSrcweir }
100cdf0e10cSrcweir )
101cdf0e10cSrcweir
102cdf0e10cSrcweir namespace
103cdf0e10cSrcweir {
lcl_ParserHelper(INetURLObject & _rParser,URL & _rURL,bool _bUseIntern)104cdf0e10cSrcweir void lcl_ParserHelper(INetURLObject& _rParser,URL& _rURL,bool _bUseIntern)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir // Get all information about this URL.
107cdf0e10cSrcweir _rURL.Protocol = INetURLObject::GetScheme( _rParser.GetProtocol() );
108cdf0e10cSrcweir _rURL.User = _rParser.GetUser ( INetURLObject::DECODE_WITH_CHARSET );
109cdf0e10cSrcweir _rURL.Password = _rParser.GetPass ( INetURLObject::DECODE_WITH_CHARSET );
110cdf0e10cSrcweir _rURL.Server = _rParser.GetHost ( INetURLObject::DECODE_WITH_CHARSET );
111cdf0e10cSrcweir _rURL.Port = (sal_Int16)_rParser.GetPort();
112cdf0e10cSrcweir
113cdf0e10cSrcweir sal_Int32 nCount = _rParser.getSegmentCount( false );
114cdf0e10cSrcweir if ( nCount > 0 )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir // Don't add last segment as it is the name!
117cdf0e10cSrcweir --nCount;
118cdf0e10cSrcweir
119cdf0e10cSrcweir rtl::OUStringBuffer aPath;
120cdf0e10cSrcweir for ( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++ )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir aPath.append( sal_Unicode( '/' ));
123cdf0e10cSrcweir aPath.append( _rParser.getName( nIndex, false, INetURLObject::NO_DECODE ));
124cdf0e10cSrcweir }
125cdf0e10cSrcweir
126cdf0e10cSrcweir if ( nCount > 0 )
127cdf0e10cSrcweir aPath.append( sal_Unicode( '/' )); // final slash!
128cdf0e10cSrcweir
129cdf0e10cSrcweir _rURL.Path = aPath.makeStringAndClear();
130cdf0e10cSrcweir _rURL.Name = _rParser.getName( INetURLObject::LAST_SEGMENT, false, INetURLObject::NO_DECODE );
131cdf0e10cSrcweir }
132cdf0e10cSrcweir else
133cdf0e10cSrcweir {
134cdf0e10cSrcweir _rURL.Path = _rParser.GetURLPath( INetURLObject::NO_DECODE );
135cdf0e10cSrcweir _rURL.Name = _rParser.GetName ( );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir _rURL.Arguments = _rParser.GetParam ( INetURLObject::NO_DECODE );
139cdf0e10cSrcweir _rURL.Mark = _rParser.GetMark ( INetURLObject::DECODE_WITH_CHARSET );
140cdf0e10cSrcweir
141cdf0e10cSrcweir // INetURLObject supports only an intelligent method of parsing URL's. So write
142cdf0e10cSrcweir // back Complete to have a valid encoded URL in all cases!
143cdf0e10cSrcweir _rURL.Complete = _rParser.GetMainURL( INetURLObject::NO_DECODE );
144cdf0e10cSrcweir if ( _bUseIntern )
145cdf0e10cSrcweir _rURL.Complete = _rURL.Complete.intern();
146cdf0e10cSrcweir
147cdf0e10cSrcweir _rParser.SetMark ( ::rtl::OUString() );
148cdf0e10cSrcweir _rParser.SetParam( ::rtl::OUString() );
149cdf0e10cSrcweir
150cdf0e10cSrcweir _rURL.Main = _rParser.GetMainURL( INetURLObject::NO_DECODE );
151cdf0e10cSrcweir }
152cdf0e10cSrcweir }
153cdf0e10cSrcweir //*****************************************************************************************************************
154cdf0e10cSrcweir // XURLTransformer
155cdf0e10cSrcweir //*****************************************************************************************************************
parseStrict(URL & aURL)156cdf0e10cSrcweir sal_Bool SAL_CALL URLTransformer::parseStrict( URL& aURL ) throw( RuntimeException )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir // Safe impossible cases.
159cdf0e10cSrcweir if (( &aURL == NULL ) ||
160cdf0e10cSrcweir ( aURL.Complete.getLength() < 1 ) )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir return sal_False;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir // Try to extract the protocol
165cdf0e10cSrcweir sal_Int32 nURLIndex = aURL.Complete.indexOf( sal_Unicode( ':' ));
166cdf0e10cSrcweir ::rtl::OUString aProtocol;
167cdf0e10cSrcweir if ( nURLIndex > 1 )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir aProtocol = aURL.Complete.copy( 0, nURLIndex+1 );
170cdf0e10cSrcweir
171cdf0e10cSrcweir // If INetURLObject knows this protocol let it parse
172cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INET_PROT_NOT_VALID )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir // Initialize parser with given URL.
175cdf0e10cSrcweir INetURLObject aParser( aURL.Complete );
176cdf0e10cSrcweir
177cdf0e10cSrcweir // Get all information about this URL.
178cdf0e10cSrcweir INetProtocol eINetProt = aParser.GetProtocol();
179cdf0e10cSrcweir if ( eINetProt == INET_PROT_NOT_VALID )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir return sal_False;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir else if ( !aParser.HasError() )
184cdf0e10cSrcweir {
185cdf0e10cSrcweir lcl_ParserHelper(aParser,aURL,false);
186cdf0e10cSrcweir // Return "URL is parsed".
187cdf0e10cSrcweir return sal_True;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir }
190cdf0e10cSrcweir else
191cdf0e10cSrcweir {
192cdf0e10cSrcweir // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
193cdf0e10cSrcweir // in framework!
194cdf0e10cSrcweir aURL.Protocol = aProtocol;
195cdf0e10cSrcweir aURL.Main = aURL.Complete;
196cdf0e10cSrcweir aURL.Path = aURL.Complete.copy( nURLIndex+1 );;
197cdf0e10cSrcweir
198cdf0e10cSrcweir // Return "URL is parsed".
199cdf0e10cSrcweir return sal_True;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir return sal_False;
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir //*****************************************************************************************************************
207cdf0e10cSrcweir // XURLTransformer
208cdf0e10cSrcweir //*****************************************************************************************************************
parseSmart(URL & aURL,const::rtl::OUString & sSmartProtocol)209cdf0e10cSrcweir sal_Bool SAL_CALL URLTransformer::parseSmart( URL& aURL ,
210cdf0e10cSrcweir const ::rtl::OUString& sSmartProtocol ) throw( RuntimeException )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir // Safe impossible cases.
213cdf0e10cSrcweir if (( &aURL == NULL ) ||
214cdf0e10cSrcweir ( aURL.Complete.getLength() < 1 ) )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir return sal_False;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir
219cdf0e10cSrcweir // Initialize parser with given URL.
220cdf0e10cSrcweir INetURLObject aParser;
221cdf0e10cSrcweir
222cdf0e10cSrcweir aParser.SetSmartProtocol( INetURLObject::CompareProtocolScheme( sSmartProtocol ));
223cdf0e10cSrcweir bool bOk = aParser.SetSmartURL( aURL.Complete );
224cdf0e10cSrcweir if ( bOk )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir lcl_ParserHelper(aParser,aURL,true);
227cdf0e10cSrcweir // Return "URL is parsed".
228cdf0e10cSrcweir return sal_True;
229cdf0e10cSrcweir }
230cdf0e10cSrcweir else
231cdf0e10cSrcweir {
232cdf0e10cSrcweir // Minmal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented
233cdf0e10cSrcweir // in framework!
234cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( sSmartProtocol ) == INET_PROT_NOT_VALID )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir // Try to extract the protocol
237cdf0e10cSrcweir sal_Int32 nIndex = aURL.Complete.indexOf( sal_Unicode( ':' ));
238cdf0e10cSrcweir ::rtl::OUString aProtocol;
239cdf0e10cSrcweir if ( nIndex > 1 )
240cdf0e10cSrcweir {
241cdf0e10cSrcweir aProtocol = aURL.Complete.copy( 0, nIndex+1 );
242cdf0e10cSrcweir
243cdf0e10cSrcweir // If INetURLObject knows this protocol something is wrong as detected before =>
244cdf0e10cSrcweir // give up and return false!
245cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INET_PROT_NOT_VALID )
246cdf0e10cSrcweir return sal_False;
247cdf0e10cSrcweir else
248cdf0e10cSrcweir aURL.Protocol = aProtocol;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir else
251cdf0e10cSrcweir return sal_False;
252cdf0e10cSrcweir
253cdf0e10cSrcweir aURL.Main = aURL.Complete;
254cdf0e10cSrcweir aURL.Path = aURL.Complete.copy( nIndex+1 );
255cdf0e10cSrcweir return sal_True;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir else
258cdf0e10cSrcweir return sal_False;
259cdf0e10cSrcweir }
260cdf0e10cSrcweir }
261cdf0e10cSrcweir
262cdf0e10cSrcweir //*****************************************************************************************************************
263cdf0e10cSrcweir // XURLTransformer
264cdf0e10cSrcweir //*****************************************************************************************************************
assemble(URL & aURL)265cdf0e10cSrcweir sal_Bool SAL_CALL URLTransformer::assemble( URL& aURL ) throw( RuntimeException )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir // Safe impossible cases.
268cdf0e10cSrcweir if ( &aURL == NULL )
269cdf0e10cSrcweir return sal_False ;
270cdf0e10cSrcweir
271cdf0e10cSrcweir // Initialize parser.
272cdf0e10cSrcweir INetURLObject aParser;
273cdf0e10cSrcweir
274cdf0e10cSrcweir if ( INetURLObject::CompareProtocolScheme( aURL.Protocol ) != INET_PROT_NOT_VALID )
275cdf0e10cSrcweir {
276cdf0e10cSrcweir ::rtl::OUStringBuffer aCompletePath( aURL.Path );
277cdf0e10cSrcweir
278cdf0e10cSrcweir // Concat the name if it is provided, just support a final slash
279cdf0e10cSrcweir if ( aURL.Name.getLength() > 0 )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir sal_Int32 nIndex = aURL.Path.lastIndexOf( sal_Unicode('/') );
282cdf0e10cSrcweir if ( nIndex == ( aURL.Path.getLength() -1 ))
283cdf0e10cSrcweir aCompletePath.append( aURL.Name );
284cdf0e10cSrcweir else
285cdf0e10cSrcweir {
286cdf0e10cSrcweir aCompletePath.append( sal_Unicode( '/' ) );
287cdf0e10cSrcweir aCompletePath.append( aURL.Name );
288cdf0e10cSrcweir }
289cdf0e10cSrcweir }
290cdf0e10cSrcweir
291cdf0e10cSrcweir bool bResult = aParser.ConcatData(
292cdf0e10cSrcweir INetURLObject::CompareProtocolScheme( aURL.Protocol ) ,
293cdf0e10cSrcweir aURL.User ,
294cdf0e10cSrcweir aURL.Password ,
295cdf0e10cSrcweir aURL.Server ,
296cdf0e10cSrcweir aURL.Port ,
297cdf0e10cSrcweir aCompletePath.makeStringAndClear() );
298cdf0e10cSrcweir
299cdf0e10cSrcweir if ( !bResult )
300cdf0e10cSrcweir return sal_False;
301cdf0e10cSrcweir
302cdf0e10cSrcweir // First parse URL WITHOUT ...
303cdf0e10cSrcweir aURL.Main = aParser.GetMainURL( INetURLObject::NO_DECODE );
304cdf0e10cSrcweir // ...and then WITH parameter and mark.
305cdf0e10cSrcweir aParser.SetParam( aURL.Arguments);
306cdf0e10cSrcweir aParser.SetMark ( aURL.Mark, INetURLObject::ENCODE_ALL );
307cdf0e10cSrcweir aURL.Complete = aParser.GetMainURL( INetURLObject::NO_DECODE );
308cdf0e10cSrcweir
309cdf0e10cSrcweir // Return "URL is assembled".
310cdf0e10cSrcweir return sal_True;
311cdf0e10cSrcweir }
312cdf0e10cSrcweir else if ( aURL.Protocol.getLength() > 0 )
313cdf0e10cSrcweir {
314cdf0e10cSrcweir // Minimal support for unknown protocols
315cdf0e10cSrcweir ::rtl::OUStringBuffer aBuffer( aURL.Protocol );
316cdf0e10cSrcweir aBuffer.append( aURL.Path );
317cdf0e10cSrcweir aURL.Complete = aBuffer.makeStringAndClear();
318cdf0e10cSrcweir aURL.Main = aURL.Complete;
319cdf0e10cSrcweir return sal_True;
320cdf0e10cSrcweir }
321cdf0e10cSrcweir
322cdf0e10cSrcweir return sal_False;
323cdf0e10cSrcweir }
324cdf0e10cSrcweir
325cdf0e10cSrcweir //*****************************************************************************************************************
326cdf0e10cSrcweir // XURLTransformer
327cdf0e10cSrcweir //*****************************************************************************************************************
getPresentation(const URL & aURL,sal_Bool bWithPassword)328cdf0e10cSrcweir ::rtl::OUString SAL_CALL URLTransformer::getPresentation( const URL& aURL ,
329cdf0e10cSrcweir sal_Bool bWithPassword ) throw( RuntimeException )
330cdf0e10cSrcweir {
331cdf0e10cSrcweir // Safe impossible cases.
332cdf0e10cSrcweir if (( &aURL == NULL ) ||
333cdf0e10cSrcweir ( aURL.Complete.getLength() < 1 ) ||
334cdf0e10cSrcweir (( bWithPassword != sal_True ) &&
335cdf0e10cSrcweir ( bWithPassword != sal_False ) ) )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir return ::rtl::OUString();
338cdf0e10cSrcweir }
339cdf0e10cSrcweir
340cdf0e10cSrcweir // Check given URL
341cdf0e10cSrcweir URL aTestURL = aURL;
342cdf0e10cSrcweir sal_Bool bParseResult = parseSmart( aTestURL, aTestURL.Protocol );
343cdf0e10cSrcweir if ( bParseResult )
344cdf0e10cSrcweir {
345cdf0e10cSrcweir if ( !bWithPassword && aTestURL.Password.getLength() > 0 )
346cdf0e10cSrcweir {
347cdf0e10cSrcweir // Exchange password text with other placeholder string
348cdf0e10cSrcweir aTestURL.Password = ::rtl::OUString::createFromAscii( "<******>" );
349cdf0e10cSrcweir assemble( aTestURL );
350cdf0e10cSrcweir }
351cdf0e10cSrcweir
352cdf0e10cSrcweir // Convert internal URLs to "praesentation"-URLs!
353cdf0e10cSrcweir rtl::OUString sPraesentationURL;
354cdf0e10cSrcweir INetURLObject::translateToExternal( aTestURL.Complete, sPraesentationURL, INetURLObject::DECODE_UNAMBIGUOUS );
355cdf0e10cSrcweir
356cdf0e10cSrcweir return sPraesentationURL;
357cdf0e10cSrcweir }
358cdf0e10cSrcweir else
359cdf0e10cSrcweir return ::rtl::OUString();
360cdf0e10cSrcweir }
361cdf0e10cSrcweir
362cdf0e10cSrcweir //_________________________________________________________________________________________________________________
363cdf0e10cSrcweir // debug methods
364cdf0e10cSrcweir //_________________________________________________________________________________________________________________
365cdf0e10cSrcweir
366cdf0e10cSrcweir
367cdf0e10cSrcweir } // namespace framework
368cdf0e10cSrcweir
369