fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / webdav / SerfLockReqProcImpl.cxx
blobff766effa1089054a4c4d5787e6a2e0cf232f77b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "SerfLockReqProcImpl.hxx"
22 #include "AprEnv.hxx"
23 #include "SerfSession.hxx"
24 #include "DAVException.hxx"
26 #include "webdavresponseparser.hxx"
27 #include <rtl/strbuf.hxx>
29 namespace http_dav_ucp
32 SerfLockReqProcImpl::SerfLockReqProcImpl( const char* inPath,
33 const DAVRequestHeaders& inRequestHeaders,
34 SerfSession& rSession,
35 const css::ucb::Lock& rLock,
36 sal_Int32* plastChanceToSendRefreshRequest )
37 : SerfRequestProcessorImpl( inPath, inRequestHeaders )
38 , m_rSession( rSession )
39 , m_aLock( rLock )
40 , m_plastChanceToSendRefreshRequest( plastChanceToSendRefreshRequest )
41 , m_xInputStream( new SerfInputStream() )
45 SerfLockReqProcImpl::~SerfLockReqProcImpl()
49 serf_bucket_t * SerfLockReqProcImpl::createSerfRequestBucket( serf_request_t * inSerfRequest )
51 serf_bucket_alloc_t* pSerfBucketAlloc = serf_request_get_alloc( inSerfRequest );
53 OStringBuffer aBody("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
54 "<lockinfo xmlns='DAV:'>\n <lockscope>");
56 // Set the lock scope
57 switch ( m_aLock.Scope )
59 case css::ucb::LockScope_EXCLUSIVE:
60 aBody.append("<exclusive/>");
61 break;
62 case css::ucb::LockScope_SHARED:
63 aBody.append("<shared/>");
64 break;
65 default:
66 throw DAVException( DAVException::DAV_INVALID_ARG );
68 aBody.append("</lockscope>\n <locktype><write/></locktype>\n");
70 // Set the lock owner
71 OUString aValue;
72 if ((m_aLock.Owner >>= aValue) && !aValue.isEmpty())
74 aBody.append(" <owner>");
75 aBody.append(OUStringToOString(aValue, RTL_TEXTENCODING_UTF8));
76 aBody.append("</owner>\n");
78 aBody.append("</lockinfo>\n");
80 const OString aBodyText(aBody.makeStringAndClear());
81 serf_bucket_t* body_bkt = 0;
83 if (!m_plastChanceToSendRefreshRequest)
84 body_bkt = serf_bucket_simple_copy_create( aBodyText.getStr(),
85 aBodyText.getLength(),
86 pSerfBucketAlloc );
88 // create serf request
89 serf_bucket_t *req_bkt = serf_request_bucket_request_create( inSerfRequest,
90 "LOCK",
91 getPathStr(),
92 body_bkt,
93 pSerfBucketAlloc );
94 if (!m_plastChanceToSendRefreshRequest)
95 handleChunkedEncoding(req_bkt, aBodyText.getLength());
97 // set request header fields
98 serf_bucket_t* hdrs_bkt = serf_bucket_request_get_headers( req_bkt );
100 // general header fields provided by caller
101 setRequestHeaders( hdrs_bkt );
103 // request specific header fields
104 const char * depth = 0;
105 switch( m_aLock.Depth )
107 case css::ucb::LockDepth_ZERO:
108 depth = "0";
109 break;
110 case css::ucb::LockDepth_ONE:
111 depth = "1";
112 break;
113 case css::ucb::LockDepth_INFINITY:
114 depth = "infinity";
115 break;
116 default:
117 throw DAVException( DAVException::DAV_INVALID_ARG );
119 if (!m_plastChanceToSendRefreshRequest)
121 serf_bucket_headers_set( hdrs_bkt, "Depth", depth );
122 serf_bucket_headers_set( hdrs_bkt, "Content-Type", "application/xml" );
124 else
126 const OString sToken( "(<" + OUStringToOString( apr_environment::AprEnv::getAprEnv()->
127 getSerfLockStore()->getLockToken( OUString::createFromAscii(getPathStr())),
128 RTL_TEXTENCODING_UTF8 ) + ">)" );
129 serf_bucket_headers_set( hdrs_bkt, "If", sToken.getStr() );
132 // Set the lock timeout
133 if (m_aLock.Timeout == -1)
134 serf_bucket_headers_set( hdrs_bkt, "Timeout", "Infinite" );
135 else if (m_aLock.Timeout > 0)
137 const OString aTimeValue("Second-" + OString::number(m_aLock.Timeout));
138 serf_bucket_headers_set( hdrs_bkt, "Timeout", aTimeValue.getStr() );
140 else
141 serf_bucket_headers_set( hdrs_bkt, "Timeout", "Second-180" );
143 osl_getSystemTime( &m_aStartCall );
145 return req_bkt;
148 void SerfLockReqProcImpl::processChunkOfResponseData( const char* data,
149 apr_size_t len )
151 if ( m_xInputStream.is() )
153 m_xInputStream->AddToStream( data, len );
157 void SerfLockReqProcImpl::handleEndOfResponseData( serf_bucket_t * )
159 const std::vector< css::ucb::Lock > aLocks( parseWebDAVLockResponse( m_xInputStream.get() ) );
161 if (!aLocks.empty())
163 for (size_t i = 0; i < aLocks.size(); ++i)
165 sal_Int64 timeout = aLocks[i].Timeout;
166 TimeValue aEnd;
167 osl_getSystemTime( &aEnd );
168 // Try to estimate a safe absolute time for sending the
169 // lock refresh request.
170 sal_Int32 lastChanceToSendRefreshRequest = -1;
171 if ( timeout != -1 )
173 sal_Int32 calltime = aEnd.Seconds - m_aStartCall.Seconds;
174 if ( calltime <= timeout )
175 lastChanceToSendRefreshRequest = aEnd.Seconds + timeout - calltime;
176 else
177 SAL_WARN("ucb.ucp.webdav", "No chance to refresh lock before timeout!" );
179 if (m_plastChanceToSendRefreshRequest)
181 *m_plastChanceToSendRefreshRequest = lastChanceToSendRefreshRequest;
182 assert(aLocks.size() == 1);
183 // We are just refreshing lock, do not add it into SerfLockStore
184 break;
186 apr_environment::AprEnv::getAprEnv()->getSerfLockStore()->addLock(
187 OUString::createFromAscii(getPathStr()),
188 aLocks[i].LockTokens[0],
189 &m_rSession, lastChanceToSendRefreshRequest );
190 SAL_INFO("ucb.ucp.webdav", "SerfSession::LOCK: created lock for "
191 << getPathStr() << ". token: " << aLocks[i].LockTokens[0]);
194 else
196 SAL_INFO("ucb.ucp.webdav", "SerfSession::LOCK: obtaining lock failed!");
200 } // namespace http_dav_ucp
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */