Package com.groupon.odo.proxylib

Source Code of com.groupon.odo.proxylib.OverrideService

/*
Copyright 2014 Groupon, Inc.

Licensed 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 com.groupon.odo.proxylib;

import com.groupon.odo.proxylib.models.EnabledEndpoint;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OverrideService {
    static final Logger logger = LoggerFactory
            .getLogger(OverrideService.class);
    private static OverrideService serviceInstance = null;
    static SQLService sqlService = null;

    public OverrideService() {
    }

    public static OverrideService getInstance() throws Exception {
        if (serviceInstance == null) {
            sqlService = SQLService.getInstance();
            serviceInstance = new OverrideService();
        }
        return serviceInstance;
    }

    /**
     * Enable specific override ID for a path
     *
     * @param overrideId
     * @param pathId
     */
    public void enableOverride(int overrideId, int pathId, String clientUUID) throws Exception {
        // get profileId from pathId
        int profileId = PathOverrideService.getInstance().getPath(pathId).getProfileId();
        int newPriority = 0;

        // we want to limit -1, -2 to only be added once since they are the Custom responses/requests
        if (overrideId == Constants.PLUGIN_REQUEST_OVERRIDE_CUSTOM) {
            if (this.getEnabledEndpoint(pathId, overrideId, null, clientUUID) != null) {
                return;
            }
        }

        // need to first determine the highest enabled order value for this path
        HashMap<String, Object> priorities = sqlService.getFirstResult(
                "SELECT * FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                        " WHERE " + Constants.REQUEST_RESPONSE_PATH_ID + "=" + pathId +
                        " AND " + Constants.GENERIC_CLIENT_UUID + "='" + clientUUID +
                        "' ORDER BY + " + Constants.ENABLED_OVERRIDES_PRIORITY + " DESC"
        );
        if (priorities != null) {
            newPriority = Integer.valueOf(priorities.get(Constants.ENABLED_OVERRIDES_PRIORITY.toUpperCase()).toString()) + 1;
        }

        PreparedStatement statement = null;
        try (Connection sqlConnection = sqlService.getConnection()) {

            statement = sqlConnection.prepareStatement(
                    "INSERT INTO " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                            "(" + Constants.GENERIC_PROFILE_ID + "," + Constants.GENERIC_CLIENT_UUID + "," +
                            Constants.REQUEST_RESPONSE_PATH_ID + "," + Constants.ENABLED_OVERRIDES_OVERRIDE_ID + "," +
                            Constants.ENABLED_OVERRIDES_PRIORITY + "," + Constants.ENABLED_OVERRIDES_ARGUMENTS + ")" +
                            " VALUES (?, ?, ?, ?, ?, ?);"
            );
            statement.setInt(1, profileId);
            statement.setString(2, clientUUID);
            statement.setInt(3, pathId);
            statement.setInt(4, overrideId);
            statement.setInt(5, newPriority);
            statement.setString(6, "");
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Update the arguments for a given enabled override
     *
     * @param overrideId - override ID to update
     * @param pathId     - path ID to update
     * @param ordinal    - can be null, Index of the enabled override to edit if multiple of the same are enabled
     * @param arguments  - Object array of arguments
     * @param clientUUID - clientUUID
     */
    public void updateArguments(int overrideId, int pathId, Integer ordinal, String arguments, String clientUUID) {
        if (ordinal == null)
            ordinal = 1;

        PreparedStatement statement = null;

        try (Connection sqlConnection = sqlService.getConnection()) {
            // get ID of the ordinal
            int enabledId = getEnabledEndpoint(pathId, overrideId, ordinal, clientUUID).getId();

            String queryString = "UPDATE " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                    " SET " + Constants.ENABLED_OVERRIDES_ARGUMENTS + " = ? " +
                    " WHERE " + Constants.GENERIC_ID + " = ?";

            statement = sqlConnection.prepareStatement(queryString);
            statement.setString(1, arguments);
            statement.setInt(2, enabledId);
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Update the repeat number for a given enabled override
     *
     * @param overrideId   - override ID to update
     * @param pathId       - path ID to update
     * @param ordinal      - can be null, Index of the enabled override to edit if multiple of the same are enabled
     * @param repeatNumber - number of times to repeat
     * @param clientUUID   - clientUUID
     */
    public void updateRepeatNumber(int overrideId, int pathId, Integer ordinal, Integer repeatNumber, String clientUUID) {
        if (ordinal == null)
            ordinal = 1;

        try {
            // get ID of the ordinal
            int enabledId = getEnabledEndpoint(pathId, overrideId, ordinal, clientUUID).getId();
            updateRepeatNumber(enabledId, repeatNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Update the repeat number for a given enabled override
     *
     * @param id - enabled override ID to update
     */
    public void updateRepeatNumber(int id, Integer repeatNumber) {
        PreparedStatement statement = null;
        try (Connection sqlConnection = sqlService.getConnection()) {

            String queryString = "UPDATE " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                    " SET " + Constants.ENABLED_OVERRIDES_REPEAT_NUMBER + "= ? " +
                    " WHERE " + Constants.GENERIC_ID + " = ?";
            statement = sqlConnection.prepareStatement(queryString);
            statement.setInt(1, repeatNumber);
            statement.setInt(2, id);
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Remove specified override id from enaled overrides for path
     *
     * @param overrideId
     * @param pathId
     * @param ordinal    index to the instance of the enabled override
     */
    public void removeOverride(int overrideId, int pathId, Integer ordinal, String clientUUID) {
        // TODO: reorder priorities after removal
        PreparedStatement statement = null;
        try (Connection sqlConnection = sqlService.getConnection()) {
            int enabledId = getEnabledEndpoint(pathId, overrideId, ordinal, clientUUID).getId();
            statement = sqlConnection.prepareStatement(
                    "DELETE FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                            " WHERE " + Constants.GENERIC_ID + " = ?"
            );
            statement.setInt(1, enabledId);
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Increase the priority of an overrideId
     *
     * @param overrideId
     * @param pathId
     */
    public void increasePriority(int overrideId, int pathId, String clientUUID) {
        logger.info("Increase priority");

        int origPriority = -1;
        int newPriority = -1;
        PreparedStatement statement = null;
        ResultSet results = null;
        try (Connection sqlConnection = sqlService.getConnection()) {
            results = null;
            statement = sqlConnection.prepareStatement(
                    "SELECT * FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                            " WHERE " + Constants.ENABLED_OVERRIDES_PATH_ID + " = ?" +
                            " AND " + Constants.GENERIC_CLIENT_UUID + " = ?" +
                            " ORDER BY " + Constants.ENABLED_OVERRIDES_PRIORITY
            );
            statement.setInt(1, pathId);
            statement.setString(2, clientUUID);
            results = statement.executeQuery();
            while (results.next()) {
                if (results.getInt(Constants.ENABLED_OVERRIDES_OVERRIDE_ID) == overrideId) {
                    origPriority = results.getInt(Constants.ENABLED_OVERRIDES_PRIORITY);
                    break;
                } else {
                    newPriority = results.getInt(Constants.ENABLED_OVERRIDES_PRIORITY);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (results != null) results.close();
            } catch (Exception e) {
            }
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }

        try (Connection sqlConnection = sqlService.getConnection()) {
            // update priorities
            if (origPriority != -1 && newPriority != -1) {
                statement = sqlConnection.prepareStatement(
                        "UPDATE " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                                " SET " + Constants.ENABLED_OVERRIDES_PRIORITY + "=?" +
                                " WHERE " + Constants.ENABLED_OVERRIDES_PRIORITY + "=?" +
                                " AND " + Constants.ENABLED_OVERRIDES_PATH_ID + "=?" +
                                " AND " + Constants.GENERIC_CLIENT_UUID + "=?"
                );
                statement.setInt(1, origPriority);
                statement.setInt(2, newPriority);
                statement.setInt(3, pathId);
                statement.setString(4, clientUUID);
                statement.executeUpdate();
                statement.close();

                statement = sqlConnection.prepareStatement(
                        "UPDATE " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                                " SET " + Constants.ENABLED_OVERRIDES_PRIORITY + "=?" +
                                " WHERE " + Constants.ENABLED_OVERRIDES_OVERRIDE_ID + "=?" +
                                " AND " + Constants.ENABLED_OVERRIDES_PATH_ID + "=?" +
                                " AND " + Constants.GENERIC_CLIENT_UUID + "=?"
                );
                statement.setInt(1, newPriority);
                statement.setInt(2, overrideId);
                statement.setInt(3, pathId);
                statement.setString(4, clientUUID);
                statement.executeUpdate();
            }
        } catch (Exception e) {
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Decreases the priority of an overrideId
     *
     * @param overrideId
     * @param pathId
     */
    public void decreasePriority(int overrideId, int pathId, String clientUUID) {
        logger.info("Decrease priority");
        int origPriority = -1;
        int newPriority = -1;
        PreparedStatement queryStatement = null;
        ResultSet results = null;
        try (Connection sqlConnection = sqlService.getConnection()) {
            queryStatement = sqlConnection.prepareStatement(
                    "SELECT * FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                            " WHERE " + Constants.ENABLED_OVERRIDES_PATH_ID + " = ?" +
                            " AND " + Constants.GENERIC_CLIENT_UUID + " = ?" +
                            " ORDER BY " + Constants.ENABLED_OVERRIDES_PRIORITY
            );
            queryStatement.setInt(1, pathId);
            queryStatement.setString(2, clientUUID);
            results = queryStatement.executeQuery();
            boolean gotOrig = false;
            while (results.next()) {
                if (results.getInt(Constants.ENABLED_OVERRIDES_OVERRIDE_ID) == overrideId) {
                    origPriority = results.getInt(Constants.ENABLED_OVERRIDES_PRIORITY);
                    gotOrig = true;
                } else {
                    newPriority = results.getInt(Constants.ENABLED_OVERRIDES_PRIORITY);

                    // break out because this is the one after the one we want to move down
                    if (gotOrig)
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (results != null) results.close();
            } catch (Exception e) {
            }
            try {
                if (queryStatement != null) queryStatement.close();
            } catch (Exception e) {
            }
        }

        PreparedStatement statement = null;
        try (Connection sqlConnection = sqlService.getConnection()) {
            // update priorities
            if (origPriority != -1 && newPriority != -1) {
                statement = sqlConnection.prepareStatement(
                        "UPDATE " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                                " SET " + Constants.ENABLED_OVERRIDES_PRIORITY + "=?" +
                                " WHERE " + Constants.ENABLED_OVERRIDES_PRIORITY + "=?" +
                                " AND " + Constants.ENABLED_OVERRIDES_PATH_ID + "=?" +
                                " AND " + Constants.GENERIC_CLIENT_UUID + "=?"
                );
                statement.setInt(1, origPriority);
                statement.setInt(2, newPriority);
                statement.setInt(3, pathId);
                statement.setString(4, clientUUID);
                statement.executeUpdate();
                statement.close();

                statement = sqlConnection.prepareStatement(
                        "UPDATE " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                                " SET " + Constants.ENABLED_OVERRIDES_PRIORITY + "=?" +
                                " WHERE " + Constants.ENABLED_OVERRIDES_OVERRIDE_ID + "=?" +
                                " AND " + Constants.ENABLED_OVERRIDES_PATH_ID + "=?" +
                                " AND " + Constants.GENERIC_CLIENT_UUID + "=?"
                );
                statement.setInt(1, newPriority);
                statement.setInt(2, overrideId);
                statement.setInt(3, pathId);
                statement.setString(4, clientUUID);
                statement.executeUpdate();
            }
        } catch (Exception e) {
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Creates a list of placeholders for use in a PreparedStatement
     *
     * @param length number of placeholders
     * @return String of placeholders, seperated by comma
     */
    private static String preparePlaceHolders(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; ) {
            builder.append("?");
            if (++i < length) {
                builder.append(",");
            }
        }
        return builder.toString();
    }

    /**
     * Disable all overrides for a specified path
     *
     * @param pathID
     */
    public void disableAllOverrides(int pathID, String clientUUID) {
        PreparedStatement statement = null;
        try (Connection sqlConnection = sqlService.getConnection()) {
            statement = sqlConnection.prepareStatement(
                    "DELETE FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                            " WHERE " + Constants.ENABLED_OVERRIDES_PATH_ID + " = ? " +
                            " AND " + Constants.GENERIC_CLIENT_UUID + " = ? "
            );
            statement.setInt(1, pathID);
            statement.setString(2, clientUUID);
            statement.execute();
            statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Disable all overrides for a specified path with overrideType
     *
     * @param pathID
     */
    public void disableAllOverrides(int pathID, String clientUUID, int overrideType) {
        PreparedStatement statement = null;

        try (Connection sqlConnection = sqlService.getConnection()) {
            ArrayList<Integer> enabledOverrides = new ArrayList<Integer>();
            enabledOverrides.add(Constants.PLUGIN_REQUEST_HEADER_OVERRIDE_ADD);
            enabledOverrides.add(Constants.PLUGIN_REQUEST_HEADER_OVERRIDE_REMOVE);
            enabledOverrides.add(Constants.PLUGIN_REQUEST_OVERRIDE_CUSTOM);

            String overridePlaceholders = preparePlaceHolders(enabledOverrides.size());

            statement = sqlConnection.prepareStatement(
                    "DELETE FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                            " WHERE " + Constants.ENABLED_OVERRIDES_PATH_ID + " = ? " +
                            " AND " + Constants.GENERIC_CLIENT_UUID + " = ? " +
                            " AND " + Constants.ENABLED_OVERRIDES_OVERRIDE_ID +
                            (overrideType == Constants.OVERRIDE_TYPE_RESPONSE ? " NOT" : "") +
                            " IN ( " + overridePlaceholders + " )"
            );
            statement.setInt(1, pathID);
            statement.setString(2, clientUUID);
            for (int i = 3; i <= enabledOverrides.size() + 2; ++i) {
                statement.setInt(i, enabledOverrides.get(i - 3));
            }
            statement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * `
     * Returns an array of the enabled endpoints as Integer IDs
     *
     * @param pathId
     * @param filters TODO
     * @return
     */
    public List<EnabledEndpoint> getEnabledEndpoints(int pathId, String clientUUID, String[] filters) throws Exception {
        ArrayList<EnabledEndpoint> enabledOverrides = new ArrayList<EnabledEndpoint>();
        PreparedStatement query = null;
        ResultSet results = null;

        try (Connection sqlConnection = sqlService.getConnection()) {
            query = sqlConnection.prepareStatement(
                    "SELECT * FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                            " WHERE " + Constants.ENABLED_OVERRIDES_PATH_ID + "=?" +
                            " AND " + Constants.GENERIC_CLIENT_UUID + "=?" +
                            " ORDER BY " + Constants.ENABLED_OVERRIDES_PRIORITY
            );
            query.setInt(1, pathId);
            query.setString(2, clientUUID);
            results = query.executeQuery();

            while (results.next()) {
                EnabledEndpoint endpoint = this.getPartialEnabledEndpointFromResultset(results);
                com.groupon.odo.proxylib.models.Method m = PathOverrideService.getInstance().getMethodForOverrideId(endpoint.getOverrideId());

                // this is an errant entry.. perhaps a method got deleted from a plugin
                // we'll also remove it from the endpoint
                if (m == null) {
                    PathOverrideService.getInstance().removeOverride(endpoint.getOverrideId());
                    continue;
                }

                // check filters and see if any match
                boolean addOverride = false;
                if (filters != null) {
                    for (String filter : filters) {
                        if (m.getMethodType().endsWith(filter)) {
                            addOverride = true;
                            break;
                        }
                    }
                } else {
                    // if there are no filters then we assume that the requester wants all enabled overrides
                    addOverride = true;
                }

                if (addOverride)
                    enabledOverrides.add(endpoint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (results != null) results.close();
            } catch (Exception e) {
            }
            try {
                if (query != null) query.close();
            } catch (Exception e) {
            }
        }

        // now go through the ArrayList and get the method for all of the endpoints
        // have to do this so we don't have overlapping SQL queries
        ArrayList<EnabledEndpoint> enabledOverridesWithMethods = new ArrayList<EnabledEndpoint>();
        for (EnabledEndpoint endpoint : enabledOverrides) {
            if (endpoint.getOverrideId() >= 0) {
                com.groupon.odo.proxylib.models.Method m = PathOverrideService.getInstance().getMethodForOverrideId(endpoint.getOverrideId());
                endpoint.setMethodInformation(m);
            }
            enabledOverridesWithMethods.add(endpoint);
        }

        return enabledOverridesWithMethods;
    }

    /**
     * @param pathId
     * @param overrideId
     * @param ordinal    - Index of the enabled override to get if multiple of the same override are enabled(default is 1)
     * @param clientUUID
     * @return
     * @throws Exception
     */
    public EnabledEndpoint getEnabledEndpoint(int pathId, int overrideId, Integer ordinal, String clientUUID) throws Exception {
        EnabledEndpoint endpoint = null;
        PreparedStatement statement = null;
        ResultSet results = null;

        if (ordinal == null)
            ordinal = 1;

        // try to get it from the database
        try (Connection sqlConnection = sqlService.getConnection()) {
            // decrease ordinal by 1 so offset works right
            ordinal--;

            String queryString = "SELECT * FROM " + Constants.DB_TABLE_ENABLED_OVERRIDE +
                    " WHERE " + Constants.ENABLED_OVERRIDES_PATH_ID + "=? " +
                    " AND " + Constants.ENABLED_OVERRIDES_OVERRIDE_ID + "=? " +
                    " AND " + Constants.GENERIC_CLIENT_UUID + "=? " +
                    " LIMIT 1 OFFSET ?";
            statement = sqlConnection.prepareStatement(queryString);
            statement.setInt(1, pathId);
            statement.setInt(2, overrideId);
            statement.setString(3, clientUUID);
            statement.setInt(4, ordinal);

            results = statement.executeQuery();
            while (results.next()) {
                endpoint = this.getPartialEnabledEndpointFromResultset(results);
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (results != null) results.close();
            } catch (Exception e) {
            }
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }

        if (endpoint != null) {
            // get the method also for a real endpoint
            if (endpoint.getOverrideId() >= 0) {
                com.groupon.odo.proxylib.models.Method m = PathOverrideService.getInstance().getMethodForOverrideId(endpoint.getOverrideId());
                endpoint.setMethodInformation(m);
            } else if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_HEADER_OVERRIDE_ADD
                    || endpoint.getOverrideId() == Constants.PLUGIN_REQUEST_HEADER_OVERRIDE_ADD) {
                // set fake method info
                com.groupon.odo.proxylib.models.Method m = new com.groupon.odo.proxylib.models.Method();

                m.setMethodArgumentNames(new String[]{"key", "value"});
                m.setMethodArguments(new Object[]{String.class, String.class});
                m.setClassName("");
                m.setMethodName("CUSTOM HEADER");

                if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_HEADER_OVERRIDE_ADD) {
                    m.setDescription("Set a response header");
                } else if (endpoint.getOverrideId() == Constants.PLUGIN_REQUEST_HEADER_OVERRIDE_ADD) {
                    m.setDescription("Set a request header");
                }

                endpoint.setMethodInformation(m);
            } else if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_HEADER_OVERRIDE_REMOVE
                    || endpoint.getOverrideId() == Constants.PLUGIN_REQUEST_HEADER_OVERRIDE_REMOVE) {
                // set fake method info
                com.groupon.odo.proxylib.models.Method m = new com.groupon.odo.proxylib.models.Method();

                m.setMethodArgumentNames(new String[]{"key"});
                m.setMethodArguments(new Object[]{String.class});
                m.setClassName("");
                m.setMethodName("REMOVE HEADER");

                if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_HEADER_OVERRIDE_REMOVE) {
                    m.setDescription("Remove a response header");
                } else if (endpoint.getOverrideId() == Constants.PLUGIN_REQUEST_HEADER_OVERRIDE_REMOVE) {
                    m.setDescription("Remove a request header");
                }

                endpoint.setMethodInformation(m);
            } else if (endpoint.getOverrideId() == Constants.PLUGIN_REQUEST_OVERRIDE_CUSTOM
                    || endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_OVERRIDE_CUSTOM) {
                // set fake method info
                com.groupon.odo.proxylib.models.Method m = new com.groupon.odo.proxylib.models.Method();

                m.setMethodArgumentNames(new String[]{"response"});
                m.setMethodArguments(new Object[]{String.class});
                m.setClassName("");
                m.setMethodName("CUSTOM");

                if (endpoint.getOverrideId() == Constants.PLUGIN_REQUEST_OVERRIDE_CUSTOM) {
                    m.setDescription("Return a custom request");
                } else if (endpoint.getOverrideId() == Constants.PLUGIN_RESPONSE_OVERRIDE_CUSTOM) {
                    m.setDescription("Return a custom response");
                }

                endpoint.setMethodInformation(m);
            }
        }

        return endpoint;
    }

    /**
     * This only gets half of the EnabledEndpoint from a JDBC ResultSet
     * Getting the method for the override id requires an additional SQL query and needs to be called after
     * the SQL connection is released
     *
     * @param result
     * @return
     * @throws Exception
     */
    private EnabledEndpoint getPartialEnabledEndpointFromResultset(ResultSet result) throws Exception {
        EnabledEndpoint endpoint = new EnabledEndpoint();
        endpoint.setId(result.getInt(Constants.GENERIC_ID));
        endpoint.setPathId(result.getInt(Constants.ENABLED_OVERRIDES_PATH_ID));
        endpoint.setOverrideId(result.getInt(Constants.ENABLED_OVERRIDES_OVERRIDE_ID));
        endpoint.setPriority(result.getInt(Constants.ENABLED_OVERRIDES_PRIORITY));
        endpoint.setRepeatNumber(result.getInt(Constants.ENABLED_OVERRIDES_REPEAT_NUMBER));

        ArrayList<Object> args = new ArrayList<Object>();
        try {
            JSONArray arr = new JSONArray(result.getString(Constants.ENABLED_OVERRIDES_ARGUMENTS));
            for (int x = 0; x < arr.length(); x++) {
                args.add(arr.get(x));
            }
        } catch (Exception e) {
            // ignore it.. this means the entry was null/corrupt
        }

        endpoint.setArguments(args.toArray(new Object[0]));

        return endpoint;
    }

    /**
     * Gets an overrideID for a class name, method name
     *
     * @param className
     * @param methodName
     * @return
     */
    public Integer getOverrideIdForMethod(String className, String methodName) {
        Integer overrideId = null;
        PreparedStatement query = null;
        ResultSet results = null;

        try (Connection sqlConnection = sqlService.getConnection()) {
            query = sqlConnection.prepareStatement(
                    "SELECT * FROM " + Constants.DB_TABLE_OVERRIDE +
                            " WHERE " + Constants.OVERRIDE_CLASS_NAME + " = ?" +
                            " AND " + Constants.OVERRIDE_METHOD_NAME + " = ?"
            );
            query.setString(1, className);
            query.setString(2, methodName);
            results = query.executeQuery();

            if (results.next()) {
                overrideId = results.getInt(Constants.GENERIC_ID);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (results != null) results.close();
            } catch (Exception e) {
            }
            try {
                if (query != null) query.close();
            } catch (Exception e) {
            }
        }

        return overrideId;
    }
}
TOP

Related Classes of com.groupon.odo.proxylib.OverrideService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.