Skip to content

Commit

Permalink
Merge pull request #6270 from malithie/rule-mgt-updates
Browse files Browse the repository at this point in the history
Read rule from db in a binary stream.
  • Loading branch information
malithie authored Jan 7, 2025
2 parents 4560523 + b604d5f commit c4f5431
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import org.wso2.carbon.identity.rule.management.constant.RuleSQLConstants;
import org.wso2.carbon.identity.rule.management.dao.RuleManagementDAO;
import org.wso2.carbon.identity.rule.management.exception.RuleManagementException;
import org.wso2.carbon.identity.rule.management.exception.RuleManagementRuntimeException;
import org.wso2.carbon.identity.rule.management.exception.RuleManagementServerException;
import org.wso2.carbon.identity.rule.management.model.ANDCombinedRule;
import org.wso2.carbon.identity.rule.management.model.Expression;
import org.wso2.carbon.identity.rule.management.model.ORCombinedRule;
import org.wso2.carbon.identity.rule.management.model.Rule;
import org.wso2.carbon.identity.rule.management.model.Value;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
Expand Down Expand Up @@ -136,7 +139,8 @@ public Rule getRuleByRuleId(String ruleId, int tenantId) throws RuleManagementEx
jdbcTemplate.withTransaction(
template -> template.fetchSingleRecord(RuleSQLConstants.Query.GET_RULE_BY_ID,
(resultSet, rowNumber) -> {
ruleData.setRuleJson(resultSet.getString(RuleSQLConstants.Column.RULE_CONTENT));
ruleData.setRuleJson(getStringValueFromInputStream(
resultSet.getBinaryStream(RuleSQLConstants.Column.RULE_CONTENT)));
ruleData.setActive(resultSet.getBoolean(RuleSQLConstants.Column.IS_ACTIVE));
return null;
},
Expand Down Expand Up @@ -322,4 +326,23 @@ private ORCombinedRule convertJsonToRule(String ruleJson) throws RuleManagementS
throw new RuleManagementServerException("Failed to convert JSON to rule.", e);
}
}

private String getStringValueFromInputStream(InputStream stream) {

if (stream == null) {
return null;
}

StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
throw new RuleManagementRuntimeException("Error while reading the rule content from storage.", e);
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.rule.management.exception;

/**
* Runtime exception class for Rule Management.
*/
public class RuleManagementRuntimeException extends RuntimeException {

public RuleManagementRuntimeException(String message, Throwable cause) {

super(message, cause);
}
}

0 comments on commit c4f5431

Please sign in to comment.