public IOpenSearchable Create(OpenSearchUrl url) { if ( soft) return new SoftGenericOpenSearchable(url, ose); return new GenericOpenSearchable(url, ose); }
/// <summary> /// Finds the open search description. /// </summary> /// <returns>The open search description.</returns> /// <param name="baseUrl">Base URL.</param> public static IOpenSearchable FindOpenSearchable(OpenSearchEngine ose, Uri baseUrl, string mimeType = null, bool soft = false) { OpenSearchUrl url = new OpenSearchUrl(baseUrl); OpenSearchDescription openSearchDescription = null; UrlBasedOpenSearchableFactory urlBasedOpenSearchableFactory; IOpenSearchable result; try { openSearchDescription = OpenSearchFactory.LoadOpenSearchDescriptionDocument(url); OpenSearchDescriptionUrl openSearchDescriptionUrl; if (mimeType == null) { openSearchDescriptionUrl = openSearchDescription.Url.First<OpenSearchDescriptionUrl>(); } else { openSearchDescriptionUrl = openSearchDescription.Url.FirstOrDefault((OpenSearchDescriptionUrl u) => u.Type == mimeType); } if (openSearchDescriptionUrl == null) { throw new InvalidOperationException("Impossible to find an OpenSearchable link for the type " + mimeType); } openSearchDescription.DefaultUrl = openSearchDescriptionUrl; } catch (InvalidOperationException ex) { if (!(ex.InnerException is FileNotFoundException) && !(ex.InnerException is SecurityException) && !(ex.InnerException is UriFormatException)) { openSearchDescription = ose.AutoDiscoverFromQueryUrl(new OpenSearchUrl(baseUrl)); urlBasedOpenSearchableFactory = new UrlBasedOpenSearchableFactory(ose, soft); result = urlBasedOpenSearchableFactory.Create(url); if (string.IsNullOrEmpty(mimeType)) return result; var murl = openSearchDescription.Url.FirstOrDefault((OpenSearchDescriptionUrl u) => u.Type == mimeType); if (murl != null) result.GetOpenSearchDescription().DefaultUrl = murl; return result; } try { url = new OpenSearchUrl(new Uri(baseUrl, "/description")); openSearchDescription = OpenSearchFactory.LoadOpenSearchDescriptionDocument(url); } catch { try { url = new OpenSearchUrl(new Uri(baseUrl, "/OSDD")); openSearchDescription = OpenSearchFactory.LoadOpenSearchDescriptionDocument(url); } catch { throw new EntryPointNotFoundException(string.Format("No OpenSearch description found around {0}", baseUrl)); } } } if (openSearchDescription == null) { throw new EntryPointNotFoundException(string.Format("No OpenSearch description found around {0}", baseUrl)); } urlBasedOpenSearchableFactory = new UrlBasedOpenSearchableFactory(ose, soft); result = urlBasedOpenSearchableFactory.Create(openSearchDescription); return result; }
/// <summary> /// Loads the OpenSearch description document. /// </summary> /// <returns>The OpenSearch description document.</returns> /// <param name="url">URL.</param> public static OpenSearchDescription LoadOpenSearchDescriptionDocument(OpenSearchUrl url) { try { XmlSerializer ser = new XmlSerializer(typeof(OpenSearchDescription)); using (XmlReader reader = XmlReader.Create(url.ToString())){ return (OpenSearchDescription)ser.Deserialize(reader); } } catch (Exception e) { throw new InvalidOperationException("Exception querying OpenSearch description at " + url.ToString() + " : " + e.Message, e); } }
/// <summary> /// Builds the request URL from template name parameters. /// </summary> /// <returns>The request URL from template name parameters.</returns> /// <param name="remoteUrlTemplate">Remote URL template.</param> /// <param name="templateSearchParameters">Template search parameters.</param> public static string BuildRequestUrlFromTemplateNameParameters(OpenSearchUrl remoteUrlTemplate, NameValueCollection templateSearchParameters) { // container for the final query url UriBuilder finalUrl = new UriBuilder(remoteUrlTemplate); // parameters for final query NameValueCollection finalQueryParameters = new NameValueCollection(); // Parse the possible parametrs of the remote urls NameValueCollection remoteParametersDef = HttpUtility.ParseQueryString(remoteUrlTemplate.Query); // For each parameter requested foreach (string parameter in templateSearchParameters.AllKeys) { string value = templateSearchParameters[parameter]; // Find the paramdef in the remote URl template foreach (string keyDef in remoteParametersDef.AllKeys) { foreach (string key2 in remoteParametersDef.GetValues(keyDef)) { Match remoteMatchParamDef = Regex.Match(key2, @"^{(" + parameter + @")\??}$"); // if martch is successful if (remoteMatchParamDef.Success) { // then add the parameter with the right key finalQueryParameters.Add(keyDef, value); } } } } string[] queryString = Array.ConvertAll(finalQueryParameters.AllKeys, key => string.Format("{0}={1}", key, HttpUtility.UrlEncode(finalQueryParameters[key]))); finalUrl.Query = string.Join("&", queryString); return finalUrl.ToString(); }
public object Post(TypeImportRequest request) { OpenSearchEngine ose = new OpenSearchEngine(); ose.LoadPlugins(); ose.DefaultTimeOut = 60000; OpenSearchUrl url = new OpenSearchUrl(request.url); IOpenSearchable entity = new GenericOpenSearchable(url, ose); IOpenSearchableElasticType type = ecf.GetOpenSearchableElasticTypeByNameOrDefault(request.IndexName, request.TypeName); IOpenSearchResultCollection osres = ose.Query(entity, new NameValueCollection()); OpenSearchFactory.RemoveLinksByRel(ref osres, "alternate"); OpenSearchFactory.RemoveLinksByRel(ref osres, "via"); OpenSearchFactory.RemoveLinksByRel(ref osres, "self"); OpenSearchFactory.RemoveLinksByRel(ref osres, "search"); IElasticCollection documents = type.FromOpenSearchResultCollection(osres); BulkRequest bulkRequest = new BulkRequest() { Refresh = true, Consistency = Consistency.One, Index = request.IndexName }; foreach (var doc in documents.Items) { bulkRequest.Operations.Add(new BulkIndexOperation<IElasticItem>((IElasticItem)doc) { Id = doc.Id }); } var response = client.Bulk(bulkRequest); return response; }
/// <summary> /// Initializes a new instance of the <see cref="Terradue.OpenSearch.GenericOpenSearchable"/> class from an OpenSearchDescription. /// </summary> /// <param name="osd">The OpenSearchDescription describing the OpenSearchable entity to represent</param> /// <param name="ose">An OpenSearchEngine instance, preferably with registered extensions able to read the query url</param> public GenericOpenSearchable(OpenSearchDescription osd, OpenSearchEngine ose) { this.osd = osd; this.ose = ose; url = null; }
/// <summary> /// Initializes a new instance of the <see cref="Terradue.OpenSearch.GenericOpenSearchable"/> class from a quaery Url /// </summary> /// <param name="url">The query URL</param> /// <param name="ose">An OpenSearchEngine instance, preferably with registered extensions able to read the query url</param> public GenericOpenSearchable(OpenSearchUrl url, OpenSearchEngine ose) { this.url = url; this.ose = ose; this.osd = ose.AutoDiscoverFromQueryUrl(url); }
/// <summary> /// Initializes a new instance of the <see cref="Terradue.OpenSearch.GenericOpenSearchable"/> class from a quaery Url /// </summary> /// <param name="url">The query URL</param> /// <param name="ose">An OpenSearchEngine instance, preferably with registered extensions able to read the query url</param> public SoftGenericOpenSearchable(OpenSearchUrl url, OpenSearchEngine ose) : base(url, ose) { }