@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 #2
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);
 }
Beispiel #3
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);
    }