Announcement Announcement Module
Collapse
No announcement yet.
Can't get java-based webscript sample to run in empty surf project Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't get java-based webscript sample to run in empty surf project

    Hey everyone!

    Hey, so been trying to get a custom Java-based webscript installed and running against Surf 1.0.0M3. I've tried this with both a freshly created Spring Roo Surf project (http://www.springsurf.org/sites/1.0....beginning.html) and the empty M3 Surf project available from http://www.springsurf.org/downloads.html, both with the same results.

    I'm able to define my class that extends AbstractWebScript and get it to load into Surf/Spring on loadup, but whenever I try and hit it's URL, it seems to be looking for a template, instead of handling the rendering.. I'm assuming this is a naming issue or something like that..

    The steps I'm going through are:
    1) Create a fresh Surf project
    2) Create and compile the Java class file WEB-INF/classes/org/alfresco/module/demoscripts/SimpleWebScript.class
    Code:
    package org.alfresco.module.demoscripts;
    
    import java.io.IOException;
    
    import org.springframework.extensions.webscripts.AbstractWebScript;
    import org.springframework.extensions.webscripts.WebScriptException;
    import org.springframework.extensions.webscripts.WebScriptRequest;
    import org.springframework.extensions.webscripts.WebScriptResponse;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class SimpleWebScript extends AbstractWebScript
    {
        public void execute(WebScriptRequest req, WebScriptResponse res)
            throws IOException
        {
        	try
        	{
    	    	// build a json object
    	    	JSONObject obj = new JSONObject();
    	    	
    	    	// put some data on it
    	    	obj.put("field1", "data1");
    	    	
    	    	// build a JSON string and send it back
    	    	String jsonString = obj.toString();
    	    	res.getWriter().write(jsonString);
        	}
        	catch(JSONException e)
        	{
        		throw new WebScriptException("Unable to serialize JSON");
        	}
        }    
    }
    3) Create the file WEB-INF/classes/org/springframework/extensions/webscripts/custom-webscripts-context.xml
    Code:
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
    
    <beans>
    
    <bean id="webscript.org.alfresco.module.demoscripts.simple.get" 
          class="org.alfresco.module.demoscripts.SimpleWebScript"
          parent="webscript">
    </bean>
    
    </beans>
    4) Create the webscript descriptor file WEB-INF/webscripts/simple/simple.get.desc.xml
    Code:
    <webscript>
      <shortname>Simple</shortname>
      <description>Simple</description>
      <url>/testme</url>
      <format default="json">argument</format>
    </webscript>
    And I always get back this error message:
    Code:
    	07050002 Cannot locate template processor for template webscripts/simple/simple.get.html
    	 
    Exception:	org.springframework.extensions.webscripts.WebScriptException - 07050002 Cannot locate template processor for template webscripts/simple/simple.get.html
    	 
    	org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:576)
    	org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:263)
    	org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:143)
    	org.springframework.extensions.webscripts.PresentationContainer.executeScript(PresentationContainer.java:69)
    	org.springframework.extensions.webscripts.LocalWebScriptRuntimeContainer.executeScript(LocalWebScriptRuntimeContainer.java:182)
    	org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:307)
    	org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:178)
    	org.springframework.extensions.webscripts.servlet.mvc.WebScriptView.renderMergedOutputModel(WebScriptView.java:94)
    	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    	org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    	org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    	org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    	org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
    	org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
    	org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
    	org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
    	org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
    	org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    	org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    	org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	java.lang.Thread.run(Thread.java:619)
    	 
    Server:	Alfresco - v1.0.0 (Milestone 3 349) schema 1,000
    Time:	Aug 5, 2010 4:48:12 PM
    A couple notes/observations:
    1) I see my custom-webscript-context.xml loading in the log file, and if the class referred to inside that XML file is missing, and error is thrown, so I know the XML file with the bean definitions is definitely getting processed.
    2) Most of this was taken straight from the example on http://wiki.alfresco.com/wiki/Java-b...ipt_.28Java.29
    3) The examples show a line in the webscript descriptor like:
    Code:
    <format default="">argument</format>
    Not sure if that is a relic from Alfresco Surf, but it doesn't run if no format is specified, which is why in my sample it looks like
    Code:
    <format default="json">any</format>
    .. Also, I've noted that the error message coming back is looking for simple.get,json, so not sure if this format thing is part of the issue or not..

    Any thoughts would be greatly appreciated!
    jim

  • #2
    Hi,

    The setup of the class and bean def etc. look fine to me.

    I assume you are hitting the url through the browser, which will be passing up html as the requested content type, and will be looking for an html response template. If you make an Ajax request for the url then you would be requesting application/json format I assume anyway.

    Please can you try setting the response type in your Java code i.e. something like this:
    res.setContentType(res.setContentType(Format.JSON. mimetype() + ";charset=UTF-8");

    I would also suggest the following at the end of all java backed webscript classes as good practice:
    res.getWriter().flush();
    res.getWriter().close();

    Cheers,

    Kev

    Comment


    • #3
      OK I've found the problem, the Spring bean ID to your Java backed webscript is formed thus:
      id="webscript.<packageId>.<serviceId>.<httpMethod> "
      as detailed here:
      http://wiki.alfresco.com/wiki/Web_Sc...an_Declaration

      but, a "clever" developer added this webscript search path as the default lookup under WEB-INF:
      Code:
         <bean id="webframework.webscripts.store.webinf" parent="webframework.store.webapp.abstract">
            <property name="path"><value>/WEB-INF</value></property>
         </bean>
      unfortunately that means the package ID of your webscript includes the "webscripts" folder - it should really have been defined as:
      Code:
         <bean id="webframework.webscripts.store.webinf" parent="webframework.store.webapp.abstract">
            <property name="path"><value>/WEB-INF/webscripts</value></property>
         </bean>
      which would have meant this problem never occured...

      So - you need to either override that bean as above OR change your Java backed WebScript bean id to:
      Code:
      <bean id="webscript.webscripts.org.alfresco.module.demoscripts.simple.get" 
            class="org.alfresco.module.demoscripts.SimpleWebScript"
            parent="webscript">
      </bean>
      I may change it for RC1 to fix the problem (as you are not the first person to hit it!) - am I concerned about backward compatability though as it is *possible* others have added webscripts under WEB-INF in a different folder, but it may be worth having those that have change it as this is a silly mistake to have in the config.

      Sorry about that, cheers.

      Kev

      Comment


      • #4
        ...I've fixed the issue on the trunk, so will be fixed in RC1

        Comment


        • #5
          Thanks!

          Very glad I found this post as it was the key to getting my Java DeclarativeWebScript working. Thanks!

          Bob

          Comment


          • #6
            Good to know!

            Kev

            Comment


            • #7
              Thanks!

              Hey Kevin!

              Thanks, just noticed the reply to this thread from a while ago! Ultimately, we moved more toward the convention of implementing our own root-scoped java and freemarker objects for business logic, but did finally get this working.. And it was exactly as you said, a pathing issue..

              With the config files posted, I was able to get it working by putting files in the following locations:

              WEB-INF/classes/org/springframework/extensions/webscripts/custom-webscripts-context.xml - The context file in which to define the beans:

              WEB-INF/classes/webscripts/org/springframework/extensions/webscripts/setLanguage.get.desc.xml - The WebScript descriptor file

              My custom classes were also implemented in the org.springframework.extensions.webscripts package, although I doubt that was a requirement..

              Anyway, thanks again!!
              jim

              Comment


              • #8
                Hi,

                We have since found some backward compatbility issues with the SpringSurf sample apps after this change, so the follow store has been reverted to using the plain /WEB-INF path:

                Code:
                   <bean id="webframework.webscripts.store.webinf" parent="webframework.store.webapp.abstract">
                      <property name="path"><value>/WEB-INF</value></property>
                   </bean>
                So if you choose to place the desc.xml files for your Java backed webscripts under the WEB-INF path, then you *will* need to add the "webscripts" sub-folder to the declaration of the bean id, for example:

                Code:
                <bean id="webscript.webscripts.org.alfresco.module.demoscripts.simple.get" 
                      class="org.alfresco.module.demoscripts.SimpleWebScript"
                      parent="webscript">
                </bean>
                Either that or override the bean yourself in your custom app.

                Thanks,

                Kevin

                Comment

                Working...
                X