ComponentEndpoint.vala 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. using Inversion;
  4. using Astralis;
  5. namespace Spry {
  6. public class ComponentEndpoint : Object, Endpoint {
  7. private PathProvider component_uri_provider = inject<PathProvider>();
  8. private CryptographyProvider cryptograpy_provider = inject<CryptographyProvider>();
  9. private Scope scope = inject<Scope>();
  10. public async Astralis.HttpResult handle_request (HttpContext http_context, RouteContext route_context) throws Error {
  11. var component_id = route_context.mapped_parameters.get_or_default ("component-id");
  12. var action_name = route_context.mapped_parameters.get_or_default ("action");
  13. var context_blob = route_context.mapped_parameters.get_or_default ("context");
  14. if(component_id == null) {
  15. return new HttpStringResult ("Missing component ID", StatusCode.BAD_REQUEST);
  16. }
  17. if(component_id == null) {
  18. return new HttpStringResult ("Missing action", StatusCode.BAD_REQUEST);
  19. }
  20. var component = component_uri_provider.get_component_instance(component_id, scope);
  21. if(component == null) {
  22. return new HttpStringResult ("Invalid component ID", StatusCode.NOT_FOUND);
  23. }
  24. if(context_blob != null) {
  25. ComponentContext context;
  26. try {
  27. context = cryptograpy_provider.read_component_context_blob (context_blob);
  28. }
  29. catch(Error e) {
  30. warning(@"Invalid component context: $(e.message)");
  31. return new HttpStringResult ("Invalid component context", StatusCode.BAD_REQUEST);
  32. }
  33. if(context.type_name != component.get_type().name()) {
  34. return new HttpStringResult ("Context mismatch", StatusCode.BAD_REQUEST);
  35. }
  36. component.context_key = context.context_key;
  37. foreach(var prop in context.data) {
  38. unowned var component_class = component.get_class ();
  39. var prop_spec = component_class.find_property (prop.key);
  40. if(prop_spec == null) {
  41. return new HttpStringResult (@"Invalid context property '$(prop.key)'", StatusCode.BAD_REQUEST);
  42. }
  43. component.set_property (prop.key, prop.value.to_value (prop_spec.value_type));
  44. }
  45. }
  46. yield component.handle_action(action_name);
  47. return yield component.to_result();
  48. }
  49. }
  50. }