//*** Other ***// /// <summary> /// Adds known parameters to the fullSync query. /// </summary> /// <param name="query"></param> /// <param name="parameters"></param> private static void AddParametersToQuery(Query query, IDictionary <string, object> parameters) { foreach (KeyValuePair <string, object> parameter in parameters) { FullSyncRequestParameter fullSyncRequestParameter = FullSyncRequestParameter.GetFullSyncRequestParameter(parameter.Key); // If the parameter corresponds to a FullSyncRequestParameter if (fullSyncRequestParameter != null) { object parameterValue = parameter.Value; Type type = fullSyncRequestParameter.type; if (type != typeof(object) && parameterValue is string) { parameterValue = C8oTranslator.StringToObject(parameterValue as string, type); } //object objectParameterValue = parameterValue; //if (parameterValue is String) //{ // // Passer par une fonction ??,, // objectParameterValue = JsonConvert.DeserializeObject(parameterValue as String, fullSyncRequestParameter.type); //} // fullSyncRequestParameter.AddToQuery(query, objectParameterValue); C8oFullSyncCblEnum.AddToQuery(query, fullSyncRequestParameter, parameterValue); } } }
internal static void SetReplication(FullSyncReplicationParameter replicationParameter, Replication replication, object value) { Init(); // Checks if the value type is String and the request parameter type is not if (typeof(string).IsAssignableFrom(value.GetType()) && !typeof(string).IsAssignableFrom(replicationParameter.type)) { try { // Tries to convert the string to the request parameter type value = C8oTranslator.StringToObject(value as string, replicationParameter.type); } catch (Exception e) { throw new C8oException(C8oExceptionMessage.ParseStringToObject(replicationParameter.type), e); } } // Checks if the type is valid if (replicationParameter.type.IsAssignableFrom(value.GetType())) { // No reasons to fail Action <Replication, object> setReplicationOp = null; if (fullSyncReplicationParameters.TryGetValue(replicationParameter, out setReplicationOp)) { setReplicationOp(replication, value); } } else { throw new ArgumentException(C8oExceptionMessage.InvalidArgumentInvalidParameterType(replicationParameter.name, "" + replicationParameter.type, "" + value.GetType())); } }
public static object GetParameterJsonValue(KeyValuePair <string, object> parameter) { var obj = parameter.Value; if (obj is JValue || obj is string) { return(obj); } var str = JsonConvert.SerializeObject(obj); return(C8oTranslator.StringToJson(str)); }
public static XDocument FullSyncJsonToXml(JObject json) { var xmlDocument = new XDocument(); // Create the root element node var rootElement = new XElement(XML_KEY_DOCUMENT); xmlDocument.Add(rootElement); var couchdb_output = new XElement(XML_KEY_COUCHDB_OUTPUT); // Translates the JSON document C8oTranslator.JsonToXml(json, couchdb_output); rootElement.Add(couchdb_output); return(xmlDocument); }
public static bool TryGetParameterObjectValue <T>(IDictionary <string, object> parameters, string name, out T value, bool useName = false, T defaultValue = default(T)) { KeyValuePair <string, object> parameter = GetParameter(parameters, name, useName); if (parameter.Key != null && parameter.Value != null) { if (parameter.Value is string && typeof(T) != typeof(string)) { value = (T)C8oTranslator.StringToObject(parameter.Value as string, typeof(T)); } else { value = (T)parameter.Value; } return(true); } value = defaultValue; return(false); }
async private Task <object> HandleRequest() { bool isFullSyncRequest = C8oFullSync.IsFullSyncRequest(parameters); if (isFullSyncRequest) { c8o.Log._Debug("Is FullSync request"); var liveid = C8oUtils.GetParameterStringValue(parameters, C8o.FS_LIVE, false); if (liveid != null) { var dbName = C8oUtils.GetParameterStringValue(parameters, C8o.ENGINE_PARAMETER_PROJECT, true).Substring(C8oFullSync.FULL_SYNC_PROJECT.Length); c8o.AddLive(liveid, dbName, this); } // The result cannot be handled here because it can be different depending to the platform // But it can be useful bor debug try { var fullSyncResult = await c8o.c8oFullSync.HandleFullSyncRequest(parameters, c8oResponseListener); return(fullSyncResult); } catch (C8oException e) { throw e; } catch (Exception e) { throw new C8oException(C8oExceptionMessage.FullSyncRequestFail(), e); } } else { string responseType; if (c8oResponseListener == null || c8oResponseListener is C8oResponseXmlListener) { responseType = C8o.RESPONSE_TYPE_XML; } else if (c8oResponseListener is C8oResponseJsonListener) { responseType = C8o.RESPONSE_TYPE_JSON; } else { return(new C8oException("wrong listener")); } //*** Local cache ***// string c8oCallRequestIdentifier = null; // Allows to enable or disable the local cache on a Convertigo requestable, default value is true C8oLocalCache localCache = C8oUtils.GetParameterObjectValue(parameters, C8oLocalCache.PARAM, false) as C8oLocalCache; bool localCacheEnabled = false; // If the engine parameter for local cache is specified if (localCache != null) { // Removes local cache parameters and build the c8o call request identifier parameters.Remove(C8oLocalCache.PARAM); if (localCacheEnabled = localCache.enabled) { c8oCallRequestIdentifier = C8oUtils.IdentifyC8oCallRequest(parameters, responseType); if (localCache.priority.IsAvailable(c8o)) { try { C8oLocalCacheResponse localCacheResponse = await c8o.c8oFullSync.GetResponseFromLocalCache(c8oCallRequestIdentifier); if (!localCacheResponse.Expired) { if (responseType == C8o.RESPONSE_TYPE_XML) { return(C8oTranslator.StringToXml(localCacheResponse.Response)); } else if (responseType == C8o.RESPONSE_TYPE_JSON) { return(C8oTranslator.StringToJson(localCacheResponse.Response)); } } } catch (C8oUnavailableLocalCacheException) { // no entry } } } } //*** Get response ***// parameters[C8o.ENGINE_PARAMETER_DEVICE_UUID] = c8o.DeviceUUID; // Build the c8o call URL c8oCallUrl = c8o.Endpoint + "/." + responseType; HttpWebResponse httpResponse = null; Exception exception = null; try { httpResponse = await c8o.httpInterface.HandleC8oCallRequest(c8oCallUrl, parameters); } catch (Exception e) { exception = e; } if (exception != null) { if (localCacheEnabled) { try { C8oLocalCacheResponse localCacheResponse = await c8o.c8oFullSync.GetResponseFromLocalCache(c8oCallRequestIdentifier); if (!localCacheResponse.Expired) { if (responseType == C8o.RESPONSE_TYPE_XML) { return(C8oTranslator.StringToXml(localCacheResponse.Response)); } else if (responseType == C8o.RESPONSE_TYPE_JSON) { return(C8oTranslator.StringToJson(localCacheResponse.Response)); } } } catch (C8oUnavailableLocalCacheException) { // no entry } } return(new C8oException(C8oExceptionMessage.handleC8oCallRequest(), exception)); } var responseStream = httpResponse.GetResponseStream(); object response; string responseString = null; if (c8oResponseListener is C8oResponseXmlListener) { response = C8oTranslator.StreamToXml(responseStream); if (localCacheEnabled) { responseString = C8oTranslator.XmlToString(response as XDocument); } } else if (c8oResponseListener is C8oResponseJsonListener) { responseString = C8oTranslator.StreamToString(responseStream); response = C8oTranslator.StringToJson(responseString); } else { return(new C8oException("wrong listener")); } if (localCacheEnabled) { // String responseString = C8oTranslator.StreamToString(responseStream); long expirationDate = -1; if (localCache.ttl > 0) { expirationDate = localCache.ttl + C8oUtils.GetUnixEpochTime(DateTime.Now); } var localCacheResponse = new C8oLocalCacheResponse(responseString, responseType, expirationDate); await c8o.c8oFullSync.SaveResponseToLocalCache(c8oCallRequestIdentifier, localCacheResponse); } return(response); } }
public static string DoubleToHexString(double d) { byte[] bytes = BitConverter.GetBytes(d); return(C8oTranslator.ByteArrayToHexString(bytes)); }