private void btnCreate_Click(object sender, EventArgs e) { this.Template = this.propConfig.SelectedObject as IStoreTemplate; List <String> errors = this.Template != null?this.Template.Validate().ToList() : new List <string> { "No template selected" }; if (errors.Count > 0) { InvalidTemplateForm invalid = new InvalidTemplateForm(errors); invalid.ShowDialog(); } else { this.DialogResult = DialogResult.OK; this.Close(); } }
/// <summary> /// Gets all available templates for creating a store /// </summary> /// <param name="id">Store ID</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> /// <returns></returns> public virtual void GetAvailableTemplates(string id, AsyncStorageCallback callback, object state) { List <IStoreTemplate> templates = new List <IStoreTemplate>(); Object[] args = new Object[] { id }; foreach (Type t in this._templateTypes) { try { IStoreTemplate template = Activator.CreateInstance(t, args) as IStoreTemplate; if (template != null) { templates.Add(template); } } catch { // Ignore and continue } } callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.AvailableTemplates, id, templates), state); }
/// <summary> /// Gets all available templates for creating a store /// </summary> /// <param name="id">Store ID</param> /// <returns></returns> public virtual IEnumerable <IStoreTemplate> GetAvailableTemplates(string id) { List <IStoreTemplate> templates = new List <IStoreTemplate>(); Object[] args = new Object[] { id }; foreach (Type t in this._templateTypes) { try { IStoreTemplate template = Activator.CreateInstance(t, args) as IStoreTemplate; if (template != null) { templates.Add(template); } } catch { // Ignore and continue } } return(templates); }
public bool CreateStore(IStoreTemplate template) { if (_stores.ContainsKey(template.ID)) return false; _stores.Add(template.ID, new MockStorageProvider()); return true; }
/// <summary> /// Creates a task for creating a Store /// </summary> /// <param name="server">Server</param> /// <param name="template">Template</param> public CreateStoreTask(IStorageServer server, IStoreTemplate template) : base("Create Store") { this._server = server; this._template = template; }
/// <summary> /// Creates a new Store on the server within the current catalog asynchronously. /// </summary> /// <param name="template">Template to create the store from.</param> /// <param name="callback">Callback.</param> /// <param name="state">State to pass to callback.</param> public override void CreateStore(IStoreTemplate template, AsyncStorageCallback callback, object state) { try { var createParams = new Dictionary <string, string> { { "override", "false" } }; var request = CreateRequest("repositories/" + template.ID, "*/*", "PUT", createParams); Tools.HttpDebugRequest(request); request.BeginGetResponse(r => { try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r); Tools.HttpDebugResponse(response); response.Close(); callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template), state); } catch (WebException webEx) { if (webEx.Response != null) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } // Got a Response so we can analyse the Response Code var response = (HttpWebResponse)webEx.Response; var code = (int)response.StatusCode; if (code == 400) { // 400 just means the store already exists so this is OK callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template), state); } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("A HTTP error occurred while trying to create a store, see inner exception for details", webEx)), state); } } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("A HTTP error occurred while trying to create a store, see inner exception for details", webEx)), state); } } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("An unexpected error occurred while trying to create a store, see inner exception for details", ex)), state); } }, state); } catch (WebException webEx) { if (webEx.Response != null) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } // Got a Response so we can analyse the Response Code HttpWebResponse response = (HttpWebResponse)webEx.Response; int code = (int)response.StatusCode; if (code == 400) { // 400 just means the store already exists so this is OK callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID), state); } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("A HTTP error occurred while trying to create a store, see inner exception for details", webEx)), state); } } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("A HTTP error occurred while trying to create a store, see inner exception for details", webEx)), state); } } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("An unexpected error occurred while trying to create a store, see inner exception for details", ex)), state); } }
/// <summary> /// Creates new callback arguments /// </summary> /// <param name="operation">Operation</param> /// <param name="storeID">Store ID</param> /// <param name="template">Template</param> public AsyncStorageCallbackArgs(AsyncStorageOperation operation, String storeID, IStoreTemplate template) : this(operation, storeID) { this.Template = template; }
/// <summary> /// Creates a new store based on the given template /// </summary> /// <param name="template">Template</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> /// <remarks> /// <para> /// Template must inherit from <see cref="BaseStardogTemplate"/> /// </para> /// </remarks> public virtual void CreateStore(IStoreTemplate template, AsyncStorageCallback callback, object state) { if (template is BaseStardogTemplate) { // POST /admin/databases // Creates a new database; expects a multipart request with a JSON specifying database name, options and filenames followed by (optional) file contents as a multipart POST request. try { // Get the Template BaseStardogTemplate stardogTemplate = (BaseStardogTemplate)template; IEnumerable <String> errors = stardogTemplate.Validate(); if (errors.Any()) { throw new RdfStorageException("Template is not valid, call Validate() on the template to see the list of errors"); } JObject jsonTemplate = stardogTemplate.GetTemplateJson(); Console.WriteLine(jsonTemplate.ToString()); // Create the request and write the JSON HttpWebRequest request = CreateAdminRequest("databases", MimeTypesHelper.Any, "POST", new Dictionary <string, string>()); String boundary = StorageHelper.HttpMultipartBoundary; byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); byte[] terminatorBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); request.ContentType = MimeTypesHelper.FormMultipart + "; boundary=" + boundary; request.BeginGetRequestStream(r => { try { using (Stream stream = request.EndGetRequestStream(r)) { // Boundary stream.Write(boundaryBytes, 0, boundaryBytes.Length); // Then the root Item String templateItem = String.Format(StorageHelper.HttpMultipartContentTemplate, "root", jsonTemplate.ToString()); byte[] itemBytes = Encoding.UTF8.GetBytes(templateItem); stream.Write(itemBytes, 0, itemBytes.Length); // Then terminating boundary stream.Write(terminatorBytes, 0, terminatorBytes.Length); stream.Close(); } Tools.HttpDebugRequest(request); // Make the request request.BeginGetResponse(r2 => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r2)) { Tools.HttpDebugResponse(response); // If we get here it completed OK response.Close(); } callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID), state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleHttpError(webEx, "creating a new Store '" + template.ID + "' asynchronously in")), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleError(ex, "creating a new Store '" + template.ID + "' asynchronously in")), state); } }, state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleHttpError(webEx, "creating a new Store '" + template.ID + "' asynchronously in")), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleError(ex, "creating a new Store '" + template.ID + "' asynchronously in")), state); } }, state); } catch (WebException webEx) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleHttpError(webEx, "creating a new Store '" + template.ID + "' asynchronously in")), state); } catch (Exception ex) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleError(ex, "creating a new Store '" + template.ID + "' asynchronously in")), state); } } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("Invalid template, templates must derive from BaseStardogTemplate")), state); } }
/// <summary> /// Creates a new Store based off the given template /// </summary> /// <param name="template">Template</param> /// <returns></returns> /// <remarks> /// <para> /// Templates must inherit from <see cref="BaseStardogTemplate"/> /// </para> /// <para> /// Uses some code based off on answers <a href="http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data">here</a> to help do the multipart form data request. /// </para> /// </remarks> public bool CreateStore(IStoreTemplate template) { if (template is BaseStardogTemplate) { //POST /admin/databases //Creates a new database; expects a multipart request with a JSON specifying database name, options and filenames followed by (optional) file contents as a multipart POST request. try { //Get the Template BaseStardogTemplate stardogTemplate = (BaseStardogTemplate)template; IEnumerable <String> errors = stardogTemplate.Validate(); if (errors.Any()) { throw new RdfStorageException("Template is not valid, call Validate() on the template to see the list of errors"); } JObject jsonTemplate = stardogTemplate.GetTemplateJson(); Console.WriteLine(jsonTemplate.ToString()); //Create the request and write the JSON HttpWebRequest request = this.CreateAdminRequest("databases", MimeTypesHelper.Any, "POST", new Dictionary <string, string>()); String boundary = StorageHelper.HttpMultipartBoundary; #if !SILVERLIGHT byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); byte[] terminatorBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); #else //Should be safe to do this for Silverlight as everything here would be in the ASCII range anyway byte[] boundaryBytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); byte[] terminatorBytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); #endif request.ContentType = MimeTypesHelper.FormMultipart + "; boundary=" + boundary; using (Stream stream = request.GetRequestStream()) { //Boundary stream.Write(boundaryBytes, 0, boundaryBytes.Length); //Then the root Item String templateItem = String.Format(StorageHelper.HttpMultipartContentTemplate, "root", jsonTemplate.ToString()); byte[] itemBytes = System.Text.Encoding.UTF8.GetBytes(templateItem); stream.Write(itemBytes, 0, itemBytes.Length); //Then terminating boundary stream.Write(terminatorBytes, 0, terminatorBytes.Length); stream.Close(); } Tools.HttpDebugRequest(request); //Make the request using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); //If we get here it completed OK response.Close(); } return(true); } catch (WebException webEx) { throw StorageHelper.HandleHttpError(webEx, "creating a new Store '" + template.ID + "' in"); } } else { throw new RdfStorageException("Invalid template, templates must derive from BaseStardogTemplate"); } }