Package com.groupon.odo.proxylib.models

Examples of com.groupon.odo.proxylib.models.Client


     */
    public List<ServerRedirect> tableServers(int clientId) {
        List<ServerRedirect> servers = new ArrayList<ServerRedirect>();

        try {
            Client client = ClientService.getInstance().getClient(clientId);
            servers = tableServers(client.getProfile().getId(), client.getActiveServerGroup());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
View Full Code Here


    public int addServerRedirectToProfile(String region, String srcUrl, String destUrl, String hostHeader,
                                          int profileId, int clientId) throws Exception {
        int serverId = -1;

        try {
            Client client = ClientService.getInstance().getClient(clientId);
            serverId = addServerRedirect(region, srcUrl, destUrl, hostHeader, profileId, client.getActiveServerGroup());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return serverId;
View Full Code Here

        // make sure client with this name does not already exist
        if (null != clientService.findClientFromFriendlyName(profileId, friendlyName)) {
          throw new Exception("Cannot add client. Friendly name already in use.");
        }
       
        Client client = clientService.add(profileId);

        // set friendly name if it was specified
        if (friendlyName != null) {
            clientService.setFriendlyName(profileId, client.getUUID(), friendlyName);
            client.setFriendlyName(friendlyName);
        }

        HashMap<String, Object> valueHash = new HashMap<String, Object>();
        valueHash.put("client", client);
        return valueHash;
View Full Code Here

    @RequestMapping(value = "edit/{profileIdentifier}", method = RequestMethod.GET)
    public String edit2Request(Model model, @PathVariable String profileIdentifier,
                               @RequestParam(defaultValue = Constants.PROFILE_CLIENT_DEFAULT_ID) String clientUUID) throws Exception {
        Integer profileId = ControllerUtils.convertProfileIdentifier(profileIdentifier);
        Client client = ClientService.getInstance().findClient(clientUUID, profileId);

        // check to see if client is null
        if (client == null) {
            // get the default client instead
            client = ClientService.getInstance().findClient(Constants.PROFILE_CLIENT_DEFAULT_ID, profileId);
        }

        model.addAttribute("clientUUID", client.getUUID());
        model.addAttribute("clientFriendlyName", client.getFriendlyName());
        model.addAttribute("profile_id", profileId);
        model.addAttribute("isActive", client.getIsActive());
        model.addAttribute("profile_name", ProfileService.getInstance().getNamefromId(profileId));
        model.addAttribute("default_content_type", Constants.PATH_PROFILE_DEFAULT_CONTENT_TYPE);
        return "edit";
    }
View Full Code Here

     * Tests to make sure canHandleRequest works for a non default client
     */
    @Test
    public void nonDefaultClientActive() throws Exception {
        // create a client
        Client client = ClientService.getInstance().add(newProfile.getId());
        assertFalse(ServerRedirectService.getInstance().canHandleRequest("api.groupon.com"));
        ClientService.getInstance().updateActive(newProfile.getId(), client.getUUID(), true);
        assertTrue(ServerRedirectService.getInstance().canHandleRequest("api.groupon.com"));
    }
View Full Code Here

        // figure out what profile to use based on source server name and matching paths
        // if no profile has matching paths then we just pick the first enabled one
        // if no profile is enabled then we pick the first one so that we have a URL mapping
        for (Profile tryProfile : ServerRedirectService.getInstance().getProfilesForServerName(origHostName)) {
            logger.info("Trying {}", tryProfile.getName());
            Client tryClient = ClientService.getInstance().findClient(history.getClientUUID(), tryProfile.getId());

            List<EndpointOverride> trySelectedRequestPaths = PathOverrideService.getInstance().getSelectedPaths(Constants.OVERRIDE_TYPE_REQUEST, tryClient,
                    tryProfile, httpServletRequest.getRequestURL() + queryString, requestType, false);
            List<EndpointOverride> trySelectedResponsePaths = PathOverrideService.getInstance().getSelectedPaths(Constants.OVERRIDE_TYPE_RESPONSE, tryClient,
                    tryProfile, httpServletRequest.getRequestURL() + queryString, requestType, false);
            logger.info("Sizes {} {}", trySelectedRequestPaths.size(), trySelectedResponsePaths.size());
            if ((trySelectedRequestPaths.size() > 0 || trySelectedResponsePaths.size() > 0) ||
                    tryClient.getIsActive() || requestInfo.profile == null) {
                logger.info("Selected {}, {}, " + httpServletRequest.getRequestURL() + "?" +
                        httpServletRequest.getQueryString(), tryProfile.getName(), tryClient.getId());
                // reset history UUID based on client
                history.setClientUUID(tryClient.getUUID());

                requestInfo.profile = tryProfile;
                requestInfo.selectedRequestPaths = new ArrayList<EndpointOverride>(trySelectedRequestPaths);
                requestInfo.selectedResponsePaths = new ArrayList<EndpointOverride>(trySelectedResponsePaths);
                requestInfo.client = tryClient;
View Full Code Here

     * @param clientId
     * @return
     * @throws Exception
     */
    public Client getClient(int clientId) throws Exception {
        Client client = null;

        PreparedStatement statement = null;
        ResultSet results = null;
        try (Connection sqlConnection = sqlService.getConnection()) {
            String queryString = "SELECT * FROM " + Constants.DB_TABLE_CLIENT +
View Full Code Here

     * @param profileId  - can be null, safer if it is not null
     * @return client object
     * @throws Exception
     */
    public Client findClient(String clientUUID, Integer profileId) throws Exception {
        Client client = null;

        // first see if the clientUUID is actually a uuid.. it might be a friendlyName and need conversion
        if (clientUUID.compareTo(Constants.PROFILE_CLIENT_DEFAULT_ID) != 0 &&
                !clientUUID.matches("[\\w]{8}-[\\w]{4}-[\\w]{4}-[\\w]{4}-[\\w]{12}")) {
            Client tmpClient = this.findClientFromFriendlyName(profileId, clientUUID);

            // if we can't find a client then fall back to the default ID
            if (tmpClient == null) {
                clientUUID = Constants.PROFILE_CLIENT_DEFAULT_ID;
            } else {
View Full Code Here

     * @param result
     * @return
     * @throws Exception
     */
    private Client getClientFromResultSet(ResultSet result) throws Exception {
        Client client = new Client();
        client.setId(result.getInt(Constants.GENERIC_ID));
        client.setUUID(result.getString(Constants.CLIENT_CLIENT_UUID));
        client.setFriendlyName(result.getString(Constants.CLIENT_FRIENDLY_NAME));
        client.setProfile(ProfileService.getInstance().findProfile(result.getInt(Constants.GENERIC_PROFILE_ID)));
        client.setIsActive(result.getBoolean(Constants.CLIENT_IS_ACTIVE));
        client.setActiveServerGroup(result.getInt(Constants.CLIENT_ACTIVESERVERGROUP));
        return client;
    }
View Full Code Here

     * @param profileId
     * @return
     * @throws Exception
     */
    public Client add(int profileId) throws Exception {
        Client client = null;
        ArrayList<Integer> pathsToCopy = new ArrayList<Integer>();
        String clientUUID = getUniqueClientUUID();

        // get profile for profileId
        Profile profile = ProfileService.getInstance().findProfile(profileId);
        PreparedStatement statement = null;
        ResultSet rs = null;

        try (Connection sqlConnection = sqlService.getConnection()) {
           
            // get the current count of clients
            statement = sqlConnection.prepareStatement("SELECT COUNT(" + Constants.GENERIC_ID + ") FROM " +
                                  Constants.DB_TABLE_CLIENT + " WHERE " + Constants.GENERIC_PROFILE_ID + "=?");
            statement.setInt(1, profileId);
            int clientCount = -1;
            rs = statement.executeQuery();
            if (rs.next()) {
              clientCount = rs.getInt(1);
            }
            statement.close();
            rs.close();

            // check count
            if (clientCount == -1) {
              throw new Exception("Error querying clients for profileId=" + profileId);
            }
            if (clientCount >= Constants.CLIENT_CLIENTS_PER_PROFILE_LIMIT) {
              throw new Exception("Profile(" + profileId + ") already contains 50 clients.  Please remove clients before adding new ones.");
            }
           
            statement = sqlConnection.prepareStatement(
                    "INSERT INTO " + Constants.DB_TABLE_CLIENT +
                            " (" + Constants.CLIENT_CLIENT_UUID + ", " +
                            Constants.CLIENT_IS_ACTIVE + ", " +
                            Constants.CLIENT_PROFILE_ID + ")" +
                            " VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS
            );
            statement.setString(1, clientUUID);
            statement.setBoolean(2, false);
            statement.setInt(3, profile.getId());
            statement.executeUpdate();
            rs = statement.getGeneratedKeys();
            int clientId = -1;
            if (rs.next()) {
                clientId = rs.getInt(1);
            } else {
                // something went wrong
                throw new Exception("Could not add client");
            }
            rs.close();
            statement.close();

            // adding entries into request response table for this new client for every path
            // basically a copy of what happens when a path gets created
            statement = sqlConnection.prepareStatement(
                    "SELECT * FROM " + Constants.DB_TABLE_REQUEST_RESPONSE +
                            " WHERE " + Constants.GENERIC_PROFILE_ID + " = ?" +
                            " AND " + Constants.GENERIC_CLIENT_UUID + " = ?"
            );
            statement.setInt(1, profile.getId());
            statement.setString(2, Constants.PROFILE_CLIENT_DEFAULT_ID);
            rs = statement.executeQuery();
            while (rs.next()) {
                // collect up the pathIds we need to copy
                pathsToCopy.add(rs.getInt(Constants.REQUEST_RESPONSE_PATH_ID));
            }
            client = new Client();
            client.setIsActive(false);
            client.setUUID(clientUUID);
            client.setId(clientId);
            client.setProfile(profile);
        } catch (SQLException e) {
            throw e;
        } finally {
            try {
                if (rs != null) rs.close();
            } catch (Exception e) {
            }
            try {
                if (statement != null) statement.close();
            } catch (Exception e) {
            }
        }

        // add all of the request response items
        for (Integer pathId : pathsToCopy) {
            PathOverrideService.getInstance().addPathToRequestResponseTable(profile.getId(), client.getUUID(), pathId);
        }

        return client;
    }
View Full Code Here

TOP

Related Classes of com.groupon.odo.proxylib.models.Client

Copyright © 2018 www.massapicom. 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.