本文整理汇总了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;
}
}
}
示例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);
}
}
示例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;
}
}
}
示例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();
}
示例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));
}
示例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);
}
}
}
示例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");
}
}
}
示例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();
}
示例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;
}
示例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);
}
}
示例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;
}
示例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);
}
}
}
示例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;
}
示例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");
}
}
示例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);
}