1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_forms.hxx"
30 
31 #include <memory>
32 
33 #include "submission_get.hxx"
34 #include "serialization_app_xml.hxx"
35 #include "serialization_urlencoded.hxx"
36 
37 #include <rtl/strbuf.hxx>
38 #include <rtl/string.hxx>
39 #include <osl/file.hxx>
40 #include <unotools/processfactory.hxx>
41 #include <ucbhelper/content.hxx>
42 
43 using namespace CSS::uno;
44 using namespace CSS::ucb;
45 using namespace CSS::task;
46 using namespace CSS::io;
47 using namespace rtl;
48 using namespace osl;
49 using namespace ucbhelper;
50 using namespace std;
51 
52 
53 CSubmissionGet::CSubmissionGet(const rtl::OUString& aURL, const CSS::uno::Reference< CSS::xml::dom::XDocumentFragment >& aFragment)
54     : CSubmission(aURL, aFragment)
55 {
56 }
57 
58 CSubmission::SubmissionResult CSubmissionGet::submit(const CSS::uno::Reference< CSS::task::XInteractionHandler >& aInteractionHandler)
59 {
60     // GET always uses apllicatin/x-www-formurlencoded
61     auto_ptr< CSerialization > apSerialization(new CSerializationURLEncoded());
62     apSerialization->setSource(m_aFragment);
63     apSerialization->serialize();
64 
65     CSS::uno::Reference< XInputStream > aInStream = apSerialization->getInputStream();
66 
67     // create a commandEnvironment and use the default interaction handler
68     CCommandEnvironmentHelper *pHelper = new CCommandEnvironmentHelper;
69     if( aInteractionHandler.is() )
70         pHelper->m_aInteractionHandler = aInteractionHandler;
71     else
72         pHelper->m_aInteractionHandler = CSS::uno::Reference< XInteractionHandler >(m_aFactory->createInstance(
73             OUString::createFromAscii("com.sun.star.task.InteractionHandler")), UNO_QUERY);
74     OSL_ENSURE(pHelper->m_aInteractionHandler.is(), "failed to create IntreractionHandler");
75     CProgressHandlerHelper *pProgressHelper = new CProgressHandlerHelper;
76     pHelper->m_aProgressHandler = CSS::uno::Reference< XProgressHandler >(pProgressHelper);
77 
78     // UCB has ownership of environment...
79     CSS::uno::Reference< XCommandEnvironment > aEnvironment(pHelper);
80 
81     // append query string to the URL
82     try {
83         OStringBuffer aUTF8QueryURL(OUStringToOString(m_aURLObj.GetMainURL(INetURLObject::NO_DECODE),
84             RTL_TEXTENCODING_UTF8));
85 		OStringBuffer aQueryString;
86 
87         const sal_Int32 size = 1024;
88         sal_Int32 n = 0;
89         Sequence< sal_Int8 > aByteBuffer(size);
90         while ((n = aInStream->readSomeBytes(aByteBuffer, size-1)) != 0)
91             aQueryString.append((sal_Char*)aByteBuffer.getArray(), n);
92 		if (aQueryString.getLength() > 0 && m_aURLObj.GetProtocol() != INET_PROT_FILE)
93 		{
94 			aUTF8QueryURL.append('?');
95 			aUTF8QueryURL.append(aQueryString.makeStringAndClear());
96 		}
97         OUString aQueryURL = OStringToOUString(aUTF8QueryURL.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
98         ucbhelper::Content aContent(aQueryURL, aEnvironment);
99         CSS::uno::Reference< XOutputStream > aPipe(m_aFactory->createInstance(
100             OUString::createFromAscii("com.sun.star.io.Pipe")), UNO_QUERY_THROW);
101         aContent.openStream(aPipe);
102         // get reply
103         try {
104             m_aResultStream = aContent.openStream();
105         } catch (Exception&) {
106             OSL_ENSURE(sal_False, "Cannot open reply stream from content");
107         }
108     } catch (Exception&)
109     {
110         // XXX
111         OSL_ENSURE(sal_False, "Exception during UCB operatration.");
112         return UNKNOWN_ERROR;
113     }
114 
115     return SUCCESS;
116 }
117 
118