Package ch.ethz.inf.vs.californium.coap

Examples of ch.ethz.inf.vs.californium.coap.Response


    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);
  }
View Full Code Here


      .append("\n").append("Source: ").append(request.getSource()).append(":").append(request.getSourcePort())
      .append("\n").append("Type: ").append(request.getType())
      .append("\n").append("MID: ").append(request.getMID())
      .append("\n").append("Token: ").append(request.getTokenString())
      .append("\n").append(request.getOptions());
    Response response = new Response(ResponseCode.CONTENT);
    response.setPayload(buffer.toString());
    response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
    exchange.sendResponse(response);
  }
View Full Code Here

  public void handleGET(CoapExchange exchange) {
    Integer ct = MediaTypeRegistry.IMAGE_PNG;
    if (exchange.getRequestOptions().hasAccept()) {
      ct = exchange.getRequestOptions().getAccept();
      if (!supported.contains(ct)) {
        exchange.respond(new Response(ResponseCode.NOT_ACCEPTABLE));
        return;
      }
    }
   
    String filename = filePath + fileName + "." + MediaTypeRegistry.toFileExtension(ct);

    // load representation from file
    System.out.println("Opening file "+filename+", "+new File(filename).getAbsolutePath());
    File file = new File(filename);
   
    if (!file.exists()) {
      exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, "Image file not found");
      return;
    }
   
    // get length of file for buffer
        int fileLength = (int)file.length();
        byte[] fileData = new byte[fileLength];
       
        try
        {
      // open input stream from file
          FileInputStream fileIn = new FileInputStream(file);
      // read file into byte array
      fileIn.read(fileData);
      fileIn.close();
     
      // create response
      Response response = new Response(ResponseCode.CONTENT);
      response.setPayload(fileData);
      response.getOptions().setContentFormat(ct);

      exchange.respond(response);
     
        } catch (Exception e) {
      exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, "I/O error");
View Full Code Here

    if (!enabled) {
      return null;
    }

    // search the desired representation
    Response response = null;
    CacheKey cacheKey = null;
    try {
      for (CacheKey acceptKey : CacheKey.fromAcceptOptions(request)) {
        response = responseCache.getIfPresent(acceptKey);
        cacheKey = acceptKey;

        if (response != null) {
          break;
        }
      }
    } catch (URISyntaxException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // if the response is not null, manage the cached response
    if (response != null) {
      LOGGER.finer("Cache hit");

      // check if the response is expired
      long currentTime = System.nanoTime();
      int nanosLeft = getRemainingLifetime(response, currentTime);
      if (nanosLeft > 0) {
        // if the response can be used, then update its max-age to
        // consider the aging of the response while in the cache
        response.getOptions().setMaxAge(nanosLeft);
        // set the current time as the response timestamp
        response.setTimestamp(currentTime);
      } else {
        LOGGER.finer("Expired response");

        // try to validate the response
        response = validate(cacheKey);
View Full Code Here

    builder.append("Available commands:\n - GET: show cached values\n - DELETE: empty the cache\n - POST: enable/disable caching\n");

    // get cache values
    builder.append("\nCached values:\n");
    for (CacheKey cachedRequest : responseCache.asMap().keySet()) {
      Response response = responseCache.asMap().get(cachedRequest);

      builder.append(cachedRequest.getProxyUri().toString() + " (" +
          MediaTypeRegistry.toString(cachedRequest.getMediaType()) + ") > " + getRemainingLifetime(response) + " seconds | (" + cachedRequest.getMediaType() + ")\n");
    }
View Full Code Here

    private static CacheKey fromContentTypeOption(Request request) throws URISyntaxException {
      if (request == null) {
        throw new IllegalArgumentException("request == null");
      }

      Response response = request.getResponse();
      if (response == null) {
        return fromAcceptOptions(request).get(0);
      }

      String proxyUri = request.getOptions().getProxyURI();
      Integer mediaType = response.getOptions().getContentFormat();
      if (mediaType == null)
        mediaType = MediaTypeRegistry.TEXT_PLAIN;
      byte[] payload = request.getPayload();

      // create the new cacheKey
View Full Code Here

        }
      }
    };
    exchange.setRequest(request);
   
    Response response = null;
    // ignore the request if it is reset or acknowledge
    // check if the proxy-uri is defined
    if (request.getType() != Type.RST && request.getType() != Type.ACK
        && request.getOptions().hasProxyURI()) {
      // get the response from the cache
      response = cacheResource.getResponse(request);
//      if (Bench_Help.DO_LOG)
        LOGGER.info("Cache returned "+response);

      // update statistics
      statsResource.updateStatistics(request, response != null);
    }

    // check if the response is present in the cache
    if (response != null) {
      // link the retrieved response with the request to set the
      // parameters request-specific (i.e., token, id, etc)
      exchange.sendResponse(response);
      return;
    } else {

      // edit the request to be correctly forwarded if the proxy-uri is
      // set
      if (request.getOptions().hasProxyURI()) {
        try {
          manageProxyUriRequest(request);
//          if (Bench_Help.DO_LOG)
            LOGGER.info("after manageProxyUriRequest: "+request);

        } catch (URISyntaxException e) {
          LOGGER.warning(String.format("Proxy-uri malformed: %s", request.getOptions().getProxyURI()));

          exchange.sendResponse(new Response(ResponseCode.BAD_OPTION));
        }
      }

      // handle the request as usual
      proxyCoapResolver.forwardRequest(exchange);
View Full Code Here

    @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 += getStats();
      Response response = new Response(ResponseCode.CONTENT);
      response.setPayload(payload);
      response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
      exchange.respond(response);
    }
View Full Code Here

          } 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);
    }
View Full Code Here

     */
    @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);
    }
View Full Code Here

TOP

Related Classes of ch.ethz.inf.vs.californium.coap.Response

Copyright © 2018 www.massapicom. 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.