Basic help to write a Java Servlet

First of all, I am (unfortunately) not a professional programmer, that’s why I am asking for some basic help as startup.
I want to write a server side script in Java to receive URL-Requests from IoT devices and perform a post request to OH rest API to send commands to an item.
Those IoT devices do not support http/post request.
For this I have written a javascript (see here) but then I found out, that some IoT devices do not support javascript - that’s why I want to write a servlet.
Basically a small replacement of the BasicUI CMD servlet.

My questions now are where and how to start.
Ideally I just write the java code and embed it so that the code runs when a specific website is opened without the requirement to install a couple of professional programmer’s development tools.

Does anybody have a good starting point or experiences you’d like to share?

Many thanks in advance

Seems you found your solution else where. Just in case if you are curious how shortest way looks a like see code below. Address to call is /cmd?item1=ON&item2=OFF&item3=10.3, so it allows to produce batch requests.

package foo;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.ItemCommandEvent;
import org.openhab.core.items.events.ItemEventFactory;
import org.openhab.core.types.Command;
import org.openhab.core.types.TypeParser;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.http.HttpService;

@Component
public class CommandServlet extends HttpServlet {

  private final HttpService httpService;
  private final ItemRegistry registry;
  private final EventPublisher eventPublisher;

  @Activate
  public CommandServlet(@Reference HttpService httpService, @Reference ItemRegistry registry, @Reference EventPublisher eventPublisher) throws Exception {
    this.httpService = httpService;
    this.registry = registry;
    this.eventPublisher = eventPublisher;

    httpService.registerServlet("/cmd", this, new Hashtable<>(), httpService.createDefaultHttpContext());
  }

  @Deactivate
  void deactivate() {
    httpService.unregister("/cmd");
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Enumeration<String> params = req.getParameterNames();
    while (params.hasMoreElements()) {
      String item = params.nextElement();
      String cmd = req.getParameter(item);

      Item itemObj = registry.get(item);
      if (itemObj instanceof GenericItem) {
        Command command = TypeParser.parseCommand(itemObj.getAcceptedCommandTypes(), cmd);
        ItemCommandEvent event = ItemEventFactory.createCommandEvent(item, command);
        eventPublisher.post(event);
      }
    }

    resp.setStatus(HttpServletResponse.SC_OK);
  }

}

Hi Lukasz,
yes Piotr has developed a nice http listener which is a good solution.
However, I wanted to start coding something on my own in java and - who knows - maybe one day a binding (most probably not within the next five years :slight_smile: ). So, I wanted to pursue this definitly further.

First of all, many thanks for your code.
On a high level, what are the next steps (compiling) and where do I have to copy the file?

Thank you very much in advance

You need to follow developer guide and install necessary dev tools (Java, IDE or text editor + maven). Once your tools are installed properly you can run binding skeleton script you can find in openhab addons repository. There you will get everything needed to attempt first compilation.

1 Like