cancel
Showing results for 
Search instead for 
Did you mean: 

'401 Unauthorized' when accessing to webscript from indicator in Alfresco Share

ardamose123
Champ on-the-rise
Champ on-the-rise
I have a Share indicator whose evaluator must retrieve the result of an Alfresco webscript.


import org.alfresco.web.evaluator.BaseEvaluator;
import org.json.simple.JSONObject;
import org.springframework.extensions.surf.ServletUtil;
import org.springframework.extensions.surf.exception.ConnectorServiceException;
import org.springframework.extensions.surf.support.ThreadLocalRequestContext;
import org.springframework.extensions.webscripts.connector.Connector;

public class CustomEvaluator extends BaseEvaluator
{
   @Override
   public boolean evaluate(JSONObject node)
   {
      try
      {
         Connector conn = ThreadLocalRequestContext.getRequestContext().getServiceRegistry().getConnectorService().getConnector("alfresco");
         return conn.call("/some/webscript/run?nodeRef=" + (String) node.get("nodeRef")).getResponse().equals("YES");
      }
      catch (ConnectorServiceException ex)
      {
         return false;
      }
   }
}


However, when the evaluator gets executed, Alfresco returns a 401 Unauthorized HTTP response status code every time.

Other observations:
<ul>
<li>If I try to access the webscript directly from the browser (since it's a simple GET method), it works fine.
<li>If I access the webscript as a Share document library action, it works fine.
<li>In the last two observations, cookies are sent in the HTTP request (JSESSION, something starting with "alf", some PHP-specific). In the evaluator one, no cookies are sent.
</ul>

So, the problem must be in the way I'm trying to access it from an evaluator.

Is there any way to include the authentication information of the current session using the Connector Service to access webscripts which require authentication?
9 REPLIES 9

rjohnson
Star Contributor
Star Contributor
As the evaluators run "inside" Alfresco I suspect that you won't be able to get the cookie (as thats a client side thing) but you should be able to get the ticket and send the ticket number on the URL like &alf_ticket=????????? that should then provide enough authorisation.

rjohnson
Star Contributor
Star Contributor
As the evaluators run "inside" Alfresco I suspect that you won't be able to get the cookie (as thats a client side thing) but you should be able to get the ticket and send the ticket number on the URL like &alf_ticket=????????? that should then provide enough authorisation.

Both the indicator and its evaluator are deployed in Share, not in Alfresco.

I tried to get the Alfresco ticket of the current session, but to no avail. Is there a way to get it without asking users to type their credentials again?

Below are some snippets of my current code. The evaluator is being executed, but it keeps giving the 401 status code. All of them are deployed in Share using a JAR file.

alfresco/web-extension/evaluator-context.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
   <!– No problems with this one –>
   <bean id="evaluator.doclib.action.isPdf" parent="evaluator.doclib.action.isMimetype">
      <property name="mimetypes">
         <list>
            <value>application/pdf</value>
         </list>
      </property>
   </bean>

   <!– This is the problematic one –>
   <bean id="evaluator.doclib.action.isItReady" class="some.internal.package.CustomEvaluator" />
</beans>


alfresco/web-extension/share-config-custom.xml

<alfresco-config>
   …
   <config evaluator="string-compare" condition="DocumentLibrary" replace="true">
      …
      <indicators>
         <indicator id="indicator.doclib.action.isItReady" icon="myicon-16.png" index="100" label="Yea!">
            <evaluator>evaluator.doclib.action.isItReady</evaluator>
         </indicator>
      </indicators>
   </config>
</alfresco-config>


This one <em>is</em> deployed in Alfresco: the webscript I'm trying to contact from the evaluator in Share. I'll include just the webscript descriptor, since the authentication configuration is there.

alfresco/extension/templates/webscipts/some/folder

<webscript>
   <shortname>Some name</shortname>
   <description>Some description</description>
   <url>/some/folder/isItReady</url>
   <authentication>user</authentication>
   <format default="json">argument</format>
   <family>Custom</family>
</webscript>


The authentication=user is needed. It won't do if it's changed to "none" or "guest".

I needed the ticket in share once so I wrote a simple webscript that I called from the share layer via the slingshot alfresco proxy which just called an FTL which returned JSON and output ${session.ticket}. I then used this ticket in a share URL. Messy because you will have to call a webscript via the proxy before you call your pukka script.

I tried to get it via Freemarker in Share, but it complained about "session" not being defined.

I enabled a webscript in Alfresco which does this and it works, but still requires "user" authentication, which takes me back to square one.

I tried to get it from some objects in Share, but to no avail.

Is there any other way to get the current session's ticket in Share from a Java evaluator?

deepak1987
Star Contributor
Star Contributor
Hi,

Use the following code snippet for Authentication.

<java>

import org.springframework.extensions.surf.ServletUtil;


Connector conn = ThreadLocalRequestContext.getRequestContext().getServiceRegistry().getConnectorService().getConnector("alfresco", ServletUtil.getSession());

</java>




Thanks for your solution, but Alfresco webscript still gives a 401 Unauthorized status code.

ardamose123
Champ on-the-rise
Champ on-the-rise
More information:

Request made by the evaluator through the "alfresco" connector in Share:


GET /alfresco/s/some/folder/isItReady?nodeRef=workspace://SpacesStore/2d4e61ab-8508-4f7f-834c-1f49170d096c HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: localhost:8080


The response from Alfresco:


HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
WWW-Authenticate: Basic realm="Alfresco"
Content-Type: text/html;charset=utf-8
Content-Length: 951
Date: Thu, 14 Nov 2013 22:23:54 GMT

(Some HTML…)
This request requires HTTP authentication.
(Some more HTML…)

deepak1987
Star Contributor
Star Contributor
Hi Ariel,

Can you tell me what are you doing in /folder/isItReady Webscript??