com.automatski.autosim.rest.SampleRestConnection.java Source code

Java tutorial

Introduction

Here is the source code for com.automatski.autosim.rest.SampleRestConnection.java

Source

/*
 *  AutoSIM - Internet of Things Simulator
 *  Copyright (C) 2014, Aditya Yadav <aditya@automatski.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU 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 General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.automatski.autosim.rest;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.automatski.autosim.environments.IConnection;
import com.automatski.autosim.environments.IConnectionListener;
import com.automatski.autosim.rest.config.AutoSIMConnectionConfig;

public class SampleRestConnection implements IConnection {

    private AutoSIMConnectionConfig config = null;
    private IConnectionListener listener = null;

    @Override
    public void connect() throws Exception {
        //nothing to do
    }

    @Override
    public void disconnect() throws Exception {
        //nothing to do

    }

    @Override
    public Object send(Object arg0) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(config.url);

        StringEntity input = new StringEntity((String) arg0);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);
        response.getEntity().consumeContent();
        // return nothing. we are as of now not interested in the response
        return null;
    }

    @Override
    public void setConfig(Object arg0) {
        config = (AutoSIMConnectionConfig) arg0;

    }

    @Override
    public void setListener(IConnectionListener listener) {
        this.listener = listener;

    }

}