throw new TranslationException("Cannot convert the http method in coap method", e);
}
// create the request
// Request coapRequest = Request.getRequestForMethod(coapMethod);
Request coapRequest = new Request(Code.valueOf(coapMethod));
// get the uri
String uriString = httpRequest.getRequestLine().getUri();
// remove the initial "/"
uriString = uriString.substring(1);
// decode the uri to translate the application/x-www-form-urlencoded
// format
try {
uriString = URLDecoder.decode(uriString, "UTF-8");
} catch (UnsupportedEncodingException e) {
LOGGER.warning("Failed to decode the uri: " + e.getMessage());
throw new TranslationException("Failed decoding the uri: " + e.getMessage());
} catch (Throwable e) {
LOGGER.warning("Malformed uri: " + e.getMessage());
throw new InvalidFieldException("Malformed uri: " + e.getMessage());
}
// if the uri contains the proxy resource name, the request should be
// forwarded and it is needed to get the real requested coap server's
// uri
// e.g.:
// /proxy/vslab-dhcp-17.inf.ethz.ch:5684/helloWorld
// proxy resource: /proxy
// coap server: vslab-dhcp-17.inf.ethz.ch:5684
// coap resource: helloWorld
if (uriString.matches(".?" + proxyResource + ".*")) {
// find the first occurrence of the proxy resource
int index = uriString.indexOf(proxyResource);
// delete the slash
index = uriString.indexOf('/', index);
uriString = uriString.substring(index + 1);
if (proxyingEnabled) {
// if the uri hasn't the indication of the scheme, add it
if (!uriString.matches("^coap://.*")) {
uriString = "coap://" + uriString;
}
// the uri will be set as a proxy-uri option
// set the proxy-uri option to allow the lower layers to underes
// Option proxyUriOption = new Option(uriString, OptionRegistry.PROXY_URI);
// coapRequest.addOption(proxyUriOption);
coapRequest.getOptions().setProxyURI(uriString);
} else {
coapRequest.setURI(uriString);
}
// set the proxy as the sender to receive the response correctly
try {
// TODO check with multihomed hosts
InetAddress localHostAddress = InetAddress.getLocalHost();
// InetSocketAddress localHostEndpoint = new InetSocketAddress(localHostAddress);
// coapRequest.setPeerAddress(localHostEndpoint);
coapRequest.setDestination(localHostAddress);
// TODO: setDestinationPort???
} catch (UnknownHostException e) {
LOGGER.warning("Cannot get the localhost address: " + e.getMessage());
throw new TranslationException("Cannot get the localhost address: " + e.getMessage());
}
} else {
// if the uri does not contains the proxy resource, it means the
// request is local to the proxy and it shouldn't be forwarded
// set the uri string as uri-path option
// Option uriPathOption = new Option(uriString, OptionRegistry.URI_PATH);
// coapRequest.setOption(uriPathOption);
coapRequest.getOptions().setURIPath(uriString);
}
// translate the http headers in coap options
List<Option> coapOptions = getCoapOptions(httpRequest.getAllHeaders());
// coapRequest.setOptions(coapOptions);
for (Option option:coapOptions)
coapRequest.getOptions().addOption(option);
// set the payload if the http entity is present
if (httpRequest instanceof HttpEntityEnclosingRequest) {
HttpEntity httpEntity = ((HttpEntityEnclosingRequest) httpRequest).getEntity();
// translate the http entity in coap payload
byte[] payload = getCoapPayload(httpEntity);
coapRequest.setPayload(payload);
// set the content-type
int coapContentType = getCoapMediaType(httpRequest);
coapRequest.getOptions().setContentFormat(coapContentType);
}
return coapRequest;
}