All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sdnwiselab.sdnwise.adapter.AdapterRest Maven / Gradle / Ivy

/* 
 * Copyright (C) 2015 SDN-WISE
 *
 * 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 .
 */
package com.github.sdnwiselab.sdnwise.adapter;

import com.github.sdnwiselab.sdnwise.packet.NetworkPacket;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import java.util.Map;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;
import javax.ws.rs.ext.Provider;

/**
 * The adapter class for the communication with a REST WS. Configuration data
 * are passed using a Map which contains all the options needed
 * in the constructor of the class.
 *
 * @author Sebastiano Milardo
 * @version 0.1
 */
public class AdapterRest extends Adapter {

    /**
     * Creates an AdapterRest object. The conf map is used to pass the
     * configuration settings.
     *
     * @param conf contains the configuration data.
     */
    public AdapterRest(Map conf) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * Opens this adapter.
     *
     * @return a boolean indicating the correct completition of the operation
     */
    @Override
    public boolean open() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * Closes this adapter.
     *
     * @return a boolean indicating the correct completition of the operation
     */
    @Override
    public boolean close() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * Sends a byte array using this adapter.
     *
     * @param data the array to be sent
     */
    @Override
    public void send(byte[] data) {
        try {
            ClientConfig config = new DefaultClientConfig();
            Client client = Client.create(config);
            WebResource service = client.resource("http://localhost:9735/WS/REST/").path("ricevi").path(URLEncoder.encode(Arrays.toString(data), "UTF-8"));
            service.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString();
        } catch (UnsupportedEncodingException e) {
            log(Level.SEVERE, e.toString());
        }
    }
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyResource {
}

@Path("/")
class REST {

    @MyResource
    ArrayBlockingQueue monitor;

    @GET
    @Path("/ricevi/{a}")
    @Produces(MediaType.TEXT_PLAIN)

    public Response ricevi(@PathParam("a") String a) {
        try {
            System.out.println(URLDecoder.decode(a, "UTF-8"));
            //monitor.add(new NetworkPacket());
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(REST.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Response.status(200).entity("Ho ricevuto: " + a).build();
    }
}

@Provider
class RicevitoreREST extends
        PerRequestTypeInjectableProvider> {

    static ArrayBlockingQueue bQ;

    public RicevitoreREST(Type t) {
        super(t);
    }

    public void configura(ArrayBlockingQueue bQ) {

        this.bQ = bQ;

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                ricevi();
            }
        });
        t.start();
    }

    public void ricevi() {
        try {
            HttpServer server = HttpServerFactory.create("http://localhost:9735/WS/REST/");
            server.start();
        } catch (IOException | IllegalArgumentException ex) {
            Logger.getLogger(RicevitoreREST.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Injectable> getInjectable(
            ComponentContext ic,
            MyResource a) {
        return new Injectable>() {
            @Override
            public ArrayBlockingQueue getValue() {
                return bQ;
            }
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy