OData Referencing Requests in a Change Set - odata

I have batch request to update data.
Data Is:
Item : {
id : int,
name : string,
RefItem : {
int : id,
name : string }
}
I need
Create Item
Update RefItem reference for this newly created Item, in the same batch. (Suppose RefItems(1) already exists in data set)
According to OData spec:
--batch_1872-f36a-7ce8
Content-Type: multipart/mixed; boundary=changeset_8c16-9ba3-2260
--changeset_8c16-9ba3-2260
Content-Type: application/http
Content-Transfer-Encoding: binary
PUT $1/$links/ItemRef HTTP/1.1
Content-ID: 2
Accept: application/atomsvc+xml;q=0.8, application/json;q=0.5, /;q=0.1
DataServiceVersion: 1.0
Content-Type: application/json
{"uri":"ItemRefs(1)"}
--changeset_8c16-9ba3-2260
Content-Type: application/http
Content-Transfer-Encoding: binary
POST Products HTTP/1.1
Content-ID: 1
Accept: application/atomsvc+xml;q=0.8, application/json;q=0.5, /;q=0.1
DataServiceVersion: 1.0
Content-Type: application/json
{"id":-1,"name":"seven"}
--changeset_8c16-9ba3-2260--
--batch_1872-f36a-7ce8--
I suppose it should work, but I have response error :
--batchresponse_4febeba8-dd43-4040-9fdb-866afde5304e
Content-Type: multipart/mixed; boundary=changesetresponse_424dbf12-fa9a-4a59-8284-963fa0fa7b77
--changesetresponse_424dbf12-fa9a-4a59-8284-963fa0fa7b77
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 500 Internal Server Error
Content-ID: 2
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
Content-Type: application/json;charset=utf-8
{"d":{"error":{"code":"","message":{"lang":"ru-RU","value":"An error occurred while processing this request."}}}}
--changesetresponse_424dbf12-fa9a-4a59-...
Can somebody tell me where I'm wrong?
Thanks.

The batch request is processed as a stream. So you can only refer to content ID which was already seen. The above sample refers to content ID 1 before the request operation with that content ID. That is not valid.
You need to first create the new instance (POST) and then add a reference to it via the $links URL.

Related

Multipart email with ical attachment displays incorrectly

