Commit 29ff6733 authored by Björn Butzin's avatar Björn Butzin
Browse files

Added first readme for jCoAP Proxy; added compiled jars; fixed accept option...

Added first readme for jCoAP Proxy; added compiled jars; fixed accept option issue, fixed statistic resource implementation
parent 2c2b5ddf
# jCoAP Proxy
Welcome to the jCoAP Proxy
This implementation provides a forward proxy between CoAP and HTTP.
I supports:
* HTTP to CoAP translation and caching
* CoAP to HTTP translation and caching and
* CoAP to CoAP caching
As forward proxy the client needs to be aware of the proxy:
The HTTP to CoAP Proxy functionality is used by a protocol-agnostic access.
This means, that the HTTP client is not aware of translation to CoAP.
The URI specified in the HTTP request is assumed as URI to the CoAP server that will be acessed by the CoAP client of the Proxy.
In order to send HTTP requests from a web-browser to the jCoAP proxy make sure to configure the proxy in your browsers settings.
The HTTP proxy server is started on Port 8080 (HTTP default port).
| jCoAP Proxy |
| |
+-------------+ +-------------+ +-------+ +-------------+ +-------------+
| HTTP Client |---->| HTTP Server |---->| Cache |---->| CoAP Client |---->| CoAP Server |
+-------------+ +-------------+ +-------+ +-------------+ +-------------+
| |
As CoAP was designed with proxies in mind CoAP messages can contain a Proxy-URI option.
The Proxy-URI option is mandatory and should either start with 'http://' or 'coap://' to select the target protocol.
Notice that https:// and coaps:// are not supported as security is not implemented yet.
As example the Proxy-URI '' will cause the proxy to fetch the 'wanted' resource using HTTP, while the Proxy-URI 'coap://' will cause the proxy to fetch the 'wanted' resource using CoAP
The CoAP proxy server is started on port 5683 (CoAP default port)
| jCoAP Proxy |
| |
+-------------+ +-------------+ +-------+ +-------------+ +-------------+
| CoAP Client |---->| CoAP Server |---->| |---->| CoAP Client |---->| CoAP Server |
+-------------+ +-------------+ | | +-------------+ +-------------+
| | Cache | |
| | | +-------------+ +-------------+
| | |---->| HTTP Client |---->| HTTP Server |
| +-------+ +-------------+ +-------------+
| |
Aditionally the proxy starts a second CoAP server on port 5684 offering the '/statistic' resource.
A closer look on the proxy implementation can be foud in:
Christian Lerche, Nico Laum, Frank Golatowski, Christoph Niedermeier, Dirk Timmermann:
[Connecting the Web with the Web of Things: Lessons Learned From Implementing a CoAP-HTTP Proxy](
Proceedings of the IoTech Workshop 2012, Las Vegas, USA, Oktober 2012
# Latest Releases
The latest releases can be downloadede here: [ws4d-jcoap-applications/release](
# Build using Maven
You need to have a working maven installation to build jCoAP.
Then simply run the following from the project's root directory:
$ mvn clean install
Executable JARs including all dependencies can be found in the `target\` folder afterwards.
# Usage in Maven Projects
jCoAP and its projects are currently not uploaded to any Maven repository!
Thus you need to build jcoap projects once on your local machine (see "Build using Maven") before you can use them.
During build, the binary will be put to the local maven repository on your machine.
To use jCoAP proxy as a library in your projects, add the following dependency to your `pom.xml`:
# Eclipse
The project can be easily imported into a recent version of the Eclipse IDE.
Make sure to have the following before importing:
* [Eclipse EGit]( (should be the case with every recent Eclipse version)
* [m2e - Maven Integration for Eclipse]( (should be the case with every recent Eclipse version)
* UTF-8 workspace text file encoding (Preferences &raquo; General &raquo; Workspace)
Then choose *[Import... &raquo; Maven &raquo; Existing Maven Projects]* to import `jCoAP projects` into Eclipse.
To run the build process from Eclipse select a project then right click *[Run As &raquo; Maven install]*
# IntelliJ
The project can also be imported to IntelliJ as follows:
In IntelliJ, choose *[File.. &raquo; Open]* then select the location of the cloned repository in your filesystem.
IntelliJ will then automatically import all projects and resolve required Maven dependencies.
# License
jCoAP is licensed under [Apache License, Version 2.0](./license.txt) see `license.txt`
# Third Party Software
jCoAP uses the folowing third party components
* [log4j]( - [Apache License - v 2.0](
* [jUnit]( - [Eclipse Public License - v 1.0](
\ No newline at end of file
......@@ -360,7 +360,10 @@ public class ProxyMapper {
Header[] headers = httpRequest.getHeaders("Accept");
if (headers.length > 0) {
for (int i = 0; i < headers.length; i++) {
httpMediaType2coapMediaType(headers[i].getValue(), coapRequest);
String[] acceptFormats = headers[i].getValue().split(",");
for(int j=0; j<acceptFormats.length; j++) {
httpMediaType2coapMediaType(acceptFormats[j], coapRequest);
......@@ -477,9 +480,12 @@ public class ProxyMapper {
// pass-through the payload, if we do not answer a head-request
if (!context.isHttpHeadMethod()) {
NStringEntity entity;
entity = new NStringEntity(new String(coapResponse.getPayload()), "UTF-8");
if(null != coapResponse.getPayload()) {
entity = new NStringEntity(new String(coapResponse.getPayload()), "UTF-8");
} else if (requestMethod.toLowerCase().contains("put")) {
setHttpMsgCode(coapResponse, "put", httpResponse);
......@@ -771,7 +777,7 @@ public class ProxyMapper {
String[] type_subtype = mediatype.split(",");
for (String value : type_subtype) {
if (value.toLowerCase().contains("text") && value.toLowerCase().contains("plain")) {
if (value.toLowerCase().contains("text")) {/// && value.toLowerCase().contains("plain")) {
// value is for example "application/xml;q=0.9"
} else if (value.toLowerCase().contains("application")) {
package org.ws4d.coap.proxy;
import java.util.List;
import java.util.Locale;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ws4d.coap.core.CoapConstants;
import org.ws4d.coap.core.enumerations.CoapMediaType;
public class ProxyRestInterface {
......@@ -30,14 +34,21 @@ public class ProxyRestInterface {
super("/statistic", new byte[0], CoapMediaType.text_plain);
public byte[] getValue() {
public synchronized CoapData get(List<String> query, List<CoapMediaType> mediaTypesAccepted) {
return get(mediaTypesAccepted);
public synchronized CoapData get(List<CoapMediaType> mediaTypesAccepted) {
StringBuilder val = new StringBuilder();
val.append("Number of HTTP Requests: " + ProxyMapper.getInstance().getHttpRequestCount() + "\n");
val.append("Number of CoAP Requests: " + ProxyMapper.getInstance().getCoapRequestCount() + "\n");
val.append("Number of Reqeusts served from cache: " + ProxyMapper.getInstance().getServedFromCacheCount()
+ "\n");
return val.toString().getBytes();
return new CoapData(val.toString().getBytes(), CoapMediaType.text_plain) ;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment