本文整理汇总了Java中org.eclipse.californium.core.coap.Request.newPost方法的典型用法代码示例。如果您正苦于以下问题:Java Request.newPost方法的具体用法?Java Request.newPost怎么用?Java Request.newPost使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.californium.core.coap.Request
的用法示例。
在下文中一共展示了Request.newPost方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newRequest
import org.eclipse.californium.core.coap.Request; //导入方法依赖的package包/类
public static Request newRequest(String method) {
if (method.equalsIgnoreCase("GET")) {
return Request.newGet();
} else if (method.equalsIgnoreCase("POST")) {
return Request.newPost();
} else if (method.equalsIgnoreCase("PUT")) {
return Request.newPut();
} else if (method.equalsIgnoreCase("DELETE")) {
return Request.newDelete();
} else if (method.equalsIgnoreCase("DISCOVER")) {
return Request.newGet();
} else if (method.equalsIgnoreCase("OBSERVE")) {
Request request = Request.newGet();
request.setObserve();
return request;
} else {
System.err.println("Unknown method: " + method);
return null;
}
}
示例2: testSuccessPlaintext
import org.eclipse.californium.core.coap.Request; //导入方法依赖的package包/类
@Test
public void testSuccessPlaintext() throws Exception {
Request request = Request.newPost();
request.setURI("coap://localhost:"+config.getCoapPort()+"/"+Constants.TOKEN_RESOURCE);
TokenRequest req = new TokenRequest();
req.setGrantType("client_credentials");
req.setAud(config.getResourceServers().get(0).getAud());
req.setClientID(config.getClients().get(0).getClient_id());
req.setClientSecret(config.getClients().get(0).getClient_secret());
req.setScopes(config.getResourceServers().get(0).getScopes());
request.getOptions().setContentFormat(MediaTypeRegistry.APPLICATION_JSON);
request.setPayload(req.toPayload(MediaTypeRegistry.APPLICATION_JSON));
Response response = request.send().waitForResponse();
TokenResponse tokenResponse = new TokenResponse(response.getPayload(), MediaTypeRegistry.APPLICATION_JSON);
TestUtils.validateToken(tokenResponse.getAccessToken().getBytes(), config.getResourceServers().get(0).getAud(), MediaTypeRegistry.APPLICATION_JSON);
}
示例3: testSuccessPlaintext
import org.eclipse.californium.core.coap.Request; //导入方法依赖的package包/类
@Test
public void testSuccessPlaintext() throws Exception {
// first create a token
JsonWebKey jwk;
jwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);
jwk.setKeyId("testkid");
TokenRequest createReq = new TokenRequest();
createReq.setGrantType("client_credentials");
createReq.setAud(config.getResourceServers().get(0).getAud());
createReq.setClientID(config.getClients().get(0).getClient_id());
createReq.setClientSecret(config.getClients().get(0).getClient_secret());
createReq.setScopes(config.getResourceServers().get(0).getScopes());
createReq.setKey(jwk);
Request request = Request.newPost();
request.setURI("coap://localhost:"+config.getCoapPort()+"/"+Constants.TOKEN_RESOURCE);
request.setPayload(createReq.toPayload(MediaTypeRegistry.APPLICATION_JSON));
request.getOptions().setContentFormat(MediaTypeRegistry.APPLICATION_JSON);
Response createResponse = request.send().waitForResponse();
Assert.assertEquals(ResponseCode.CONTENT, createResponse.getCode());
TokenResponse tokenResponse = new TokenResponse(createResponse.getPayload(), MediaTypeRegistry.APPLICATION_JSON);
// see of token is valid
IntrospectRequest introspectionReq = new IntrospectRequest();
introspectionReq.setToken(tokenResponse.getAccessToken());
introspectionReq.setClientID(config.getClients().get(0).getClient_id());
introspectionReq.setClientSecret(config.getClients().get(0).getClient_secret());
Request introspectionRequest = Request.newPost();
introspectionRequest.setURI("coap://localhost:"+config.getCoapPort()+"/"+Constants.INSTROSPECTION_RESOURCE);
introspectionRequest.setPayload(introspectionReq.toPayload(MediaTypeRegistry.APPLICATION_JSON));
introspectionRequest.getOptions().setContentFormat(MediaTypeRegistry.APPLICATION_JSON);
Response introspectionResponse = introspectionRequest.send().waitForResponse();
Assert.assertEquals(introspectionResponse.getCode(), ResponseCode.CONTENT);
IntrospectResponse introspectResponse = new IntrospectResponse(introspectionResponse.getPayload(), MediaTypeRegistry.APPLICATION_JSON);
Assert.assertTrue(introspectResponse.isActive());
}
示例4: asymmetricEcClient
import org.eclipse.californium.core.coap.Request; //导入方法依赖的package包/类
private static void asymmetricEcClient() throws JoseException {
JsonWebKey popKey = EcJwkGenerator.generateJwk(EllipticCurves.P256);
// generate a unique kid for the newly generated key
String kid = new BigInteger(130, random).toString(32);
popKey.setKeyId(kid);
TokenRequest req = new TokenRequest();
req.setGrantType("client_credentials");
req.setAud(config.getRsAud());
req.setClientID(config.getClientId());
req.setClientSecret(config.getClientSecret());
req.setScopes(config.getRsScopes());
// add key to the request so that public part can be sent to AS
req.setKey(popKey);
Response response;
try {
// send token request to AS and include the public key
response = DTLSUtils.dtlsPSKRequest("coaps://localhost:"+config.getAsCoapsPort()+"/"+Constants.TOKEN_RESOURCE, "POST", req.toPayload(MediaTypeRegistry.APPLICATION_JSON), MediaTypeRegistry.APPLICATION_JSON, config.getAsPskIdentity(), config.getAsPskKey().getBytes());
TokenResponse tokenResponse = new TokenResponse(response.getPayload(), response.getOptions().getContentFormat());
String accessToken = tokenResponse.getAccessToken();
EllipticCurveJsonWebKey rpk = tokenResponse.getRpk();
if(rpk != null) {
String keyType = rpk.getKeyType();
PublicKey publicKey = null;
if(keyType.equalsIgnoreCase("ec")) {
EllipticCurveJsonWebKey ecjwk = new EllipticCurveJsonWebKey((ECPublicKey) rpk.getKey());
publicKey = ecjwk.getPublicKey();
}
else if(keyType.equalsIgnoreCase("rsa")) {
RsaJsonWebKey rsajwk = new RsaJsonWebKey((RSAPublicKey) rpk.getKey());
publicKey = rsajwk.getPublicKey();
}
ArrayList<PublicKey> trustedPublicKeys = new ArrayList<PublicKey>();
trustedPublicKeys.add(publicKey);
// send key to resource servers authz-info resource over unencrypted DTLS
Request authzInfoRequest = Request.newPost();
authzInfoRequest.setURI("coap://localhost:"+config.getRsCoapPort()+"/"+Constants.AUTHZ_INFO_RESOURCE);
authzInfoRequest.getOptions().setContentFormat(Constants.MediaTypeRegistry_APPLICATION_JWT);
authzInfoRequest.setPayload(accessToken.getBytes());
Response authzInfoResponse = authzInfoRequest.send().waitForResponse();
if(authzInfoResponse.getCode() == ResponseCode.CREATED) {
// get the temperature
response = DTLSUtils.dtlsRPKRequest("coaps://localhost:"+config.getRsCoapsPort()+"/temperature", "POST", "".getBytes(), MediaTypeRegistry.APPLICATION_JSON, popKey, trustedPublicKeys);
TemperatureResponse temperatureResponse = new TemperatureResponse(response.getPayload(), response.getOptions().getContentFormat());
logger.info("Temp: " + temperatureResponse);
}
else {
logger.info("Access token not valid. Response code: " + response.getCode());
}
}
} catch (Exception e) {
logger.error(e);
}
}