Example #1
0
        /// <summary>
        /// Sets the parameters of the incoming http response from a CoAP response.
	    /// The status code is mapped through the properties file and is set through
	    /// the StatusLine. The options are translated to the corresponding headers
	    /// and the max-age (in the header cache-control) is set to the default value
	    /// (60 seconds) if not already present. If the request method was not HEAD
	    /// and the coap response has a payload, the entity and the content-type are
        /// set in the http response.
        /// </summary>
        public static void GetHttpResponse(IHttpRequest httpRequest, Response coapResponse, IHttpResponse httpResponse)
        {
            if (httpRequest == null)
                throw ThrowHelper.ArgumentNull("httpRequest");
            if (coapResponse == null)
                throw ThrowHelper.ArgumentNull("coapResponse");
            if (httpResponse == null)
                throw ThrowHelper.ArgumentNull("httpResponse");

            HttpStatusCode httpCode;

            if (!coap2httpCode.TryGetValue(coapResponse.StatusCode, out httpCode))
                throw ThrowHelper.TranslationException("Cannot convert the coap code in http status code: " + coapResponse.StatusCode);

            httpResponse.StatusCode = (Int32)httpCode;

            NameValueCollection nvc = GetHttpHeaders(coapResponse.GetOptions());
            // set max-age if not already set
            if (nvc["cache-control"] == null)
                nvc.Set("cache-control", "max-age=" + CoapConstants.DefaultMaxAge);

            foreach (String key in nvc.Keys)
            {
                httpResponse.AppendHeader(key, nvc[key]);
            }

            Byte[] payload = coapResponse.Payload;
            if (payload != null)
            {
                httpResponse.OutputStream.Write(payload, 0, payload.Length);
                String contentType;
                if (coap2httpContentType.TryGetValue(coapResponse.ContentType, out contentType))
                    httpResponse.AppendHeader("content-type", contentType);
            }
        }