cancel
Showing results for 
Search instead for 
Did you mean: 

Problem authenticating POST request

workingpeter
Champ in-the-making
Champ in-the-making
I have a web script based on the file upload example, using Community 2.9B.  I am failing to get a form-based client to authenticate when issuing a POST to the web script. The script has the authentication attribute set to "user". After entering the basic auth credentials, the script executes but an exception is thrown that "companyhome is not defined". This seems to be consistent with executing the script as an unauthenticated user.

GET requests to other scripts that also use companyhome do not have this problem. Is this a problem with POST's?

Note that the same script executes correctly under Enterprise 2.1.1. Am I trying to implement an Enterprise-only feature?

Thanks.
14 REPLIES 14

davidc
Star Contributor
Star Contributor
No, that may be a regression on 2.9.  The Web Script f/w was refactored to support some stuff coming in 3.0, so bugs could have crept in.

Can you post a simple reproducible case? Thanks.

workingpeter
Champ in-the-making
Champ in-the-making
Only minor path changes to original file upload example from Web Script Examples doco. I just confirmed that this script works under AlfrescoEnterprise 2.1.1 Linux, but under AlfrescoCommunity 2.9.0 Linux fails with org.mozilla.javascript.EcmaError - ReferenceError: "companyhome" is not defined. (AlfrescoScript#33).
Note I haven't attempted 2.1 as the formdata JS variable does not seem to be available.

Descriptor

<webscript>
  <shortname>File Upload</shortname>
  <description>Upload file content and meta-data into Repository</description>
  <url>/media/asset/upload</url>
  <format default="html">argument</format>
  <authentication>user</authentication>
</webscript>

Script

var filename = null;
var content = null;
var title = "";
var description = "";

// locate file attributes
for each (field in formdata.fields)
{
  if (field.name == "title")
  {
    title = field.value;
  }
  else if (field.name == "desc")
  {
    description = field.value;
  }
  else if (field.name == "file" && field.isFile)
  {
    filename = field.filename;
    content = field.content;
  }
}

// ensure mandatory file attributes have been located
if (filename == undefined || content == undefined)
{
  status.code = 400;
  status.message = "Uploaded file cannot be located in request";
  status.redirect = true;
}
else
{
  var folder = companyhome.childByNamePath("Media/Projects");
  upload = folder.createFile("asset" + folder.children.length + "_" + filename) ;
  upload.properties.content.write(content);
  upload.properties.content.mimetype = "UTF-8";
  upload.properties.title = title;
  upload.properties.description = description;
  upload.save();

  // setup model for response template
  model.upload = upload;
}

Template

<html>
<head>
   <title>Upload Web Script Sample</title>
   <link rel="stylesheet" href="${url.context}/css/main.css" TYPE="text/css">
</head>
<body>
   <table>
     <tr>
       <td><img src="${url.context}/images/logo/AlfrescoLogo32.png" alt="Alfresco" /></td>
       <td><nobr>Upload Web Script Sample</nobr></td>
     </tr>
     <tr><td><td>Alfresco ${server.edition} v${server.version}
     <tr><td><td>
     <tr><td><td>Uploaded <a href="${url.serviceContext}/sample/folder${upload.displayPath}">${upload.name}</a> of size ${upload.properties.content.size}.
   </table>
</body>
</html>

HTML form

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <title>File Upload</title>
   </head>
<body>
    <form method="POST" name="uploadForm" enctype='multipart/form-data' id="upload_form" action="http://localhost:8080/alfresco/service/media/asset/upload">
        <input type="hidden" name="projectName" id="uploadProjectName" value="testing project name"/>

        <label>
            Name:
        </label>
        <br/>
        <input type="text" name="title" id="title_input"/><p/>
        <label>
            Description:
        </label>
        <br/>
        <textarea name="desc" id="desc_input"></textarea>

        <p/>
        <label>
            File:
        </label>
        <br/>
        <input class="button" type="file" name="file" id="file_input"/><p/>
      <input class="button" type="submit" name="submit" value="Upload"/>
    </form>
</body>
</html>

Exception
 
Failed to execute script 'workspace://SpacesStore//Company Home/Data Dictionary/Web Scripts Extensions/com/dius/upload.post.js': Failed to execute script 'workspace://SpacesStore//Company Home/Data Dictionary/Web Scripts Extensions/com/dius/upload.post.js': ReferenceError: "companyhome" is not defined. (AlfrescoScript#33)

Exception:   org.mozilla.javascript.EcmaError - ReferenceError: "companyhome" is not defined. (AlfrescoScript#33)
   
   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.c2._c0(AlfrescoScript:33)
   org.mozilla.javascript.gen.c2.call(AlfrescoScript)
   org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
   org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
   org.mozilla.javascript.gen.c2.call(AlfrescoScript)
   org.mozilla.javascript.gen.c2.exec(AlfrescoScript)
   org.mozilla.javascript.Context.evaluateString(Context.java:1196)
   org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:516)
   org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:115)
   org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:243)
   org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:108)
   org.alfresco.web.scripts.AbstractWebScript.executeScript(AbstractWebScript.java:471)
   org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:109)
   org.alfresco.repo.web.scripts.RepositoryContainer$1.execute(RepositoryContainer.java:275)
   org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:236)
   org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:166)
   org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:286)
   org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:230)
   org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:227)
   org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:130)
   org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:108)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
   org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
   java.lang.Thread.run(Thread.java:619)

