Package ch.ethz.inf.vs.californium.examples

Source Code of ch.ethz.inf.vs.californium.examples.ProxyHttpCoAPServer$TargetResource

/*******************************************************************************
* Copyright (c) 2014, Institute for Pervasive Computing, ETH Zurich.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Californium (Cf) CoAP framework.
******************************************************************************/
package ch.ethz.inf.vs.californium.examples;

import java.io.IOException;

import ch.ethz.inf.vs.californium.network.config.NetworkConfig;
import ch.ethz.inf.vs.californium.network.config.NetworkConfigDefaults;
import ch.ethz.inf.vs.californium.proxy.DirectProxyCoAPResolver;
import ch.ethz.inf.vs.californium.proxy.ProxyHttpServer;
import ch.ethz.inf.vs.californium.resources.proxy.ForwardingResource;
import ch.ethz.inf.vs.californium.resources.proxy.ProxyCoapClientResource;
import ch.ethz.inf.vs.californium.resources.proxy.ProxyHttpClientResource;
import ch.ethz.inf.vs.californium.server.Server;
import ch.ethz.inf.vs.californium.server.resources.CoapExchange;
import ch.ethz.inf.vs.californium.server.resources.ResourceBase;

/**
* Http2CoAP: Insert in browser:
*     URI: http://localhost:8080/proxy/coap://localhost:PORT/target
*
* CoAP2CoAP: Insert in Copper:
*     URI: coap://localhost:PORT/coap2coap
*     Proxy: coap://localhost:PORT/targetA
*
* CoAP2Http: Insert in Copper:
*     URI: coap://localhost:PORT/coap2http
*     Proxy: http://lantersoft.ch/robots.txt
*/
public class ProxyHttpCoAPServer {
 
  private static final int PORT = NetworkConfig.getStandard().getInt(NetworkConfigDefaults.DEFAULT_COAP_PORT);

  private Server targetServerA;
 
  public ProxyHttpCoAPServer() throws IOException {
    ForwardingResource coap2coap = new ProxyCoapClientResource("coap2coap");
    ForwardingResource coap2http = new ProxyHttpClientResource("coap2http");
   
    // Create CoAP Server on PORT with proxy resources form CoAP to CoAP and HTTP
    targetServerA = new Server(PORT);
    targetServerA.add(coap2coap);
    targetServerA.add(coap2http);
    targetServerA.add(new TargetResource("target"));
    targetServerA.start();
   
    ProxyHttpServer httpServer = new ProxyHttpServer(8080);
    httpServer.setProxyCoapResolver(new DirectProxyCoAPResolver(coap2coap));
   
    System.out.println("CoAP resource \"target\" available over HTTP at: http://localhost:8080/proxy/coap://localhost:PORT/target");
  }
 
  /**
   * A simple resource that responds to GET requests with a small response
   * containing the resource's name.
   */
  private static class TargetResource extends ResourceBase {
   
    private int counter = 0;
   
    public TargetResource(String name) {
      super(name);
    }
   
    @Override
    public void handleGET(CoapExchange exchange) {
      exchange.respond("Response "+(++counter)+" from resource " + getName());
    }
  }
 
  public static void main(String[] args) throws Exception {
    new ProxyHttpCoAPServer();
  }

}
TOP

Related Classes of ch.ethz.inf.vs.californium.examples.ProxyHttpCoAPServer$TargetResource

TOP
Copyright © 2018 www.massapi.com. 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.