当前位置: 首页>>代码示例>>Java>>正文


Java CoapExchange类代码示例

本文整理汇总了Java中org.eclipse.californium.core.server.resources.CoapExchange的典型用法代码示例。如果您正苦于以下问题:Java CoapExchange类的具体用法?Java CoapExchange怎么用?Java CoapExchange使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CoapExchange类属于org.eclipse.californium.core.server.resources包,在下文中一共展示了CoapExchange类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: handlePOST

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
@Override
public void handlePOST(CoapExchange exchange) {
  Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest());
  if (!featureType.isPresent()) {
    log.trace("Missing feature type parameter");
    exchange.respond(ResponseCode.BAD_REQUEST);
  } else {
    switch (featureType.get()) {
    case ATTRIBUTES:
      processRequest(exchange, MsgType.POST_ATTRIBUTES_REQUEST);
      break;
    case TELEMETRY:
      processRequest(exchange, MsgType.POST_TELEMETRY_REQUEST);
      break;
    case RPC:
      Optional<Integer> requestId = getRequestId(exchange.advanced().getRequest());
      if (requestId.isPresent()) {
        processRequest(exchange, MsgType.TO_DEVICE_RPC_RESPONSE);
      } else {
        processRequest(exchange, MsgType.TO_SERVER_RPC_REQUEST);
      }
      break;
    }
  }
}
 
开发者ID:osswangxining,项目名称:iothub,代码行数:26,代码来源:CoapTransportResource.java

示例2: handleGET

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
@Override
public void handleGET(CoapExchange exchange) {
    Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest());
    if (!featureType.isPresent()) {
        log.trace("Missing feature type parameter");
        exchange.respond(ResponseCode.BAD_REQUEST);
    } else if (featureType.get() == FeatureType.TELEMETRY) {
        log.trace("Can't fetch/subscribe to timeseries updates");
        exchange.respond(ResponseCode.BAD_REQUEST);
    } else if (exchange.getRequestOptions().hasObserve()) {
        processExchangeGetRequest(exchange, featureType.get());
    } else if (featureType.get() == FeatureType.ATTRIBUTES) {
        processRequest(exchange, MsgType.GET_ATTRIBUTES_REQUEST);
    } else {
        log.trace("Invalid feature type parameter");
        exchange.respond(ResponseCode.BAD_REQUEST);
    }
}
 
开发者ID:thingsboard,项目名称:thingsboard,代码行数:19,代码来源:CoapTransportResource.java

示例3: handlePOST

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
@Override
public void handlePOST(CoapExchange exchange) {
    Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest());
    if (!featureType.isPresent()) {
        log.trace("Missing feature type parameter");
        exchange.respond(ResponseCode.BAD_REQUEST);
    } else {
        switch (featureType.get()) {
            case ATTRIBUTES:
                processRequest(exchange, MsgType.POST_ATTRIBUTES_REQUEST);
                break;
            case TELEMETRY:
                processRequest(exchange, MsgType.POST_TELEMETRY_REQUEST);
                break;
            case RPC:
                Optional<Integer> requestId = getRequestId(exchange.advanced().getRequest());
                if (requestId.isPresent()) {
                    processRequest(exchange, MsgType.TO_DEVICE_RPC_RESPONSE);
                } else {
                    processRequest(exchange, MsgType.TO_SERVER_RPC_REQUEST);
                }
                break;
        }
    }
}
 
开发者ID:thingsboard,项目名称:thingsboard,代码行数:26,代码来源:CoapTransportResource.java

示例4: getPayload

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
public static byte[] getPayload(CoapExchange exchange, int contentFormat) {
	logger.info("---------------------------    REQUEST    -----------------------------------------");
	logger.info("Header: " + exchange.getRequestCode() + "(T=" + exchange.advanced().getRequest().getType() + ", Code=XXX, MID=" + exchange.advanced().getRequest().getMID() + ")");
	logger.info("Options: " + exchange.advanced().getRequest().getOptions().toString());
	logger.info("Authn: " + exchange.advanced().getRequest().getSenderIdentity());
	logger.info("Token: " + exchange.advanced().getRequest().getTokenString());
	
	logger.info("Uri-Path: " + exchange.advanced().getRequest().getURI());
			
    if(contentFormat == MediaTypeRegistry.APPLICATION_JSON) {
    	logger.info("Payload:\n" + new String(exchange.getRequestPayload()) + "\n");
    }
    else if(contentFormat == MediaTypeRegistry.APPLICATION_CBOR) {
    	logger.info("Payload (Diagnostic):\n" + new String(exchange.getRequestPayload()) + "\n\n");
    }
	
	return exchange.getRequestPayload();
}
 
开发者ID:erwah,项目名称:acetest,代码行数:19,代码来源:Exchange.java

