Skip to content

Commit

Permalink
Provide the support to work with common ai component
Browse files Browse the repository at this point in the history
  • Loading branch information
sahandilshan committed Jan 17, 2025
1 parent b5bab1d commit 85c3582
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<artifactId>org.wso2.carbon.identity.application.common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.ai.service.mgt</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.inbound.auth.oauth2</groupId>
<artifactId>org.wso2.carbon.identity.oauth</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.wso2.carbon.identity.ai.service.mgt.exceptions.AIClientException;
import org.wso2.carbon.identity.ai.service.mgt.exceptions.AIServerException;
import org.wso2.carbon.identity.api.server.application.management.common.ApplicationManagementServiceHolder;
import org.wso2.carbon.identity.api.server.application.management.v1.LoginFlowGenerateRequest;
import org.wso2.carbon.identity.api.server.application.management.v1.LoginFlowGenerateResponse;
Expand All @@ -30,8 +32,6 @@
import org.wso2.carbon.identity.api.server.application.management.v1.StatusEnum;
import org.wso2.carbon.identity.api.server.common.error.APIError;
import org.wso2.carbon.identity.api.server.common.error.ErrorResponse;
import org.wso2.carbon.identity.application.mgt.ai.LoginFlowAIClientException;
import org.wso2.carbon.identity.application.mgt.ai.LoginFlowAIServerException;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -89,9 +89,9 @@ public LoginFlowGenerateResponse generateAuthenticationSequence(LoginFlowGenerat
LoginFlowGenerateResponse response = new LoginFlowGenerateResponse();
response.setOperationId(operationId);
return response;
} catch (LoginFlowAIServerException e) {
} catch (AIServerException e) {
throw handleServerException(e);
} catch (LoginFlowAIClientException e) {
} catch (AIClientException e) {
throw handleClientException(e);
}
}
Expand All @@ -111,15 +111,15 @@ public LoginFlowResultResponse getLoginFlowAIGenerationResult(String operationId
Map<String, Object> generationResultMap = (Map<String, Object>) generationResult;
response.setStatus(getStatusFromResult(generationResultMap));
if (!((Map<?, ?>) generationResult).containsKey("data")) {
throw new LoginFlowAIServerException(ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT_STATUS.getMessage(),
throw new AIServerException(ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT_STATUS.getMessage(),
ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT_STATUS.getCode());
}
Map<String, Object> dataMap = (Map<String, Object>) generationResultMap.get("data");
response.setData(dataMap);
return response;
} catch (LoginFlowAIServerException e) {
} catch (AIServerException e) {
throw handleServerException(e);
} catch (LoginFlowAIClientException e) {
} catch (AIClientException e) {
throw handleClientException(e);
}
}
Expand All @@ -140,15 +140,15 @@ public LoginFlowStatusResponse getLoginFlowAIStatus(String operationId) {

response.status(convertObjectToMap(generationStatus));
return response;
} catch (LoginFlowAIServerException e) {
} catch (AIServerException e) {
throw handleServerException(e);
} catch (LoginFlowAIClientException e) {
} catch (AIClientException e) {
throw handleClientException(e);
}
}

private StatusEnum getStatusFromResult(Map<String, Object> resultMap)
throws LoginFlowAIServerException {
throws AIServerException {

if (resultMap.containsKey("status")) {
String status = (String) resultMap.get("status");
Expand All @@ -160,31 +160,31 @@ private StatusEnum getStatusFromResult(Map<String, Object> resultMap)
return StatusEnum.FAILED;
}
}
throw new LoginFlowAIServerException(ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT.getMessage(),
throw new AIServerException(ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT.getMessage(),
ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT.getCode());
}

private APIError handleClientException(LoginFlowAIClientException error) {
private APIError handleClientException(AIClientException error) {

ErrorResponse.Builder errorResponseBuilder = new ErrorResponse.Builder()
.withCode(error.getErrorCode())
.withMessage(error.getMessage());
if (error.getLoginFlowAIResponse() != null) {
Response.Status status = Response.Status.fromStatusCode(error.getLoginFlowAIResponse().getStatusCode());
errorResponseBuilder.withDescription(error.getLoginFlowAIResponse().getResponseBody());
if (error.getServerMessage() != null) {
Response.Status status = Response.Status.fromStatusCode(error.getServerStatusCode());
errorResponseBuilder.withDescription(error.getServerMessage());
return new APIError(status, errorResponseBuilder.build());
}
return new APIError(Response.Status.BAD_REQUEST, errorResponseBuilder.build());
}

private APIError handleServerException(LoginFlowAIServerException error) {
private APIError handleServerException(AIServerException error) {

ErrorResponse.Builder errorResponseBuilder = new ErrorResponse.Builder()
.withCode(error.getErrorCode())
.withMessage(error.getMessage());
if (error.getBrandingAIResponse() != null) {
Response.Status status = Response.Status.fromStatusCode(error.getBrandingAIResponse().getStatusCode());
errorResponseBuilder.withDescription(error.getBrandingAIResponse().getResponseBody());
if (error.getServerMessage() != null) {
Response.Status status = Response.Status.fromStatusCode(error.getServerStatusCode());
errorResponseBuilder.withDescription(error.getServerMessage());
return new APIError(status, errorResponseBuilder.build());
}
return new APIError(Response.Status.INTERNAL_SERVER_ERROR, errorResponseBuilder.build());
Expand Down Expand Up @@ -232,4 +232,3 @@ private static Object[] convertListToArray(List<?> list) {
return array;
}
}

6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@
<version>${carbon.identity.framework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.ai.service.mgt</artifactId>
<version>${carbon.identity.framework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.admin.advisory.mgt</artifactId>
Expand Down

0 comments on commit 85c3582

Please sign in to comment.