-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move deserialize logic to ServerActionManagementService
- Loading branch information
1 parent
1bea095
commit b34a578
Showing
6 changed files
with
157 additions
and
70 deletions.
There are no files selected for viewing
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
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
130 changes: 130 additions & 0 deletions
130
...ava/org/wso2/carbon/identity/api/server/action/management/v1/util/ActionDeserializer.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,130 @@ | ||
/* | ||
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.wso2.carbon.identity.api.server.action.management.v1.util; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.wso2.carbon.identity.action.management.model.Action; | ||
import org.wso2.carbon.identity.api.server.action.management.v1.ActionModel; | ||
import org.wso2.carbon.identity.api.server.action.management.v1.ActionUpdateModel; | ||
import org.wso2.carbon.identity.api.server.action.management.v1.PreUpdatePasswordActionModel; | ||
import org.wso2.carbon.identity.api.server.action.management.v1.PreUpdatePasswordActionUpdateModel; | ||
import org.wso2.carbon.identity.api.server.action.management.v1.constants.ActionMgtEndpointConstants; | ||
|
||
import java.util.Set; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.ConstraintViolationException; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.ValidatorFactory; | ||
import javax.ws.rs.core.Response; | ||
|
||
/** | ||
* Utility class for action deserialization. | ||
*/ | ||
public class ActionDeserializer { | ||
|
||
/** | ||
* Deserialize the action model. | ||
* | ||
* @param actionType Action type. | ||
* @param jsonBody JSON body. | ||
* @return Action model. | ||
*/ | ||
public static ActionModel deserializeActionModel(Action.ActionTypes actionType, String jsonBody) { | ||
|
||
ActionModel actionModel = null; | ||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
switch (actionType) { | ||
case PRE_ISSUE_ACCESS_TOKEN: | ||
actionModel = objectMapper.readValue(jsonBody, ActionModel.class); | ||
// Validate the object | ||
validateActionModel(actionModel, ActionModel.class); | ||
break; | ||
case PRE_UPDATE_PASSWORD: | ||
actionModel = objectMapper.readValue(jsonBody, PreUpdatePasswordActionModel.class); | ||
// Validate the object | ||
validateActionModel((PreUpdatePasswordActionModel) actionModel, PreUpdatePasswordActionModel.class); | ||
break; | ||
default: | ||
break; | ||
} | ||
} catch (JsonProcessingException e) { | ||
throw ActionMgtEndpointUtil.handleException(Response.Status.BAD_REQUEST, | ||
ActionMgtEndpointConstants.ErrorMessage.ERROR_INVALID_PAYLOAD); | ||
} | ||
|
||
return actionModel; | ||
} | ||
|
||
/** | ||
* Deserialize the action update model. | ||
* | ||
* @param actionType Action type. | ||
* @param jsonBody JSON body. | ||
* @return Action update model. | ||
*/ | ||
public static ActionUpdateModel deserializeActionUpdateModel(Action.ActionTypes actionType, String jsonBody) { | ||
|
||
ActionUpdateModel actionUpdateModel = null; | ||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
switch (actionType) { | ||
case PRE_ISSUE_ACCESS_TOKEN: | ||
actionUpdateModel = objectMapper.readValue(jsonBody, ActionUpdateModel.class); | ||
// Validate the object | ||
validateActionModel(actionUpdateModel, ActionUpdateModel.class); | ||
break; | ||
case PRE_UPDATE_PASSWORD: | ||
actionUpdateModel = objectMapper.readValue(jsonBody, PreUpdatePasswordActionUpdateModel.class); | ||
// Validate the object | ||
validateActionModel((PreUpdatePasswordActionUpdateModel) actionUpdateModel, | ||
PreUpdatePasswordActionUpdateModel.class); | ||
break; | ||
default: | ||
break; | ||
} | ||
} catch (JsonProcessingException e) { | ||
throw ActionMgtEndpointUtil.handleException(Response.Status.BAD_REQUEST, | ||
ActionMgtEndpointConstants.ErrorMessage.ERROR_INVALID_PAYLOAD); | ||
} | ||
|
||
return actionUpdateModel; | ||
} | ||
|
||
/** | ||
* Validate the action model. | ||
* | ||
* @param actionModel Action model to be validated. | ||
* @param modelClass Class of the action model. | ||
* @param <T> Type of the action model. | ||
*/ | ||
private static <T> void validateActionModel(T actionModel, Class<T> modelClass) { | ||
|
||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); | ||
Validator validator = factory.getValidator(); | ||
Set<ConstraintViolation<T>> violations = validator.validate(actionModel); | ||
|
||
if (!violations.isEmpty()) { | ||
throw new ConstraintViolationException(violations); | ||
} | ||
} | ||
} |