com.github.stephanarts.cas.ticket.registry.provider.DeleteMethod.java Source code

Java tutorial

Introduction

Here is the source code for com.github.stephanarts.cas.ticket.registry.provider.DeleteMethod.java

Source

/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.stephanarts.cas.ticket.registry.provider;

import java.util.HashMap;

import org.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.jasig.cas.ticket.Ticket;

import com.github.stephanarts.cas.ticket.registry.support.IMethod;
import com.github.stephanarts.cas.ticket.registry.support.JSONRPCException;

/**
 * DeleteMethod Class.
 */
public final class DeleteMethod implements IMethod {

    /**
     * Logging Class.
     */
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private HashMap<Integer, Ticket> map;

    /**
     * Constructor.
     *
     * @param map ticket-map.
     */
    public DeleteMethod(final HashMap<Integer, Ticket> map) {
        this.map = map;
    }

    /**
     * Execute the JSONRPCFunction.
     *
     * @param params    JSONRPC Method Parameters.
     *
     * @return          JSONRPC result object
     *
     * @throws JSONRPCException implementors can throw JSONRPCExceptions containing the error.
     */
    public JSONObject execute(final JSONObject params) throws JSONRPCException {

        JSONObject result = new JSONObject();

        String ticketId = null;

        if (params.length() != 1) {
            throw new JSONRPCException(-32602, "Invalid Params");
        }
        if (!(params.has("ticket-id"))) {
            throw new JSONRPCException(-32602, "Invalid Params");
        }

        ticketId = params.getString("ticket-id");

        logger.debug("Delete key {}", ticketId);

        if (!this.map.containsKey(ticketId.hashCode())) {
            logger.warn("Missing Key {}", ticketId);
            throw new JSONRPCException(-32503, "Missing Key");
        }

        this.map.remove(ticketId.hashCode());

        result.put("ticket-id", ticketId);
        result.put("status", "REMOVED");

        return result;
    }
}