private OM2MResponsePrimitive DoRequestCore(OM2MRequestPrimitive request) { var response = new OM2MResponsePrimitive(CseConfig, request); try { OM2MController controller = null; if (request.TargetId == null && request.To == null) { throw new OM2MBadRequestException("To parameter is missing."); } if (request.TargetId == null) { request.TargetId = request.To; } // Remove last "/" if (request.TargetId.EndsWith("/", StringComparison.Ordinal)) { request.TargetId = request.TargetId.Substring(0, request.TargetId.Length - 1); } // Uri -> ResourceId using (var db = new OM2MDbContext()) { var uriMapEntity = db.UriMaps.Find(request.TargetId); if (uriMapEntity != null) { request.TargetId = uriMapEntity.ResourceId; } } // Recv-6.0.1: Receiver is Registrar CSE & Originator is AE & operation is create if (m_cseBasePattern.IsMatch(request.TargetId) && (request.From == null || m_aePattern.IsMatch(request.From)) && request.Operation == OM2MOperation.Create) { // Recv-6.0.2: “Check Service Subscription Profile” // ToDo: } if (request.Content == null) { if (request.PrimitiveContent != null && request.PrimitiveContent.Any != null && request.PrimitiveContent.Any.Count > 0) { request.Content = request.PrimitiveContent.Any[0]; } } // Recv-6.1: Hosting CSE of the targeted resource? if (IsHostingCseResouce(request.TargetId)) { if (request.Operation == OM2MOperation.Create) { if (request.ResourceType == null) { throw new OM2MBadRequestException($"Resouce type is missing for creation request."); } controller = GetControllerFromResouceType(request.ResourceType.Value); } else { controller = GetControllerFromUri(request.TargetId); } if (controller == null) { throw new OM2MBadRequestException($"Invalid request."); } response = controller.DoRequest(request); if (request.ResultContent != null && request.ResultContent == OM2MResultContent.Nothing) { response.Content = null; } } else { // ToDo:Recv-6.9: CMDH processing supported? // Now Not supported // Recv-6.11: “Forwarding” response = Fowarding(request); } } catch (OM2MException ex) { response.ResponseStatusCode = ex.ResponseStatusCode; } catch (Exception ex) { response.ResponseStatusCode = OM2MResponseStatusCode.InternalServerError; } return(response); }
public void DoRequest(OM2MRequestPrimitive request, IOM2MOriginator originator) { if (!IsInitalized) { throw new InvalidOperationException("CseService is not initialized."); } try { ValidateRequest(request); } catch (OM2MException ex) { var response = HandleOM2MException(ex, request); originator.SendResponse(response); return; } catch (Exception ex) { var response = HandleOtherException(ex, request); originator.SendResponse(response); return; } var responseType = OM2MResponseType.BlockingRequest; if (request.ResponseType != null && request.ResponseType.ResponseTypeValue != null) { responseType = request.ResponseType.ResponseTypeValue.Value; } if (responseType == OM2MResponseType.NonBlockingRequestAsynch || responseType == OM2MResponseType.NonBlockingRequestSynch) { // ToDo: // Recv-3.0: Create <request> resource locally OM2MRequest req = new OM2MRequest(); req.ResourceType = OM2MResourceType.Request; req.ExpirationTime = OM2MTimeStamp.GetTimeStamp(request.RequestExpirationTimestamp); req.CreationTime = OM2MTimeStamp.NowTimeStamp; req.LastModifiedTime = req.CreationTime; // ToDo:request.AccessControlPolicyIDs // ToDo:req.Labels req.StateTag = 0; req.Operation = request.Operation; req.Target = request.To; req.Originator = request.From; req.RequestID = request.RequestIdentifier; // ToDo:request.MetaInformation = req.PrimitiveContent = request.PrimitiveContent; req.RequestStatus = OM2MRequestStatus.Pending; req.OperationResult = null; // Recv-4.0: Create a success Response var response = new OM2MResponsePrimitive(CseConfig, request); response.ResponseStatusCode = OM2MResponseStatusCode.Accepted; response.OriginatingTimestamp = OM2MTimeStamp.NowTimeStamp; // Recv-5.0: Send Response primitive originator.SendResponse(response); // Recv-6.0: Resource handling procedures response = DoBlockingRequest(request); // Recv-7.0: “Update <request> resource” req.LastModifiedTime = OM2MTimeStamp.NowTimeStamp; req.StateTag++; req.RequestStatus = OM2MRequestStatus.Forwarded; if (responseType == OM2MResponseType.NonBlockingRequestAsynch) { // Recv-8.0: “Send Notification” var notify = new OM2MRequestPrimitive(); notify.RequestIdentifier = Guid.NewGuid().ToString(); notify.Operation = OM2MOperation.Notify; notify.From = request.TargetId; notify.To = request.From; notify.PrimitiveContent = new OM2MPrimitiveContent(); notify.Content = response.Content; var notifyResponse = originator.SendRequest(notify); // Recv-9.0: “Wait for Response primitive” } } else { var response = DoRequestCore(request); originator.SendResponse(response); } }