/// <summary> /// Get the query parameter /// </summary> /// <param name="paramKey">The parameter key</param> /// <returns>string if key found else null</returns> public string GetQueryParam(string paramKey) { String qString = null; if (paramKey == null || paramKey.Trim().Length == 0) { return(null); } foreach (CoAPHeaderOption headerOption in this.Options) { if (headerOption.Number == CoAPHeaderOption.URI_QUERY) { qString = AbstractByteUtils.ByteToStringUTF8(headerOption.Value); if (qString.IndexOf("=") > 0) { string[] qpParts = qString.Split(new char[] { '=' }); //index 0 will have key and 1 will have value if (paramKey.Trim().ToLower() == qpParts[0].Trim().ToLower()) { return(AbstractURIUtils.UrlDecode(qpParts[1])); } } else if (qString.Trim().ToLower() == paramKey.Trim().ToLower()) { return("");//Only key no value } } } return(null); }
/// <summary> /// Check if the given resource is being observed or not /// </summary> /// <param name="observableResourceURL">A URL indicating an observable resource</param> /// <returns>bool</returns> public bool IsResourceBeingObserved(string observableResourceURL) { if (observableResourceURL == null || observableResourceURL.Trim().Length == 0) { throw new ArgumentNullException("Observable resource cannot be NULL or an empty string"); } if (!AbstractURIUtils.IsValidFullUri(observableResourceURL)) { throw new ArgumentException("URL does not seem right. Must be a fully-qualified URL"); } bool isResourceObserved = false; this._observableListSync.WaitOne(); foreach (string key in this._observers.Keys) { if (key == observableResourceURL.Trim().ToLower()) { isResourceObserved = true; break; } } this._observableListSync.Set(); return(isResourceObserved); }
/// <summary> /// Add an observable resource to the list /// </summary> /// <param name="observableResourceURL">A fully-qualified URL indicating an observable resource</param> public void AddObservableResource(string observableResourceURL) { if (observableResourceURL == null || observableResourceURL.Trim().Length == 0) { throw new ArgumentNullException("Observable resource cannot be NULL or an empty string"); } if (!AbstractURIUtils.IsValidFullUri(observableResourceURL)) { throw new ArgumentException("URL does not seem right. Must be a fully-qualified URL"); } this._observableListSync.WaitOne(); if (!this._observers.Contains(observableResourceURL.Trim().ToLower())) { this._observers.Add(observableResourceURL.Trim().ToLower(), new ArrayList()); } this._observableListSync.Set(); }
/// <summary> /// Set the location URL. This is set by the response to indicate "Created" result if the /// request is POST (to create a new resource) /// </summary> /// <param name="locationURL">The location URL relative to the URI that got created</param> public void SetLocation(string locationURL) { if (locationURL == null || locationURL.Trim().Length == 0) { throw new ArgumentException("Invalid CoAP location URL"); } locationURL = locationURL.Trim().ToLower(); if (locationURL.IndexOf("#") >= 0) { throw new ArgumentException("Fragments not allowed in CoAP location URL"); } //Add these items as option //Path components string[] segments = AbstractURIUtils.GetUriSegments(locationURL); if (segments != null && segments.Length > 0) { foreach (string segment in segments) { if (segment.Trim().Length == 0) { continue; } this.Options.AddOption(CoAPHeaderOption.LOCATION_PATH, AbstractByteUtils.StringToByteUTF8(AbstractURIUtils.UrlDecode(segment))); } } //Query string[] qParams = AbstractURIUtils.GetQueryParameters(locationURL); if (qParams != null && qParams.Length > 0) { foreach (string queryComponent in qParams) { if (queryComponent.Trim().Length == 0) { continue; } this.Options.AddOption(CoAPHeaderOption.LOCATION_QUERY, AbstractByteUtils.StringToByteUTF8(AbstractURIUtils.UrlDecode(queryComponent))); } } }
/// <summary> /// Set the request URL /// </summary> /// <param name="coapURL">The coap URL associated with the request</param> public void SetURL(string coapURL) { if (coapURL == null || coapURL.Trim().Length == 0) { throw new ArgumentNullException("Invalid CoAP URL"); } coapURL = coapURL.Trim(); /* * The URI object provided by NETMF does not work if the scheme is not http/https * Therefore, after checking for the scheme, we replace it with http * and then use the Uri class for other items */ string scheme = AbstractURIUtils.GetUriScheme(coapURL); if (scheme == null || (scheme.Trim().ToLower() != "coap" && scheme.Trim().ToLower() != "coaps")) { throw new ArgumentException("Invalid CoAP URL"); } if (scheme.Trim().ToLower() == "coaps") { this.IsSecure = true; } if (coapURL.IndexOf("#") >= 0) { throw new ArgumentException("Fragments not allowed in CoAP URL"); } //Add these items as option //The host this.Options.AddOption(CoAPHeaderOption.URI_HOST, AbstractByteUtils.StringToByteUTF8(AbstractURIUtils.GetUriHost(coapURL))); //The port this.Options.AddOption(CoAPHeaderOption.URI_PORT, AbstractByteUtils.GetBytes((UInt16)AbstractURIUtils.GetUriPort(coapURL))); //Path components string[] segments = AbstractURIUtils.GetUriSegments(coapURL); if (segments != null && segments.Length > 0) { foreach (string segment in segments) { if (segment.Trim().Length == 0) { continue; } this.Options.AddOption(CoAPHeaderOption.URI_PATH, AbstractByteUtils.StringToByteUTF8(AbstractURIUtils.UrlDecode(segment))); } } //Query string[] qParams = AbstractURIUtils.GetQueryParameters(coapURL); if (qParams != null && qParams.Length > 0) { foreach (string queryComponent in qParams) { if (queryComponent.Trim().Length == 0) { continue; } this.Options.AddOption(CoAPHeaderOption.URI_QUERY, AbstractByteUtils.StringToByteUTF8(AbstractURIUtils.UrlDecode(queryComponent))); } } }