You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Günther Schmidt <gu...@web.de> on 2012/05/24 22:13:45 UTC

Uploading binary data via json

Hi everyone,

I'm using sling to store, and index, my emails. Once I figured out how 
to do that via the JSON import it became a lot easier to even upload 
quite complex structures.
No moving on to the attachments, I'd like to store those too within 
sling and together with the emails they belong to.

I've just found out that I cannot mix a JSON import for the emails data 
and a regular file upload within the same HttpPost request, sling will 
ignore the non-json import parts. So I guess I'll have to place the 
binary data within the JSON string.

How would I upload a file via JSON import anyway? Encoding the binary 
data as base64? What would the JSON structure have to be?

Günther

Re: Uploading binary data via json - halfway there

Posted by Felix Meschberger <fm...@adobe.com>.
Hi,

Problem: Uploading binary along with JSON import in the Sling POST Servlet.

Approach (1):

> Am 24.05.2012 um 22:13 schrieb Günther Schmidt:
>> How would I upload a file via JSON import anyway? Encoding the binary 
>> data as base64? What would the JSON structure have to be?

Approach (2):

> Am 25.05.2012 um 00:44 schrieb Mark Adamcin:
> 
>> Maybe a multipart POST request format could be devised.


I think from the POV of the Sling POST Servlet we could live with both approaches.

The first approach would require additional support in the JSONLoader. This would also benefit the initial content functionality which currently requires a two step approach for JSON and files: JSON for the non-binary structure and plain files for the binary parts.

The second would just require some additional code to the ImportOperation leveraging already present support for file uploads in the POST Servlet.

Opinions ? Takers ?

Regards
Felix



Am 25.05.2012 um 01:07 schrieb David G.:

> Gunter, i assume its storing jcr:data with the the base64 encoded value as a String, right?  
> 
> This is because sling's jsonreader does support detection of string representation of binary objects. (See previous email with link to JSONReader.java)
> 
> When parsing the JSON you're sending in, sling runs down a list of checks to see if it can figure out what property type the property should be -- unfortunately there is no check for Binary, and it will simply fall through and treat the data as a String.
> 
> You wont get around this without a patch, and Mark's approach of supporting multi-form content seems like the best approach (if a change needs to be made, might as well make the one with the most use cases).

