Skip to content

Commit

Permalink
🔐 add psk for coap get、put、post and delete operations
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshengshui committed Nov 3, 2023
1 parent 70a4904 commit a33ba34
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
import iot.technology.client.toolkit.coap.command.sub.*;
import iot.technology.client.toolkit.common.constants.ExitCodeEnum;
import iot.technology.client.toolkit.common.constants.StorageConstants;
import iot.technology.client.toolkit.common.utils.StringUtils;
import picocli.CommandLine;

import java.util.ResourceBundle;
import java.util.concurrent.Callable;

import static iot.technology.client.toolkit.common.utils.ColorUtils.colorItalic;

/**
* @author mushuwei
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
package iot.technology.client.toolkit.coap.command.sub;

import iot.technology.client.toolkit.coap.service.CoapClientService;
import iot.technology.client.toolkit.coap.validator.CoapCommandParamValidator;
import iot.technology.client.toolkit.common.constants.ExitCodeEnum;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.CoAP;
import picocli.CommandLine;

import java.net.URI;
Expand All @@ -40,7 +38,7 @@
footerHeading = "%nCopyright (c) 2019-2023, ${bundle:general.copyright}",
footer = "%nDeveloped by mushuwei"
)
public class CoapDeleteCommand implements Callable<Integer> {
public class CoapDeleteCommand extends AbstractCoapContext implements Callable<Integer> {

private final CoapClientService coapClientService = new CoapClientService();

Expand All @@ -52,12 +50,35 @@ public class CoapDeleteCommand implements Callable<Integer> {
description = "${bundle:coap.uri.description}")
private URI uri;

/* ********************************** Identity Section ******************************** */
@CommandLine.ArgGroup(exclusive = false,
heading = "%n@|bold,underline PSK Options|@ %n%n"//
+ "@|italic " //
+ "By default use non secure connection.%n"//
+ "To use CoAP over DTLS with Pre-Shared Key, -i and -p options should be used together." //
+ "|@%n%n")
private PskSection psk = new PskSection();

public static class PskSection {
@CommandLine.Option(required = true,
names = { "-i", "--psk-identity" },
description = { //
"Set the server PSK identity in ascii." })
public String identity;

@CommandLine.Option(required = true,
names = { "-p", "--psk-key" },
description = { //
"Set the server Pre-Shared-Key" })
public String sharekey;
}

