cancel
Showing results for 
Search instead for 
Did you mean: 

How to get a SiteService object in Java Backed Webscript

driekhof
Champ in-the-making
Champ in-the-making
I haven't been able to find any examples of how to get a reference to a SiteService object in a webscript.  If my webscript Java class starts like this, how do I get a hold of a SiteService object (using latest alfresco 3.3 release)?


public class SiteMgrWebScript extends AbstractWebScript
{
    public void execute(WebScriptRequest req, WebScriptResponse res)  throws IOException
    {
       try   {

Do I need to extend another class other than AbstracWebScript?  Do I need to edit some Spring config file to define the SiteService bean.  Some example code would be most welcome.

Is it possible to create sites exclusively in Java code, or will I need to use a combination of Javascript and Java code to create sites?
6 REPLIES 6

hsohaib
Champ on-the-rise
Champ on-the-rise
you need to add reference to SiteService in your code :


public class SiteMgrWebScript extends AbstractWebScript
{
protected SiteService siteService;
public void setSiteService(SiteService siteService){ this.siteService =siteService; }

and in your spring configuration file :


<bean id="yourwebscriptId"
class="yourPackage.SiteMgrWebScript"
parent="webscript">
     <property name="siteService">
<ref bean="SiteService" />
</property>
</bean>

that's all.

driekhof
Champ in-the-making
Champ in-the-making
Thank you!  That worked.  I now have a ref to SiteService.

Next problem - I was getting a authentication error, which I fixed in my webscript descriptor file with this (authentication tag) to make it prompt for user/password:


<webscript>
  <shortname>Create some sites</shortname>
  <description>Hands back a little bit of JSON</description>
  <url>/demo/simple</url>
  <authentication>user</authentication>
  <transaction>required</transaction>
  <format default="">argument</format>
</webscript>

Now hopefully I can create some sites with the SiteService object.  But from what I've seen in other forum postings, it looks complicated and difficult to create them and get them to show up in Share from webscript backend java code.

driekhof
Champ in-the-making
Champ in-the-making
Now I'm having a problem creating a Site in my Java backed webscript.  I can create the site and see it in Share on the list of sites.  But after executing the webscript, logging into Share, and going to the new site's dashboard I get the following error:


HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Could not resolve view with name 'site/testShort/dashboard' in servlet with name 'Spring Surf Dispatcher Servlet'
   org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1042)
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)

From skimming the forums, I'm guessing I'm missing the part that sets up the dashboard and other stuff in Share.  But I haven't found enough info about that to be able to code it in my back-end Java class.  Can I do this from a webscript in my backend Java class?  Or do I need to use some other technique?  Any tips/code greatly appreciated–we'd like to be able to script the creation of a bunch of sites when rolling out to new environments.

Here's my backend Java code that creates the site, but fails when going to dashboard in Share:


public class SiteMgrWebScript extends AbstractWebScript
{
    protected SiteService siteService;
    public void setSiteService(SiteService ss)   {   siteService = ss;  }

    public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException
    {
       try   {
            List<SiteInfo> sites = siteService.listSites( "admin" );
            SiteInfo siTest = siteService.getSite( "testShort" );
            if ( siTest != null ) {
System.out.println( "SITE ALREADY EXISTS, deleting first." );
                siteService.deleteSite( "testShort" );
            } else {
System.out.println( "SITE DOES NOT EXIST, no need deleting." );
            }
            SiteInfo siNew = siteService.createSite(
                "site-dashboard", "testShort", "testTitle", "testDesc", SiteVisibility.PUBLIC );

System.out.println( "Now we need to do the Share stuff, make the dashboard, etc???  How to do in Java???" );

          // build a json object and put some data in it
          JSONObject obj = new JSONObject();
          obj.put("Success", "Site created without throwing exception.");
          String jsonString = obj.toString();
          res.getWriter().write(jsonString);
       } catch ( JSONException e ) {
            System.err.println( e.toString() );
          throw new WebScriptException("Unable to serialize JSON");
       }
    }

barbara
Champ in-the-making
Champ in-the-making
Have you solved this? I'm having the same problem.

tommorris
Champ in-the-making
Champ in-the-making
I struggled for a while, but finally got a working approach. However, I'm pretty sure this isn't the best way.

If you look in the AVM you'll see a bunch of XML files that contain all the dashboard configurations, I tried using the site service and then creating the appropriate nodes in the AVM (using FTP is simplest), which sometimes worked, but was unreliable - I think because at the time I didn't fully understand the file formats.

In the end, I simply called the same Share webscript that is called when you manually create a site through the web UI.
http://hostaname:8080/share/service/modules/create-site

I needed to automatically create a select of Share sites from an ANT build script. So created a custom java-backed alfresco webscript that I could call from ANT.

Embarassingly, I couldn't quickly determine how Share ticketed authentication worked, so simulated the same calls that a user would manually make during log-on, whilst retaining the authentiation state in the cookie.

I can publish my ant-script / java webscript, if you're interested.

Tom
http://www.ixxus.com

driekhof
Champ in-the-making
Champ in-the-making
Yes, please post ant script and webscript code.  I would be interested in seeing it.

Although I wish I could find an example that would just do it all (create a working share site) from webscript backend Java code to keep it clean and simple, all in one place with no moving parts.