示例5: handlePUT

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
@Override
public void handlePUT(CoapExchange exchange) {
    Request request = exchange.advanced().getRequest();

    LOG.debug("UPDATE received : {}", request);
    if (!Type.CON.equals(request.getType())) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }

    List<String> uri = exchange.getRequestOptions().getUriPath();
    if (uri == null || uri.size() != 2 || !RESOURCE_NAME.equals(uri.get(0))) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }

    LOG.debug(
            "Warning a client made a registration update using a CoAP PUT, a POST must be used since version V1_0-20150615-D of the specification. Request: {}",
            request);
    handleUpdate(exchange, request, uri.get(1));
}
 
开发者ID:eclipse,项目名称:leshan,代码行数:22,代码来源:RegisterResource.java

示例6: handle

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
public void handle(CoapExchange exchange) {
  if (shuttingDown) {
    LOG.debug("Shutting down, discarding incoming request from '{}'", exchange.getSourceAddress());
    exchange.respond(CoAP.ResponseCode.SERVICE_UNAVAILABLE);
  } else {
    long start = System.currentTimeMillis();
    LOG.debug("Request accepted from '{}'", exchange.getSourceAddress());
    try {
      if (receiver.process(exchange.getRequestPayload())) {
        exchange.respond(CoAP.ResponseCode.VALID);
        exchange.accept();
        requestMeter.mark();
      } else {
        exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
        exchange.accept();
        errorRequestMeter.mark();
      }
    } catch (IOException ex) {
      exchange.reject();
      errorQueue.offer(ex);
      errorRequestMeter.mark();
    } finally {
      requestTimer.update(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS);
    }
  }
}
 
开发者ID:streamsets,项目名称:datacollector,代码行数:27,代码来源:CoapReceiverResource.java

示例7: authorize

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
private void authorize(CoapExchange exchange, String method) throws UnauthorizedException, TokenExpiredException {
    if(m_restListener.hasProtection()) {
        Optional<Option> tokenOption = exchange.getRequestOptions().asSortedList()
                .parallelStream()
                .filter(option -> option.getNumber() == 65000)
                .findFirst();

        if (tokenOption.isPresent()) {
            String auth = tokenOption.get().getStringValue();
            String jwt = null;
            if (auth != null) {
                if (auth.startsWith("Bearer ")) {
                    jwt = auth.substring("Bearer ".length());
                }
            }
            m_restListener.validate(method.toUpperCase(), this.getURI(), jwt);
        } else {
            throw new UnauthorizedException("No security token found");
        }
    }
}
 
开发者ID:thingweb,项目名称:thingweb,代码行数:22,代码来源:WotCoapResource.java

示例8: setUp

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
  int port =  TestHttpClientTarget.getFreePort();
  coapServer = new CoapServer(NetworkConfig.createStandardWithoutFile(), port);
  coapServer.add(new CoapResource("test") {
    @Override
    public void handlePOST(CoapExchange exchange) {
      serverRequested = true;
      if (returnErrorResponse) {
        exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
        return;
      }
      requestPayload = new String(exchange.getRequestPayload());
      exchange.respond(CoAP.ResponseCode.VALID);
    }
  });
  resourceURl = "coap://localhost:" + port + "/test";
  coapServer.start();
}
 
开发者ID:streamsets,项目名称:datacollector,代码行数:20,代码来源:TestCoapClientTarget.java

示例9: CoapSessionCtx

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
public CoapSessionCtx(CoapExchange exchange, DeviceAuthService authService, long timeout) {
  this.authService = authService;
  Request request = exchange.advanced().getRequest();
  this.token = request.getTokenString();
  this.sessionId = new CoapSessionId(request.getSource().getHostAddress(), request.getSourcePort(), this.token);
  this.exchange = exchange;
  this.timeout = timeout;
}
 
开发者ID:osswangxining,项目名称:iothub,代码行数:9,代码来源:CoapSessionCtx.java

示例10: handleGET

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
@Override
public void handleGET(CoapExchange exchange) {
  Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest());
  if (!featureType.isPresent()) {
    log.trace("Missing feature type parameter");
    exchange.respond(ResponseCode.BAD_REQUEST);
  } else if (featureType.get() == FeatureType.TELEMETRY) {
    log.trace("Can't fetch/subscribe to timeseries updates");
    exchange.respond(ResponseCode.BAD_REQUEST);
  } else if (exchange.getRequestOptions().hasObserve()) {
    boolean unsubscribe = exchange.getRequestOptions().getObserve() == 1;
    MsgType msgType;
    if (featureType.get() == FeatureType.RPC) {
      msgType = unsubscribe ? MsgType.UNSUBSCRIBE_RPC_COMMANDS_REQUEST : MsgType.SUBSCRIBE_RPC_COMMANDS_REQUEST;
    } else {
      msgType = unsubscribe ? MsgType.UNSUBSCRIBE_ATTRIBUTES_REQUEST : MsgType.SUBSCRIBE_ATTRIBUTES_REQUEST;
    }
    Optional<SessionId> sessionId = processRequest(exchange, msgType);
    if (sessionId.isPresent()) {
      if (exchange.getRequestOptions().getObserve() == 1) {
        exchange.respond(ResponseCode.VALID);
      }
    }
  } else if (featureType.get() == FeatureType.ATTRIBUTES) {
    processRequest(exchange, MsgType.GET_ATTRIBUTES_REQUEST);
  } else {
    log.trace("Invalid feature type parameter");
    exchange.respond(ResponseCode.BAD_REQUEST);
  }
}
 
