com.conwet.silbops.connectors.comet.handlers.UnadvertiseResponse.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.connectors.comet.handlers.UnadvertiseResponse.java

Source

package com.conwet.silbops.connectors.comet.handlers;

/*
 * #%L
 * SilboPS Service
 * %%
 * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

import com.conwet.silbops.apibroker.EndPoint;
import com.conwet.silbops.broker.Broker;
import com.conwet.silbops.connectors.comet.ConnectionStatus;
import com.conwet.silbops.connectors.comet.HttpEndPoint;
import com.conwet.silbops.model.Advertise;
import com.conwet.silbops.msg.UnadvertiseMsg;

/**
 * This class handles the response for an unadvertise command
 * 
 * @author sergio
 */
public class UnadvertiseResponse extends AbstractResponse {

    public UnadvertiseResponse(Broker broker, String recMsg, ConnectionStatus connStatus) {

        super(broker, recMsg, connStatus);
    }

    private Advertise extract(HttpServletRequest request, String param) throws ParseException {

        String advertise = retriveParameter(request, param);
        JSONArray jsonAdvertise = (JSONArray) JSONValue.parseWithException(advertise);

        return Advertise.fromJSON(jsonAdvertise);
    }

    @Override
    protected void reply(HttpServletRequest req, HttpServletResponse res, PrintWriter writer,
            HttpEndPoint httpEndPoint, EndPoint connection) {

        try {
            UnadvertiseMsg msg = new UnadvertiseMsg();
            msg.setAdvertise(extract(req, "advertise"));
            msg.setContext(extract(req, "context"));
            logger.debug("Endpoint {}, UnadvertiseRequest: {}", httpEndPoint, msg);

            broker.receiveMessage(msg, connection);
            writeOut(writer, httpEndPoint.getEndPoint(), "unadvertise success");

        } catch (Exception ex) {
            logger.warn("exception on UnadvertiseRquest=" + ex.getMessage());
            try {
                res.sendError(HttpServletResponse.SC_BAD_REQUEST);
            } catch (IOException ex1) {
                logger.warn(ex1.getMessage());
            }
        }
    }

    @Override
    protected boolean supportPublisherOperations() {

        return true;
    }

    @Override
    protected boolean supportSubscriberOperations() {

        return false;
    }
}