-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from sinch/DEVEXP-215-SAVML-helpers-shared-by-…
- Loading branch information
Showing
127 changed files
with
5,357 additions
and
2,009 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...src/main/com/sinch/sdk/domains/common/adapters/converters/OffsetDateTimeDtoConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.sinch.sdk.domains.common.adapters.converters; | ||
|
||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
|
||
public class OffsetDateTimeDtoConverter { | ||
|
||
public static Instant convert(OffsetDateTime dto) { | ||
return null != dto ? dto.toInstant() : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
client/src/main/com/sinch/sdk/domains/voice/WebHooksService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.sinch.sdk.domains.voice; | ||
|
||
import com.sinch.sdk.core.exceptions.ApiMappingException; | ||
import com.sinch.sdk.domains.voice.models.svaml.SVAMLControl; | ||
import com.sinch.sdk.domains.voice.models.webhooks.WebhooksEvent; | ||
import java.util.Map; | ||
|
||
/** | ||
* Webhooks service | ||
* | ||
* @see <a | ||
* href="https://developers.sinch.com/docs/voice/api-reference/voice/tag/Callbacks">https://developers.sinch.com/docs/voice/api-reference/voice/tag/Callbacks/</a> | ||
* @since 1.0 | ||
*/ | ||
public interface WebHooksService { | ||
|
||
/** | ||
* The Sinch Platform can initiate callback requests to a URL you define (Callback URL) on request | ||
* and result events. All callback requests are signed using your Application key and secret pair | ||
* found on your dashboard. The signature is included in the Authorization header of the request | ||
* | ||
* <p>By using following function, you can ensure authentication according to received payload | ||
* from your backend | ||
* | ||
* @param method The HTTP method used ot handle the callback | ||
* @param path The path to you backend endpoint used for callback | ||
* @param headers Received headers | ||
* @param jsonPayload Received payload | ||
* @return Is authentication is validated (true) or not (false) | ||
* <p>see <a | ||
* href="https://developers.sinch.com/docs/voice/api-reference/authentication/callback-signed-request">https://developers.sinch.com/docs/voice/api-reference/authentication/callback-signed-request/</a> | ||
* @since 1.0 | ||
*/ | ||
boolean validateAuthenticatedRequest( | ||
String method, String path, Map<String, String> headers, String jsonPayload); | ||
|
||
/** | ||
* This function can be called to deserialize received payload onto callback onto proper java | ||
* Voice event class | ||
* | ||
* @param jsonPayload Received payload to be deserialized | ||
* @return The Voice event instance class | ||
* <p>see <a | ||
* href="https://developers.sinch.com/docs/voice/api-reference/voice/tag/Callbacks/">https://developers.sinch.com/docs/voice/api-reference/voice/tag/Callbacks/</a> | ||
* @since 1.0 | ||
*/ | ||
WebhooksEvent unserializeWebhooksEvent(String jsonPayload) throws ApiMappingException; | ||
|
||
/** | ||
* This function can be called to serialize a Voice response to be sent as JSON | ||
* | ||
* @param response The response to be serialized | ||
* @return The JSON string to be sent | ||
* <p>see <a | ||
* href="https://developers.sinch.com/docs/voice/api-reference/voice/tag/Callbacks/">https://developers.sinch.com/docs/voice/api-reference/voice/tag/Callbacks/</a> | ||
* @since 1.0 | ||
*/ | ||
String serializeWebhooksResponse(SVAMLControl response) throws ApiMappingException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
client/src/main/com/sinch/sdk/domains/voice/adapters/WebHooksService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.sinch.sdk.domains.voice.adapters; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.sinch.sdk.core.exceptions.ApiMappingException; | ||
import com.sinch.sdk.core.http.AuthManager; | ||
import com.sinch.sdk.core.utils.databind.Mapper; | ||
import com.sinch.sdk.domains.voice.adapters.converters.CallsDtoConverter; | ||
import com.sinch.sdk.domains.voice.adapters.converters.WebhooksEventDtoConverter; | ||
import com.sinch.sdk.domains.voice.models.dto.v1.SVAMLRequestBodyDto; | ||
import com.sinch.sdk.domains.voice.models.dto.v1.WebhooksEventDto; | ||
import com.sinch.sdk.domains.voice.models.svaml.SVAMLControl; | ||
import com.sinch.sdk.domains.voice.models.webhooks.WebhooksEvent; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.logging.Logger; | ||
|
||
public class WebHooksService implements com.sinch.sdk.domains.voice.WebHooksService { | ||
private static final Logger LOGGER = Logger.getLogger(WebHooksService.class.getName()); | ||
|
||
private final Map<String, AuthManager> authManagers; | ||
|
||
public WebHooksService(Map<String, AuthManager> authManagers) { | ||
this.authManagers = authManagers; | ||
} | ||
|
||
public boolean validateAuthenticatedRequest( | ||
String method, String path, Map<String, String> headers, String jsonPayload) { | ||
|
||
// convert header keys to use case-insensitive map keys | ||
Map<String, String> caseInsensitiveHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); | ||
caseInsensitiveHeaders.putAll(headers); | ||
|
||
String authorizationHeader = caseInsensitiveHeaders.get("Authorization"); | ||
|
||
// no authorization required | ||
if (null == authorizationHeader) { | ||
return true; | ||
} | ||
|
||
String[] split = authorizationHeader.split(" "); | ||
String authorizationKeyword = split.length > 0 ? split[0] : ""; | ||
|
||
AuthManager authManager = authManagers.get(authorizationKeyword); | ||
if (null == authManager) { | ||
// unknown auth manager | ||
LOGGER.severe( | ||
String.format("Auth manager for authorization '%s' not found", authorizationKeyword)); | ||
return false; | ||
} | ||
return authManager.validateAuthenticatedRequest(method, path, headers, jsonPayload); | ||
} | ||
|
||
@Override | ||
public WebhooksEvent unserializeWebhooksEvent(String jsonPayload) throws ApiMappingException { | ||
try { | ||
WebhooksEventDto o = Mapper.getInstance().readValue(jsonPayload, WebhooksEventDto.class); | ||
return WebhooksEventDtoConverter.convert(o); | ||
} catch (JsonProcessingException e) { | ||
throw new ApiMappingException(jsonPayload, e); | ||
} | ||
} | ||
|
||
@Override | ||
public String serializeWebhooksResponse(SVAMLControl response) throws ApiMappingException { | ||
SVAMLRequestBodyDto dto = CallsDtoConverter.convert(response); | ||
try { | ||
return Mapper.getInstance().writeValueAsString(dto); | ||
} catch (JsonProcessingException e) { | ||
throw new ApiMappingException(response.toString(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
client/src/main/com/sinch/sdk/domains/voice/adapters/converters/ControlDtoConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.sinch.sdk.domains.voice.adapters.converters; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.sinch.sdk.core.exceptions.ApiMappingException; | ||
import com.sinch.sdk.core.utils.databind.Mapper; | ||
import com.sinch.sdk.domains.voice.models.requests.Control; | ||
import com.sinch.sdk.domains.voice.models.requests.ControlUrl; | ||
import com.sinch.sdk.domains.voice.models.svaml.SVAMLControl; | ||
import java.util.logging.Logger; | ||
|
||
public class ControlDtoConverter { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(SVAMLActionDtoConverter.class.getName()); | ||
|
||
public static String convert(Control client) { | ||
if (null == client) { | ||
return null; | ||
} | ||
String dto; | ||
if (client instanceof SVAMLControl) { | ||
SVAMLControl value = (SVAMLControl) client; | ||
dto = convertControlToEscapedJSON(value); | ||
} else if (client instanceof ControlUrl) { | ||
ControlUrl value = (ControlUrl) client; | ||
dto = value.getURL(); | ||
} else { | ||
LOGGER.severe(String.format("Unexpected class '%s'", client.getClass())); | ||
dto = client.toString(); | ||
} | ||
return dto; | ||
} | ||
|
||
private static String convertControlToEscapedJSON(SVAMLControl client) { | ||
try { | ||
return Mapper.getInstance().writeValueAsString(CallsDtoConverter.convert(client)); | ||
} catch (JsonProcessingException e) { | ||
throw new ApiMappingException(client.toString(), e); | ||
} | ||
} | ||
} |
Oops, something went wrong.