update dev300-m58
[ooovba.git] / comphelper / source / misc / mediadescriptor.cxx
blob5f6d2a480ee2e0e0fb3aa0c0df1b9095da931309
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mediadescriptor.cxx,v $
10 * $Revision: 1.20.22.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/mediadescriptor.hxx>
35 //_______________________________________________
36 // includes
38 #ifndef __COM_SUN_STAR_UCB_XCONTENT_HPP__
39 #include <com/sun/star/ucb/XContent.hpp>
40 #endif
42 #ifndef __COM_SUN_STAR_UCB_XCOMMANDENVIRONMENT_HPP__
43 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
44 #endif
46 #ifndef __COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP__
47 #include <com/sun/star/task/XInteractionHandler.hpp>
48 #endif
50 #ifndef __COM_SUN_STAR_IO_XSTREAM_HPP__
51 #include <com/sun/star/io/XStream.hpp>
52 #endif
53 #include <com/sun/star/io/XActiveDataSink.hpp>
54 #include <com/sun/star/io/XSeekable.hpp>
56 #ifndef __COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP__
57 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
58 #endif
59 #include <com/sun/star/lang/IllegalArgumentException.hpp>
61 #ifndef __COM_SUN_STAR_UTIL_XURLTRANSFORMER_HPP__
62 #include <com/sun/star/util/XURLTransformer.hpp>
63 #endif
65 #ifndef __COM_SUN_STAR_UCB_INTERACTIVEIOEXCEPTION_HPP__
66 #include <com/sun/star/ucb/InteractiveIOException.hpp>
67 #endif
69 #ifndef __COM_SUN_STAR_UCB_UNSUPPORTEDDATASINKEXCEPTION_HPP__
70 #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
71 #endif
73 #ifndef __COM_SUN_STAR_UCB_COMMANDFAILEDEXCEPTION_HPP__
74 #include <com/sun/star/ucb/CommandFailedException.hpp>
75 #endif
77 #ifndef __COM_SUN_STAR_TASK_XINTERACTIONABORT_HPP__
78 #include <com/sun/star/task/XInteractionAbort.hpp>
79 #endif
81 #ifndef __COM_SUN_STAR_URI_XURIREFERENCEFACTORY_HPP__
82 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
83 #endif
85 #ifndef __COM_SUN_STAR_URI_XURIREFERENCE_HPP__
86 #include <com/sun/star/uri/XUriReference.hpp>
87 #endif
88 #include <com/sun/star/ucb/PostCommandArgument2.hpp>
89 #include <com/sun/star/container/XNameAccess.hpp>
91 #include <ucbhelper/interceptedinteraction.hxx>
92 #include <ucbhelper/content.hxx>
93 #include <ucbhelper/commandenvironment.hxx>
94 #include <ucbhelper/activedatasink.hxx>
95 #include <comphelper/processfactory.hxx>
96 #include <comphelper/configurationhelper.hxx>
98 #if OSL_DEBUG_LEVEL>0
99 #ifndef _RTL_USTRBUF_HXX_
100 #include <rtl/ustrbuf.hxx>
101 #endif
102 #endif
104 //_______________________________________________
105 // namespace
107 namespace comphelper{
109 namespace css = ::com::sun::star;
111 //_______________________________________________
112 // definitions
114 /*-----------------------------------------------
115 10.03.2004 07:35
116 -----------------------------------------------*/
117 const ::rtl::OUString& MediaDescriptor::PROP_ABORTED()
119 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Aborted"));
120 return sProp;
123 const ::rtl::OUString& MediaDescriptor::PROP_ASTEMPLATE()
125 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("AsTemplate"));
126 return sProp;
129 const ::rtl::OUString& MediaDescriptor::PROP_CHARACTERSET()
131 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("CharacterSet"));
132 return sProp;
135 const ::rtl::OUString& MediaDescriptor::PROP_COMPONENTDATA()
137 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("ComponentData"));
138 return sProp;
141 const ::rtl::OUString& MediaDescriptor::PROP_DEEPDETECTION()
143 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("DeepDetection"));
144 return sProp;
147 const ::rtl::OUString& MediaDescriptor::PROP_DETECTSERVICE()
149 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("DetectService"));
150 return sProp;
153 const ::rtl::OUString& MediaDescriptor::PROP_DOCUMENTSERVICE()
155 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("DocumentService"));
156 return sProp;
159 const ::rtl::OUString& MediaDescriptor::PROP_EXTENSION()
161 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Extension"));
162 return sProp;
165 const ::rtl::OUString& MediaDescriptor::PROP_FILENAME()
167 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("FileName"));
168 return sProp;
171 const ::rtl::OUString& MediaDescriptor::PROP_FILTERNAME()
173 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("FilterName"));
174 return sProp;
177 const ::rtl::OUString& MediaDescriptor::PROP_FILTEROPTIONS()
179 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("FilterOptions"));
180 return sProp;
183 const ::rtl::OUString& MediaDescriptor::PROP_FORMAT()
185 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Format"));
186 return sProp;
189 const ::rtl::OUString& MediaDescriptor::PROP_FRAMENAME()
191 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("FrameName"));
192 return sProp;
195 const ::rtl::OUString& MediaDescriptor::PROP_HIDDEN()
197 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Hidden"));
198 return sProp;
201 const ::rtl::OUString& MediaDescriptor::PROP_INPUTSTREAM()
203 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("InputStream"));
204 return sProp;
207 const ::rtl::OUString& MediaDescriptor::PROP_INTERACTIONHANDLER()
209 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler"));
210 return sProp;
213 const ::rtl::OUString& MediaDescriptor::PROP_JUMPMARK()
215 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("JumpMark"));
216 return sProp;
219 const ::rtl::OUString& MediaDescriptor::PROP_MACROEXECUTIONMODE()
221 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("MacroExecutionMode"));
222 return sProp;
225 const ::rtl::OUString& MediaDescriptor::PROP_MEDIATYPE()
227 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("MediaType"));
228 return sProp;
231 const ::rtl::OUString& MediaDescriptor::PROP_MINIMIZED()
233 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Minimized"));
234 return sProp;
237 const ::rtl::OUString& MediaDescriptor::PROP_NOAUTOSAVE()
239 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("NoAutoSave"));
240 return sProp;
243 const ::rtl::OUString& MediaDescriptor::PROP_OPENNEWVIEW()
245 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("OpenNewView"));
246 return sProp;
249 const ::rtl::OUString& MediaDescriptor::PROP_OUTPUTSTREAM()
251 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("OutputStream"));
252 return sProp;
255 const ::rtl::OUString& MediaDescriptor::PROP_PATTERN()
257 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Pattern"));
258 return sProp;
261 const ::rtl::OUString& MediaDescriptor::PROP_POSSIZE()
263 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("PosSize"));
264 return sProp;
267 const ::rtl::OUString& MediaDescriptor::PROP_POSTDATA()
269 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("PostData"));
270 return sProp;
273 const ::rtl::OUString& MediaDescriptor::PROP_POSTSTRING()
275 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("PostString"));
276 return sProp;
279 const ::rtl::OUString& MediaDescriptor::PROP_PREVIEW()
281 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Preview"));
282 return sProp;
285 const ::rtl::OUString& MediaDescriptor::PROP_READONLY()
287 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
288 return sProp;
291 const ::rtl::OUString& MediaDescriptor::PROP_REFERRER()
293 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Referer"));
294 return sProp;
297 const ::rtl::OUString& MediaDescriptor::PROP_SILENT()
299 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Silent"));
300 return sProp;
303 const ::rtl::OUString& MediaDescriptor::PROP_STATUSINDICATOR()
305 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("StatusIndicator"));
306 return sProp;
309 const ::rtl::OUString& MediaDescriptor::PROP_STREAM()
311 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Stream"));
312 return sProp;
315 const ::rtl::OUString& MediaDescriptor::PROP_STREAMFOROUTPUT()
317 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("StreamForOutput"));
318 return sProp;
321 const ::rtl::OUString& MediaDescriptor::PROP_TEMPLATENAME()
323 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("TemplateName"));
324 return sProp;
327 const ::rtl::OUString& MediaDescriptor::PROP_TEMPLATEREGIONNAME()
329 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("TemplateRegionName"));
330 return sProp;
333 const ::rtl::OUString& MediaDescriptor::PROP_TYPENAME()
335 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("TypeName"));
336 return sProp;
339 const ::rtl::OUString& MediaDescriptor::PROP_UCBCONTENT()
341 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("UCBContent"));
342 return sProp;
345 const ::rtl::OUString& MediaDescriptor::PROP_UPDATEDOCMODE()
347 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("UpdateDocMode"));
348 return sProp;
351 const ::rtl::OUString& MediaDescriptor::PROP_URL()
353 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("URL"));
354 return sProp;
357 const ::rtl::OUString& MediaDescriptor::PROP_VERSION()
359 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Version"));
360 return sProp;
363 const ::rtl::OUString& MediaDescriptor::PROP_VIEWID()
365 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("ViewId"));
366 return sProp;
369 const ::rtl::OUString& MediaDescriptor::PROP_REPAIRPACKAGE()
371 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("RepairPackage"));
372 return sProp;
375 const ::rtl::OUString& MediaDescriptor::PROP_DOCUMENTTITLE()
377 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle"));
378 return sProp;
381 const ::rtl::OUString& MediaDescriptor::PROP_MODEL()
383 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Model"));
384 return sProp;
387 const ::rtl::OUString& MediaDescriptor::PROP_PASSWORD()
389 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Password"));
390 return sProp;
393 const ::rtl::OUString& MediaDescriptor::PROP_TITLE()
395 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("Title"));
396 return sProp;
399 const ::rtl::OUString& MediaDescriptor::PROP_SALVAGEDFILE()
401 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("SalvagedFile"));
402 return sProp;
405 const ::rtl::OUString& MediaDescriptor::PROP_VIEWONLY()
407 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("ViewOnly"));
408 return sProp;
411 const ::rtl::OUString& MediaDescriptor::PROP_DOCUMENTBASEURL()
413 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("DocumentBaseURL"));
414 return sProp;
417 const ::rtl::OUString& MediaDescriptor::PROP_VIEWCONTROLLERNAME()
419 static const ::rtl::OUString sProp(RTL_CONSTASCII_USTRINGPARAM("ViewControllerName"));
420 return sProp;
422 /*-----------------------------------------------
423 10.03.2004 08:09
424 -----------------------------------------------*/
425 MediaDescriptor::MediaDescriptor()
426 : SequenceAsHashMap()
430 /*-----------------------------------------------
431 10.03.2004 08:09
432 -----------------------------------------------*/
433 MediaDescriptor::MediaDescriptor(const css::uno::Any& aSource)
434 : SequenceAsHashMap(aSource)
438 /*-----------------------------------------------
439 10.03.2004 08:09
440 -----------------------------------------------*/
441 MediaDescriptor::MediaDescriptor(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
442 : SequenceAsHashMap(lSource)
446 /*-----------------------------------------------
447 10.03.2004 08:09
448 -----------------------------------------------*/
449 MediaDescriptor::MediaDescriptor(const css::uno::Sequence< css::beans::NamedValue >& lSource)
450 : SequenceAsHashMap(lSource)
454 /*-----------------------------------------------
455 18.11.2004 13:37
456 -----------------------------------------------*/
457 sal_Bool MediaDescriptor::isStreamReadOnly() const
459 static ::rtl::OUString CONTENTSCHEME_FILE = ::rtl::OUString::createFromAscii("file");
460 static ::rtl::OUString CONTENTPROP_ISREADONLY = ::rtl::OUString::createFromAscii("IsReadOnly");
461 static sal_Bool READONLY_FALLBACK = sal_False;
463 sal_Bool bReadOnly = READONLY_FALLBACK;
465 // check for explicit readonly state
466 const_iterator pIt = find(MediaDescriptor::PROP_READONLY());
467 if (pIt != end())
469 pIt->second >>= bReadOnly;
470 return bReadOnly;
473 // streams based on post data are readonly by definition
474 pIt = find(MediaDescriptor::PROP_POSTDATA());
475 if (pIt != end())
476 return sal_True;
478 // A XStream capsulate XInputStream and XOutputStream ...
479 // If it exists - the file must be open in read/write mode!
480 pIt = find(MediaDescriptor::PROP_STREAM());
481 if (pIt != end())
482 return sal_False;
484 // Only file system content provider is able to provide XStream
485 // so for this content impossibility to create XStream triggers
486 // switch to readonly mode.
489 css::uno::Reference< css::ucb::XContent > xContent = getUnpackedValueOrDefault(MediaDescriptor::PROP_UCBCONTENT(), css::uno::Reference< css::ucb::XContent >());
490 if (xContent.is())
492 css::uno::Reference< css::ucb::XContentIdentifier > xId(xContent->getIdentifier(), css::uno::UNO_QUERY);
493 ::rtl::OUString aScheme;
494 if (xId.is())
495 aScheme = xId->getContentProviderScheme();
497 if (aScheme.equalsIgnoreAsciiCase(CONTENTSCHEME_FILE))
498 bReadOnly = sal_True;
499 else
501 ::ucbhelper::Content aContent(xContent, css::uno::Reference< css::ucb::XCommandEnvironment >());
502 aContent.getPropertyValue(CONTENTPROP_ISREADONLY) >>= bReadOnly;
506 catch(const css::uno::RuntimeException& exRun)
507 { throw exRun; }
508 catch(const css::uno::Exception&)
511 return bReadOnly;
514 /*-----------------------------------------------
515 10.03.2004 09:02
516 -----------------------------------------------*/
517 sal_Bool MediaDescriptor::addInputStream()
519 return impl_addInputStream( sal_True );
522 /*-----------------------------------------------*/
523 sal_Bool MediaDescriptor::addInputStreamOwnLock()
525 // Own lock file implementation
527 sal_Bool bUseLock = sal_True; // the system file locking is used per default
531 css::uno::Reference< css::uno::XInterface > xCommonConfig = ::comphelper::ConfigurationHelper::openConfig(
532 ::comphelper::getProcessServiceFactory(),
533 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Common" ) ),
534 ::comphelper::ConfigurationHelper::E_STANDARD );
535 if ( !xCommonConfig.is() )
536 throw css::uno::RuntimeException();
538 ::comphelper::ConfigurationHelper::readRelativeKey(
539 xCommonConfig,
540 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Misc/" ) ),
541 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseDocumentSystemFileLocking" ) ) ) >>= bUseLock;
543 catch( const css::uno::Exception& )
547 return impl_addInputStream( bUseLock );
550 /*-----------------------------------------------*/
551 sal_Bool MediaDescriptor::impl_addInputStream( sal_Bool bLockFile )
553 // check for an already existing stream item first
554 const_iterator pIt = find(MediaDescriptor::PROP_INPUTSTREAM());
555 if (pIt != end())
556 return sal_True;
560 // No stream available - create a new one
561 // a) data comes as PostData ...
562 pIt = find(MediaDescriptor::PROP_POSTDATA());
563 if (pIt != end())
565 const css::uno::Any& rPostData = pIt->second;
566 css::uno::Reference< css::io::XInputStream > xPostData;
567 rPostData >>= xPostData;
569 return impl_openStreamWithPostData( xPostData );
572 // b) ... or we must get it from the given URL
573 ::rtl::OUString sURL = getUnpackedValueOrDefault(MediaDescriptor::PROP_URL(), ::rtl::OUString());
574 if (!sURL.getLength())
575 throw css::uno::Exception(
576 ::rtl::OUString::createFromAscii("Found no URL."),
577 css::uno::Reference< css::uno::XInterface >());
579 // Parse URL! Only the main part has to be used further. E.g. a jumpmark can make trouble
580 ::rtl::OUString sNormalizedURL = impl_normalizeURL( sURL );
581 return impl_openStreamWithURL( sNormalizedURL, bLockFile );
583 #if OSL_DEBUG_LEVEL>0
584 catch(const css::uno::Exception& ex)
586 ::rtl::OUStringBuffer sMsg(256);
587 sMsg.appendAscii("Invalid MediaDescriptor detected:\n");
588 sMsg.append (ex.Message );
589 OSL_ENSURE(sal_False, ::rtl::OUStringToOString(sMsg.makeStringAndClear(), RTL_TEXTENCODING_UTF8).getStr());
591 #else
592 catch(const css::uno::Exception&)
594 #endif
596 return sal_False;
599 /*-----------------------------------------------
600 25.03.2004 12:38
601 -----------------------------------------------*/
602 sal_Bool MediaDescriptor::impl_openStreamWithPostData( const css::uno::Reference< css::io::XInputStream >& _rxPostData )
603 throw(::com::sun::star::uno::RuntimeException)
605 if ( !_rxPostData.is() )
606 throw css::lang::IllegalArgumentException(
607 ::rtl::OUString::createFromAscii("Found invalid PostData."),
608 css::uno::Reference< css::uno::XInterface >(), 1);
610 // PostData can't be used in read/write mode!
611 (*this)[MediaDescriptor::PROP_READONLY()] <<= sal_True;
613 // prepare the environment
614 css::uno::Reference< css::task::XInteractionHandler > xInteraction = getUnpackedValueOrDefault(
615 MediaDescriptor::PROP_INTERACTIONHANDLER(),
616 css::uno::Reference< css::task::XInteractionHandler >());
617 css::uno::Reference< css::ucb::XProgressHandler > xProgress;
618 ::ucbhelper::CommandEnvironment* pCommandEnv = new ::ucbhelper::CommandEnvironment(xInteraction, xProgress);
619 css::uno::Reference< css::ucb::XCommandEnvironment > xCommandEnv(static_cast< css::ucb::XCommandEnvironment* >(pCommandEnv), css::uno::UNO_QUERY);
621 // media type
622 ::rtl::OUString sMediaType = getUnpackedValueOrDefault(MediaDescriptor::PROP_MEDIATYPE(), ::rtl::OUString());
623 if (!sMediaType.getLength())
625 sMediaType = ::rtl::OUString::createFromAscii("application/x-www-form-urlencoded");
626 (*this)[MediaDescriptor::PROP_MEDIATYPE()] <<= sMediaType;
629 // url
630 ::rtl::OUString sURL( getUnpackedValueOrDefault( PROP_URL(), ::rtl::OUString() ) );
632 css::uno::Reference< css::io::XInputStream > xResultStream;
635 // seek PostData stream to the beginning
636 css::uno::Reference< css::io::XSeekable > xSeek( _rxPostData, css::uno::UNO_QUERY );
637 if ( xSeek.is() )
638 xSeek->seek( 0 );
640 // a content for the URL
641 ::ucbhelper::Content aContent( sURL, xCommandEnv );
643 // use post command
644 css::ucb::PostCommandArgument2 aPostArgument;
645 aPostArgument.Source = _rxPostData;
646 css::uno::Reference< css::io::XActiveDataSink > xSink( new ucbhelper::ActiveDataSink );
647 aPostArgument.Sink = xSink;
648 aPostArgument.MediaType = sMediaType;
649 aPostArgument.Referer = getUnpackedValueOrDefault( PROP_REFERRER(), ::rtl::OUString() );
651 ::rtl::OUString sCommandName( RTL_CONSTASCII_USTRINGPARAM( "post" ) );
652 aContent.executeCommand( sCommandName, css::uno::makeAny( aPostArgument ) );
654 // get result
655 xResultStream = xSink->getInputStream();
657 catch( const css::uno::Exception& )
661 // success?
662 if ( !xResultStream.is() )
664 OSL_ENSURE( false, "no valid reply to the HTTP-Post" );
665 return sal_False;
668 (*this)[MediaDescriptor::PROP_INPUTSTREAM()] <<= xResultStream;
669 return sal_True;
672 /*-----------------------------------------------*/
673 class StillReadWriteInteraction : public ::ucbhelper::InterceptedInteraction
675 private:
676 static const sal_Int32 HANDLE_INTERACTIVEIOEXCEPTION = 0;
677 static const sal_Int32 HANDLE_UNSUPPORTEDDATASINKEXCEPTION = 1;
679 sal_Bool m_bUsed;
680 sal_Bool m_bHandledByMySelf;
681 sal_Bool m_bHandledByInternalHandler;
683 public:
684 StillReadWriteInteraction(const css::uno::Reference< css::task::XInteractionHandler >& xHandler)
685 : m_bUsed (sal_False)
686 , m_bHandledByMySelf (sal_False)
687 , m_bHandledByInternalHandler(sal_False)
689 ::std::vector< ::ucbhelper::InterceptedInteraction::InterceptedRequest > lInterceptions;
690 ::ucbhelper::InterceptedInteraction::InterceptedRequest aInterceptedRequest;
692 aInterceptedRequest.Handle = HANDLE_INTERACTIVEIOEXCEPTION;
693 aInterceptedRequest.Request <<= css::ucb::InteractiveIOException();
694 aInterceptedRequest.Continuation = ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0));
695 aInterceptedRequest.MatchExact = sal_False;
696 lInterceptions.push_back(aInterceptedRequest);
698 aInterceptedRequest.Handle = HANDLE_UNSUPPORTEDDATASINKEXCEPTION;
699 aInterceptedRequest.Request <<= css::ucb::UnsupportedDataSinkException();
700 aInterceptedRequest.Continuation = ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0));
701 aInterceptedRequest.MatchExact = sal_False;
702 lInterceptions.push_back(aInterceptedRequest);
704 setInterceptedHandler(xHandler);
705 setInterceptions(lInterceptions);
708 void resetInterceptions()
710 setInterceptions(::std::vector< ::ucbhelper::InterceptedInteraction::InterceptedRequest >());
713 void resetErrorStates()
715 m_bUsed = sal_False;
716 m_bHandledByMySelf = sal_False;
717 m_bHandledByInternalHandler = sal_False;
720 sal_Bool wasWriteError()
722 return (m_bUsed && m_bHandledByMySelf);
725 private:
726 virtual ucbhelper::InterceptedInteraction::EInterceptionState intercepted(const ::ucbhelper::InterceptedInteraction::InterceptedRequest& aRequest,
727 const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionRequest >& xRequest)
729 // we are used!
730 m_bUsed = sal_True;
732 // check if its a real interception - might some parameters are not the right ones ...
733 sal_Bool bAbort = sal_False;
734 switch(aRequest.Handle)
736 case HANDLE_INTERACTIVEIOEXCEPTION:
738 css::ucb::InteractiveIOException exIO;
739 xRequest->getRequest() >>= exIO;
740 bAbort = (
741 (exIO.Code == css::ucb::IOErrorCode_ACCESS_DENIED )
742 || (exIO.Code == css::ucb::IOErrorCode_LOCKING_VIOLATION )
743 #ifdef MACOSX
744 // this is a workaround for MAC, on this platform if the file is locked
745 // the returned error code looks to be wrong
746 || (exIO.Code == css::ucb::IOErrorCode_GENERAL )
747 #endif
750 break;
752 case HANDLE_UNSUPPORTEDDATASINKEXCEPTION:
754 bAbort = sal_True;
756 break;
759 // handle interaction by ourself
760 if (bAbort)
762 m_bHandledByMySelf = sal_True;
763 css::uno::Reference< css::task::XInteractionContinuation > xAbort = ::ucbhelper::InterceptedInteraction::extractContinuation(
764 xRequest->getContinuations(),
765 ::getCppuType(static_cast< css::uno::Reference< css::task::XInteractionAbort >* >(0)));
766 if (!xAbort.is())
767 return ::ucbhelper::InterceptedInteraction::E_NO_CONTINUATION_FOUND;
768 xAbort->select();
769 return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED;
772 // Otherwhise use internal handler.
773 if (m_xInterceptedHandler.is())
775 m_bHandledByInternalHandler = sal_True;
776 m_xInterceptedHandler->handle(xRequest);
778 return ::ucbhelper::InterceptedInteraction::E_INTERCEPTED;
782 /*-----------------------------------------------
783 25.03.2004 12:29
784 -----------------------------------------------*/
785 sal_Bool MediaDescriptor::impl_openStreamWithURL( const ::rtl::OUString& sURL, sal_Bool bLockFile )
786 throw(::com::sun::star::uno::RuntimeException)
788 // prepare the environment
789 css::uno::Reference< css::task::XInteractionHandler > xOrgInteraction = getUnpackedValueOrDefault(
790 MediaDescriptor::PROP_INTERACTIONHANDLER(),
791 css::uno::Reference< css::task::XInteractionHandler >());
793 StillReadWriteInteraction* pInteraction = new StillReadWriteInteraction(xOrgInteraction);
794 css::uno::Reference< css::task::XInteractionHandler > xInteraction(static_cast< css::task::XInteractionHandler* >(pInteraction), css::uno::UNO_QUERY);
796 css::uno::Reference< css::ucb::XProgressHandler > xProgress;
797 ::ucbhelper::CommandEnvironment* pCommandEnv = new ::ucbhelper::CommandEnvironment(xInteraction, xProgress);
798 css::uno::Reference< css::ucb::XCommandEnvironment > xCommandEnv(static_cast< css::ucb::XCommandEnvironment* >(pCommandEnv), css::uno::UNO_QUERY);
800 // try to create the content
801 // no content -> no stream => return immediatly with FALSE
802 ::ucbhelper::Content aContent;
803 css::uno::Reference< css::ucb::XContent > xContent;
806 aContent = ::ucbhelper::Content(sURL, xCommandEnv);
807 xContent = aContent.get();
809 catch(const css::uno::RuntimeException&)
810 { throw; }
811 catch(const css::ucb::ContentCreationException&)
812 { return sal_False; } // TODO error handling
813 catch(const css::uno::Exception&)
814 { return sal_False; } // TODO error handling
816 // try to open the file in read/write mode
817 // (if its allowed to do so).
818 // But handle errors in a "hidden mode". Because
819 // we try it readonly later - if read/write isnt an option.
820 css::uno::Reference< css::io::XStream > xStream ;
821 css::uno::Reference< css::io::XInputStream > xInputStream;
823 sal_Bool bReadOnly = sal_False;
824 sal_Bool bModeRequestedExplicitly = sal_False;
825 const_iterator pIt = find(MediaDescriptor::PROP_READONLY());
826 if (pIt != end())
828 pIt->second >>= bReadOnly;
829 bModeRequestedExplicitly = sal_True;
832 if ( !bReadOnly && bLockFile )
836 // TODO: use "special" still interaction to supress error messages
837 xStream = aContent.openWriteableStream();
838 if (xStream.is())
839 xInputStream = xStream->getInputStream();
841 catch(const css::uno::RuntimeException&)
842 { throw; }
843 catch(const css::uno::Exception&)
845 // ignore exception, if reason was problem reasoned on
846 // open it in WRITEABLE mode! Then we try it READONLY
847 // later a second time.
848 // All other errors must be handled as real error an
849 // break this method.
850 if (!pInteraction->wasWriteError() || bModeRequestedExplicitly)
851 return sal_False;
852 xStream.clear();
853 xInputStream.clear();
857 // If opening of the stream in read/write mode wasnt allowed
858 // or failed by an error - we must try it in readonly mode.
859 if (!xInputStream.is())
861 rtl::OUString aScheme;
865 css::uno::Reference< css::ucb::XContentIdentifier > xContId(
866 aContent.get().is() ? aContent.get()->getIdentifier() : 0 );
868 if ( xContId.is() )
869 aScheme = xContId->getContentProviderScheme();
871 // Only file system content provider is able to provide XStream
872 // so for this content impossibility to create XStream triggers
873 // switch to readonly mode in case of opening with locking on
874 if( bLockFile && aScheme.equalsIgnoreAsciiCaseAscii( "file" ) )
875 bReadOnly = sal_True;
876 else
878 sal_Bool bRequestReadOnly = bReadOnly;
879 aContent.getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsReadOnly" ) ) ) >>= bReadOnly;
880 if ( bReadOnly && !bRequestReadOnly && bModeRequestedExplicitly )
881 return sal_False; // the document is explicitly requested with WRITEABLE mode
884 catch(const css::uno::RuntimeException&)
885 { throw; }
886 catch(const css::uno::Exception&)
887 { /* no error handling if IsReadOnly property does not exist for UCP */ }
889 if ( bReadOnly )
890 (*this)[MediaDescriptor::PROP_READONLY()] <<= bReadOnly;
892 pInteraction->resetInterceptions();
893 pInteraction->resetErrorStates();
896 // all the contents except file-URLs should be opened as usual
897 if ( bLockFile || !aScheme.equalsIgnoreAsciiCaseAscii( "file" ) )
898 xInputStream = aContent.openStream();
899 else
900 xInputStream = aContent.openStreamNoLock();
902 catch(const css::uno::RuntimeException&)
903 { throw; }
904 catch(const css::uno::Exception&)
905 { return sal_False; }
908 // add streams to the descriptor
909 if (xContent.is())
910 (*this)[MediaDescriptor::PROP_UCBCONTENT()] <<= xContent;
911 if (xStream.is())
912 (*this)[MediaDescriptor::PROP_STREAM()] <<= xStream;
913 if (xInputStream.is())
914 (*this)[MediaDescriptor::PROP_INPUTSTREAM()] <<= xInputStream;
916 // At least we need an input stream. The r/w stream is optional ...
917 return xInputStream.is();
920 /*-----------------------------------------------
921 10.09.2004 10:51
922 -----------------------------------------------*/
923 ::rtl::OUString MediaDescriptor::impl_normalizeURL(const ::rtl::OUString& sURL)
925 /* Remove Jumpmarks (fragments) of an URL only here.
926 They are not part of any URL and as a result may be
927 no ucb content can be created then.
928 On the other side arguments must exists ... because
929 they are part of an URL.
931 Do not use the URLTransformer service here. Because
932 it parses the URL in another way. It's main part isnt enough
933 and it's complete part contains the jumpmark (fragment) parameter ...
935 static ::rtl::OUString SERVICENAME_URIREFERENCEFACTORY = ::rtl::OUString::createFromAscii("com.sun.star.uri.UriReferenceFactory");
939 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = ::comphelper::getProcessServiceFactory();
940 css::uno::Reference< css::uri::XUriReferenceFactory > xUriFactory(xSMGR->createInstance(SERVICENAME_URIREFERENCEFACTORY), css::uno::UNO_QUERY_THROW);
941 css::uno::Reference< css::uri::XUriReference > xUriRef = xUriFactory->parse(sURL);
942 if (xUriRef.is())
944 xUriRef->clearFragment();
945 return xUriRef->getUriReference();
948 catch(const css::uno::RuntimeException& exRun)
949 { throw exRun; }
950 catch(const css::uno::Exception&)
953 // If an error ocurred ... return the original URL.
954 // It's a try .-)
955 return sURL;
958 } // namespace comphelper