@Override
public Integer call() throws Exception {
CoapCommandParamValidator.validateUri(uri);
String scheme = validateUri(uri);
String protocol = CoAP.getProtocolForScheme(scheme);

CoapClient coapClient = coapClientService.getCoapClient(uri);
CoapResponse response = coapClient.delete();
var response = coapClientService.deletePayload(uri, protocol, psk.identity, psk.sharekey);

StringBuffer result = new StringBuffer();
String requestInfo = coapClientService.requestInfo("delete", uri.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package iot.technology.client.toolkit.coap.command.sub;

import iot.technology.client.toolkit.coap.service.CoapClientService;
import iot.technology.client.toolkit.coap.validator.CoapCommandParamValidator;
import iot.technology.client.toolkit.common.constants.ExitCodeEnum;
import iot.technology.client.toolkit.common.constants.StorageConstants;
import iot.technology.client.toolkit.common.utils.StringUtils;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.WebLink;
import org.eclipse.californium.core.coap.CoAP;
import picocli.CommandLine;

import java.net.URI;
Expand All @@ -44,7 +44,7 @@
footerHeading = "%nCopyright (c) 2019-2023, ${bundle:general.copyright}",
footer = "%nDeveloped by mushuwei"
)
public class CoapDiscoverCommand implements Callable<Integer> {
public class CoapDiscoverCommand extends AbstractCoapContext implements Callable<Integer> {

ResourceBundle bundle = ResourceBundle.getBundle(StorageConstants.LANG_MESSAGES);

Expand All @@ -58,11 +58,35 @@ public class CoapDiscoverCommand implements Callable<Integer> {
description = "${bundle:coap.uri.description}")
private URI uri;

/* ********************************** Identity Section ******************************** */
/* ********************************** Identity Section ******************************** */
@CommandLine.ArgGroup(exclusive = false,
heading = "%n@|bold,underline PSK Options|@ %n%n"//
+ "@|italic " //
+ "By default use non secure connection.%n"//
+ "To use CoAP over DTLS with Pre-Shared Key, -i and -p options should be used together." //
+ "|@%n%n")
private PskSection psk = new PskSection();

public Integer call() throws Exception {
CoapCommandParamValidator.validateUri(uri);
public static class PskSection {
@CommandLine.Option(required = true,
names = { "-i", "--psk-identity" },
description = { //
"Set the server PSK identity in ascii." })
public String identity;

@CommandLine.Option(required = true,
names = { "-p", "--psk-key" },
description = { //
"Set the server Pre-Shared-Key" })
public String sharekey;
}

CoapClient coapClient = coapClientService.getCoapClient(uri);

public Integer call() throws Exception {
String scheme = validateUri(uri);
String protocol = CoAP.getProtocolForScheme(scheme);
CoapClient coapClient = CoapClientService.getCoapClient(uri, protocol, psk.identity, psk.sharekey);

Set<WebLink> webLinks = coapClient.discover();
String availableResources = coapClientService.getAvailableResources(webLinks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
package iot.technology.client.toolkit.coap.command.sub;

import iot.technology.client.toolkit.coap.service.CoapClientService;
import iot.technology.client.toolkit.coap.validator.CoapCommandParamValidator;
import iot.technology.client.toolkit.common.constants.ExitCodeEnum;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.Response;
Expand Down Expand Up @@ -64,16 +61,35 @@ public class CoapGetCommand extends AbstractCoapContext implements Callable<Inte
private String accept;

/* ********************************** Identity Section ******************************** */
@CommandLine.ArgGroup(exclusive = true)
public CoapIdentitySection identity = new CoapIdentitySection();
@CommandLine.ArgGroup(exclusive = false,
heading = "%n@|bold,underline PSK Options|@ %n%n"//
+ "@|italic " //
+ "By default use non secure connection.%n"//
+ "To use CoAP over DTLS with Pre-Shared Key, -i and -p options should be used together." //
+ "|@%n%n")
private PskSection psk = new PskSection();

public static class PskSection {
@CommandLine.Option(required = true,
names = { "-i", "--psk-identity" },
description = { //
"Set the server PSK identity in ascii." })
public String identity;

@CommandLine.Option(required = true,
names = { "-p", "--psk-key" },
description = { //
"Set the server Pre-Shared-Key" })
public String sharekey;
}


@Override
public Integer call() throws Exception {
String scheme = validateUri(uri);
String protocol = CoAP.getProtocolForScheme(scheme);

Response response = coapClientService.getResponse(uri, protocol, identity, accept);
Response response = coapClientService.getResponse(uri, protocol, psk.identity, psk.sharekey, accept);

StringBuffer result = new StringBuffer();
String requestInfo = coapClientService.requestInfo("get", uri.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import iot.technology.client.toolkit.coap.service.CoapClientService;
import iot.technology.client.toolkit.coap.validator.CoapCommandParamValidator;
import iot.technology.client.toolkit.common.constants.ExitCodeEnum;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.CoAP;
import picocli.CommandLine;

import java.net.URI;
Expand All @@ -41,7 +40,7 @@
footerHeading = "%nCopyright (c) 2019-2023, ${bundle:general.copyright}",
footer = "%nDeveloped by mushuwei"
)
public class CoapPostCommand implements Callable<Integer> {
public class CoapPostCommand extends AbstractCoapContext implements Callable<Integer> {

private final CoapClientService coapClientService = new CoapClientService();

Expand Down Expand Up @@ -73,17 +72,38 @@ public class CoapPostCommand implements Callable<Integer> {
defaultValue = COAP_TEXT_PLAIN)
private String accept;

/* ********************************** Identity Section ******************************** */
@CommandLine.ArgGroup(exclusive = false,
heading = "%n@|bold,underline PSK Options|@ %n%n"//
+ "@|italic " //
+ "By default use non secure connection.%n"//
+ "To use CoAP over DTLS with Pre-Shared Key, -i and -p options should be used together." //
+ "|@%n%n")
private PskSection psk = new PskSection();

public static class PskSection {
@CommandLine.Option(required = true,
names = { "-i", "--psk-identity" },
description = { //
"Set the server PSK identity in ascii." })
public String identity;

@CommandLine.Option(required = true,
names = { "-pk", "--psk-key" },
description = { //
"Set the server Pre-Shared-Key" })
public String sharekey;
}

@Override
public Integer call() throws Exception {
CoapCommandParamValidator.validateUri(uri);
String payloadContent = CoapCommandParamValidator.validatePayloadAndFile(payload);
var scheme = validateUri(uri);
var protocol = CoAP.getProtocolForScheme(scheme);
var payloadContent = CoapCommandParamValidator.validatePayloadAndFile(payload);

CoapClient coapClient = coapClientService.getCoapClient(uri);
int formatCode = coapClientService.coapContentType(format);
int acceptCode = coapClientService.coapContentType(accept);
var response = coapClientService.postPayload(uri, protocol, psk.identity, psk.sharekey, payloadContent, format, accept);

StringBuffer result = new StringBuffer();
CoapResponse response = coapClient.post(payloadContent, formatCode, acceptCode);
String requestInfo = coapClientService.requestInfo("post", uri.toString());
String responseStr = coapClientService.prettyPrint(response, requestInfo);
result.append(responseStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import iot.technology.client.toolkit.coap.service.CoapClientService;
import iot.technology.client.toolkit.coap.validator.CoapCommandParamValidator;
import iot.technology.client.toolkit.common.constants.ExitCodeEnum;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.CoAP;
import picocli.CommandLine;

import java.net.URI;
Expand All @@ -41,7 +40,7 @@
footerHeading = "%nCopyright (c) 2019-2023, ${bundle:general.copyright}",
footer = "%nDeveloped by mushuwei"
)
public class CoapPutCommand implements Callable<Integer> {
public class CoapPutCommand extends AbstractCoapContext implements Callable<Integer> {

private final CoapClientService coapClientService = new CoapClientService();

Expand All @@ -67,13 +66,36 @@ public class CoapPutCommand implements Callable<Integer> {
defaultValue = COAP_TEXT_PLAIN)
private String format;

/* ********************************** Identity Section ******************************** */
@CommandLine.ArgGroup(exclusive = false,
heading = "%n@|bold,underline PSK Options|@ %n%n"//
+ "@|italic " //
+ "By default use non secure connection.%n"//
+ "To use CoAP over DTLS with Pre-Shared Key, -i and -p options should be used together." //
+ "|@%n%n")
private PskSection psk = new PskSection();

public static class PskSection {
@CommandLine.Option(required = true,
names = { "-i", "--psk-identity" },
description = { //
"Set the server PSK identity in ascii." })
public String identity;

@CommandLine.Option(required = true,
names = { "-pk", "--psk-key" },
description = { //
"Set the server Pre-Shared-Key" })
public String sharekey;
}


@Override
public Integer call() throws Exception {
CoapCommandParamValidator.validateUri(uri);
String payloadContent = CoapCommandParamValidator.validatePayloadAndFile(payload);

CoapClient coapClient = coapClientService.getCoapClient(uri);
CoapResponse response = coapClient.put(payloadContent, coapClientService.coapContentType(format));
var scheme = validateUri(uri);
var protocol = CoAP.getProtocolForScheme(scheme);
var payloadContent = CoapCommandParamValidator.validatePayloadAndFile(payload);
var response = coapClientService.putPayload(uri, protocol, psk.identity, psk.sharekey, payloadContent, format);

String requestInfo = coapClientService.requestInfo("put", uri.toString());
String responseStr = coapClientService.prettyPrint(response, requestInfo);
Expand Down
Loading

0 comments on commit a33ba34

Please sign in to comment.