AnsweredAssumed Answered

How to create a share site using the REST API ?

Question asked by ferbru60 on Oct 3, 2009
Latest reply on Oct 12, 2012 by joemc3
I try to create (using PERL) a share site using the REST API. I read the doc http://wiki.alfresco.com/wiki/3.0_REST_API#Site_Service and I coded that :

use warnings;
use strict;

use HTTP::Request::Common;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use LWP::UserAgent;

use JSON;

my $alfTicket;

my $host = 'localhost';
my $port = 8080;

sub login ()
{
   my ($username, $password) = @_;

   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'text/x-json', Accept=>'text/x-json');
   my $GetReq = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/alfresco/service/api/login",
         $h,
         '{"username" : "' . $username . '","password" : "' . $password . '"}'
      );
   my $res = $ua->request($GetReq);
    # Check the outcome of the response
    if ($res->is_success)
    {
      my $hashRef = JSON::decode_json ($res->content);
      $alfTicket = $hashRef->{"data"}->{"ticket"};
        return 1;
    }
    else
    {
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
        return 0;
    }
}

sub createSite ()
{
   my ($siteInfosRef) = @_;
   my $content = JSON::encode_json ($siteInfosRef);
   my $ua = LWP::UserAgent->new;
   my $h = HTTP::Headers->new;
   $h->header ('Content-Type' => 'text/x-json', Accept=>'text/x-json');
   my $GetReq = HTTP::Request->new
      (
         'POST',
         "http://$host:$port/alfresco/service/api/sites?alf_ticket=$alfTicket",
         $h,
         $content
      );
   print "—————————-\n";
   print "Request :\n" . $GetReq->as_string . "\n";
   print "—————————-\n";
   my $res = $ua->request($GetReq);
    if ($res->is_success)
    {
      %{$siteInfosRef} = %{JSON::decode_json ($res->content)};
        return 1;
    }
    else
    {
      print "—————————-\n";
      print "Response :\n";
      print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
      print "\t" . $res->content . "\n";
      print "—————————-\n";
        return 0;
    }
}

print "username: ";
my $username = <STDIN>;
chomp ($username);

print "password: ";
my $password = <STDIN>;
chomp ($password);

if (&login ($username, $password))
{
   print "Site creation :\n";
   print "Short name : ";
   my $shortName = <STDIN>;
   chomp ($shortName);
   print "Description : ";
   my $description = <STDIN>;
   chomp ($description);
   print "Title : ";
   my $title = <STDIN>;
   chomp ($title);
   
   my %siteInfos;
   $siteInfos {"visibility"} = 'PRIVATE';
   $siteInfos {"shortName"} = $shortName;
   $siteInfos {"description"} = $description;
   $siteInfos {"title"} = $title;
   $siteInfos {"sitePreset"} = 'site-dashboard';

   print "$username is logged on !\n";
   if (&createSite (\%siteInfos))
   {
      print "$shortName created !\n";
   }
   else
   {
      print "Can't create site $shortName\n";
   }
}
else
{
   print "$username can't log in $host:$port\n";
}

An the results are :
—————————-
Request :
POST http://localhost:8080/alfresco/service/api/sites?alf_ticket=TICKET_f476c578a1740b4753eb05d146120ec7e23ac9c9
Accept: text/x-json
Content-Type: text/x-json

{"visibility":"PRIVATE","sitePreset":"site-dashboard","title":"My Site","description":"One more site !!!","shortName":"mybfsite"}

—————————-
—————————-
Response :
Error (500) : Erreur Interne de Servlet
   {
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  }, 
 
  "message" : "09030197 Wrapped Exception (with status template): 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator.", 
  "exception" : "org.alfresco.web.scripts.WebScriptException - 09030197 Wrapped Exception (with status template): 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator.",
 
  "callstack" :
  [
       ""      ,"freemarker.core.InvalidReferenceException: Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl."
      ,"freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)"
      ,"freemarker.core.Expression.getStringValue(Expression.java:118)"
      ,"freemarker.core.AddConcatExpression._getAsTemplateModel(AddConcatExpression.java:98)"
      ,"freemarker.core.Expression.getAsTemplateModel(Expression.java:89)"
      ,"freemarker.core.ListLiteral.getModelList(ListLiteral.java:119)"
      ,"freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:89)"
      ,"freemarker.core.Expression.getAsTemplateModel(Expression.java:89)"
      ,"freemarker.core.Expression.getStringValue(Expression.java:93)"
      ,"freemarker.core.DollarVariable.accept(DollarVariable.java:76)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.MixedContent.accept(MixedContent.java:92)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.Macro$Context.runMacro(Macro.java:164)"
      ,"freemarker.core.Environment.visit(Environment.java:601)"
      ,"freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.MixedContent.accept(MixedContent.java:92)"
      ,"freemarker.core.Environment.visit(Environment.java:208)"
      ,"freemarker.core.Environment.process(Environment.java:188)"
      ,"freemarker.template.Template.process(Template.java:237)"
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:202)"
      ,"org.alfresco.web.scripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:523)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:241)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:357)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:326)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:407)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:424)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:288)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:262)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)"
      ,"org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:122)"
      ,"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.StandardWrapperValve.invoke(StandardWrapperValve.java:233)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)"
      ,"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:286)"
      ,"org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)"
      ,"org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)"
      ,"java.lang.Thread.run(Thread.java:619)"
      ,"org.alfresco.service.cmr.repository.TemplateException: 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator."
      ,"org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:206)"
      ,"org.alfresco.web.scripts.WebScriptException: 09030197 Wrapped Exception (with status template): 09030196 Error during processing of the template 'Expression site.shortName is undefined on line 4, column 57 in org\/alfresco\/repository\/site\/site.lib.ftl.'. Please contact your system administrator."
      ,"org.alfresco.web.scripts.AbstractWebScript.createStatusException(AbstractWebScript.java:613)"

  ],
 
  "server" : "Alfresco Community v3.2.0 (2039) schema 2 019",
  "time" : "3 oct. 2009 11:24:58"
}


—————————-
Can't create site mybfsite


Why "It" says that there's an empty shortName ? Someone may help me ?

Sorry for my poor english I'm french !

Bruno

Outcomes