Skip to content

Commit

Permalink
Use EnumSet<Binding> for BootstrapConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 6, 2020
1 parent 6797352 commit 2f75190
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.util.EnumSet;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
import org.eclipse.leshan.server.bootstrap.EditableBootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.InMemoryBootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.InvalidConfigurationException;
import org.eclipse.leshan.server.bootstrap.demo.json.BindingModeTypeAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -69,6 +72,9 @@ public JSONFileBootstrapStore(String filename) {
Validate.notEmpty(filename);
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
builder.registerTypeAdapter(new TypeToken<EnumSet<BindingMode>>() {
}.getType(), new BindingModeTypeAdapter());

this.gson = builder.create();
this.gsonType = new TypeToken<Map<String, BootstrapConfig>>() {
}.getType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2020 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.bootstrap.demo.json;

import java.io.IOException;
import java.util.EnumSet;

import org.eclipse.leshan.core.request.BindingMode;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

public class BindingModeTypeAdapter extends TypeAdapter<EnumSet<BindingMode>> {

@Override
public void write(JsonWriter out, EnumSet<BindingMode> value) throws IOException {
out.value(BindingMode.toString(value));
}

@Override
public EnumSet<BindingMode> read(JsonReader in) throws IOException {
return BindingMode.parse(in.nextString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
import org.eclipse.leshan.server.bootstrap.EditableBootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.InvalidConfigurationException;
import org.eclipse.leshan.server.bootstrap.demo.json.BindingModeTypeAdapter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -41,6 +44,7 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

/**
* Servlet for REST API in charge of adding bootstrap information to the bootstrap server.
Expand Down Expand Up @@ -70,8 +74,10 @@ public JsonElement serialize(Byte src, Type typeOfSrc, JsonSerializationContext
public BootstrapServlet(EditableBootstrapConfigStore bsStore) {
this.bsStore = bsStore;

this.gson = new GsonBuilder().registerTypeHierarchyAdapter(Byte.class, new SignedByteUnsignedByteAdapter())
.create();
this.gson = new GsonBuilder()//
.registerTypeAdapter(new TypeToken<EnumSet<BindingMode>>() {
}.getType(), new BindingModeTypeAdapter()) //
.registerTypeHierarchyAdapter(Byte.class, new SignedByteUnsignedByteAdapter()).create();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static class ServerConfig implements Serializable {
* This Resource defines the transport binding configured for the LwM2M Client. If the LwM2M Client supports the
* binding specified in this Resource, the LwM2M Client MUST use that transport for the Current Binding Mode.
*/
public BindingMode binding = BindingMode.U;
public EnumSet<BindingMode> binding = EnumSet.of(BindingMode.U);

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.core.request.BootstrapDeleteRequest;
import org.eclipse.leshan.core.request.BootstrapDownlinkRequest;
import org.eclipse.leshan.core.request.BootstrapWriteRequest;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static LwM2mObjectInstance toServerInstance(int instanceId, ServerConfig
resources.add(LwM2mSingleResource.newIntegerResource(5, serverConfig.disableTimeout));
resources.add(LwM2mSingleResource.newBooleanResource(6, serverConfig.notifIfDisabled));
if (serverConfig.binding != null)
resources.add(LwM2mSingleResource.newStringResource(7, serverConfig.binding.name()));
resources.add(LwM2mSingleResource.newStringResource(7, BindingMode.toString(serverConfig.binding)));

return new LwM2mObjectInstance(instanceId, resources);
}
Expand Down Expand Up @@ -121,13 +122,12 @@ public static BootstrapWriteRequest toWriteRequest(int instanceId, ACLConfig acl
return new BootstrapWriteRequest(path, securityInstance, contentFormat);
}

public static List<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(
BootstrapConfig bootstrapConfig) {
public static List<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(BootstrapConfig bootstrapConfig) {
return toRequests(bootstrapConfig, ContentFormat.TLV);
}

public static List<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(
BootstrapConfig bootstrapConfig, ContentFormat contentFormat) {
public static List<BootstrapDownlinkRequest<? extends LwM2mResponse>> toRequests(BootstrapConfig bootstrapConfig,
ContentFormat contentFormat) {
List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requests = new ArrayList<>();
// handle delete
for (String path : bootstrapConfig.toDelete) {
Expand Down

0 comments on commit 2f75190

Please sign in to comment.