if (hasUriHost()) options.add(new Option(OptionNumberRegistry.URI_HOST, getUriHost())); if (etag_list != null) for (byte[] value:etag_list) options.add(new Option(OptionNumberRegistry.ETAG, value)); if (hasIfNoneMatch()) options.add(new Option(OptionNumberRegistry.IF_NONE_MATCH)); if (hasUriPort()) options.add(new Option(OptionNumberRegistry.URI_PORT, getUriPort())); if (location_path_list != null) for (String str:location_path_list) options.add(new Option(OptionNumberRegistry.LOCATION_PATH, str)); if (uri_path_list != null) for (String str:uri_path_list) options.add(new Option(OptionNumberRegistry.URI_PATH, str)); if (hasContentFormat()) options.add(new Option(OptionNumberRegistry.CONTENT_FORMAT, getContentFormat())); if (hasMaxAge()) options.add(new Option(OptionNumberRegistry.MAX_AGE, getMaxAge())); if (uri_query_list != null) for (String str:uri_query_list) options.add(new Option(OptionNumberRegistry.URI_QUERY, str)); if (hasAccept()) options.add(new Option(OptionNumberRegistry.ACCEPT, getAccept())); if (location_query_list != null) for (String str:location_query_list) options.add(new Option(OptionNumberRegistry.LOCATION_QUERY, str)); if (hasProxyUri()) options.add(new Option(OptionNumberRegistry.PROXY_URI, getProxyUri())); if (hasProxyScheme()) options.add(new Option(OptionNumberRegistry.PROXY_SCHEME, getProxyScheme())); if (hasObserve()) options.add(new Option(OptionNumberRegistry.OBSERVE, getObserve()));
/** * Removes all Uri-Path options. * Returns the current OptionSet object for a fluent API. * @return this OptionSet */ public OptionSet clearUriPath() { getUriPath().clear(); return this; }
public CC14(String serverURI) { super(CC14.class.getSimpleName()); // create the request Request request = new Request(Code.GET, Type.CON); // add query request.getOptions().addUriQuery("first=1"); request.getOptions().addUriQuery("second=2"); request.getOptions().addUriQuery("third=3"); // set the parameters and execute the request executeRequest(request, serverURI, RESOURCE_URI); }
/** * Sets the complete relative Uri-Path. * Returns the current OptionSet object for a fluent API. * @param path the Uri-Path to set * @return this OptionSet */ public OptionSet setUriPath(String path) { final String slash = "/"; // remove leading slash if (path.startsWith(slash)) { path = path.substring(slash.length()); } clearUriPath(); for (String segment : path.split(slash)) { // empty path segments are allowed (e.g., /test vs /test/) addUriPath(segment); } return this; }
/** * Checks if this request is used to establish an observe relation. * * @return {@code true} if this request's <em>observe</em> option is set to 0. */ public final boolean isObserve() { return getOptions().hasObserve() && getOptions().getObserve() == 0; }
private void parseUriQuery() { if (getRequestOptions().getURIQueryCount() > 0) { queryParameters = new HashMap<>(); for (String param : getRequestOptions().getUriQuery()) { addParameter(param); } } }
@Override public void handlePUT(CoapExchange exchange) { if (!exchange.getRequestOptions().hasContentFormat()) { exchange.respond(BAD_REQUEST, "Content-Format not set"); return; } // store payload storeData(exchange.getRequestPayload(), exchange.getRequestOptions().getContentFormat()); // complete the request exchange.respond(CHANGED); }
@Override public void visit(BootstrapRequest request) { coapRequest = Request.newPost(); buildRequestSettings(); coapRequest.getOptions().addUriPath("bs"); coapRequest.getOptions().addUriQuery("ep=" + request.getEndpointName()); }
public CB05(String serverURI) { super(CB05.class.getSimpleName()); Request request = Request.newPost(); request.setPayload(data); request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN); // set the parameters and execute the request executeRequest(request, serverURI, "/large-post"); }
protected boolean checkResponse(Request request, Response response) { boolean success = true; success &= checkInt(EXPECTED_RESPONSE_CODE.value, response.getCode().value, "code"); success &= checkOption(MediaTypeRegistry.APPLICATION_LINK_FORMAT, response.getOptions().getContentFormat(), "Content-Format"); success &= hasNonEmptyPalyoad(response); return success; } }
/** * Removes all Uri-Query options. * Returns the current OptionSet object for a fluent API. * @return this OptionSet */ public OptionSet clearUriQuery() { getUriQuery().clear(); return this; }
@Override public void handlePUT(CoapExchange exchange) { if (exchange.getRequestOptions().isIfMatch(etag)) { if (exchange.getRequestOptions().hasContentFormat()) { storeData(exchange.getRequestPayload(), exchange.getRequestOptions().getContentFormat()); exchange.setETag(etag.clone()); exchange.respond(CHANGED); } else { exchange.respond(BAD_REQUEST, "Content-Format not set"); } } else if (exchange.getRequestOptions().hasIfNoneMatch() && data==null) { storeData(exchange.getRequestPayload(), exchange.getRequestOptions().getContentFormat()); exchange.respond(CREATED); } else { exchange.respond(PRECONDITION_FAILED); // automatically change now storeData(null, UNDEFINED); } }
@Override public void handlePUT(CoapExchange exchange) { if (data!=null && exchange.getRequestOptions().hasIfNoneMatch()) { exchange.respond(PRECONDITION_FAILED); // automatically reset data = null; } else { if (exchange.getRequestOptions().hasContentFormat()) { storeData(exchange.getRequestPayload(), exchange.getRequestOptions().getContentFormat()); exchange.respond(CREATED); } else { exchange.respond(BAD_REQUEST, "Content-Format not set"); } } }
@Override public void handleGET(CoapExchange exchange) { ServerIdentity identity = extractServerIdentity(exchange, bootstrapHandler); String URI = exchange.getRequestOptions().getUriPathString(); if (exchange.getRequestOptions().getAccept() == MediaTypeRegistry.APPLICATION_LINK_FORMAT) { DiscoverResponse response = nodeEnabler.discover(identity, new DiscoverRequest(URI)); if (response.getCode().isError()) { if (exchange.getRequestOptions().hasAccept()) { requestedContentFormat = ContentFormat.fromCode(exchange.getRequestOptions().getAccept()); if (!encoder.isSupported(requestedContentFormat)) { exchange.respond(ResponseCode.NOT_ACCEPTABLE); if (exchange.getRequestOptions().hasObserve()) { ObserveRequest observeRequest = new ObserveRequest(URI); ObserveResponse response = nodeEnabler.observe(identity, observeRequest);
/** * Checks whether this response is a notification for * an observed resource. * * @return {@code true} if this response has the observe option set. */ public boolean isNotification() { return getOptions().hasObserve(); }
private static void earlyBlock2Negotiation(final Exchange exchange, final Request request) { // Call this method when a request has completely arrived (might have // been sent in one piece without blockwise). BlockOption block2 = request.getOptions().getBlock2(); if (block2 != null) { BlockwiseStatus status2 = new BlockwiseStatus(request.getOptions().getContentFormat(), block2.getNum(), block2.getSzx()); LOGGER.log(Level.FINE, "Request with early block negotiation {0}. Create and set new Block2 status: {1}", new Object[]{block2, status2}); exchange.setResponseBlockStatus(status2); } }
switch (exchange.getRequestOptions().getAccept()) { case UNDEFINED: case TEXT_PLAIN: response.getOptions().setContentFormat(TEXT_PLAIN); format = "Status type: \"%s\"\nCode: \"%s\"\nMID: \"%d\"\nAccept: \"%d\""; break; response.getOptions().setContentFormat(APPLICATION_XML); format = "<msg type=\"%s\" code=\"%s\" mid=\"%d\" accept=\"%d\"/>"; // should fit 64 bytes break; response.getOptions().setContentFormat(APPLICATION_JSON); format = "{ \"type\":\"%s\", \"code\":\"%s\", \"mid\":%d, \"accept\":%d }"; // should fit 64 bytes jsonG.writeStringField("code", request.getCode().name()); jsonG.writeNumberField("mid", request.getMID()); jsonG.writeNumberField("accept", request.getOptions().getAccept()); jsonG.writeEndObject(); jsonG.close(); response.getOptions().setContentFormat(APPLICATION_CBOR); cborG.writeStringField("code", request.getCode().name()); cborG.writeNumberField("mid", request.getMID()); cborG.writeNumberField("accept", request.getOptions().getAccept()); cborG.writeEndObject(); cborG.close(); request.getCode(),
public OdlOnem2mCoapRequestPrimitiveBuilder setTo(String value) { onem2mRequest.optionsSet.addUriPath(value); // M3 onem2mRequest.optionsSet.addUriPath(value); return this; } public OdlOnem2mCoapRequestPrimitiveBuilder setFrom(String value) {
public CC23(String serverURI) { super(CC23.class.getSimpleName()); Request request = new Request(Code.PUT, Type.CON); // request.setIfNoneMatch(); request.getOptions().setIfNoneMatch(true); request.setPayload("TD_COAP_CORE_23 Part A"); request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN); executeRequest(request, serverURI, RESOURCE_URI); }