1818use OCP \Federation \ICloudFederationProviderManager ;
1919use OCP \Federation \ICloudFederationShare ;
2020use OCP \Federation \ICloudIdManager ;
21+ use OCP \Http \Client \IClient ;
2122use OCP \Http \Client \IClientService ;
2223use OCP \Http \Client \IResponse ;
2324use OCP \IAppConfig ;
@@ -105,25 +106,11 @@ public function getCloudFederationProvider($resourceType) {
105106 public function sendShare (ICloudFederationShare $ share ) {
106107 $ cloudID = $ this ->cloudIdManager ->resolveCloudId ($ share ->getShareWith ());
107108 try {
108- $ ocmProvider = $ this ->discoveryService ->discover ($ cloudID ->getRemote ());
109- } catch (OCMProviderException $ e ) {
110- return false ;
111- }
112-
113- $ client = $ this ->httpClientService ->newClient ();
114- try {
115- // signing the payload using OCMSignatoryManager before initializing the request
116- $ uri = $ ocmProvider ->getEndPoint () . '/shares ' ;
117- $ payload = array_merge ($ this ->getDefaultRequestOptions (), ['body ' => json_encode ($ share ->getShare ())]);
118- if (!$ this ->appConfig ->getValueBool ('core ' , OCMSignatoryManager::APPCONFIG_SIGN_DISABLED , lazy: true )) {
119- $ signedPayload = $ this ->signatureManager ->signOutgoingRequestIClientPayload (
120- $ this ->signatoryManager ,
121- $ payload ,
122- 'post ' , $ uri
123- );
109+ try {
110+ $ response = $ this ->postOcmPayload ($ cloudID ->getRemote (), '/shares ' , json_encode ($ share ->getShare ()));
111+ } catch (OCMProviderException ) {
112+ return false ;
124113 }
125- $ response = $ client ->post ($ uri , $ signedPayload ?? $ payload );
126-
127114 if ($ response ->getStatusCode () === Http::STATUS_CREATED ) {
128115 $ result = json_decode ($ response ->getBody (), true );
129116 return (is_array ($ result )) ? $ result : [];
@@ -149,22 +136,9 @@ public function sendShare(ICloudFederationShare $share) {
149136 */
150137 public function sendCloudShare (ICloudFederationShare $ share ): IResponse {
151138 $ cloudID = $ this ->cloudIdManager ->resolveCloudId ($ share ->getShareWith ());
152- $ ocmProvider = $ this ->discoveryService ->discover ($ cloudID ->getRemote ());
153-
154139 $ client = $ this ->httpClientService ->newClient ();
155140 try {
156- // signing the payload using OCMSignatoryManager before initializing the request
157- $ uri = $ ocmProvider ->getEndPoint () . '/shares ' ;
158- $ payload = array_merge ($ this ->getDefaultRequestOptions (), ['body ' => json_encode ($ share ->getShare ())]);
159- if (!$ this ->appConfig ->getValueBool ('core ' , OCMSignatoryManager::APPCONFIG_SIGN_DISABLED , lazy: true )) {
160- $ signedPayload = $ this ->signatureManager ->signOutgoingRequestIClientPayload (
161- $ this ->signatoryManager ,
162- $ payload ,
163- 'post ' , $ uri
164- );
165- }
166-
167- return $ client ->post ($ uri , $ signedPayload ?? $ payload );
141+ return $ this ->postOcmPayload ($ cloudID ->getRemote (), '/shares ' , json_encode ($ share ->getShare ()), $ client );
168142 } catch (\Throwable $ e ) {
169143 $ this ->logger ->error ('Error while sending share to federation server: ' . $ e ->getMessage (), ['exception ' => $ e ]);
170144 try {
@@ -183,26 +157,11 @@ public function sendCloudShare(ICloudFederationShare $share): IResponse {
183157 */
184158 public function sendNotification ($ url , ICloudFederationNotification $ notification ) {
185159 try {
186- $ ocmProvider = $ this ->discoveryService ->discover ($ url );
187- } catch (OCMProviderException $ e ) {
188- return false ;
189- }
190-
191- $ client = $ this ->httpClientService ->newClient ();
192- try {
193-
194- // signing the payload using OCMSignatoryManager before initializing the request
195- $ uri = $ ocmProvider ->getEndPoint () . '/notifications ' ;
196- $ payload = array_merge ($ this ->getDefaultRequestOptions (), ['body ' => json_encode ($ notification ->getMessage ())]);
197- if (!$ this ->appConfig ->getValueBool ('core ' , OCMSignatoryManager::APPCONFIG_SIGN_DISABLED , lazy: true )) {
198- $ signedPayload = $ this ->signatureManager ->signOutgoingRequestIClientPayload (
199- $ this ->signatoryManager ,
200- $ payload ,
201- 'post ' , $ uri
202- );
160+ try {
161+ $ response = $ this ->postOcmPayload ($ url , '/notifications ' , json_encode ($ notification ->getMessage ()));
162+ } catch (OCMProviderException ) {
163+ return false ;
203164 }
204- $ response = $ client ->post ($ uri , $ signedPayload ?? $ payload );
205-
206165 if ($ response ->getStatusCode () === Http::STATUS_CREATED ) {
207166 $ result = json_decode ($ response ->getBody (), true );
208167 return (is_array ($ result )) ? $ result : [];
@@ -222,21 +181,9 @@ public function sendNotification($url, ICloudFederationNotification $notificatio
222181 * @throws OCMProviderException
223182 */
224183 public function sendCloudNotification (string $ url , ICloudFederationNotification $ notification ): IResponse {
225- $ ocmProvider = $ this ->discoveryService ->discover ($ url );
226-
227184 $ client = $ this ->httpClientService ->newClient ();
228185 try {
229- // signing the payload using OCMSignatoryManager before initializing the request
230- $ uri = $ ocmProvider ->getEndPoint () . '/notifications ' ;
231- $ payload = array_merge ($ this ->getDefaultRequestOptions (), ['body ' => json_encode ($ notification ->getMessage ())]);
232- if (!$ this ->appConfig ->getValueBool ('core ' , OCMSignatoryManager::APPCONFIG_SIGN_DISABLED , lazy: true )) {
233- $ signedPayload = $ this ->signatureManager ->signOutgoingRequestIClientPayload (
234- $ this ->signatoryManager ,
235- $ payload ,
236- 'post ' , $ uri
237- );
238- }
239- return $ client ->post ($ uri , $ signedPayload ?? $ payload );
186+ return $ this ->postOcmPayload ($ url , '/notifications ' , json_encode ($ notification ->getMessage ()), $ client );
240187 } catch (\Throwable $ e ) {
241188 $ this ->logger ->error ('Error while sending notification to federation server: ' . $ e ->getMessage (), ['exception ' => $ e ]);
242189 try {
@@ -256,6 +203,40 @@ public function isReady() {
256203 return $ this ->appManager ->isEnabledForUser ('cloud_federation_api ' );
257204 }
258205
206+ /**
207+ * @param string $cloudId
208+ * @param string $uri
209+ * @param string $payload
210+ *
211+ * @return IResponse
212+ * @throws OCMProviderException
213+ */
214+ private function postOcmPayload (string $ cloudId , string $ uri , string $ payload , ?IClient $ client = null ): IResponse {
215+ $ ocmProvider = $ this ->discoveryService ->discover ($ cloudId );
216+ $ uri = $ ocmProvider ->getEndPoint () . '/ ' . ltrim ($ uri , '/ ' );
217+ $ client = $ client ?? $ this ->httpClientService ->newClient ();
218+ return $ client ->post ($ uri , $ this ->prepareOcmPayload ($ uri , $ payload ));
219+ }
220+
221+ /**
222+ * @param string $uri
223+ * @param string $payload
224+ *
225+ * @return array
226+ */
227+ private function prepareOcmPayload (string $ uri , string $ payload ): array {
228+ $ payload = array_merge ($ this ->getDefaultRequestOptions (), ['body ' => $ payload ]);
229+ if (!$ this ->appConfig ->getValueBool ('core ' , OCMSignatoryManager::APPCONFIG_SIGN_DISABLED , lazy: true )) {
230+ $ signedPayload = $ this ->signatureManager ->signOutgoingRequestIClientPayload (
231+ $ this ->signatoryManager ,
232+ $ payload ,
233+ 'post ' , $ uri
234+ );
235+ }
236+
237+ return $ signedPayload ?? $ payload ;
238+ }
239+
259240 private function getDefaultRequestOptions (): array {
260241 return [
261242 'headers ' => ['content-type ' => 'application/json ' ],
0 commit comments