diff options
Diffstat (limited to 'forms/source/xforms/submission')
-rw-r--r-- | forms/source/xforms/submission/replace.cxx | 131 | ||||
-rw-r--r-- | forms/source/xforms/submission/serialization.hxx | 60 | ||||
-rw-r--r-- | forms/source/xforms/submission/serialization_app_xml.cxx | 126 | ||||
-rw-r--r-- | forms/source/xforms/submission/serialization_app_xml.hxx | 40 | ||||
-rw-r--r-- | forms/source/xforms/submission/serialization_urlencoded.cxx | 181 | ||||
-rw-r--r-- | forms/source/xforms/submission/serialization_urlencoded.hxx | 47 | ||||
-rw-r--r-- | forms/source/xforms/submission/submission.hxx | 137 | ||||
-rw-r--r-- | forms/source/xforms/submission/submission_get.cxx | 103 | ||||
-rw-r--r-- | forms/source/xforms/submission/submission_get.hxx | 33 | ||||
-rw-r--r-- | forms/source/xforms/submission/submission_post.cxx | 78 | ||||
-rw-r--r-- | forms/source/xforms/submission/submission_post.hxx | 32 | ||||
-rw-r--r-- | forms/source/xforms/submission/submission_put.cxx | 67 | ||||
-rw-r--r-- | forms/source/xforms/submission/submission_put.hxx | 33 |
13 files changed, 1068 insertions, 0 deletions
diff --git a/forms/source/xforms/submission/replace.cxx b/forms/source/xforms/submission/replace.cxx new file mode 100644 index 000000000..e6154ef61 --- /dev/null +++ b/forms/source/xforms/submission/replace.cxx @@ -0,0 +1,131 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <memory> +#include "submission.hxx" +#include "serialization_app_xml.hxx" + +#include <rtl/ustring.hxx> +#include <rtl/ref.hxx> +#include <tools/diagnose_ex.h> +#include <o3tl/string_view.hxx> + +#include <comphelper/processfactory.hxx> +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/xml/dom/XDocument.hpp> +#include <com/sun/star/xml/dom/DocumentBuilder.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/frame/XComponentLoader.hpp> +#include <com/sun/star/frame/FrameSearchFlag.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/task/InteractionHandler.hpp> + +using namespace com::sun::star::uno; +using namespace com::sun::star::ucb; +using namespace com::sun::star::frame; +using namespace com::sun::star::lang; +using namespace com::sun::star::beans; +using namespace com::sun::star::task; +using namespace com::sun::star::xml::dom; + +CSubmission::SubmissionResult CSubmission::replace(std::u16string_view aReplace, const Reference<XDocument>& aDocument, const Reference<XFrame>& aFrame) +{ + if (!m_aResultStream.is()) + return CSubmission::UNKNOWN_ERROR; + + try { + Reference< XComponentContext > xContext = comphelper::getProcessComponentContext(); + if (o3tl::equalsIgnoreAsciiCase(aReplace, u"all") + || o3tl::equalsIgnoreAsciiCase(aReplace, u"document")) { + Reference< XComponentLoader > xLoader; + if (aFrame.is()) + xLoader.set(aFrame, UNO_QUERY); + + if (!xLoader.is()) + xLoader.set( Desktop::create(xContext), UNO_QUERY_THROW); + + // open the stream from the result... + // build media descriptor + Sequence< PropertyValue > descriptor{ + PropertyValue("InputStream", + -1, Any(m_aResultStream), PropertyState_DIRECT_VALUE), + PropertyValue("ReadOnly", + -1, Any(true), PropertyState_DIRECT_VALUE) + }; + + OUString aURL = m_aURLObj.GetMainURL(INetURLObject::DecodeMechanism::NONE); + xLoader->loadComponentFromURL(aURL, "_default", FrameSearchFlag::ALL, descriptor); + + return CSubmission::SUCCESS; + + } else if (o3tl::equalsIgnoreAsciiCase(aReplace, u"instance")) { + if (aDocument.is()) { + // parse the result stream into a new document + Reference< XDocumentBuilder > xBuilder(DocumentBuilder::create(xContext)); + Reference< XDocument > aNewDocument = xBuilder->parse(m_aResultStream); + + if (aNewDocument.is()) { + // and replace the content of the current instance + Reference< XElement > oldRoot = aDocument->getDocumentElement(); + Reference< XElement > newRoot = aNewDocument->getDocumentElement(); + + Reference< XNode > aImportedNode = aDocument->importNode(newRoot, true); + aDocument->replaceChild(aImportedNode, oldRoot); + return CSubmission::SUCCESS; + } else { + return CSubmission::UNKNOWN_ERROR; + } + } else { + // nothing to replace + return CSubmission::UNKNOWN_ERROR; + } + } else if (o3tl::equalsIgnoreAsciiCase(aReplace, u"none")) { + // do nothing \o/ + return CSubmission::SUCCESS; + } + } catch (const Exception&) { + TOOLS_WARN_EXCEPTION( "forms.xforms", "Exception during replace"); + } + return CSubmission::UNKNOWN_ERROR; +} + +::std::unique_ptr< CSerialization > CSubmission::createSerialization(const Reference< XInteractionHandler >& _xHandler,Reference<XCommandEnvironment>& _rOutEnv) +{ + // PUT always uses application/xml + ::std::unique_ptr< CSerialization > apSerialization(new CSerializationAppXML()); + apSerialization->setSource(m_aFragment); + apSerialization->serialize(); + + // create a commandEnvironment and use the default interaction handler + rtl::Reference<CCommandEnvironmentHelper> pHelper = new CCommandEnvironmentHelper; + if( _xHandler.is() ) + pHelper->m_aInteractionHandler = _xHandler; + else + pHelper->m_aInteractionHandler.set( + InteractionHandler::createWithParent(m_xContext, nullptr), UNO_QUERY_THROW); + + rtl::Reference<CProgressHandlerHelper> pProgressHelper = new CProgressHandlerHelper; + pHelper->m_aProgressHandler.set(pProgressHelper); + + // UCB has ownership of environment... + _rOutEnv = pHelper; + return apSerialization; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/serialization.hxx b/forms/source/xforms/submission/serialization.hxx new file mode 100644 index 000000000..19f072b0b --- /dev/null +++ b/forms/source/xforms/submission/serialization.hxx @@ -0,0 +1,60 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/xml/dom/XDocumentFragment.hpp> + +/** +Serialize an XObject +*/ + +class CSerialization +{ +protected: + css::uno::Reference<css::xml::dom::XDocumentFragment> m_aFragment; + +public: + virtual ~CSerialization() {} + + /** + sets the XObject that is to serialized + */ + void setSource(const css::uno::Reference<css::xml::dom::XDocumentFragment>& aFragment) + { + m_aFragment = aFragment; + } + + /** + start the serialization process + */ + virtual void serialize() = 0; + + /** + get the serialized bytes. + reads up to buffer->getLength() bytes and returns the number of + bytes read. + returns -1 on error + */ + virtual css::uno::Reference<css::io::XInputStream> getInputStream() = 0; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/serialization_app_xml.cxx b/forms/source/xforms/submission/serialization_app_xml.cxx new file mode 100644 index 000000000..61729d027 --- /dev/null +++ b/forms/source/xforms/submission/serialization_app_xml.cxx @@ -0,0 +1,126 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "serialization_app_xml.hxx" + +#include <com/sun/star/io/Pipe.hpp> +#include <com/sun/star/xml/dom/DocumentBuilder.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XDocument.hpp> +#include <com/sun/star/xml/dom/NodeType.hpp> +#include <com/sun/star/xml/sax/XSAXSerializable.hpp> +#include <com/sun/star/xml/sax/Writer.hpp> +#include <com/sun/star/beans/StringPair.hpp> +#include <com/sun/star/xml/dom/XDocumentBuilder.hpp> + +#include <tools/diagnose_ex.h> +#include <comphelper/processfactory.hxx> + +using ::com::sun::star::uno::Reference; +using ::com::sun::star::uno::Exception; +using ::com::sun::star::uno::Sequence; +using ::com::sun::star::uno::UNO_QUERY; +using ::com::sun::star::uno::UNO_QUERY_THROW; +using ::com::sun::star::uno::UNO_SET_THROW; +using ::com::sun::star::xml::dom::DocumentBuilder; +using ::com::sun::star::xml::dom::XNode; +using ::com::sun::star::xml::dom::XDocument; +using ::com::sun::star::xml::sax::XSAXSerializable; +using ::com::sun::star::beans::StringPair; +using ::com::sun::star::xml::dom::NodeType_DOCUMENT_NODE; +using ::com::sun::star::xml::dom::NodeType_ELEMENT_NODE; +using ::com::sun::star::xml::dom::XDocumentBuilder; +using ::com::sun::star::xml::sax::Writer; +using ::com::sun::star::xml::sax::XDocumentHandler; + +CSerializationAppXML::CSerializationAppXML() + : m_xBuffer(css::io::Pipe::create(comphelper::getProcessComponentContext())) +{ +} + +Reference< css::io::XInputStream > +CSerializationAppXML::getInputStream() +{ + // The pipes output is provided through it's + // XOutputStream interface aspect + return m_xBuffer; +} + +void +CSerializationAppXML::serialize_node(const Reference< XNode >& rNode) +{ + try + { + Reference< XSAXSerializable > xSerializer( rNode, UNO_QUERY ); + if ( !xSerializer.is() ) + { + // ensure we have a "real" node + Reference< XNode > xNode = rNode; + if ( xNode->getNodeType() == NodeType_DOCUMENT_NODE ) + { + Reference< XDocument > const xDoc( xNode, UNO_QUERY_THROW ); + xNode.set( xDoc->getDocumentElement(), UNO_QUERY_THROW ); + } + ENSURE_OR_RETURN_VOID( xNode->getNodeType() == NodeType_ELEMENT_NODE, + "CSerializationAppXML::serialize_node: invalid node type!" ); + + // create a new document + Reference< XDocumentBuilder > const xDocBuilder = DocumentBuilder::create( comphelper::getProcessComponentContext() ); + Reference< XDocument > const xDocument( xDocBuilder->newDocument(), UNO_SET_THROW ); + + // copy the to-be-serialized node + Reference< XNode > const xImportedNode( xDocument->importNode( xNode, true ), UNO_SET_THROW ); + xDocument->appendChild( xImportedNode ); + + // ask the doc for the serializer + xSerializer.set( xDocument, UNO_QUERY ); + } + + ENSURE_OR_RETURN_VOID( xSerializer.is(), + "CSerializationAppXML::serialize_node: no serialization access to the node/document!" ); + + // create a SAXWriter to take the serialization events, and connect it to our pipe + Reference< css::xml::sax::XWriter > const xSaxWriter = Writer::create( comphelper::getProcessComponentContext() ); + xSaxWriter->setOutputStream( Reference< css::io::XOutputStream >( m_xBuffer, UNO_QUERY_THROW) ); + + // do the serialization + xSerializer->serialize( Reference< XDocumentHandler >(xSaxWriter, UNO_QUERY_THROW), Sequence< StringPair >() ); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("forms.xforms"); + } +} + +void +CSerializationAppXML::serialize() +{ + if (!m_aFragment.is()) return; + + Reference< XNode > cur = m_aFragment->getFirstChild(); + while (cur.is()) + { + serialize_node(cur); + cur = cur->getNextSibling(); + } + m_xBuffer->closeOutput(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/serialization_app_xml.hxx b/forms/source/xforms/submission/serialization_app_xml.hxx new file mode 100644 index 000000000..51fb1eb54 --- /dev/null +++ b/forms/source/xforms/submission/serialization_app_xml.hxx @@ -0,0 +1,40 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <com/sun/star/io/XPipe.hpp> + +#include "serialization.hxx" + +class CSerializationAppXML : public CSerialization +{ +private: + css::uno::Reference<css::io::XPipe> m_xBuffer; + + void serialize_node(const css::uno::Reference<css::xml::dom::XNode>& aNode); + +public: + CSerializationAppXML(); + + virtual void serialize() override; + virtual css::uno::Reference<css::io::XInputStream> getInputStream() override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/serialization_urlencoded.cxx b/forms/source/xforms/submission/serialization_urlencoded.cxx new file mode 100644 index 000000000..22a625ccb --- /dev/null +++ b/forms/source/xforms/submission/serialization_urlencoded.cxx @@ -0,0 +1,181 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <com/sun/star/io/Pipe.hpp> +#include <com/sun/star/xml/xpath/XPathObjectType.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XText.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <com/sun/star/xml/dom/NodeType.hpp> +#include <rtl/character.hxx> +#include <rtl/ustrbuf.hxx> +#include <rtl/strbuf.hxx> +#include <comphelper/processfactory.hxx> + +#include <stdio.h> + +#include "serialization_urlencoded.hxx" + +using namespace css::uno; +using namespace css::io; +using namespace css::xml::xpath; +using namespace css::xml::dom; + +CSerializationURLEncoded::CSerializationURLEncoded() + : m_aPipe(Pipe::create(comphelper::getProcessComponentContext())) +{ +} + + +/* + rfc2396 + reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | + "$" | "," + mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" + unreserved = alphanum | mark +*/ +bool CSerializationURLEncoded::is_unreserved(char c) +{ + if (rtl::isAsciiAlphanumeric(static_cast<unsigned char>(c))) + return true; + switch (c) { + case '-': + case '_': + case '.': + case '!': + case '~': + case '*': + case '\'': + case '(': + case ')': + return true; + } + return false; +} +void CSerializationURLEncoded::encode_and_append( + std::u16string_view aString, OStringBuffer& aBuffer) +{ + OString utf8String = OUStringToOString(aString, RTL_TEXTENCODING_UTF8); + const sal_uInt8 *pString = reinterpret_cast< const sal_uInt8 * >( utf8String.getStr() ); + char tmpChar[4]; + + while( *pString != 0) + { + if( *pString < 0x80 ) + { + if ( is_unreserved(*pString) ) { + aBuffer.append(char(*pString)); + } else if (*pString == 0x20) { + aBuffer.append('+'); + } else if (*pString == 0x0d && *(pString+1) == 0x0a) { + aBuffer.append("%0D%0A"); + pString++; + } else if (*pString == 0x0a) { + aBuffer.append("%0D%0A"); + } else { + snprintf(tmpChar, 4, "%%%X", *pString % 0x100); + aBuffer.append(tmpChar); + } + } else { + snprintf(tmpChar, 4, "%%%X", *pString % 0x100); + aBuffer.append(tmpChar); + while (*pString >= 0x80) { + // continuation... + pString++; + snprintf(tmpChar, 4, "%%%X", *pString % 0x100); + aBuffer.append(tmpChar); + } + } + pString++; + } +} + +void CSerializationURLEncoded::serialize_node(const Reference< XNode >& aNode) +{ + // serialize recursive + // every element node E that has a text child T will be serialized in document order + // <E1>T1<E2>T2</E2></E1><E3>T3</E3> -> E1=T2&E2=T2&E3=T3 (En := local name) + + // this node + Reference< XNodeList > aChildList = aNode->getChildNodes(); + Reference< XNode > aChild; + // is this an element node? + if (aNode->getNodeType() == NodeType_ELEMENT_NODE) + { + OUString aName = aNode->getNodeName(); + // find any text children + OUStringBuffer aValue; + Reference< XText > aText; + for(sal_Int32 i=0; i < aChildList->getLength(); i++) + { + aChild = aChildList->item(i); + if (aChild->getNodeType() == NodeType_TEXT_NODE) + { + aText.set(aChild, UNO_QUERY); + aValue.append(aText->getData()); + } + } + + // found anything? + if (!aValue.isEmpty()) + { + OUString aUnencValue = aValue.makeStringAndClear(); + OStringBuffer aEncodedBuffer; + encode_and_append(aName, aEncodedBuffer); + aEncodedBuffer.append("="); + encode_and_append(aUnencValue, aEncodedBuffer); + aEncodedBuffer.append("&"); + sal_Int8 const *pData = reinterpret_cast<sal_Int8 const *>(aEncodedBuffer.getStr()); + Sequence< sal_Int8 > sData(pData, aEncodedBuffer.getLength()); + m_aPipe->writeBytes(sData); + } + } + + // element children... + for(sal_Int32 i=0; i < aChildList->getLength(); i++) + { + aChild = aChildList->item(i); + // if this is an element node, it might be a candidate for serialization + if (aChild.is() && aChild->getNodeType() == NodeType_ELEMENT_NODE) + serialize_node(aChild); + } +} + +void CSerializationURLEncoded::serialize() +{ + + // output stream to the pipe buffer + + css::uno::Reference< css::xml::dom::XNode > cur = m_aFragment->getFirstChild(); + while (cur.is()) + { + serialize_node(cur); + cur = cur->getNextSibling(); + } + m_aPipe->closeOutput(); +} + +Reference< XInputStream > CSerializationURLEncoded::getInputStream() +{ + return m_aPipe; +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/serialization_urlencoded.hxx b/forms/source/xforms/submission/serialization_urlencoded.hxx new file mode 100644 index 000000000..2ae91c1f1 --- /dev/null +++ b/forms/source/xforms/submission/serialization_urlencoded.hxx @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <sal/config.h> + +#include <string_view> + +#include <com/sun/star/io/XPipe.hpp> + +#include <rtl/strbuf.hxx> + +#include "serialization.hxx" + +class CSerializationURLEncoded : public CSerialization +{ +private: + css::uno::Reference<css::io::XPipe> m_aPipe; + + static bool is_unreserved(char); + static void encode_and_append(std::u16string_view aString, OStringBuffer& aBuffer); + void serialize_node(const css::uno::Reference<css::xml::dom::XNode>& aNode); + +public: + CSerializationURLEncoded(); + virtual void serialize() override; + virtual css::uno::Reference<css::io::XInputStream> getInputStream() override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/submission.hxx b/forms/source/xforms/submission/submission.hxx new file mode 100644 index 000000000..ac80947aa --- /dev/null +++ b/forms/source/xforms/submission/submission.hxx @@ -0,0 +1,137 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <tools/urlobj.hxx> +#include <osl/conditn.hxx> +#include <osl/mutex.hxx> +#include <comphelper/processfactory.hxx> +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/xml/dom/XDocumentFragment.hpp> + +#include <com/sun/star/ucb/XCommandEnvironment.hpp> +#include <com/sun/star/ucb/XProgressHandler.hpp> + +#include <com/sun/star/task/XInteractionHandler.hpp> + +#include <com/sun/star/frame/XFrame.hpp> + +#include <cppuhelper/implbase.hxx> + +#include "serialization.hxx" + +#include <memory> + +class CSubmissionPut; +class CSubmissionPost; +class CSubmissionGet; + +class CCommandEnvironmentHelper final : public cppu::WeakImplHelper< css::ucb::XCommandEnvironment > +{ + friend class CSubmissionPut; + friend class CSubmissionPost; + friend class CSubmissionGet; + friend class CSubmission; + + css::uno::Reference< css::task::XInteractionHandler > m_aInteractionHandler; + css::uno::Reference< css::ucb::XProgressHandler > m_aProgressHandler; + +public: + virtual css::uno::Reference< css::task::XInteractionHandler > SAL_CALL getInteractionHandler() override + { + return m_aInteractionHandler; + } + virtual css::uno::Reference< css::ucb::XProgressHandler > SAL_CALL getProgressHandler() override + { + return m_aProgressHandler; + } +}; + +class CProgressHandlerHelper final : public cppu::WeakImplHelper< css::ucb::XProgressHandler > +{ + friend class CSubmissionPut; + friend class CSubmissionPost; + friend class CSubmissionGet; + osl::Condition m_cFinished; + osl::Mutex m_mLock; + sal_Int32 m_count; +public: + CProgressHandlerHelper() + : m_count(0) + {} + virtual void SAL_CALL push( const css::uno::Any& /*aStatus*/) override + { + m_mLock.acquire(); + m_count++; + m_mLock.release(); + } + virtual void SAL_CALL update(const css::uno::Any& /*aStatus*/) override + { + } + virtual void SAL_CALL pop() override + { + m_mLock.acquire(); + m_count--; + if (m_count == 0) + m_cFinished.set(); + m_mLock.release(); + } +}; + +class CSubmission +{ + +protected: + INetURLObject m_aURLObj; + css::uno::Reference< css::xml::dom::XDocumentFragment > m_aFragment; + css::uno::Reference< css::io::XInputStream > m_aResultStream; + css::uno::Reference< css::uno::XComponentContext > m_xContext; + + ::std::unique_ptr< CSerialization > createSerialization(const css::uno::Reference< css::task::XInteractionHandler >& aHandler + ,css::uno::Reference<css::ucb::XCommandEnvironment>& _rOutEnv); + +public: + enum SubmissionResult { + SUCCESS, + UNKNOWN_ERROR + }; + + CSubmission(std::u16string_view aURL, const css::uno::Reference< css::xml::dom::XDocumentFragment >& aFragment) + : m_aURLObj(aURL) + , m_aFragment(aFragment) + , m_xContext(::comphelper::getProcessComponentContext()) + {} + + bool IsWebProtocol() const + { + INetProtocol eProtocol = m_aURLObj.GetProtocol(); + return eProtocol == INetProtocol::Http || eProtocol == INetProtocol::Https; + } + + virtual ~CSubmission() {} + + virtual SubmissionResult submit(const css::uno::Reference< css::task::XInteractionHandler >& ) = 0; + + SubmissionResult replace(std::u16string_view, const css::uno::Reference< css::xml::dom::XDocument >&, const css::uno::Reference< css::frame::XFrame>&); + +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/submission_get.cxx b/forms/source/xforms/submission/submission_get.cxx new file mode 100644 index 000000000..5a82aeae0 --- /dev/null +++ b/forms/source/xforms/submission/submission_get.cxx @@ -0,0 +1,103 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "submission_get.hxx" +#include "serialization_urlencoded.hxx" + +#include <rtl/strbuf.hxx> +#include <osl/diagnose.h> +#include <ucbhelper/content.hxx> +#include <com/sun/star/io/Pipe.hpp> +#include <com/sun/star/task/InteractionHandler.hpp> +#include <tools/diagnose_ex.h> + +using namespace css::uno; +using namespace css::ucb; +using namespace css::task; +using namespace css::io; +using namespace osl; +using namespace ucbhelper; + + +CSubmissionGet::CSubmissionGet(std::u16string_view aURL, const css::uno::Reference< css::xml::dom::XDocumentFragment >& aFragment) + : CSubmission(aURL, aFragment) +{ +} + +CSubmission::SubmissionResult CSubmissionGet::submit(const css::uno::Reference< css::task::XInteractionHandler >& aInteractionHandler) +{ + // GET always uses application/x-www-formurlencoded + CSerializationURLEncoded aSerialization; + aSerialization.setSource(m_aFragment); + aSerialization.serialize(); + + css::uno::Reference< XInputStream > aInStream = aSerialization.getInputStream(); + + // create a commandEnvironment and use the default interaction handler + rtl::Reference<CCommandEnvironmentHelper> pHelper = new CCommandEnvironmentHelper; + if( aInteractionHandler.is() ) + pHelper->m_aInteractionHandler = aInteractionHandler; + else + pHelper->m_aInteractionHandler.set( + css::task::InteractionHandler::createWithParent(m_xContext, nullptr), UNO_QUERY_THROW); + rtl::Reference<CProgressHandlerHelper> pProgressHelper = new CProgressHandlerHelper; + pHelper->m_aProgressHandler.set(pProgressHelper); + + // UCB has ownership of environment... + css::uno::Reference< XCommandEnvironment > aEnvironment(pHelper); + + // append query string to the URL + try { + OStringBuffer aUTF8QueryURL(OUStringToOString(m_aURLObj.GetMainURL(INetURLObject::DecodeMechanism::NONE), + RTL_TEXTENCODING_UTF8)); + OStringBuffer aQueryString; + + const sal_Int32 size = 1024; + sal_Int32 n = 0; + Sequence< sal_Int8 > aByteBuffer(size); + while ((n = aInStream->readSomeBytes(aByteBuffer, size-1)) != 0) + aQueryString.append(reinterpret_cast<char const *>(aByteBuffer.getConstArray()), n); + if (!aQueryString.isEmpty() && m_aURLObj.GetProtocol() != INetProtocol::File) + { + aUTF8QueryURL.append('?'); + aUTF8QueryURL.append(aQueryString); + } + OUString aQueryURL = OStringToOUString(aUTF8QueryURL.makeStringAndClear(), RTL_TEXTENCODING_UTF8); + ucbhelper::Content aContent(aQueryURL, aEnvironment, m_xContext); + css::uno::Reference< XOutputStream > aPipe( css::io::Pipe::create(m_xContext), UNO_QUERY_THROW ); + if (!aContent.openStream(aPipe)) + return UNKNOWN_ERROR; + // get reply + try { + m_aResultStream = aContent.openStream(); + } catch (const Exception&) { + OSL_FAIL("Cannot open reply stream from content"); + } + } catch (const Exception&) + { + // XXX + TOOLS_WARN_EXCEPTION( "forms.misc", "Exception during UCB operation."); + return UNKNOWN_ERROR; + } + + return SUCCESS; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/submission_get.hxx b/forms/source/xforms/submission/submission_get.hxx new file mode 100644 index 000000000..086fab25b --- /dev/null +++ b/forms/source/xforms/submission/submission_get.hxx @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "submission.hxx" + +class CSubmissionGet : public CSubmission +{ +public: + CSubmissionGet(std::u16string_view aURL, + const css::uno::Reference<css::xml::dom::XDocumentFragment>& aFragment); + virtual SubmissionResult + submit(const css::uno::Reference<css::task::XInteractionHandler>& aInteractionHandler) override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/submission_post.cxx b/forms/source/xforms/submission/submission_post.cxx new file mode 100644 index 000000000..89ffd2323 --- /dev/null +++ b/forms/source/xforms/submission/submission_post.cxx @@ -0,0 +1,78 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <memory> + +#include "submission_post.hxx" + +#include <osl/diagnose.h> +#include <ucbhelper/content.hxx> +#include <ucbhelper/activedatasink.hxx> +#include <com/sun/star/ucb/PostCommandArgument2.hpp> +#include <tools/diagnose_ex.h> + +using namespace css::uno; +using namespace css::ucb; +using namespace css::task; +using namespace css::io; +using namespace osl; +using namespace ucbhelper; + + +CSubmissionPost::CSubmissionPost(std::u16string_view aURL, const css::uno::Reference< css::xml::dom::XDocumentFragment >& aFragment) + : CSubmission(aURL, aFragment) +{ +} + +CSubmission::SubmissionResult CSubmissionPost::submit(const css::uno::Reference< css::task::XInteractionHandler >& aInteractionHandler) +{ + // PUT always uses application/xml + css::uno::Reference< XCommandEnvironment > aEnvironment; + std::unique_ptr< CSerialization > apSerialization(createSerialization(aInteractionHandler,aEnvironment)); + + try { + ucbhelper::Content aContent(m_aURLObj.GetMainURL(INetURLObject::DecodeMechanism::NONE), aEnvironment, comphelper::getProcessComponentContext()); + + // use post command + PostCommandArgument2 aPostArgument; + aPostArgument.Source = apSerialization->getInputStream(); + css::uno::Reference< XActiveDataSink > aSink(new ucbhelper::ActiveDataSink); + aPostArgument.Sink = aSink; + aPostArgument.MediaType = "application/xml"; + aPostArgument.Referer.clear(); + Any aCommandArgument; + aCommandArgument <<= aPostArgument; + aContent.executeCommand( "post", aCommandArgument); + + try { + m_aResultStream = aSink->getInputStream(); + } catch (const Exception&) { + OSL_FAIL("Cannot open reply stream from content"); + } + } catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "forms.misc", "Exception during UCB operation."); + return UNKNOWN_ERROR; + } + + return SUCCESS; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/submission_post.hxx b/forms/source/xforms/submission/submission_post.hxx new file mode 100644 index 000000000..0fb63472b --- /dev/null +++ b/forms/source/xforms/submission/submission_post.hxx @@ -0,0 +1,32 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "submission.hxx" + +class CSubmissionPost : public CSubmission +{ +public: + CSubmissionPost(std::u16string_view aURL, const css::uno::Reference< css::xml::dom::XDocumentFragment >& aFragment); + virtual SubmissionResult submit(const css::uno::Reference< css::task::XInteractionHandler >& aInteractionHandler) override; + +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/submission_put.cxx b/forms/source/xforms/submission/submission_put.cxx new file mode 100644 index 000000000..678cd7921 --- /dev/null +++ b/forms/source/xforms/submission/submission_put.cxx @@ -0,0 +1,67 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <memory> + +#include "submission_put.hxx" + +#include <comphelper/processfactory.hxx> +#include <ucbhelper/content.hxx> +#include <tools/diagnose_ex.h> + +using namespace css::uno; +using namespace css::ucb; +using namespace css::task; +using namespace css::io; +using namespace osl; +using namespace ucbhelper; + + +CSubmissionPut::CSubmissionPut(std::u16string_view aURL, const css::uno::Reference< css::xml::dom::XDocumentFragment >& aFragment) + : CSubmission(aURL, aFragment) +{ +} + +CSubmission::SubmissionResult CSubmissionPut::submit(const css::uno::Reference< css::task::XInteractionHandler >& aInteractionHandler) +{ + css::uno::Reference< XCommandEnvironment > aEnvironment; + std::unique_ptr< CSerialization > apSerialization(createSerialization(aInteractionHandler,aEnvironment)); + + try { + ucbhelper::Content aContent(m_aURLObj.GetMainURL(INetURLObject::DecodeMechanism::NONE), aEnvironment, comphelper::getProcessComponentContext()); + + // insert serialized data to content -> PUT + css::uno::Reference< XInputStream > aInStream = apSerialization->getInputStream(); + aContent.writeStream(aInStream, true); + //aContent.closeStream(); + + // no content as a result of put... + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "forms.misc", "Exception during UCB operation." ); + return UNKNOWN_ERROR; + } + + + return SUCCESS; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/forms/source/xforms/submission/submission_put.hxx b/forms/source/xforms/submission/submission_put.hxx new file mode 100644 index 000000000..2dce84307 --- /dev/null +++ b/forms/source/xforms/submission/submission_put.hxx @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include "submission.hxx" + +class CSubmissionPut : public CSubmission +{ +public: + CSubmissionPut(std::u16string_view aURL, + const css::uno::Reference<css::xml::dom::XDocumentFragment>& aFragment); + virtual SubmissionResult + submit(const css::uno::Reference<css::task::XInteractionHandler>& aInteractionHandler) override; +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |