cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript Controller Fails

mduduzi
Confirmed Champ
Confirmed Champ
Hi,

I am executing a script in the javascript console and it works. When I use the very same script in a web script and deploy it in web script extension folder it compains. Pasted below is the part of the script that fails and the error log:


var ctx =Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var qname = Packages.org.alfresco.service.namespace.QName;
var def = Packages.org.alfresco.service.cmr.dictionary;
var ns = Packages.org.alfresco.service.namespace.NamespaceService;
var iterator = Packages.java.util.Iterator;
//var ListOfValuesConstraint = new Packages.org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
var dictionaryService = ctx.getBean("DictionaryService");


error log


The Web Script /alfresco/service/sita/projserv/projservregister/ has responded with a status of 500 - Internal Error.

500 Description:    An error inside the HTTP server which prevented it from fulfilling the request.

Message:   05280246 Wrapped Exception (with status template): 05281343 Failed to execute script '/Project And Service Register/projservregister.get.js (in repository store workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions)': 05281342 ReferenceError: "Packages" is not defined. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js#1)

Exception:   org.mozilla.javascript.EcmaError - ReferenceError: "Packages" is not defined. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js#1)

org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3413)
org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1612)
org.mozilla.javascript.gen.c66._c0(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js:1)
org.mozilla.javascript.gen.c66.call(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
org.mozilla.javascript.gen.c66.call(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js)
org.mozilla.javascript.gen.c66.exec(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js)
org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:492)
org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:198)
org.alfresco.repo.processor.ScriptServiceImpl.execute(ScriptServiceImpl.java:212)
org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:174)
org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:102)
org.springframework.extensions.webscripts.AbstractWebScript.executeScript(AbstractWebScript.java:1305)
org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:86)
org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:417)
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:401)
org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:479)
org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:517)
org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:333)
org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:377)
org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)
org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)
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.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)
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.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
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:293)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
java.lang.Thread.run(Thread.java:662)

Exception:   org.alfresco.error.AlfrescoRuntimeException - 05281342 ReferenceError: "Packages" is not defined. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js#1)

org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:516)

Exception:   org.alfresco.scripts.ScriptException - 05281343 Failed to execute script '/Project And Service Register/projservregister.get.js (in repository store workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions)': 05281342 ReferenceError: "Packages" is not defined. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js#1)

org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:202)

Exception:   org.springframework.extensions.webscripts.WebScriptException - 05280246 Wrapped Exception (with status template): 05281343 Failed to execute script '/Project And Service Register/projservregister.get.js (in repository store workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions)': 05281342 ReferenceError: "Packages" is not defined. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/Project And Service Register/projservregister.get.js#1)

org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:1067)


THanks!
5 REPLIES 5

mitpatoliya
Star Collaborator
Star Collaborator
that is because both the context are different.
There are some root objects provided by Alfresco JavaScript like company_home,search, user_home etc..
but availability of those are depends on the context in which you are executing that script.

Please give details about your requirement what you are trying to achieve.

Thanks for the response.
I have defined a webscript and its javascript controller get custom constraint list to populate the drop-down in template. When the same script is tested javascript it works fine and when I deploy it as part of the webscript it complains about the package not defined.

Hi,
I have reproduce the error using a simple webscript. Pasted are files for the sample drop-down webscript:
Descriptor:

<webscript>
  <shortname>Hello</shortname>
  <description>Polite greeting</description>
  <url>/sample/dropdown</url>
  <authentication>user</authentication>
</webscript>


Controller:

var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var model = Packages.org.alfresco.model.ContentModel;
var dictionaryService = ctx.getBean("DictionaryService");
var types = dictionaryService.getSubTypes(model.TYPE_CONTENT, true).toArray();
model.yrsearch = types;


Template:

<table>
   <tr>
    <td>
        <#assign props = yrsearch.properties?keys>
        <select name="idRequest" id="idRequest">
              <option value="NOT SELECTED" selected="selected">[–SELECT YEAR–]</option>
              <#list props as t>
                  <option value="${t}">${t}</option>
              </#list>
        </select>
     </td>
    </tr>
</table>


The error is the same i.e Package not defined

Please help
Thanks!

scouil
Star Contributor
Star Contributor
Hi Mduduzi,

As Mits told you, it's a difference in the context.
The javascript console adds some custom root-level objects (like   space) that don't exist in a normal webscript environment.
That's probably the case of Packages as well.
so your first line
var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();

will fail since the "Packages" root-level object doesn't exist.

You can retrieve the list of the root-level javascript items here:
http://docs.alfresco.com/4.2/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Freferences%2FAPI-JS-ro...

Lucas

lourens
Champ in-the-making
Champ in-the-making
Hi Mdu

What you need to do is create a your own custom reference to a rootobject, such as Service Registry. See:
http://wiki.alfresco.com/wiki/Configuring_the_ServiceRegistry_as_a_Javascript_Root_Object

You then register a bean to that class.

e.g. I used the following java code to create my custom class:
<blockcode>
package org.alfresco.repo.extension.jscript;


import java.util.Collection;

import javax.xml.namespace.QName;

import org.alfresco.cmis.CMISDictionaryService;
import org.alfresco.cmis.CMISQueryService;
import org.alfresco.cmis.CMISServices;
import org.alfresco.mbeans.VirtServerRegistry;
import org.alfresco.repo.admin.SysAdminParams;
import org.alfresco.repo.forms.FormService;
import org.alfresco.repo.imap.ImapService;
import org.alfresco.repo.lock.JobLockService;
import org.alfresco.repo.nodelocator.NodeLocatorService;
import org.alfresco.repo.processor.BaseProcessorExtension;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.admin.RepoAdminService;
import org.alfresco.service.cmr.attributes.AttributeService;
import org.alfresco.service.cmr.audit.AuditService;
import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.avm.deploy.DeploymentService;
import org.alfresco.service.cmr.avm.locking.AVMLockingService;
import org.alfresco.service.cmr.avmsync.AVMSyncService;
import org.alfresco.service.cmr.blog.BlogService;
import org.alfresco.service.cmr.calendar.CalendarService;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.invitation.InvitationService;
import org.alfresco.service.cmr.lock.LockService;
import org.alfresco.service.cmr.ml.ContentFilterLanguagesService;
import org.alfresco.service.cmr.ml.EditionService;
import org.alfresco.service.cmr.ml.MultilingualContentService;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.notification.NotificationService;
import org.alfresco.service.cmr.rating.RatingService;
import org.alfresco.service.cmr.rendition.RenditionService;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.CrossRepositoryCopyService;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.ScriptService;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.search.CategoryService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.OwnableService;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.security.PublicServiceAccessService;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.cmr.tagging.TaggingService;
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.view.ExporterService;
import org.alfresco.service.cmr.view.ImporterService;
import org.alfresco.service.cmr.webdav.WebDavService;
import org.alfresco.service.cmr.workflow.WorkflowService;
import org.alfresco.service.descriptor.DescriptorService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.wcm.asset.AssetService;
import org.alfresco.wcm.preview.PreviewURIService;
import org.alfresco.wcm.sandbox.SandboxService;
import org.alfresco.wcm.webproject.WebProjectService;
import org.alfresco.model.ContentModel;

