Skip to content

Commit

Permalink
Link.java cleaning : add some javadocs + rewrite serialize method.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Apr 17, 2019
1 parent 965d0e1 commit 8042fb4
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions leshan-core/src/main/java/org/eclipse/leshan/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.leshan;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -27,15 +28,24 @@
/**
* A Link as defined in http://tools.ietf.org/html/rfc6690.
*/
// TODO we should have a look at org.eclipse.californium.core.coap.LinkFormat
// TODO this class has several problems :
// 1) Parsing is too permissive...
// e.g. it should not accept URI-Reference which are not between < >.
// e.g URI-reference should be parsed to respect RFC3986.
//
// 2) we should have Parser/Serializer class instead of static method to allow custom implementation.
// 3) attributes is not well named, it does not respect rfc naming : url => uriRef
//
// Maybe we could look at existing implementation :
// https://github.com/google/coapblaster/blob/master/src/main/java/com/google/iot/coap/LinkFormat.java
// https://github.com/eclipse/californium/blob/2.0.x/californium-core/src/main/java/org/eclipse/californium/core/coap/LinkFormat.java
// https://github.com/ARMmbed/java-coap/blob/master/coap-core/src/main/java/com/mbed/coap/linkformat/LinkFormat.java
public class Link implements Serializable {

private static final long serialVersionUID = 1L;

private final String url;

// TODO a map from String to Object could be a problem with serialization.
// TODO we are not able to make difference between string and quoted string.
private final Map<String, Object> attributes;

/**
Expand Down Expand Up @@ -100,6 +110,12 @@ public String toString() {
return builder.toString();
}

/**
* Parse a byte arrays representation of a {@code String} encoding with UTF_8 {@link Charset}.
*
* @param a byte arrays representing {@code String} encoding with UTF_8 {@link Charset}.
* @return an array of {@code Link}
*/
public static Link[] parse(byte[] content) {
if (content == null) {
return new Link[] {};
Expand Down Expand Up @@ -143,22 +159,25 @@ public static Link[] parse(byte[] content) {
return linksResult;
}

public static final String INVALID_LINK_PAYLOAD = "<>";
private static final String TRAILER = ", ";

/***
* Serialize severals {@code Link} to {@code String} as defined in http://tools.ietf.org/html/rfc6690.
*
* @param linkObjects links to serialize.
*
* @return a {@code String} representation like defined in http://tools.ietf.org/html/rfc6690. If LinkObjects is
* empty return an empty {@code String};
*/
public static String serialize(Link... linkObjects) {
try {
StringBuilder builder = new StringBuilder();
for (Link link : linkObjects) {
builder.append(link.toString()).append(TRAILER);
StringBuilder builder = new StringBuilder();
if (linkObjects.length != 0) {
builder.append(linkObjects[0].toString());
for (int i = 1; i < linkObjects.length; i++) {
builder.append(TRAILER).append(linkObjects[i].toString());
}

builder.delete(builder.length() - TRAILER.length(), builder.length());

return builder.toString();
} catch (Exception e) {
return null;
}
return builder.toString();
}

@Override
Expand Down Expand Up @@ -191,4 +210,4 @@ public boolean equals(Object obj) {
return false;
return true;
}
}
}

0 comments on commit 8042fb4

Please sign in to comment.