davidc
Star Contributor
Star Contributor
Thanks - let me reproduce.

davidc
Star Contributor
Star Contributor
I've been able to execute this script on the latest HEAD.

Can you make sure you have appropriate permissions set on Company Home.

Also, I've noticed that Firefox 3 beta 2 is unreliable when a POST responds with 401 (unauthorized).  Which browser are you using?

workingpeter
Champ in-the-making
Champ in-the-making
David,

I've been able to execute the script successfully on a Mac! So using AlfrescoCommunity 2.9.0B failed on Linux but works on Mac. Both tests were with Firefox 2.0.0 and permissions on Company Home the same (everyone consumer, authenticating as admin). I've only tested from an HTML form so I guess the browser comes into play. I will be moving to an Ajax client, so if anything else comes up I'll repost. Thanks for your responses.

Peter

eg
Champ in-the-making
Champ in-the-making
Hi -

I tried the code in the example and got an error in the JS about formdata not being defined.

Is this because I am running the community edition (2.1)? Do I need to be running the enterprise edition in order to avail of the file upload feature within Web Scripts?

Thanks,

ERROR DETAILS


500 Description: An error inside the HTTP server which prevented it from fulfilling the request. 
  
Message: Failed to execute script 'C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\com\cdi\Upload.post.js': Failed to execute script 'C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\com\cdi\Upload.post.js': ReferenceError: "formdata" is not defined. (AlfrescoScript#7) 
  
Exception: org.mozilla.javascript.EcmaError - ReferenceError: "formdata" is not defined. (AlfrescoScript#7) 
   
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3226) 
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3216) 
org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3289) 
org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1567) 
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3159) 
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2248) 
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:158) 
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:337) 
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2755) 
org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:169) 
org.mozilla.javascript.Context.evaluateString(Context.java:1144) 
org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:510) 
org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:112) 
org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:242) 
sun.reflect.GeneratedMethodAccessor271.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
java.lang.reflect.Method.invoke(Method.java:585) 
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:281) 
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154) 
org.alfresco.repo.security.permissions.impl.AlwaysProceedMethodInterceptor.invoke(AlwaysProceedMethodInterceptor.java:40) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) 
org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:49) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) 
org.alfresco.repo.audit.AuditComponentImpl.auditImpl(AuditComponentImpl.java:256) 
org.alfresco.repo.audit.AuditComponentImpl.audit(AuditComponentImpl.java:191) 
org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:69) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) 
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) 
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210) 
$Proxy48.executeScript(Unknown Source) 
org.alfresco.web.scripts.ScriptProcessor.executeScript(ScriptProcessor.java:108) 
org.alfresco.web.scripts.AbstractWebScript.executeScript(AbstractWebScript.java:537) 
org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:115) 
org.alfresco.web.scripts.WebScriptRuntime.wrappedExecute(WebScriptRuntime.java:342) 
org.alfresco.web.scripts.WebScriptRuntime.authenticatedExecute(WebScriptRuntime.java:308) 
org.alfresco.web.scripts.WebScriptRuntime$1.execute(WebScriptRuntime.java:163) 
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:225) 
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:155) 
org.alfresco.web.scripts.WebScriptRuntime.executeScript(WebScriptRuntime.java:174) 
org.alfresco.web.scripts.WebScriptServlet.service(WebScriptServlet.java:109) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210) 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174) 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151) 
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870) 
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) 
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) 
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) 
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685) 
java.lang.Thread.run(Thread.java:595) 
   
Exception: org.alfresco.error.AlfrescoRuntimeException - ReferenceError: "formdata" is not defined. (AlfrescoScript#7) 
   
org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:518) 
   
Exception: org.alfresco.service.cmr.repository.ScriptException - Failed to execute script 'C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\com\cdi\Upload.post.js': ReferenceError: "formdata" is not defined. (AlfrescoScript#7) 
   
org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:116) 
   
Exception: org.alfresco.service.cmr.repository.ScriptException - Failed to execute script 'C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\com\cdi\Upload.post.js': Failed to execute script 'C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\com\cdi\Upload.post.js': ReferenceError: "formdata" is not defined. (AlfrescoScript#7) 
   
org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:246) 
   
Server: Alfresco Community Network v2.1.0 (482) schema 64 
Time: Feb 7, 2008 3:58:31 PM 
  
Diagnostics: Inspect Web Script (com/cdi/Upload.post)

workingpeter
Champ in-the-making
Champ in-the-making
The formdata variable is not available in 2.1. It was introduced into the Enterprise version initially and has made its way into Community 2.9.

As a migration note, when I moved from 2.1 to 2.9 for this very reason I found that roothome is not available unless the script execution is authenticated (in 2.1 guest execution has access to roothome).

eg
Champ in-the-making
Champ in-the-making
Thanks. Do you know if formdata is available in 2.1.1 Enterprise version?

workingpeter
Champ in-the-making
Champ in-the-making
Yes it is.