public class JavascriptCompatibleServiceRegistry extends BaseProcessorExtension implements ServiceRegistry
{

private ServiceRegistry impl;
private ContentModel implContentModel;


public void setServiceRegistry(final ServiceRegistry impl)
{
    this.impl = impl;
}



/**e
* @see org.alfresco.service.ServiceRegistry#getAVMLockingAwareService()
*/
public AVMService getAVMLockingAwareService()
{
    return(impl.getAVMLockingAwareService());
}

// …
// Other getters in ServiceRegistry go here
// …

/**
* @see org.alfresco.service.ServiceRegistry#isServiceProvided(org.alfresco.service.namespace.QName)
*/
public boolean isServiceProvided(final org.alfresco.service.namespace.QName qname)
{
    return(impl.isServiceProvided(qname));
}

@Override
public AVMLockingService getAVMLockingService() {
   // TODO Auto-generated method stub
   return null;
}


@Override
public AVMSyncService getAVMSyncService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ActionService getActionService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public AssetService getAssetService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public AttributeService getAttributeService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public AuditService getAuditService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public MutableAuthenticationService getAuthenticationService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public AuthorityService getAuthorityService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public BlogService getBlogService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CMISDictionaryService getCMISDictionaryService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CMISQueryService getCMISQueryService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CMISServices getCMISService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CalendarService getCalendarService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CategoryService getCategoryService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CheckOutCheckInService getCheckOutCheckInService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ContentFilterLanguagesService getContentFilterLanguagesService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ContentService getContentService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CopyService getCopyService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public CrossRepositoryCopyService getCrossRepositoryCopyService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public DeploymentService getDeploymentService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public DescriptorService getDescriptorService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public DictionaryService getDictionaryService() {
   // TODO Auto-generated method stub
   return(impl.getDictionaryService());

}

public org.alfresco.service.namespace.QName getTypeContent()
{
     org.alfresco.service.namespace.QName a = org.alfresco.model.ContentModel.TYPE_CONTENT;
     return(a);
}

public ContentModel getContentModel() {
   // TODO Auto-generated method stub


   return(this.implContentModel);

}



@Override
public EditionService getEditionService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ExporterService getExporterService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public FileFolderService getFileFolderService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public FormService getFormService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ImapService getImapService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ImporterService getImporterService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public InvitationService getInvitationService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public JobLockService getJobLockService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public LockService getLockService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public MimetypeService getMimetypeService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public MultilingualContentService getMultilingualContentService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public NamespaceService getNamespaceService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public NodeLocatorService getNodeLocatorService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public NodeService getNodeService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public NotificationService getNotificationService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public OwnableService getOwnableService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public PermissionService getPermissionService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public PersonService getPersonService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public PreviewURIService getPreviewURIService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public PublicServiceAccessService getPublicServiceAccessService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public RatingService getRatingService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public RenditionService getRenditionService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public RepoAdminService getRepoAdminService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public RetryingTransactionHelper getRetryingTransactionHelper() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public RuleService getRuleService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public SandboxService getSandboxService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ScriptService getScriptService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public SearchService getSearchService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public Object getService(org.alfresco.service.namespace.QName arg0) {
   // TODO Auto-generated method stub
   return null;
}

@Override
public Collection<org.alfresco.service.namespace.QName> getServices() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public SiteService getSiteService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public SysAdminParams getSysAdminParams() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public TaggingService getTaggingService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public TemplateService getTemplateService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public ThumbnailService getThumbnailService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public TransactionService getTransactionService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public VersionService getVersionService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public VirtServerRegistry getVirtServerRegistry() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public WebDavService getWebDavService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public WebProjectService getWebProjectService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public WorkflowService getWorkflowService() {
   // TODO Auto-generated method stub
   return null;
}

@Override
public AVMService getAVMService() {
   // TODO Auto-generated method stub
   return null;
}


/*
public boolean isServiceProvided(org.alfresco.service.namespace.QName arg0) {
   // TODO Auto-generated method stub
   return false;
}
*/
}
</blockcode>
The I created a bean(-context) file:
<blockcode>
bean id="javascriptServiceRegistry" parent="baseJavaScriptExtension" class="org.alfresco.repo.extension.jscript.JavascriptCompatibleServiceRegistry">
  <property name="serviceRegistry" ref="ServiceRegistry" />
  <property name="extensionName"   value="serviceRegistry" />
/bean>
</blockcode>

In my .js controler I reference the service context as such:

<blockcode>
var dictionaryService = serviceRegistry.getDictionaryService();
var tc = serviceRegistry.getTypeContent();
types = dictionaryService.getSubTypes(tc, true).toArray();
</blockcode>