I'm not sure if this is an issue with my code, ActionMailer, Mail, or maybe even the icalendar gem?
A user registers for an event and they get an email with an ical attachment:
# app/mailers/registration_mailer.rb
class RegistrationMailer < ApplicationMailer
helper MailerHelper
def created(registration)
...
cal = Icalendar::Calendar.new
cal.event do |e|
e.dtstart = #event.start_time
e.dtend = #event.end_time
e.organizer = 'mailto:filterbuilds#20liters.org'
e.attendee = #recipient
e.location = #location.addr_one_liner
e.summary = #summary
e.description = #description
end
cal.append_custom_property('METHOD', 'REQUEST')
mail.attachments[#attachment_title] = { mime_type: 'text/calendar', content: cal.to_ical }
mail(to: #recipient.email, subject: "[20 Liters] You registered for a filter build on #{#event.mailer_time}")
end
...
end
I have text and HTML views:
app/views/registration_mailer/created.text.erb
app/views/registration_mailer/created.html.erb
When I omit the attachment, the email is structured like this:
Header stuff...
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="--==_mimepart_63358693571_1146901122e"; charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_63358693571_1146901122e
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the text version of the email here]
----==_mimepart_63358693571_1146901122e
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the HTML version of the email here]
----==_mimepart_63358693571_1146901122e--
When the attachment is present, the email is structured like this:
Header stuff...
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="--==_mimepart_6335c3388b140_114924286ed"; charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_6335c3388b140_114924286ed
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the text version of the email here]
----==_mimepart_6335c3388b140_114924286ed
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the HTML version of the email here]
----==_mimepart_6335c3388b140_114924286ed
Content-Type: multipart/alternative; boundary="--==_mimepart_6335c3389bc30_114924287a3"; charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_6335c3389bc30_114924287a3
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the text version of the email AGAIN]
----==_mimepart_6335c3389bc30_114924287a3
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the HTML version of the email AGAIN]
----==_mimepart_6335c3389bc30_114924287a3--
----==_mimepart_6335c3388b140_114924286ed
Content-Type: text/calendar; charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=20Liters_filterbuild_20221011T0900.ical
Content-ID: <6335c3389e99c_1149242894f#railway.mail>
[numbers and letters]
----==_mimepart_6335c3388b140_114924286ed--
It's a weird tree suddenly:
1. Content-Type: multipart/mixed
A. Content-Type: text/plain
B. Content-Type: text/html
C. Content-Type: multipart/alternative
i. Content-Type: text/plain
ii. Content-Type: text/html
D. Content-Type: text/calendar
Rails' mailer preview doesn't reproduce this issue, nor does using Litmus' email client previews (because it seems to remove the text part and attachments), but I'm assuming with the deformed structure of content-types this isn't just a client-specific rendering issue.
I'm thinking this is coming from the Mail gem underneath ActionMailer structuring the content-types oddly, but I'm a bit out of my depth here. It could be ActionMailer, I really don't know how to tell.
I'm not very well versed in this, but I think I want this structure:
1. Content-Type: multipart/mixed
A. Content-Type: multipart/alternative
i. Content-Type: text/plain
ii. Content-Type: text/html
B. Content-Type: text/calendar
So, two questions:
1. If it's my code, what am I doing wrong?
2. If it's not my code, can I force the structure I want?
I've been combing through ActionMailer and Mail code bases, but haven't found a way to manually form my email to this level.
After more digging, I'm blaming ActionMailer, though I'm still not sure why text and html parts are getting added twice.
A monkey patch for my specific use was to let ActionMailer and Mail build the mail object and then just manually remove the unwanted parts:
# app/mailers/registration_mailer.rb
...
mail.attachments[#attachment_title] = { mime_type: 'text/calendar', content: cal.to_ical }
mail(to: #recipient.email, subject: "[20 Liters] You registered for a filter build on #{#event.mailer_time}")
# PATCH: text and html parts are getting inserted in multipart/mixed (top level) as well as multipart/alternative (2nd level)
mail.parts.reject! { |part| !part.attachment? && !part.multipart? }
This only works for my specific case. If the nesting that mail creates for you is different, your reject! statement will need to be different.
In my case, mail builds this structure:
1. Content-Type: multipart/mixed
A. Content-Type: text/plain
B. Content-Type: text/html
C. Content-Type: multipart/alternative
i. Content-Type: text/plain
ii. Content-Type: text/html
D. Content-Type: text/calendar
So I step into the first level (A. - D.) and reject any parts that are not multipart and not an attachment:
1. Content-Type: multipart/mixed
A. Content-Type: text/plain <-- not multipart, not attachment = rejected
B. Content-Type: text/html <-- not multipart, not attachment = rejected
C. Content-Type: multipart/alternative <-- is multipart, not attachment = kept
i. Content-Type: text/plain
ii. Content-Type: text/html
D. Content-Type: text/calendar <-- not multipart, is attachment = kept
If you are facing this issue, I recommend you use a debugger to inspect your mail object, specifically focusing on the parts. Keep in mind that parts can be deeply nested.
Mail::Part inherits from Mail::Message which has some helpful methods you can use to determine the "shape" of your message:
multipart?
attachment?
attachments
has_attachments?
boundary
parts
all_parts
And good luck.

ff9 in Wireshark Capture

I am attempting to save a wireshark capture for later analysis and replay and I noticed that the reconstituted stream has a series off ff9/n scattered through it which is breaking up the message. What is causing this sequence to be embedded in the stream? I do not see this artifact in other requests so is this just a reflection of what is actually on the wire or just a idiosyncrasy with wireshark?
Ex.
POST /gatewayvnext/xdr.svc HTTP/1.1
Content-Type: multipart/related; type="application/xop+xml"; boundary="uuid:b08aed3e-8972-4f75-a5f1-74ee6f0f9a4e"; start="<root.message#cxf.apache.org>"; start-info="application/soap+xml"; action="urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b"
Accept: */*
User-Agent: Apache CXF 2.2.9
Cache-Control: no-cache
Pragma: no-cache
Host: directuat
Connection: keep-alive
Transfer-Encoding: chunked
ff9
--uuid:b08aed3e-8972-4f75-a5f1-74ee6f0f9a4e
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"; action="urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b";
Content-Transfer-Encoding: binary
Content-ID: <root.message#cxf.apache.org>
It's a reflection of what's actually on the wire.
Note that the HTTP headers include "Transfer-Encoding: chunked". Those items are part of chunked transfer encoding.

make HTTP Post request from blackberry java application

In my Blackberry java native application, i am making a call to a dotnet web service whose Request and Response format is defined as below. Here In response XML is being returned. I have been searching the forums and trying for 2-3 days but no success...
Request format :
POST /cinews.asmx/GetSeatLayout HTTP/1.1
Host: webservices.mclients.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
strCinews=string&strTransId1=string&lngSessionId=string&blnScreenOnTop=string&strMergeOption=string&strAreaCats=string
Response format :
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
xml

Post batch request with Breezejs

I've been trying to POST an entity using Breezejs and WebAPI OData Controllers.
Here are the configurations:
config.Routes.MapODataRoute(
routeName: "odata",
routePrefix: "odata",
model: model,
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
Where the model is very straight forward:
public class ServiceMetadata
{
public int ServiceMetadataId { get; set; }
public string ServiceName { get; set; }
public string Description { get; set; }
public ObjectState? State { get; set; }
public DateTime? LastUpdated { get; set; }
}
And it is mapped through the default:
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
The Client is also very simple taken using AngularJs and partially from the Todo example: http://www.breezejs.com/samples/todo-angular
breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);
var serviceName = 'http://localhost:8081/odata/';
breeze.config.initializeAdapterInstances({ dataService: "OData" });
var manager = new breeze.EntityManager(serviceName);
manager.enableSaveQueuing(true);
The actual Posting is done using the default createEntity() method:
function createServiceMetadata(initialValues) {
return manager.createEntity('ServiceMetadata', initialValues);
}
And the whole thing looks like:
serviceMetadatas.createServiceMetadata({
ServiceName: $scope.newServiceName,
Description: $scope.newServiceDescription
});
serviceMetadatas.saveChanges();
However, the request is not being transferred to the correct controller (ServiceMetadatasController which inherits from EntitySetController), or any other controller for that matter.
The HTTP request looks like this:
POST http://localhost:8081/odata/$batch HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: multipart/mixed
Accept-Language: he-IL,he;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DataServiceVersion: 2.0
Content-Type: multipart/mixed; charset=UTF-8;boundary=batch_4f09-d7cf-dd99
MaxDataServiceVersion: 2.0
Referer: http://localhost:9000/
Content-Length: 580
Origin: http://localhost:9000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
--batch_4f09-d7cf-dd99
Content-Type: multipart/mixed; boundary=changeset_ca0c-06b7-ddbe
--changeset_ca0c-06b7-ddbe
Content-Type: application/http
Content-Transfer-Encoding: binary
POST ServiceMetadatas HTTP/1.1
Content-ID: 1
DataServiceVersion: 2.0
Accept: application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1
Content-Type: application/json;odata=verbose
MaxDataServiceVersion: 2.0
{"ServiceMetadataId":-1,"ServiceName":"sdf sdf","Description":"sd fgs df","LastUpdated":null}
--changeset_ca0c-06b7-ddbe--
--batch_4f09-d7cf-dd99--
And the response:
HTTP/1.1 202 Accepted
Cache-Control: no-cache
Pragma: no-cache
Content-Type: multipart/mixed; boundary=batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac
Expires: -1
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: http://localhost:9000
Access-Control-Allow-Credentials: true
DataServiceVersion: 2.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcVG9tZXJcRG9jdW1lbnRzXFZpc3VhbCBTdHVkaW8gMjAxMlxQcm9qZWN0c1xFYXN5Qml6eVxFYXN5Qml6eS5XZWJBUElcb2RhdGFcJGJhdGNo?=
X-Powered-By: ASP.NET
Date: Sun, 15 Sep 2013 14:32:39 GMT
Content-Length: 443
--batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac
Content-Type: multipart/mixed; boundary=changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4
--changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 406 Not Acceptable
Content-ID: 1
--changesetresponse_44da5dcf-877d-4041-a82b-c51d06a4e9a4--
--batchresponse_966d4460-e00e-4900-b1c9-85b17081cfac--
Any idea what the hack is going on?
B.T.W GET requests works great.
P.S.
After looking at couple of demos, I though the using BreezeJS will be straight forward considering WebApi and OData.
I must say it is Far from being easy to configure this JS library. I hope it will turn out to be hard-to-setup but easy-to-use.
Thanks.
#UPDATE See Javier's great answer!!
In after digging allllot on the breeze code, I came to realize that the problem is laying deep in the createChangeRequests() of breezejs, right here:
request.requestUri = entity.entityType.defaultResourceName;
Where for some reason the defaultResouceName, completely ignores the path to this entity.
Long story short, the following is a hack to resolve:
manager.metadataStore.getEntityType(ENTITY_TYPE).setProperties({defaultResourceName: THE_MISSING_PART_FROM_THE_URL + ENTITY_TYPE});
manager.createEntity(ENTITY_TYPE, values);
Not very nice, but still works!
New answer is .. use webApiOData data service instead of "OData":
breeze.config.initializeAdapterInstances({
dataService : 'webApiOData'
});
The problem is in the url of the inner request. The url needs to be relative to the host. Let's say your service is hosted in host/service (in our case, service will be the equivalent as the odata prefix), so normally you send requests like host/service/Customers or /service/Customers.
When you issue a batch request, the urls in the inner requests might be absolute or relative to the host. The problem is that in your request, the url is ServiceMetadatas which is relative to the service root, not the host.
Web API is interpreting the relative url as host/ServiceMetadatas instead of as host/service/ServiceMetadatas and that's what causes the error.
Based on your repro project, the following request works fine:
POST http://localhost:6974/odata/$batch HTTP/1.1
Host: localhost:6974
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: multipart/mixed
Accept-Language: he-IL,he;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DataServiceVersion: 2.0
Content-Type: multipart/mixed; charset=UTF-8;boundary=batch_4f09-d7cf-dd99
MaxDataServiceVersion: 2.0
Referer: http://localhost:9000/
Content-Length: 565
Origin: http://localhost:9000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
--batch_4f09-d7cf-dd99
Content-Type: multipart/mixed; boundary=changeset_ca0c-06b7-ddbe
--changeset_ca0c-06b7-ddbe
Content-Type: application/http
Content-Transfer-Encoding: binary
POST odata/ServiceMetadatas HTTP/1.1
Content-ID: 1
DataServiceVersion: 2.0
Accept: application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1
Content-Type: application/json;odata=verbose
MaxDataServiceVersion: 2.0
{"ServiceMetadataId":-1,"ServiceName":"sdf sdf","Description":"sd fgs df"}
--changeset_ca0c-06b7-ddbe--
--batch_4f09-d7cf-dd99--
The associated response is the following one:
HTTP/1.1 202 Accepted
Cache-Control: no-cache
Pragma: no-cache
Content-Type: multipart/mixed; boundary=batchresponse_6779b5e5-6e40-4363-9a98-5a33d062da28
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 2.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcamFjYWx2YXJcRG93bmxvYWRzXE9EYXRhQmF0Y2gtbWFzdGVyXENsZWFuV2ViQXBpUHJvamVjdFxvZGF0YVwkYmF0Y2g=?=
X-Powered-By: ASP.NET
Date: Tue, 17 Sep 2013 16:48:50 GMT
Content-Length: 872
--batchresponse_6779b5e5-6e40-4363-9a98-5a33d062da28
Content-Type: multipart/mixed; boundary=changesetresponse_b63ca946-ce66-43e6-a78f-d44a5b8f2d5c
--changesetresponse_b63ca946-ce66-43e6-a78f-d44a5b8f2d5c
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 201 Created
Location: http://localhost:6974/odata/ServiceMetadatas(-1)
Content-ID: 1
Content-Type: application/json; odata=verbose; charset=utf-8
DataServiceVersion: 2.0
{
"d":{
"__metadata":{
"id":"http://localhost:6974/odata/ServiceMetadatas(-1)","uri":"http://localhost:6974/odata/ServiceMetadatas(-1)","type":"CleanWebApiProject.Models.ServiceMetadata"
},"ServiceMetadataId":-1,"ServiceName":"sdf sdf","Description":"sd fgs df"
}
}
--changesetresponse_b63ca946-ce66-43e6-a78f-d44a5b8f2d5c--
--batchresponse_6779b5e5-6e40-4363-9a98-5a33d062da28--
The only change that I made in the controller is the following (and not related to batch):
public class ServiceMetadatasController : EntitySetController<ServiceMetadata, int>
{
protected override ServiceMetadata CreateEntity(ServiceMetadata entity)
{
return entity;
}
protected override int GetKey(ServiceMetadata entity)
{
return entity.ServiceMetadataId;
}
public override IQueryable<ServiceMetadata> Get()
{
return new List<ServiceMetadata>
{
new ServiceMetadata() {ServiceName = "Service1", Description = "Desc1"},
new ServiceMetadata() {ServiceName = "Service2", Description = "Desc1"}
}.AsQueryable();
}
}
I hope this solves your problem, also let me know if you are generating the url for the inner request manually or if it's breezejs doing it for you, so that I can follow up and make sure it gets fixed.
Not sure if this is your issue but Microsoft's ODataModelBuilder does not implement a complete OData model. In particular, it doesn't generate foreign key constraints. MS is aware of this and is planning to update it in a later release. Until then you are better off using WCF data services to create your OData endpoints.

Upload to D2L LOR Failing with Permission Error

In trying to upload a scorm package via the REST API the upload is not working.
PUT /d2l/api/lr/(D2LVERSION: version)/objects/
http://docs.valence.desire2learn.com/res/lor.html
We are always refused based on 403 permission.
Are there any settings I should look at? Also looking for a trace of this working.
As long as the account you are authenticated with can perform the function in the web UI it is expected that the API will also function.
If you are getting keys back from the login process but this call is failing you could try a basic call from the sample such as "whoami". If that works your keys are correct.
If the keys are correct and some GET calls are working, you may need to pass a different method to the signing call in the sdk. (The method is part of the signature).
Here is an example of a working trace. 403 errors often have a body that will provide additional information. Note the security parameters are: x_a,x_b,x_c,x_d,x_t parameters on the call.
PUT http://smihai-7:44459/d2l/api/LR/1.0/objects/?repositoryId=1&x_t=1339183935&x_a=L2Hd9WvDTcyiyu5n2AEgpg&x_c=tfJFhSUaczOeOGqDFPXPq8NSBPj2sOhz4U3RacqWRMY&x_b=TestToken&x_d=BEl7kdCcrjenkpBt9ri5dkt4bdEgCo6xfZDWIpkKctA HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp 101.3.0.0
Host: smihai-7:44459
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
Content-Length: 1203
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
-------------------------------28947758029299
Content-Disposition: form-data; name="Resource"; filename="Hello World Module.zip"
Content-Type: application/zip
PK???u?h?]G?g???????????Hello World Topic.htmlM??
?#??A??wwo?xH+???h????b
[o+ ????oD??RYW9u??????}J,?q
d2?[!%E|Crj?Wo{34??Xg??s???L?3??+??/?????*??W?W3?Fyb w?>?cR?Zrf?*???b??PK???u?h?N2?T??n?????imsmanifest.xml?V??0?#?Q??m#TI?.b ?XF??5?????k,?$~???6I??Vt??s?=g?x???%7)??J?i4 P?f\???????????DR?W`]?(?WL???g??d???s?,.&i?q????r??jT?kI??E?C?fsmd6-?q??G? .?f?i??4???!??v\???o?7$\jH%
??K??~P??m?`E?¥a)?C????v???6????#???U~?????x???[fe?.?3????~W???;B_?,???V#B?HE???:??q?e???s??_E? 1wK??<R????T??.9YE??SkP`?????*UT??3???j??#'??#2?;?e_c?#g.????}?p?>?c??????\?
????~,u????\s?M*L?U???E??
??????Kzp\E?X#?%\p???\??R
X-????%??C??????7?|??/&?=???h????l?\?\???????P???s??))??Td??K?????{?Y?+????v?gTN??h$?
?E'E?aB?UD????PK-????u?h?]G?g?????????????????????????Hello World Topic.htmlPK-????u?h?N2?T??n???????????????????imsmanifest.xmlPK??????????E????
-------------------------------28947758029299--
And the response is like this:
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Length: 69
Content-Type: application/json; charset=UTF-8
Expires: -1
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
X-XSS-Protection: 0
Date: Fri, 08 Jun 2012 19:32:22 GMT
{"IdentId":4,"Version":1,"ExecutionMessage":null,"ExecutionStatus":0}

Resources