开发者ID:osswangxining,项目名称:iothub,代码行数:31,代码来源:CoapTransportResource.java

示例11: CoapSessionCtx

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
public CoapSessionCtx(CoapExchange exchange, CoapTransportAdaptor adaptor, SessionMsgProcessor processor, DeviceAuthService authService, long timeout) {
    super(processor, authService);
    Request request = exchange.advanced().getRequest();
    this.token = request.getTokenString();
    this.sessionId = new CoapSessionId(request.getSource().getHostAddress(), request.getSourcePort(), this.token);
    this.exchange = exchange;
    this.adaptor = adaptor;
    this.timeout = timeout;
}
 
开发者ID:thingsboard,项目名称:thingsboard,代码行数:10,代码来源:CoapSessionCtx.java

示例12: processExchangeGetRequest

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
private void processExchangeGetRequest(CoapExchange exchange, FeatureType featureType) {
    boolean unsubscribe = exchange.getRequestOptions().getObserve() == 1;
    MsgType msgType;
    if (featureType == FeatureType.RPC) {
        msgType = unsubscribe ? MsgType.UNSUBSCRIBE_RPC_COMMANDS_REQUEST : MsgType.SUBSCRIBE_RPC_COMMANDS_REQUEST;
    } else {
        msgType = unsubscribe ? MsgType.UNSUBSCRIBE_ATTRIBUTES_REQUEST : MsgType.SUBSCRIBE_ATTRIBUTES_REQUEST;
    }
    Optional<SessionId> sessionId = processRequest(exchange, msgType);
    if (sessionId.isPresent()) {
        if (exchange.getRequestOptions().getObserve() == 1) {
            exchange.respond(ResponseCode.VALID);
        }
    }
}
 
开发者ID:thingsboard,项目名称:thingsboard,代码行数:16,代码来源:CoapTransportResource.java

示例13: sendCoapResponse

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
private boolean sendCoapResponse(OneM2mResponse resMessage, CoapExchange exchange) {
	
	if (exchange == null) {
		return false;
	}
	
	coapMap.remove(resMessage.getRequestIdentifier());
	
	try {
		Response response = CoapResponseCodec.encode(resMessage, exchange);
		
		log.debug("<< SEND CoAP MESSAGE:");
		log.debug(response.toString());
		log.debug(response.getPayloadString());
		exchange.respond(response);
		// added in 2017-10-31 to support CSE-relative Unstructured addressing 
		System.out.println("############### uripath-size=" + exchange.getRequestOptions().getUriPath().size());
		//System.out.println("############### resMessage.getResponseStatusCode()=" + resMessage.getResponseStatusCode());
		///System.out.println("############### resourceId=" + ((Resource)resMessage.getContentObject()).getResourceID());
		
		int len = exchange.getRequestOptions().getUriPath().size();
		int resCode = resMessage.getResponseStatusCode();
		
		if(resCode == 2001 && len == 1) {
			String resourceId = ((Resource)resMessage.getContentObject()).getResourceID();
			coapServer.add(coapServer.new HCoapResource(resourceId));
		}
		//coapServer.add(resources)
		
		return true;
	} catch (Exception e) {
		
		e.printStackTrace();
		
		exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, "Respose encoding failed");
	}
	
	return false;
}
 
开发者ID:iotoasis,项目名称:SI,代码行数:40,代码来源:InCse.java

示例14: receiveRequest

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
private void receiveRequest(CoapExchange exchange) {
	if(listener != null) {
		listener.receiveCoapRequest(exchange);
	} else {
		exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, "Not found listener");
	}
}
 
开发者ID:iotoasis,项目名称:SI,代码行数:8,代码来源:HCoapServer.java

示例15: handleGET

import org.eclipse.californium.core.server.resources.CoapExchange; //导入依赖的package包/类
@Override
		public void handleGET(CoapExchange exchange) {	
			// respond to the request
			//try { Thread.sleep(2000); } catch(Exception e) {} 
			
//			System.out.println("RequestCode=" + exchange.getRequestCode());
//			System.out.println("RequestPayload=" +exchange.getRequestPayload());
//			System.out.println("Options=" + exchange.getRequestOptions());
//			System.out.println("UriPath=" + exchange.getRequestOptions().getUriPathString());
			
//			exchange.respond("Hello World!");
			
			receiveRequest(exchange);
		}
 
开发者ID:iotoasis,项目名称:SI,代码行数:15,代码来源:HCoapServer.java


注:本文中的org.eclipse.californium.core.server.resources.CoapExchange类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。