@Override
 public void handleGET(CoapExchange exchange) {
   if (content != null) {
     exchange.respond(content);
   } else {
     String subtree = LinkFormat.serializeTree(this);
     exchange.respond(ResponseCode.CONTENT, subtree, MediaTypeRegistry.APPLICATION_LINK_FORMAT);
   }
 }
  @Override
  public void handlePOST(CoapExchange exchange) {
    String payload = exchange.getRequestText();
    String[] parts = payload.split("\\?");
    String[] path = parts[0].split("/");
    Resource resource = create(new LinkedList<String>(Arrays.asList(path)));

    Response response = new Response(ResponseCode.CREATED);
    response.getOptions().setLocationPath(resource.getURI());
    exchange.respond(response);
  }
  @Override
  public void handlePOST(CoapExchange exchange) {

    Response response = new Response(CoAP.ResponseCode.CONTENT);

    int contentType = exchange.getRequestOptions().getContentFormat();
    int acceptTypeVal = exchange.getRequestOptions().getAccept();
    String acceptType = "";
    switch (acceptTypeVal) {
      case MediaTypeRegistry.APPLICATION_EXI:
        break;
      case MediaTypeRegistry.APPLICATION_XML:
        acceptType = MediaType.APPLICATION_XML.getSubType();
        break;
      case MediaTypeRegistry.APPLICATION_JSON:
        acceptType = MediaType.APPLICATION_JSON.getSubType();
        break;
    }

    Resource02_Discovery rd = new Resource02_Discovery();
    byte[] message = exchange.getRequestPayload();
    InputStream isMsg = new ByteArrayInputStream(message);
    StringRepresentation sr = new StringRepresentation("");

    try {
      switch (contentType) {
        case MediaTypeRegistry.APPLICATION_EXI:
          //              decode first! TODO
          //                 sr = rc.registerXmlHandler(isMsg, acceptType);
          //                 byte[] exiMessage = codeSchemaLess(message);
          //                 response.setPayload(exiMessage);
          break;
        case MediaTypeRegistry.APPLICATION_JSON:
          sr = rd.discoveryJsonHandler(isMsg, acceptType);
          response.setPayload(sr.getText());
          break;
        default:
          response.setPayload(
              "accept types supported: application/exi; application/xml; application/json");
      }

    } catch (ResourceException ex) {
      Logger.getLogger(CoapR02_Discovery.class.getName()).log(Level.SEVERE, null, ex);
    }

    exchange.respond(response);
  }
Beispiel #4
0
 @Override
 public void handleGET(CoapExchange exchange) {
   String payload =
       "Available commands:\n - GET: show statistics\n - POST write stats to file\n - DELETE: reset statistics\n\n";
   payload += getStatString();
   Response response = new Response(ResponseCode.CONTENT);
   response.setPayload(payload);
   response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
   exchange.respond(response);
 }
 public void run() {
   /*
    * Calls performXXX Method. If an exception occurs it must be
    * caught, to ensure, the thread doesn't stop.
    */
   try {
     resource.handleRequest(request.advanced());
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Beispiel #6
0
    @Override
    public void handlePOST(CoapExchange exchange) {
      // TODO include stopping the writing => make something for the whole
      // proxy
      // executor.shutdown();
      // request.respond(CodeRegistry.RESP_DELETED, "Stopped",
      // MediaTypeRegistry.TEXT_PLAIN);

      // starting to log the stats on a new file

      // create the new file
      String logName = System.nanoTime() + CACHE_LOG_NAME;
      final File cacheLog = new File(logName);
      try {
        cacheLog.createNewFile();

        // write the header
        com.google.common.io.Files.write(
            "hits%, avg. load, #evictions \n", cacheLog, Charset.defaultCharset());
      } catch (IOException e) {
      }

      executor.scheduleWithFixedDelay(
          new Runnable() {

            @Override
            public void run() {
              CacheStats cacheStats = cacheResource.getCacheStats().minus(relativeCacheStats);

              String csvStats =
                  String.format(
                      "%.3f, %.3f, %d %n",
                      cacheStats.hitRate(),
                      cacheStats.averageLoadPenalty(),
                      cacheStats.evictionCount());
              try {
                com.google.common.io.Files.append(csvStats, cacheLog, Charset.defaultCharset());
              } catch (IOException e) {
              }
            }
          },
          0,
          DEFAULT_LOGGING_DELAY,
          TimeUnit.SECONDS);

      Response response = new Response(ResponseCode.CREATED);
      response.setPayload("Creted log: " + logName);
      response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
      exchange.respond(response);
    }
Beispiel #7
0
  @Override
  public void handleGET(CoapExchange exchange) {

    exchange.setMaxAge(5);
    exchange.respond(CONTENT, time, TEXT_PLAIN);
  }
 /**
  * Handles the DELETE request in the given CoAPExchange. By default it responds with a 4.05
  * (Method Not Allowed). Override this method to respond differently to DELETE requests. The
  * response code to a DELETE request should be a Deleted (2.02).
  *
  * @param exchange the CoapExchange for the simple API
  */
 public void handleDELETE(CoapExchange exchange) {
   exchange.respond(ResponseCode.METHOD_NOT_ALLOWED);
 }
Beispiel #9
0
 @Override
 public void handleGET(CoapExchange exchange) {
   exchange.respond(CONTENT, LinkFormat.serializeTree(this), APPLICATION_LINK_FORMAT);
 }
Beispiel #10
0
 @Override
 public void handleGET(CoapExchange exchange) {
   exchange.respond(this.getURI() + this.getChildren());
 }
 @Override
 public void handleDELETE(CoapExchange exchange) {
   this.delete();
   exchange.respond(ResponseCode.DELETED);
 }
 @Override
 public void handlePUT(CoapExchange exchange) {
   content = exchange.getRequestText();
   exchange.respond(ResponseCode.CHANGED);
 }
Beispiel #13
0
 @Override
 public void handleDELETE(CoapExchange exchange) {
   // reset all the statistics
   statsTable.clear();
   exchange.respond(ResponseCode.DELETED);
 }
Beispiel #14
0
 @Override
 public void handleDELETE(CoapExchange exchange) {
   // reset the cache
   relativeCacheStats = cacheResource.getCacheStats().minus(relativeCacheStats);
   exchange.respond(ResponseCode.DELETED);
 }