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 using namespace std;
18 
19 namespace cmis
20 {
21  URL::URL( OUString const & urlStr )
22  {
23  INetURLObject aUrl( urlStr );
24 
25  // Decode the authority to get the binding URL and repository id
26  OUString sDecodedHost = aUrl.GetHost( INetURLObject::DecodeMechanism::WithCharset );
27  INetURLObject aHostUrl( sDecodedHost );
28  m_sBindingUrl = aHostUrl.GetURLNoMark( );
29  m_sRepositoryId = aHostUrl.GetMark( );
30 
33 
34  // Store the path to the object
37 
38  if ( m_sPath == "/" && m_sBindingUrl.indexOf( "google" ) != -1 )
39  m_sId = "root";
40  }
41 
42 
43  void URL::setObjectPath( const OUString& sPath )
44  {
45  m_sPath = sPath;
46  }
47 
48  void URL::setObjectId( const OUString& sId )
49  {
50  m_sId = sId;
51  }
52 
53  void URL::setUsername( const OUString& sUser )
54  {
55  m_sUser = sUser;
56  }
57 
58  OUString URL::asString( )
59  {
60  OUString sUrl;
61  // Related tdf#96174, can no longer save on Google Drive
62  // the user field may contain characters that need to be escaped according to
63  // RFC3896 userinfo URI field
64  // see <https://tools.ietf.org/html/rfc3986#section-3.2.1>
65  OUString sEncodedUser = ( m_sUser.isEmpty() ?
66  OUString() :
67  rtl::Uri::encode( m_sUser, rtl_UriCharClassUserinfo,
68  rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8) );
69  OUString sEncodedBinding = rtl::Uri::encode(
70  m_sBindingUrl + "#" + m_sRepositoryId,
71  rtl_UriCharClassRelSegment,
72  rtl_UriEncodeKeepEscapes,
73  RTL_TEXTENCODING_UTF8 );
74  sUrl = "vnd.libreoffice.cmis://" +
75  ( sEncodedUser.isEmpty() ? OUString( ) : (sEncodedUser + "@") ) +
76  sEncodedBinding;
77 
78  if ( !m_sPath.isEmpty( ) )
79  {
80  sal_Int32 nPos = -1;
81  OUStringBuffer sEncodedPath;
82  do
83  {
84  sal_Int32 nStartPos = nPos + 1;
85  nPos = m_sPath.indexOf( '/', nStartPos );
86  sal_Int32 nLen = nPos - nStartPos;
87  if ( nPos == -1 )
88  nLen = m_sPath.getLength( ) - nStartPos;
89  OUString sSegment = m_sPath.copy( nStartPos, nLen );
90 
91  if ( !sSegment.isEmpty( ) )
92  {
93  sEncodedPath.append("/").append(rtl::Uri::encode( sSegment,
94  rtl_UriCharClassRelSegment,
95  rtl_UriEncodeKeepEscapes,
96  RTL_TEXTENCODING_UTF8 ));
97  }
98  }
99  while ( nPos != -1 );
100  sUrl += sEncodedPath.makeStringAndClear();
101  } else if ( !m_sId.isEmpty( ) )
102  {
103  sUrl += "#" + rtl::Uri::encode( m_sId,
104  rtl_UriCharClassRelSegment,
105  rtl_UriEncodeKeepEscapes,
106  RTL_TEXTENCODING_UTF8 );
107  }
108 
109  return sUrl;
110  }
111 }
112 
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetURLPath(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetUser(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetURLNoMark(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetHost(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetMark(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetPass(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
sal_Int32 nPos