cors - CorsFilter losing Representation entity -
my works corsfilter when file upload post i'm losing representation entity @ resource level. please i'm stuck. following source (/collections get), (/fileupload post not working).
corsfilter corsfilter = new corsfilter(getcontext(), router); corsfilter.setallowedorigins(new hashset(arrays.aslist("*"))); corsfilter.setallowedcredentials(true); corsfilter.setallowingallrequestedheaders(true); corsfilter.setskippingresourceforcorsoptions(true); corsfilter.setnext(collectionsresource.class); router.attach("/collections", corsfilter); corsfilter.setnext(fileuploadresource.class); router.attach("/fileupload", corsfilter);
in representation entity in resource debugger saying entity [false/*]
. representation null.
i think should this: [multipart/form-data; .....]
.
the following html & ajax call please take look. missing in that? works fine me not post:
<html> <head> <script type="text/javascript" src="https://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script> </head> <body> <form id="assetupload"> <input id="filetoupload" name="filetoupload" type="file" /> <input type="submit" value="submit"/> </form> </body> </html> $("#assetupload").submit(function(evt){ evt.preventdefault(); var formdata = new formdata($(this)[0]); alert("fire"); $.ajax({ url: 'http://localhost:14502/api/v1/fileupload', type: 'post', data: formdata, async: false, cache: false, contenttype: 'false', enctype: 'multipart/form-data', processdata: false, success: function (response) { alert(response); } });
i think try use corsservice
instead of corsfilter
. it's more convenient configure.
here way configure on restlet application itself:
corsservice corsservice = new corsservice(); corsservice.setallowedorigins(new hashset(arrays.aslist("*"))); corsservice.setallowedcredentials(true); corsservice.setallowingallrequestedheaders(true); corsservice.setskippingresourceforcorsoptions(true); application.getservices().add(corsservice); component.getdefaulthost().attachdefault(application);
with approach don't need anymore handle filter when configuring routing of application. here refactored code:
public class myapplication extends application { public restlet createinboutroot() { router router = new router(getcontext()); router.attach("/collections", collectionsresource.class); router.attach("/fileupload", fileuploadresource.class); return router; } }
i think problem came way defined routing. in fact, set twice next attribute of corsfilter
, last 1 wins! both paths (/collections
, /fileupload
) handled fileuploadresource
class.
this blog post interest you: https://templth.wordpress.com/2014/11/12/understanding-and-using-cors/.
edited
if want explicitly use corsfilter
, should refactor code described below:
public class myapplication extends application { public restlet createinboutroot() { router router = new router(getcontext()); corsfilter corsfilter = new corsfilter(getcontext(), router); corsfilter.setallowedorigins(new hashset(arrays.aslist("*"))); corsfilter.setallowedcredentials(true); corsfilter.setallowingallrequestedheaders(true); corsfilter.setskippingresourceforcorsoptions(true); router.attach("/collections", collectionsresource.class); router.attach("/fileupload", fileuploadresource.class); return corsfilter; } }
hope helps you, thierry
Comments
Post a Comment