LibreOffice Module ucb (master)  1
cmis_url.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #include <sal/config.h>
11 
12 #include <rtl/uri.hxx>
13 #include <tools/urlobj.hxx>
14 
15 #include "cmis_url.hxx"
16 
17 namespace cmis
18 {
19  URL::URL( OUString const & urlStr )
20  {
21  INetURLObject aUrl( urlStr );
22 
23  // Decode the authority to get the binding URL and repository id
24  OUString sDecodedHost = aUrl.GetHost( INetURLObject::DecodeMechanism::WithCharset );
25  INetURLObject aHostUrl( sDecodedHost );
26  m_sBindingUrl = aHostUrl.GetURLNoMark( );
27  m_sRepositoryId = aHostUrl.GetMark( );
28 
31 
32  // Store the path to the object
35 
36  if ( m_sPath == "/" && m_sBindingUrl.indexOf( "google" ) != -1 )
37  m_sId = "root";
38  }
39 
40 
41  void URL::setObjectPath( const OUString& sPath )
42  {
43  m_sPath = sPath;
44  }
45 
46  void URL::setObjectId( const OUString& sId )
47  {
48  m_sId = sId;
49  }
50 
51  void URL::setUsername( const OUString& sUser )
52  {
53  m_sUser = sUser;
54  }
55 
56  OUString URL::asString( ) const
57  {
58  OUString sUrl;
59  // Related tdf#96174, can no longer save on Google Drive
60  // the user field may contain characters that need to be escaped according to
61  // RFC3896 userinfo URI field
62  // see <https://tools.ietf.org/html/rfc3986#section-3.2.1>
63  OUString sEncodedUser = ( m_sUser.isEmpty() ?
64  OUString() :
65  rtl::Uri::encode( m_sUser, rtl_UriCharClassUserinfo,
66  rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8) );
67  OUString sEncodedBinding = rtl::Uri::encode(
69  rtl_UriCharClassRelSegment,
70  rtl_UriEncodeKeepEscapes,
71  RTL_TEXTENCODING_UTF8 );
72  sUrl = "vnd.libreoffice.cmis://" +
73  ( sEncodedUser.isEmpty() ? OUString( ) : (sEncodedUser + "@") ) +
74  sEncodedBinding;
75 
76  if ( !m_sPath.isEmpty( ) )
77  {
78  sal_Int32 nPos = -1;
79  OUStringBuffer sEncodedPath;
80  do
81  {
82  sal_Int32 nStartPos = nPos + 1;
83  nPos = m_sPath.indexOf( '/', nStartPos );
84  sal_Int32 nLen = nPos - nStartPos;
85  if ( nPos == -1 )
86  nLen = m_sPath.getLength( ) - nStartPos;
87  OUString sSegment = m_sPath.copy( nStartPos, nLen );
88 
89  if ( !sSegment.isEmpty( ) )
90  {
91  sEncodedPath.append("/" + rtl::Uri::encode( sSegment,
92  rtl_UriCharClassRelSegment,
93  rtl_UriEncodeKeepEscapes,
94  RTL_TEXTENCODING_UTF8 ));
95  }
96  }
97  while ( nPos != -1 );
98  sUrl += sEncodedPath.makeStringAndClear();
99  } else if ( !m_sId.isEmpty( ) )
100  {
101  sUrl += "#" + rtl::Uri::encode( m_sId,
102  rtl_UriCharClassRelSegment,
103  rtl_UriEncodeKeepEscapes,
104  RTL_TEXTENCODING_UTF8 );
105  }
106 
107  return sUrl;
108  }
109 }
110 
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
URL(OUString const &urlStr)
Definition: cmis_url.cxx:19
OUString m_sBindingUrl
Definition: cmis_url.hxx:18
OUString GetURLPath(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString m_sUser
Definition: cmis_url.hxx:22
OUString asString() const
Definition: cmis_url.cxx:56
OUString GetUser(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
void setUsername(const OUString &sUser)
Definition: cmis_url.cxx:51
OUString GetURLNoMark(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString m_sRepositoryId
Definition: cmis_url.hxx:19
OUString m_sPass
Definition: cmis_url.hxx:23
void setObjectId(const OUString &sId)
Definition: cmis_url.cxx:46
OUString m_sPath
Definition: cmis_url.hxx:20
OUString GetHost(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
void setObjectPath(const OUString &sPath)
Definition: cmis_url.cxx:41
OUString GetMark(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString m_sId
Definition: cmis_url.hxx:21
OUString GetPass(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
sal_uInt16 nPos