public IEnumerable <ChargePoint> Get() { // use custom query string parsing for compatibility var filter = new APIRequestParams(); //set defaults var paramList = new NullSafeDictionary <string, string>(); foreach (var k in Request.Query.Keys) { paramList.Add(k.ToLower(), Request.Query[k]); } filter.ParseParameters(filter, paramList); if (string.IsNullOrEmpty(filter.APIKey)) { if (Request.Headers.ContainsKey("X-API-Key")) { filter.APIKey = Request.Headers["X-API-Key"]; } } var api = new POIManager(); var list = api.GetPOIList(filter); return(list); }
public void ProcessRequest(HttpContext context) { if (dataSummaryManager == null) { dataSummaryManager = new DataSummaryManager(); } APIRequestParams filterSettings = new APIRequestParams(); filterSettings.ParseParameters(filterSettings, context); string action = "totals_per_country"; if (!String.IsNullOrEmpty(context.Request["action"])) { action = context.Request["action"].ToString(); } if (action == "totals_per_country") { context.Response.ContentType = "application/javascript"; context.Response.Write(dataSummaryManager.GetTotalsPerCountrySummary(true, "ocm_getdatasummary", filterSettings)); context.Response.Flush(); } if (action == "full_summary") { // Output JSON summary of: // - Current totals per country // - Total added (per country? based on date range, location/distance filter?) // - Total modified // - User Comments Count // - per month values, current month first? default last 12 months } if (action == "activity_summary") { // Based on date range, location and distance: // - list of recent comments, checkins, id & title etc of items added & modified var o = new JSONOutputProvider(); context.Response.ContentType = o.ContentType; var summary = dataSummaryManager.GetActivitySummary(filterSettings); o.PerformSerialisationV2(context.Response.OutputStream, summary, filterSettings.Callback); context.Response.Flush(); } }
/// <summary> /// Handle input to API /// </summary> /// <param name="context"></param> private void PerformInput(HttpContext context) { if (!bool.Parse(ConfigurationManager.AppSettings["EnableDataWrites"])) { OutputBadRequestMessage(context, "API is read only. Submissions not currently being accepted."); return; } OCM.API.InputProviders.IInputProvider inputProvider = null; var filter = new APIRequestParams(); //set defaults filter.ParseParameters(context); //override ?v=2 etc if called via /api/v2/ or /api/v1 if (APIBehaviourVersion > 0) { filter.APIVersion = APIBehaviourVersion; } if (APIBehaviourVersion >= 2) { filter.Action = DefaultAction; } if (context.Request.Url.Host.ToLower().StartsWith("api") && filter.APIVersion == null) { //API version is mandatory for api V2 onwards via api.openchargemap.* hostname OutputBadRequestMessage(context, "mandatory API Version not specified in request"); return; } bool performSubmissionCompletedRedirects = false; //Use JSON format submission if explictly specified or by default if API v3 if (context.Request["format"] == "json" || (String.IsNullOrEmpty(context.Request["format"]) && filter.APIVersion >= 3)) { inputProvider = new InputProviders.JSONInputProvider(); } else { inputProvider = new InputProviders.HTMLFormInputProvider(); performSubmissionCompletedRedirects = true; } SubmissionManager submissionManager = new SubmissionManager(); //attempt to determine user from api call User user = inputProvider.GetUserFromAPICall(context); if (user != null && user.IsCurrentSessionTokenValid == false) { //session token provided didn't match latest in user details, reject user details. context.Response.StatusCode = 401; } else { //allow contact us input whether use is authenticated or not if (context.Request["action"] == "contactus_submission" || filter.Action == "contact") { ContactSubmission contactSubmission = new ContactSubmission(); bool processedOK = inputProvider.ProcessContactUsSubmission(context, ref contactSubmission); bool resultOK = submissionManager.SendContactUsMessage(contactSubmission.Name, contactSubmission.Email, contactSubmission.Comment); if (resultOK == true) { context.Response.Write("OK"); } else { context.Response.Write("Error"); } } //if user not authenticated reject any other input if (user == null) { context.Response.StatusCode = 401; return; } //gather input variables if (context.Request["action"] == "cp_submission" || filter.Action == "poi") { //gather/process data for submission OCM.API.Common.Model.ChargePoint cp = new Common.Model.ChargePoint(); bool processedOK = inputProvider.ProcessEquipmentSubmission(context, ref cp); bool submittedOK = false; if (processedOK == true) { //perform submission int submissionId = submissionManager.PerformPOISubmission(cp, user); if (submissionId > -1) { submittedOK = true; } } if (processedOK && submittedOK) { if (performSubmissionCompletedRedirects) { if (submittedOK) { context.Response.Redirect("http://openchargemap.org/submissions/complete.aspx", true); } else { context.Response.Redirect("http://openchargemap.org/submissions/error.aspx", true); } } else { context.Response.StatusCode = 202; } } else { if (performSubmissionCompletedRedirects) { context.Response.Redirect("http://openchargemap.org/submissions/error.aspx", true); } else { context.Response.StatusCode = 500; } } } if (context.Request["action"] == "comment_submission" || filter.Action == "comment") { UserComment comment = new UserComment(); bool processedOK = inputProvider.ProcessUserCommentSubmission(context, ref comment); if (processedOK == true) { //perform submission int result = submissionManager.PerformSubmission(comment, user); if (filter.APIVersion >= 3) { if (result > 0) { OutputSubmissionReceivedMessage(context, "OK", true); } else { OutputBadRequestMessage(context, "Failed"); } } else { if (result >= 0) { context.Response.Write("OK:" + result); } else { context.Response.Write("Error:" + result); } } } else { context.Response.Write("Error: Validation Failed"); } } if (context.Request["action"] == "mediaitem_submission" || filter.Action == "mediaitem") { var p = inputProvider;// as OCM.API.InputProviders.HTMLFormInputProvider; MediaItem m = new MediaItem(); bool accepted = false; string msg = ""; try { accepted = p.ProcessMediaItemSubmission(context, ref m, user.ID); } catch (Exception exp) { msg += exp.ToString(); } if (accepted) { submissionManager.PerformSubmission(m, user); //OutputSubmissionReceivedMessage(context, "OK :" + m.ID, true); if (filter.APIVersion >= 3) { OutputSubmissionReceivedMessage(context, "OK", true); } else { context.Response.Write("OK"); } } else { if (filter.APIVersion >= 3) { OutputBadRequestMessage(context, "Failed"); } else { context.Response.Write("Error"); } } } } }
/// <summary> /// Handle output from API /// </summary> /// <param name="context"></param> private async void PerformOutput(HttpContext context) { HttpContext.Current.Server.ScriptTimeout = 120; //max script execution time is 2 minutes System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); //decide correct reponse type IOutputProvider outputProvider = null; var filter = new APIRequestParams(); //set defaults string outputType = "xml"; filter.DistanceUnit = DistanceUnit.Miles; filter.MaxResults = 100; filter.EnableCaching = true; filter.ParseParameters(context); //override ?v=2 etc if called via /api/v2/ or /api/v1 if (APIBehaviourVersion > 0) { filter.APIVersion = APIBehaviourVersion; } if (APIBehaviourVersion >= 2) { filter.Action = DefaultAction; } if (context.Request.Url.Host.ToLower().StartsWith("api") && filter.APIVersion == null) { //API version is mandatory for api V2 onwards via api.openchargemap.* hostname OutputBadRequestMessage(context, "mandatory API Version not specified in request"); return; } if (!String.IsNullOrEmpty(context.Request["output"])) { outputType = ParseString(context.Request["output"]); } else { //new default after API V2 is json instead of XML if (filter.APIVersion >= 2) { outputType = "json"; } } //change defaults and override settings for deprecated api features if (filter.APIVersion >= 2) { //the following output types are deprecated and will default as JSON if (outputType == "carwings" || outputType == "rss") { OutputBadRequestMessage(context, "specified output type not supported in this API version"); return; } } if (IsRequestByRobot) { OutputBadRequestMessage(context, "API requests by robots are temporarily disabled.", statusCode: 503); return; } //determine output provider switch (outputType) { case "xml": outputProvider = new XMLOutputProvider(); break; case "carwings": case "rss": outputProvider = new RSSOutputProvider(); if (outputType == "carwings") { ((RSSOutputProvider)outputProvider).EnableCarwingsMode = true; } break; case "json": outputProvider = new JSONOutputProvider(); break; case "geojson": outputProvider = new GeoJSONOutputProvider(); break; case "csv": outputProvider = new CSVOutputProvider(); break; case "kml": outputProvider = new KMLOutputProvider(KMLOutputProvider.KMLVersion.V2); break; case "png": outputProvider = new ImageOutputProvider(); break; default: outputProvider = new XMLOutputProvider(); break; } if (outputProvider != null) { context.Response.ContentEncoding = Encoding.Default; context.Response.ContentType = outputProvider.ContentType; if (!(filter.Action == "getcorereferencedata" && String.IsNullOrEmpty(context.Request["SessionToken"]))) { //by default output is cacheable for 24 hrs, unless requested by a specific user. context.Response.Cache.SetExpires(DateTime.Now.AddDays(1)); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetValidUntilExpires(true); } if (ConfigurationManager.AppSettings["EnableOutputCompression"] == "true") { //apply compression if accepted string encodings = context.Request.Headers.Get("Accept-Encoding"); if (encodings != null) { encodings = encodings.ToLower(); if (encodings.ToLower().Contains("gzip")) { context.Response.Filter = new GZipStream(context.Response.Filter, CompressionLevel.Optimal, false); context.Response.AppendHeader("Content-Encoding", "gzip"); //context.Trace.Warn("GZIP Compression on"); } else { context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress); context.Response.AppendHeader("Content-Encoding", "deflate"); //context.Trace.Warn("Deflate Compression on"); } } } if (filter.Action == "getchargepoints" || filter.Action == "getpoilist") { System.Diagnostics.Debug.WriteLine("At getpoilist output: " + stopwatch.ElapsedMilliseconds + "ms"); OutputPOIList(outputProvider, context, filter); } if (filter.Action == "getcompactpoilist") { //experimental:: OutputCompactPOIList(context, filter); } if (filter.Action == "getcorereferencedata") { OutputCoreReferenceData(outputProvider, context, filter); } if (filter.Action == "geocode") { this.OutputGeocodingResult(outputProvider, context, filter); } if (filter.Action == "availability") { this.OutputAvailabilityResult(outputProvider, context, filter); } if (filter.Action == "profile.authenticate") { this.OutputProfileSignInResult(outputProvider, context, filter); } if (filter.Action == "refreshmirror") { try { var itemsUpdated = await OCM.Core.Data.CacheManager.RefreshCachedData(); new JSONOutputProvider().GetOutput(context.Response.OutputStream, new { POICount = itemsUpdated, Status = "OK" }, filter); } catch (Exception exp) { new JSONOutputProvider().GetOutput(context.Response.OutputStream, new { Status = "Error", Message = exp.ToString() }, filter); } } stopwatch.Stop(); System.Diagnostics.Debug.WriteLine("Total output time: " + stopwatch.ElapsedMilliseconds + "ms"); } }
/// <summary> /// Handle output from API /// </summary> /// <param name="context"></param> private async Task <bool> PerformOutput(HttpContext context) { //decide correct reponse type IOutputProvider outputProvider = null; var filter = new APIRequestParams(); //set defaults string outputType = "xml"; filter.DistanceUnit = DistanceUnit.Miles; filter.MaxResults = 100; filter.EnableCaching = true; var paramList = new NullSafeDictionary <string, string>(); foreach (var k in context.Request.Query.Keys) { paramList.Add(k.ToLower(), context.Request.Query[k]); } filter.ParseParameters(filter, paramList); if (string.IsNullOrEmpty(filter.APIKey)) { if (context.Request.Headers.ContainsKey("X-API-Key")) { filter.APIKey = context.Request.Headers["X-API-Key"]; } } //override ?v=2 etc if called via /api/v2/ or /api/v1 if (APIBehaviourVersion > 0) { filter.APIVersion = APIBehaviourVersion; } if (APIBehaviourVersion >= 2) { filter.Action = DefaultAction; } try { if ((_settings.IsCacheOnlyMode || context.Request.GetUri().Host.ToLower().StartsWith("api")) && (filter.APIVersion == null || filter.APIVersion == 1)) { //API version is mandatory for api V2 onwards via api.openchargemap.* hostname or for API mirrors operating in cached mode await OutputBadRequestMessage(context, "mandatory API Version not specified in request"); return(true); } } catch (System.UriFormatException) { _logger?.LogWarning("Failed to parse URI " + context.Request.Host.Value); } if (!String.IsNullOrEmpty(context.Request.Query["output"])) { outputType = ParseString(context.Request.Query["output"]).ToLower(); } else { //new default after API V2 is json instead of XML if (filter.APIVersion >= 2) { outputType = "json"; } } //change defaults and override settings for deprecated api features if (filter.APIVersion >= 2) { //the following output types are deprecated and will default as JSON if (outputType == "carwings" || outputType == "rss") { await OutputBadRequestMessage(context, "specified output type not supported in this API version"); return(true); } } if (_settings.IsCacheOnlyMode) { filter.AllowMirrorDB = true; filter.AllowDataStoreDB = false; } if (IsRequestByRobot(context)) { await OutputBadRequestMessage(context, "API requests by robots are temporarily disabled.", statusCode : 503); return(true); } //determine output provider switch (outputType) { case "xml": outputProvider = new XMLOutputProvider(); break; case "carwings": case "rss": outputProvider = new RSSOutputProvider(); if (outputType == "carwings") { ((RSSOutputProvider)outputProvider).EnableCarwingsMode = true; } break; case "json": outputProvider = new JSONOutputProvider(); break; case "geojson": outputProvider = new GeoJSONOutputProvider(); break; case "csv": outputProvider = new CSVOutputProvider(); break; case "kml": outputProvider = new KMLOutputProvider(KMLOutputProvider.KMLVersion.V2); break; default: outputProvider = new XMLOutputProvider(); break; } if (outputProvider != null) { context.Response.ContentType = outputProvider.ContentType; if (filter.Action == "getchargepoints" || filter.Action == "poi") { await OutputPOIList(outputProvider, context, filter); return(true); } if (filter.Action == "getcompactpoilist") { //experimental:: await OutputCompactPOIList(context, filter); return(true); } if (filter.Action == "getcorereferencedata") { await OutputCoreReferenceData(outputProvider, context, filter); return(true); } if (filter.Action == "geocode") { OutputGeocodingResult(outputProvider, context, filter); return(true); } if (filter.Action == "availability") { OutputAvailabilityResult(outputProvider, context, filter); return(true); } if (filter.Action == "profile.authenticate") { await OutputProfileSignInResult(outputProvider, context, filter); return(true); } if (filter.Action == "profile.register") { await OutputProfileRegisterResult(outputProvider, context, filter); return(true); } if (filter.Action == "refreshmirror") { try { var itemsUpdated = await OCM.Core.Data.CacheManager.RefreshCachedData(); await new JSONOutputProvider().GetOutput(context, context.Response.Body, new { POICount = itemsUpdated, Status = "OK" }, filter); return(true); } catch (Exception exp) { await new JSONOutputProvider().GetOutput(context, context.Response.Body, new { Status = "Error", Message = exp.ToString() }, filter); return(true); } } } // request did not match an existing handler return(false); }
/// <summary> /// Handle input to API /// </summary> /// <param name="context"></param> private async Task <bool> PerformInput(HttpContext context) { if (!_settings.EnableDataWrites) { await OutputBadRequestMessage(context, "API is read only. Submissions not currently being accepted."); return(true); } OCM.API.InputProviders.IInputProvider inputProvider = null; var filter = new APIRequestParams(); //set defaults var paramList = new NullSafeDictionary <string, string>(); foreach (var k in context.Request.Query.Keys) { paramList.Add(k.ToLower(), context.Request.Query[k]); } filter.ParseParameters(filter, paramList); if (string.IsNullOrEmpty(filter.APIKey)) { if (context.Request.Headers.ContainsKey("X-API-Key")) { filter.APIKey = context.Request.Headers["X-API-Key"]; } } //override ?v=2 etc if called via /api/v2/ or /api/v1 if (APIBehaviourVersion > 0) { filter.APIVersion = APIBehaviourVersion; } if (APIBehaviourVersion >= 2) { filter.Action = DefaultAction; } if (context.Request.GetUri().Host.ToLower().StartsWith("api") && filter.APIVersion == null) { //API version is mandatory for api V2 onwards via api.openchargemap.* hostname await OutputBadRequestMessage(context, "mandatory API Version not specified in request"); return(true); } bool performSubmissionCompletedRedirects = false; //Use JSON format submission if explictly specified or by default if API v3 if (context.Request.Query["format"] == "json" || (String.IsNullOrEmpty(context.Request.Query["format"]) && filter.APIVersion >= 3)) { inputProvider = new InputProviders.JSONInputProvider(); } else { inputProvider = new InputProviders.HTMLFormInputProvider(); performSubmissionCompletedRedirects = true; } SubmissionManager submissionManager = new SubmissionManager(); //attempt to determine user from api call User user = inputProvider.GetUserFromAPICall(context, filter.APIKey); if (user != null && user.IsCurrentSessionTokenValid == false) { //session token provided didn't match latest in user details, reject user details. context.Response.StatusCode = 401; } else { //allow contact us input whether use is authenticated or not if (context.Request.Query["action"] == "contactus_submission" || filter.Action == "contact") { ContactSubmission contactSubmission = new ContactSubmission(); bool processedOK = inputProvider.ProcessContactUsSubmission(context, ref contactSubmission); bool resultOK = submissionManager.SendContactUsMessage(contactSubmission.Name, contactSubmission.Email, contactSubmission.Comment); if (resultOK == true) { await context.Response.WriteAsync("OK"); } else { await context.Response.WriteAsync("Error"); } return(true); } //if user not authenticated reject any other input if (user == null) { context.Response.StatusCode = 401; return(true); } //gather input variables if (context.Request.Query["action"] == "cp_submission" || filter.Action == "poi") { //gather/process data for submission var processingResult = await inputProvider.ProcessEquipmentSubmission(context); ValidationResult submissionResult = new ValidationResult { IsValid = false }; if (processingResult.IsValid) { //perform submission submissionResult = await submissionManager.PerformPOISubmission((ChargePoint)processingResult.Item, user); } if (processingResult.IsValid && submissionResult.IsValid) { if (performSubmissionCompletedRedirects) { context.Response.Redirect("http://openchargemap.org/submissions/complete.aspx", true); } else { context.Response.StatusCode = 202; } return(true); } else { // validation or submission failed if (performSubmissionCompletedRedirects) { context.Response.Redirect("http://openchargemap.org/submissions/error.aspx", true); } else { context.Response.StatusCode = 400; await context.Response.WriteAsync(processingResult.IsValid?submissionResult.Message : processingResult.Message); } return(true); } } if (context.Request.Query["action"] == "cp_batch_submission") { var sr = new System.IO.StreamReader(context.Request.Body); string contentJson = sr.ReadToEnd().Trim(); var list = JsonConvert.DeserializeObject <List <ChargePoint> >(contentJson); foreach (var cp in list) { var validationResult = POIManager.IsValid(cp); ValidationResult submissionResult = new ValidationResult { IsValid = false }; if (validationResult.IsValid) { //perform submission submissionResult = await submissionManager.PerformPOISubmission(cp, user, performCacheRefresh : false); } else { System.Diagnostics.Debug.WriteLine("Invalid POI: " + cp.ID); } } // refresh cache var cacheTask = System.Threading.Tasks.Task.Run(async() => { return(await Core.Data.CacheManager.RefreshCachedData()); }); cacheTask.Wait(); context.Response.StatusCode = 202; return(true); } if (context.Request.Query["action"] == "comment_submission" || filter.Action == "comment") { UserComment comment = new UserComment(); bool processedOK = await inputProvider.ProcessUserCommentSubmission(context, comment); if (processedOK == true) { //perform submission int result = await submissionManager.PerformSubmission(comment, user); if (filter.APIVersion >= 3) { if (result > 0) { await OutputSubmissionReceivedMessage(context, "OK", true); } else { await OutputBadRequestMessage(context, "Failed"); } } else { if (result >= 0) { await context.Response.WriteAsync("OK:" + result); } else { await context.Response.WriteAsync("Error:" + result); } } } else { await context.Response.WriteAsync("Error: Validation Failed"); } return(true); } if (context.Request.Query["action"] == "mediaitem_submission" || filter.Action == "mediaitem") { var p = inputProvider; MediaItem m = new MediaItem(); bool accepted = false; string msg = ""; try { var tempPath = Path.Join(System.IO.Path.GetTempPath(), "_ocm"); if (!System.IO.Directory.Exists(tempPath)) { System.IO.Directory.CreateDirectory(tempPath); } accepted = await p.ProcessMediaItemSubmission(tempPath, context, m, user.ID); } catch (Exception exp) { msg += exp.ToString(); } if (accepted) { submissionManager.PerformSubmission(m, user); //OutputSubmissionReceivedMessage(context, "OK :" + m.ID, true); if (filter.APIVersion >= 3) { await OutputSubmissionReceivedMessage(context, "OK", true); } else { await context.Response.WriteAsync("OK"); } } else { if (filter.APIVersion >= 3) { await OutputBadRequestMessage(context, "Failed"); } else { await context.Response.WriteAsync("Error"); } } return(true); } } // submission was not handled return(false); }