1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xmlsignaturehelper.hxx>
21 #include <xmlsignaturehelper2.hxx>
23 #include <tools/solar.h>
24 #include <unotools/streamhelper.hxx>
26 #include <com/sun/star/embed/XStorage.hpp>
27 #include <com/sun/star/embed/XStorageRawAccess.hpp>
28 #include <com/sun/star/embed/ElementModes.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <osl/diagnose.h>
31 #include <rtl/uri.hxx>
33 using namespace com::sun::star
;
37 UriBindingHelper::UriBindingHelper()
41 UriBindingHelper::UriBindingHelper( const css::uno::Reference
< css::embed::XStorage
>& rxStorage
)
43 mxStorage
= rxStorage
;
46 void SAL_CALL
UriBindingHelper::setUriBinding( const OUString
& /*uri*/, const uno::Reference
< io::XInputStream
>&)
50 uno::Reference
< io::XInputStream
> SAL_CALL
UriBindingHelper::getUriBinding( const OUString
& uri
)
52 uno::Reference
< io::XInputStream
> xInputStream
;
55 xInputStream
= OpenInputStream( mxStorage
, uri
);
59 SvFileStream
* pStream
= new SvFileStream( uri
, StreamMode::READ
);
60 pStream
->Seek( STREAM_SEEK_TO_END
);
61 sal_uLong nBytes
= pStream
->Tell();
62 pStream
->Seek( STREAM_SEEK_TO_BEGIN
);
63 SvLockBytesRef xLockBytes
= new SvLockBytes( pStream
, true );
64 xInputStream
= new utl::OInputStreamHelper( xLockBytes
, nBytes
);
69 uno::Reference
< io::XInputStream
> UriBindingHelper::OpenInputStream( const uno::Reference
< embed::XStorage
>& rxStore
, const OUString
& rURI
)
71 OSL_ASSERT(!rURI
.isEmpty());
72 uno::Reference
< io::XInputStream
> xInStream
;
75 // Ignore leading slash, don't attempt to open a storage with name "".
76 if (aURI
.startsWith("/"))
78 // Ignore query part of the URI.
79 sal_Int32 nQueryPos
= aURI
.indexOf('?');
81 aURI
= aURI
.copy(0, nQueryPos
);
84 sal_Int32 nSepPos
= aURI
.indexOf( '/' );
87 // Cloning because of I can't keep all storage references open
88 // MBA with think about a better API...
89 const OUString sName
= ::rtl::Uri::decode(
90 aURI
, rtl_UriDecodeStrict
, rtl_UriCharClassRelSegment
);
91 if (sName
.isEmpty() && !aURI
.isEmpty())
92 throw uno::Exception("Could not decode URI for stream element.", nullptr);
94 uno::Reference
< io::XStream
> xStream
;
95 uno::Reference
<container::XNameAccess
> xNameAccess(rxStore
, uno::UNO_QUERY
);
96 if (!xNameAccess
->hasByName(sName
))
97 SAL_WARN("xmlsecurity.helper", "expected stream, but not found: " << sName
);
99 xStream
= rxStore
->cloneStreamElement( sName
);
101 throw uno::RuntimeException();
102 xInStream
= xStream
->getInputStream();
106 const OUString aStoreName
= ::rtl::Uri::decode(
107 aURI
.copy( 0, nSepPos
), rtl_UriDecodeStrict
, rtl_UriCharClassRelSegment
);
108 if (aStoreName
.isEmpty() && !aURI
.isEmpty())
109 throw uno::Exception("Could not decode URI for stream element.", nullptr);
111 OUString aElement
= aURI
.copy( nSepPos
+1 );
112 uno::Reference
< embed::XStorage
> xSubStore
= rxStore
->openStorageElement( aStoreName
, embed::ElementModes::READ
);
113 xInStream
= OpenInputStream( xSubStore
, aElement
);
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */