Example usage for java.sql ResultSet getBinaryStream

List of usage examples for java.sql ResultSet getBinaryStream

Introduction

In this page you can find the example usage for java.sql ResultSet getBinaryStream.

Prototype

java.io.InputStream getBinaryStream(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a stream of uninterpreted bytes.

Usage

From source file:org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.java

/**
 * Retrieves {@link SubscriptionPolicy} with name <code>policyName</code> and tenant Id <code>tenantNId</code>
 *
 * @param policyName name of the policy to retrieve from the database
 * @param tenantId   tenantId of the policy
 * @return {@link SubscriptionPolicy}/* w  w  w  . j a  v a 2 s. co  m*/
 * @throws APIManagementException
 */
public SubscriptionPolicy getSubscriptionPolicy(String policyName, int tenantId) throws APIManagementException {
    SubscriptionPolicy policy = null;
    Connection connection = null;
    PreparedStatement selectStatement = null;
    ResultSet resultSet = null;

    String sqlQuery = SQLConstants.GET_SUBSCRIPTION_POLICY_SQL;
    if (forceCaseInsensitiveComparisons) {
        sqlQuery = SQLConstants.GET_SUBSCRIPTION_POLICY_SQL;
    }

    try {
        connection = APIMgtDBUtil.getConnection();
        selectStatement = connection.prepareStatement(sqlQuery);
        selectStatement.setString(1, policyName);
        selectStatement.setInt(2, tenantId);

        // Should return only single row
        resultSet = selectStatement.executeQuery();
        if (resultSet.next()) {
            policy = new SubscriptionPolicy(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
            setCommonPolicyDetails(policy, resultSet);
            policy.setRateLimitCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
            policy.setRateLimitTimeUnit(
                    resultSet.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
            policy.setStopOnQuotaReach(
                    resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
            policy.setBillingPlan(resultSet.getString(ThrottlePolicyConstants.COLUMN_BILLING_PLAN));
            InputStream binary = resultSet.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
            if (binary != null) {
                byte[] customAttrib = APIUtil.toByteArray(binary);
                policy.setCustomAttributes(customAttrib);
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get subscription policy: " + policyName + '-' + tenantId, e);
    } catch (IOException e) {
        handleException("Error while converting input stream to byte array", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(selectStatement, connection, resultSet);
    }
    return policy;
}

From source file:org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.java

public ArrayList<URITemplate> getAllURITemplatesAdvancedThrottle(String apiContext, String version)
        throws APIManagementException {
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    ArrayList<URITemplate> uriTemplates = new ArrayList<URITemplate>();

    // TODO : FILTER RESULTS ONLY FOR ACTIVE APIs
    String query = SQLConstants.ThrottleSQLConstants.GET_CONDITION_GROUPS_FOR_POLICIES_SQL;
    try {//from   w  ww  .j a v  a  2 s  .  c o m
        connection = APIMgtDBUtil.getConnection();
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, apiContext);
        prepStmt.setString(2, version);

        rs = prepStmt.executeQuery();
        Map<String, Set<ConditionGroupDTO>> mapByHttpVerbURLPatternToId = new HashMap<String, Set<ConditionGroupDTO>>();
        while (rs != null && rs.next()) {

            String httpVerb = rs.getString("HTTP_METHOD");
            String authType = rs.getString("AUTH_SCHEME");
            String urlPattern = rs.getString("URL_PATTERN");
            String policyName = rs.getString("THROTTLING_TIER");
            String conditionGroupId = rs.getString("CONDITION_GROUP_ID");
            String applicableLevel = rs.getString("APPLICABLE_LEVEL");
            String policyConditionGroupId = "_condition_" + conditionGroupId;

            String key = httpVerb + ":" + urlPattern;
            if (mapByHttpVerbURLPatternToId.containsKey(key)) {
                if (StringUtils.isEmpty(conditionGroupId)) {
                    continue;
                }

                // Converting ConditionGroup to a lightweight ConditionGroupDTO.
                ConditionGroupDTO groupDTO = createConditionGroupDTO(Integer.parseInt(conditionGroupId));
                groupDTO.setConditionGroupId(policyConditionGroupId);
                //               mapByHttpVerbURLPatternToId.get(key).add(policyConditionGroupId);
                mapByHttpVerbURLPatternToId.get(key).add(groupDTO);

            } else {
                String script = null;
                URITemplate uriTemplate = new URITemplate();
                uriTemplate.setThrottlingTier(policyName);
                uriTemplate.setAuthType(authType);
                uriTemplate.setHTTPVerb(httpVerb);
                uriTemplate.setUriTemplate(urlPattern);
                uriTemplate.setApplicableLevel(applicableLevel);
                InputStream mediationScriptBlob = rs.getBinaryStream("MEDIATION_SCRIPT");
                if (mediationScriptBlob != null) {
                    script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
                }

                uriTemplate.setMediationScript(script);
                Set<ConditionGroupDTO> conditionGroupIdSet = new HashSet<ConditionGroupDTO>();
                mapByHttpVerbURLPatternToId.put(key, conditionGroupIdSet);
                uriTemplates.add(uriTemplate);
                if (StringUtils.isEmpty(conditionGroupId)) {
                    continue;
                }
                ConditionGroupDTO groupDTO = createConditionGroupDTO(Integer.parseInt(conditionGroupId));
                groupDTO.setConditionGroupId(policyConditionGroupId);
                conditionGroupIdSet.add(groupDTO);

            }

        }

        for (URITemplate uriTemplate : uriTemplates) {
            String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
            if (mapByHttpVerbURLPatternToId.containsKey(key)) {
                if (!mapByHttpVerbURLPatternToId.get(key).isEmpty()) {
                    Set<ConditionGroupDTO> conditionGroupDTOs = mapByHttpVerbURLPatternToId.get(key);
                    ConditionGroupDTO defaultGroup = new ConditionGroupDTO();
                    defaultGroup.setConditionGroupId(APIConstants.THROTTLE_POLICY_DEFAULT);
                    conditionGroupDTOs.add(defaultGroup);
                    //                  uriTemplate.getThrottlingConditions().addAll(mapByHttpVerbURLPatternToId.get(key));
                    uriTemplate.getThrottlingConditions().add(APIConstants.THROTTLE_POLICY_DEFAULT);
                    uriTemplate.setConditionGroups(conditionGroupDTOs.toArray(new ConditionGroupDTO[] {}));
                }

            }

            if (uriTemplate.getThrottlingConditions().isEmpty()) {
                uriTemplate.getThrottlingConditions().add(APIConstants.THROTTLE_POLICY_DEFAULT);
                ConditionGroupDTO defaultGroup = new ConditionGroupDTO();
                defaultGroup.setConditionGroupId(APIConstants.THROTTLE_POLICY_DEFAULT);
                uriTemplate.setConditionGroups(new ConditionGroupDTO[] { defaultGroup });
            }

        }
    } catch (SQLException e) {
        handleException("Error while fetching all URL Templates", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
    }
    return uriTemplates;
}