1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_ucb.hxx"
24 
25 #include <rtl/ustring.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include "DAVProperties.hxx"
28 #include "UCBDeadPropertyValue.hxx"
29 
30 #include "SerfPropPatchReqProcImpl.hxx"
31 #include "SerfTypes.hxx"
32 
33 namespace http_dav_ucp
34 {
35 
36 SerfPropPatchReqProcImpl::SerfPropPatchReqProcImpl( const char* inPath,
37                                                     const DAVRequestHeaders& inRequestHeaders,
38                                                     const std::vector< ProppatchValue > & inProperties )
39     : SerfRequestProcessorImpl( inPath, inRequestHeaders )
40     , mpProperties( &inProperties )
41 {
42 }
43 
44 SerfPropPatchReqProcImpl::~SerfPropPatchReqProcImpl()
45 {
46 }
47 
48 #define PROPPATCH_HEADER "<?xml version=\"1.0\" encoding=\"utf-8\"?><propertyupdate xmlns=\"DAV:\">"
49 #define PROPPATCH_TRAILER "</propertyupdate>"
50 
51 serf_bucket_t * SerfPropPatchReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
52 {
53     serf_bucket_alloc_t* pSerfBucketAlloc = serf_request_get_alloc( inSerfRequest );
54 
55     // body bucket
56     serf_bucket_t* body_bkt = 0;
57     rtl::OString aBodyText;
58     {
59         // create and fill body bucket with properties to be set or removed
60         static const struct
61         {
62             const char *str;
63             sal_Int32   len;
64         }
65         OpCode [] = {
66             { RTL_CONSTASCII_STRINGPARAM( "set" ) },
67             { RTL_CONSTASCII_STRINGPARAM( "remove" ) }
68         };
69         const int nPropCount = ( mpProperties != 0 )
70                                ? mpProperties->size()
71                                : 0;
72         if ( nPropCount > 0 )
73         {
74             rtl::OUStringBuffer aBuffer;
75             // add PropPatch xml header in front
76             aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( PROPPATCH_HEADER ));
77 
78             // <*operation code*><prop>
79 
80             ProppatchOperation lastOp = (*mpProperties)[ 0 ].operation;
81             aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "<" ));
82             aBuffer.appendAscii( OpCode[lastOp].str, OpCode[lastOp].len );
83             aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "><prop>" ));
84 
85             SerfPropName thePropName;
86             for ( int n = 0; n < nPropCount; ++n )
87             {
88                 const ProppatchValue & rProperty = (*mpProperties)[ n ];
89                 // split fullname into namespace and name!
90                 DAVProperties::createSerfPropName( rProperty.name,
91                                                    thePropName );
92 
93                 if ( rProperty.operation != lastOp )
94                 {
95                     // </prop></*last operation code*><*operation code><prop>
96                     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "</prop></" ));
97                     aBuffer.appendAscii( OpCode[lastOp].str, OpCode[lastOp].len );
98                     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "><" ));
99                     aBuffer.appendAscii( OpCode[rProperty.operation].str, OpCode[rProperty.operation].len );
100                     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "><prop>" ));
101                 }
102 
103                 // <*propname* xmlns="*propns*"
104                 aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "<" ));
105                 aBuffer.appendAscii( thePropName.name );
106                 aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " xmlns=\"" ));
107                 aBuffer.appendAscii( thePropName.nspace );
108                 aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "\"" ));
109 
110                 if ( rProperty.operation == PROPSET )
111                 {
112                     // >*property value*</*propname*>
113                     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( ">" ));
114 
115                     rtl::OUString aStringValue;
116                     if ( DAVProperties::isUCBDeadProperty( thePropName ) )
117                     {
118                         UCBDeadPropertyValue::toXML( rProperty.value,
119                                                      aStringValue );
120                     }
121                     else
122                     {
123                         rProperty.value >>= aStringValue;
124                     }
125                     aBuffer.append( aStringValue );
126                     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "</" ));
127                     aBuffer.appendAscii( thePropName.name );
128                     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( ">" ));
129                 }
130                 else
131                 {
132                     // />
133                     aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "/>" ));
134                 }
135 
136                 lastOp = rProperty.operation;
137             }
138 
139             // </prop></*last operation code*>
140             aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "</prop></" ));
141             aBuffer.appendAscii( OpCode[lastOp].str, OpCode[lastOp].len );
142             aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( ">" ));
143 
144             // add PropPatch xml trailer at end
145             aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( PROPPATCH_TRAILER ));
146 
147             aBodyText = rtl::OUStringToOString( aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 );
148             body_bkt = serf_bucket_simple_copy_create( aBodyText.getStr(),
149                                                        aBodyText.getLength(),
150                                                        pSerfBucketAlloc );
151         }
152     }
153 
154     // create serf request
155     serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
156                                                                  "PROPPATCH",
157                                                                  getPathStr(),
158                                                                  body_bkt,
159                                                                  pSerfBucketAlloc ) ;
160     handleChunkedEncoding(req_bkt, aBodyText.getLength());
161 
162     // set request header fields
163     serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
164     if (hdrs_bkt != NULL)
165     {
166         // general header fields provided by caller
167         setRequestHeaders( hdrs_bkt );
168 
169         // request specific header fields
170         if ( body_bkt != 0 && aBodyText.getLength() > 0 )
171         {
172             serf_bucket_headers_set( hdrs_bkt, "Content-Type", "application/xml" );
173         }
174     }
175     else
176     {
177         OSL_ASSERT("Headers Bucket missing");
178     }
179 
180     return req_bkt;
181 }
182 
183 void SerfPropPatchReqProcImpl::processChunkOfResponseData( const char* /*data*/,
184                                                            apr_size_t /*len*/ )
185 {
186     // nothing to do;
187 }
188 
189 void SerfPropPatchReqProcImpl::handleEndOfResponseData( serf_bucket_t * /*inSerfResponseBucket*/ )
190 {
191     // nothing to do;
192 }
193 
194 } // namespace http_dav_ucp
195