Search in sources :

Example 6 with EndpointContext

use of org.eclipse.californium.elements.EndpointContext in project leshan by eclipse.

the class EndpointContextSerDes method deserialize.

public static EndpointContext deserialize(JsonObject peer) {
    String address = peer.get(KEY_ADDRESS).asString();
    int port = peer.get(KEY_PORT).asInt();
    InetSocketAddress socketAddress = new InetSocketAddress(address, port);
    Principal principal = null;
    JsonValue value = peer.get(KEY_ID);
    if (value != null) {
        principal = new PreSharedKeyIdentity(value.asString());
    } else if ((value = peer.get(KEY_RPK)) != null) {
        try {
            byte[] rpk = Hex.decodeHex(value.asString().toCharArray());
            X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
            PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
            principal = new RawPublicKeyIdentity(publicKey);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
            throw new IllegalStateException("Invalid security info content", e);
        }
    } else if ((value = peer.get(KEY_DN)) != null) {
        principal = new X500Principal(value.asString());
    }
    EndpointContext endpointContext;
    value = peer.get(KEY_ATTRIBUTES);
    if (value == null) {
        endpointContext = new AddressEndpointContext(socketAddress, principal);
    } else {
        int index = 0;
        String[] attributes = new String[value.asObject().size() * 2];
        for (Member member : value.asObject()) {
            attributes[index++] = member.getName();
            attributes[index++] = member.getValue().asString();
        }
        endpointContext = new MapBasedEndpointContext(socketAddress, principal, attributes);
    }
    return endpointContext;
}
Also used : AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) InetSocketAddress(java.net.InetSocketAddress) PublicKey(java.security.PublicKey) RawPublicKeyIdentity(org.eclipse.californium.elements.auth.RawPublicKeyIdentity) JsonValue(com.eclipsesource.json.JsonValue) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) X500Principal(javax.security.auth.x500.X500Principal) PreSharedKeyIdentity(org.eclipse.californium.elements.auth.PreSharedKeyIdentity) Member(com.eclipsesource.json.JsonObject.Member) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Example 7 with EndpointContext

use of org.eclipse.californium.elements.EndpointContext in project leshan by eclipse.

the class ObservationSerDes method deserialize.

public static Observation deserialize(byte[] data) {
    JsonObject v = (JsonObject) Json.parse(new String(data));
    EndpointContext endpointContext = EndpointContextSerDes.deserialize(v.get("peer").asObject());
    byte[] req = Hex.decodeHex(v.getString("request", null).toCharArray());
    RawData rawData = RawData.outbound(req, endpointContext, null, false);
    Request request = (Request) parser.parseMessage(rawData);
    request.setDestinationContext(endpointContext);
    JsonValue ctxValue = v.get("context");
    if (ctxValue != null) {
        Map<String, String> context = new HashMap<>();
        JsonObject ctxObject = (JsonObject) ctxValue;
        for (String name : ctxObject.names()) {
            context.put(name, ctxObject.getString(name, null));
        }
        request.setUserContext(context);
    }
    return new Observation(request, endpointContext);
}
Also used : RawData(org.eclipse.californium.elements.RawData) EndpointContext(org.eclipse.californium.elements.EndpointContext) HashMap(java.util.HashMap) Request(org.eclipse.californium.core.coap.Request) JsonValue(com.eclipsesource.json.JsonValue) Observation(org.eclipse.californium.core.observe.Observation) JsonObject(com.eclipsesource.json.JsonObject)

Aggregations

EndpointContext (org.eclipse.californium.elements.EndpointContext)7 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)3 JsonValue (com.eclipsesource.json.JsonValue)2 RawData (org.eclipse.californium.elements.RawData)2 JsonObject (com.eclipsesource.json.JsonObject)1 Member (com.eclipsesource.json.JsonObject.Member)1 InetSocketAddress (java.net.InetSocketAddress)1 Principal (java.security.Principal)1 PublicKey (java.security.PublicKey)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 HashMap (java.util.HashMap)1 X500Principal (javax.security.auth.x500.X500Principal)1 OptionSet (org.eclipse.californium.core.coap.OptionSet)1 Request (org.eclipse.californium.core.coap.Request)1 Response (org.eclipse.californium.core.coap.Response)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Endpoint (org.eclipse.californium.core.network.Endpoint)1 UdpDataSerializer (org.eclipse.californium.core.network.serialization.UdpDataSerializer)1 Observation (org.eclipse.californium.core.observe.Observation)1 EndpointMismatchException (org.eclipse.californium.elements.EndpointMismatchException)1