> 
> Wish i had better news! JSON import is a pretty great feature though!  
> 
> --  
> David Gonzalez
> Sent with Sparrow (http://www.sparrowmailapp.com/?sig)
> 
> 
> On Thursday, May 24, 2012 at 6:48 PM, Günther Schmidt wrote:
> 
>> Hello,
>> 
>> it seems that it is possible to create an actual valid file node with  
>> this JSON:
>> 
>> {"sampleFile.pdf":{
>> "jcr:primaryType":"nt:file",
>> "jcr:content":{
>> "jcr:primaryType":"nt:resource",
>> "jcr:data":""
>> }
>> }}
>> 
>> However when I filled the jcr:data with base64 encoded binary data it  
>> seems to have remained base64 encoded on the server side.
>> I'll give it a couple more tries but even getting this far is nice.
>> 
>> Günther  
> 


Re: Uploading binary data via json - halfway there

Posted by "David G." <da...@gmail.com>.
Gunter, i assume its storing jcr:data with the the base64 encoded value as a String, right?  

This is because sling's jsonreader does support detection of string representation of binary objects. (See previous email with link to JSONReader.java)

When parsing the JSON you're sending in, sling runs down a list of checks to see if it can figure out what property type the property should be -- unfortunately there is no check for Binary, and it will simply fall through and treat the data as a String.

You wont get around this without a patch, and Mark's approach of supporting multi-form content seems like the best approach (if a change needs to be made, might as well make the one with the most use cases).

Wish i had better news! JSON import is a pretty great feature though!  

--  
David Gonzalez
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


On Thursday, May 24, 2012 at 6:48 PM, Günther Schmidt wrote:

> Hello,
>  
> it seems that it is possible to create an actual valid file node with  
> this JSON:
>  
> {"sampleFile.pdf":{
> "jcr:primaryType":"nt:file",
> "jcr:content":{
> "jcr:primaryType":"nt:resource",
> "jcr:data":""
> }
> }}
>  
> However when I filled the jcr:data with base64 encoded binary data it  
> seems to have remained base64 encoded on the server side.
> I'll give it a couple more tries but even getting this far is nice.
>  
> Günther  


Re: Uploading binary data via json - halfway there

Posted by Günther Schmidt <gu...@web.de>.
Hello,

it seems that it is possible to create an actual valid file node with 
this JSON:

{"sampleFile.pdf":{
     "jcr:primaryType":"nt:file",
     "jcr:content":{
         "jcr:primaryType":"nt:resource",
         "jcr:data":""
     }
}}

However when I filled the jcr:data with base64 encoded binary data it 
seems to have remained base64 encoded on the server side.
I'll give it a couple more tries but even getting this far is nice.

Günther

Re: Uploading binary data via json

Posted by Günther Schmidt <gu...@kmmd.de>.
Hi Bertrand,

as a matter of fact this is what will ultimately happen. Actually I want 
to build a "groupware application" based on sling. The server side will 
poll mailboxes (pop3, imap) and store them in the JCR. Posting digested 
emails to sling from some client is an intermediate step, which given 
the problems I think I'll just skip.
Günther

Am 25.05.12 14:02, schrieb Bertrand Delacretaz:
> Hi,
>
> On Thu, May 24, 2012 at 10:13 PM, Günther Schmidt<gu...@web.de>  wrote:
>> ...I'm using sling to store, and index, my emails. Once I figured out how to do
>> that via the JSON import it became a lot easier to even upload quite complex
>> structures.
>> No moving on to the attachments, I'd like to store those too within sling
>> and together with the emails they belong to....
> IIUC you're parsing your messages on the client side, and posting json
> structures to Sling?
>
> Why don't you post the raw email message to Sling in RFC 822 format,
> and let your POST handler parse and store it?
>
> -Bertrand


-- 
KMMD IT-Consulting UG (haftungsbeschränkt)
Offenburger Str. 45
68239 Mannheim
Tel: +49-621-4393887
HRB 712101 Amtsgericht Mannheim


Re: Uploading binary data via json

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Thu, May 24, 2012 at 10:13 PM, Günther Schmidt <gu...@web.de> wrote:
> ...I'm using sling to store, and index, my emails. Once I figured out how to do
> that via the JSON import it became a lot easier to even upload quite complex
> structures.
> No moving on to the attachments, I'd like to store those too within sling
> and together with the emails they belong to....

IIUC you're parsing your messages on the client side, and posting json
structures to Sling?

Why don't you post the raw email message to Sling in RFC 822 format,
and let your POST handler parse and store it?

-Bertrand

Re: Uploading binary data via json

Posted by Mark Adamcin <ad...@gmail.com>.
Maybe a multipart POST request format could be devised.

Mark Adamcin
On May 24, 2012 3:30 PM, "Justin Edelson" <ju...@gmail.com> wrote:

> Gunter,
> If you have a suggestion on how to handle this, please consider submitting
> a patch.
>
> Regards,
> Justin
>
> On May 24, 2012, at 6:15 PM, "David G." <da...@gmail.com> wrote:
>
> > Gunter,
> >
> > I don't believe you can do that.
> >
> > If you take a look at sling's JSONReader impl [1], it doesn't support
> Binary properties types.
> >
> > You may have to split your POST into 2 requests; one for the JSON node
> rep and one for the Binary data.
> >
> > [1]
> http://www.jarvana.com/jarvana/view/org/apache/sling/org.apache.sling.jcr.contentloader/2.1.2/org.apache.sling.jcr.contentloader-2.1.2-sources.jar!/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java?format=ok
> >
> >
> >
> > --
> > David Gonzalez
> > Sent with Sparrow (http://www.sparrowmailapp.com/?sig)
> >
> >
> > On Thursday, May 24, 2012 at 4:13 PM, Günther Schmidt wrote:
> >
> >> Hi everyone,
> >>
> >> I'm using sling to store, and index, my emails. Once I figured out how
> >> to do that via the JSON import it became a lot easier to even upload
> >> quite complex structures.
> >> No moving on to the attachments, I'd like to store those too within
> >> sling and together with the emails they belong to.
> >>
> >> I've just found out that I cannot mix a JSON import for the emails data
> >> and a regular file upload within the same HttpPost request, sling will
> >> ignore the non-json import parts. So I guess I'll have to place the
> >> binary data within the JSON string.
> >>
> >> How would I upload a file via JSON import anyway? Encoding the binary
> >> data as base64? What would the JSON structure have to be?
> >>
> >> Günther
> >
>

Re: Uploading binary data via json

Posted by Justin Edelson <ju...@gmail.com>.
Gunter,
If you have a suggestion on how to handle this, please consider submitting a patch.

Regards,
Justin

On May 24, 2012, at 6:15 PM, "David G." <da...@gmail.com> wrote:

> Gunter,
> 
> I don't believe you can do that.  
> 
> If you take a look at sling's JSONReader impl [1], it doesn't support Binary properties types.
> 
> You may have to split your POST into 2 requests; one for the JSON node rep and one for the Binary data.
> 
> [1] http://www.jarvana.com/jarvana/view/org/apache/sling/org.apache.sling.jcr.contentloader/2.1.2/org.apache.sling.jcr.contentloader-2.1.2-sources.jar!/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java?format=ok
> 
> 
> 
> --  
> David Gonzalez
> Sent with Sparrow (http://www.sparrowmailapp.com/?sig)
> 
> 
> On Thursday, May 24, 2012 at 4:13 PM, Günther Schmidt wrote:
> 
>> Hi everyone,
>> 
>> I'm using sling to store, and index, my emails. Once I figured out how  
>> to do that via the JSON import it became a lot easier to even upload  
>> quite complex structures.
>> No moving on to the attachments, I'd like to store those too within  
>> sling and together with the emails they belong to.
>> 
>> I've just found out that I cannot mix a JSON import for the emails data  
>> and a regular file upload within the same HttpPost request, sling will  
>> ignore the non-json import parts. So I guess I'll have to place the  
>> binary data within the JSON string.
>> 
>> How would I upload a file via JSON import anyway? Encoding the binary  
>> data as base64? What would the JSON structure have to be?
>> 
>> Günther  
> 

Re: Uploading binary data via json

Posted by "David G." <da...@gmail.com>.
Gunter,

I don't believe you can do that.  

If you take a look at sling's JSONReader impl [1], it doesn't support Binary properties types.

You may have to split your POST into 2 requests; one for the JSON node rep and one for the Binary data.

[1] http://www.jarvana.com/jarvana/view/org/apache/sling/org.apache.sling.jcr.contentloader/2.1.2/org.apache.sling.jcr.contentloader-2.1.2-sources.jar!/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java?format=ok



--  
David Gonzalez
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


On Thursday, May 24, 2012 at 4:13 PM, Günther Schmidt wrote:

> Hi everyone,
>  
> I'm using sling to store, and index, my emails. Once I figured out how  
> to do that via the JSON import it became a lot easier to even upload  
> quite complex structures.
> No moving on to the attachments, I'd like to store those too within  
> sling and together with the emails they belong to.
>  
> I've just found out that I cannot mix a JSON import for the emails data  
> and a regular file upload within the same HttpPost request, sling will  
> ignore the non-json import parts. So I guess I'll have to place the  
> binary data within the JSON string.
>  
> How would I upload a file via JSON import anyway? Encoding the binary  
> data as base64? What would the JSON structure have to be?
>  
> Günther