Announcement Announcement Module
Collapse
No announcement yet.
Processing forms in webscript which is part of page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Processing forms in webscript which is part of page

    Hi all,

    I created a RESTful service (webscript) with a form on it. It processes GET (responds with a form) and POST (shows the result) at the moment. It works fine till I add that webscript to a page that contains other webscript components like header and footer. Once I submit the form I get exceptions that neither header nor footer supports the method POST.

    Please could someone advise what is the proper way to process forms? I don't want to include the whole page into the webscript respond because it goes against the main idea of surf (as I understand it) arranging multiple components on one page.

    Thanks!

  • #2
    You have two options, in the trunk we have added a feature so that all components fall back to the .get implementation if no .post implementation is found -this is ideal for POSTing to pages where you only want to add specific .post logic for some components - so update to trunk codeline will fix it for you. The other option is to add .post implementations for all the components on the page you are POSTing too - this is obviously annoying as it is a copy/paste exercise - but if you don't want to update to trunk that is your option currently.

    Cheers,

    Kev

    Comment


    • #3
      If you do take the duplicate post file route then you can import the get js and freemarker files into the post files to avoid duplicating the file contents.

      Comment


      • #4
        Thanks a lot for the quick response.

        The behavior to fall back to the .get implementation for all the components that doesn't have .post is exactly what I would expect. Thanks for adding that feature!

        Comment


        • #5
          Tested. Everything works fine except... post parameters aren't reachable as
          args["paramName"]

          Comment


          • #6
            I assume you are talking about the .post implementation webscript? As you wouldn't expect POST params from a FORM to be available to .get webscript impls. Also if you are submitting a FORM then the data should be available in the "formdata" model object.

            Cheers,

            Kev

            Comment


            • #7
              Hi Kev,

              Yes, you're right, I'm talking about .post implementation of the mentioned above component. Unfortunately I wasn't able to see the parameters in the formdata as well, at least not in a debugger console. I will do more tests soon to double check it.

              As a side question, please could you clarify is there a proper way (I found some but not sure they are correct) of protecting /service/console on public server? Currently the scripts refreshing could lead to the whole site become unreachable.

              Thanks again for your quick response.

              Comment


              • #8
                >As a side question, please could you clarify is there a proper way (I found some but not sure they are correct) of protecting /service/console on public server? Currently the scripts refreshing could lead to the whole site become unreachable.

                To protect the scripts, override the following bean as follows:

                Code:
                   <!-- Override WebScript View Resolver - authenticated for admin webscripts via alfresco-feed connector -->
                   <bean id="webscriptViewResolver" class="org.springframework.extensions.webscripts.servlet.mvc.WebScriptViewResolver">
                      <property name="container" ref="webscripts.container" />
                      <property name="authenticatorFactory" ref="webscripts.authenticator.basic" />
                   </bean>
                This ensure all console webscripts go through the 'webscripts.authenticator.basic' authenticator. We do this in Alfresco Share where we have the 'alfresco-feed' endpoint (used by that authenticator) authenticating using basic http auth against the Alfresco server.

                The other option is to remove those scripts from a runtime instance.

                Cheers,

                Kev

                Comment


                • #9
                  Just rebuild the latest 1.0.0.CI-SNAPSHOT and "formdata" is not defined in .post script. Neither I can see the content of the formdata object in the debugger (but I see that formdata root object does exists).

                  Thanks!

                  Comment


                  • #10
                    OK. Can you give me a bit more detail - what are you posting (FORM data i assume since it's a page post) and what encoding? application/x-www-form-urlencoded or multipart/form-data?

                    Comment


                    • #11
                      Yes, I created a simple webscript with .get that returns a form with two fields. Then I submit the form to the same webscript with .post script. When I call the webscript separately it works fine, if I put it into a template it gives no error now (after fix) but no data passed (quick summary what I have at the moment

                      I tried both enctype-s: "application/x-www-form-urlencoded" - no parameters in args, no formdata, "multipart/form-data" - no parameters in args, formdata is in the root objects list but has no parameters in it (maybe I read them wrongly but debugger doesn't show them too).

                      Thanks!

                      Comment


                      • #12
                        As part of my testing I added a file input field to the form. But still nothing got passed to the web script even when it was called directly and not as part of a page. In debugger formdata.fields.length returns 0.

                        So probably it's not a problem with components but rather with post parameters in general. Do I do something wrong?

                        Cheers!

                        Comment


                        • #13
                          OK thanks for the extra info - I will investigate the issue now.

                          Kev

                          Comment


                          • #14
                            OK I have written a test case that shows formdata processing for a webscript is working. This is posting a form to the webscipt - not too a page, I haven't tried that yet, but it should just be a matter of mapping the page url args (which contain the formdata expect for "file" data) into the webscript component url (see my previous posts in the forums relating to this).

                            testformdatamultipartprocessing2.post.desc.xml
                            Code:
                            <webscript>
                              <shortname>Test formdata processing enabled</shortname>
                              <description>Test forms with enctype of multipart/form-data</description>
                              <url>/testformdatamultipartprocessing2</url>
                              <authentication>none</authentication>
                            </webscript>
                            testformdatamultipartprocessing2.post.js
                            Code:
                            var result = (formdata != null);
                            
                            model.result = result;
                            
                            // should be a multi-part form
                            model.isMultiPart = (formdata.getIsMultiPart());
                            
                            // example of getting a form field via the "args" map - but cannot get "file" field this way
                            model.args_name = args.name;
                            
                            // Parse form attributes - including "file" field
                            for each (field in formdata.fields)
                            {
                               switch (String(field.name).toLowerCase())
                               {
                                  case "file":
                                     if (field.isFile)
                                     {
                                        model.form_filename = field.filename;
                                     }
                                     break;
                            
                                  case "name":
                                     model.form_name = field.value;
                                     break;
                                  
                                  case "title":
                                     model.form_title = field.value;
                                     break;
                                  
                                  case "hiddenvalue":
                                     model.form_hiddenvalue = field.value;
                                     break;
                                  
                               }
                            }
                            testformdatamultipartprocessing2.post.html.ftl
                            Code:
                            <#if result>
                            Success, formdata is available.<br>
                            isMultiPart = ${isMultiPart?string}<br>
                            arg.name = ${args_name}<br>
                            name = ${form_name}<br>
                            title = ${form_title}<br>
                            name = ${form_hiddenvalue}<br>
                            filename = ${form_filename}<br>
                            <#else>
                            Failed as no formdata!
                            </#if>
                            And the test html page that POSTs:
                            testformdatamultipartprocessing2.html
                            Code:
                            <html>
                               <head></head>
                               <body>
                                  <form action="http://YOURSERVER:8080/YOURAPPSERVER/service/testformdatamultipartprocessing2" method="post" enctype="multipart/form-data" accept-charset="UTF-8" onsubmit="return validate();">
                                  <tr><td>Name:&nbsp;</td><td><input name="name" id="name" maxlength="1024"></td></tr>
                                  <tr><td>Title:&nbsp;</td><td><input name="title" maxlength="1024"></td></tr>
                                  <tr><td>File:&nbsp;</td><td><input name="file" type="file"></td></tr>
                                  <tr><td><input type="submit" name="submit" value="OK"></td></tr>
                                  <input type="hidden" name="hiddenvalue" value="BISCUITS">
                                  </form>
                               </body>
                            </html>
                            Loading the page, filling in the fields (including selecting a file) and hitting OK gives me:
                            Code:
                            Success, formdata is available.
                            isMultiPart = true
                            arg.name = 1
                            name = 1
                            title = 2
                            hiddenvalue = BISCUITS
                            filename = alfresco_csrf.html
                            Hope this helps!

                            Kev

                            Comment


                            • #15
                              Thanks for your detailed post. I tried (copy/pasted) the code from your post and... I think my build wasn't correct, I didn't get the parameters on the result page. I will rebuild the Surf and try again. Now I have a sample that I know definitely works

                              Comment

                              Working...
                              X