401 error with AtomPubBinding

Showing results for 
Search instead for 
Did you mean: 
Member II

401 error with AtomPubBinding

New to Alfresco.

This code works when run as Java main method, but throws 401 (unauthorized) error, when run as Java web script.

It happens with the server running in local Windows machine.

With AtomPub:


public Session getSession() {

if (session == null) {
   SessionFactory factory = SessionFactoryImpl.newInstance();
   Map<String, String> parameter = new HashMap<String, String>();
   // user credentials
   parameter.put(SessionParameter.USER, "admin");
   parameter.put(SessionParameter.PASSWORD, "admin");

   // connection settings
   parameter.put(SessionParameter.ATOMPUB_URL, getServiceUrl()); 
   parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value()); 

   List<Repository> repositories = factory.getRepositories(parameter);
   this.session = repositories.get(0).createSession();
   return this.session;

The web script code is:

<shortname>Get documents</shortname>
<description>Return documents </description>
<format default="json">extension</format>
<authentication runas="admin">none</authentication>

public class MyDocumentSearchWebScript extends AbstractWebScript{

public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
   // This is where I have more code to call a service, which will return JSON response
catch(Exception e)
throw new WebScriptException("Unable to serialize JSON");



Inside the code, I get 401 error code in the variable resp in internal code of Alfresco. 

protected Response read(UrlBuilder url) {
// make the call
Response resp = getHttpInvoker().invokeGET(url, session);

// check response code
if (resp.getResponseCode() != 200) {
throw convertStatusCode(resp.getResponseCode(), resp.getResponseMessage(), resp.getErrorContent(), null);

return resp;

The password and user ID of admin/admin are correct.

2 Replies

Re: 401 error with AtomPubBinding

Why do you need to use CMIS from inside a Java-backed web script? Just inject the services you need (like NodeService, SearchService, etc.) and use those services (aka, the Alfresco Foundation API) to hit Alfresco natively.

Is your web script running on the repository tier or the Share tier?

Member II

Re: 401 error with AtomPubBinding

Tq Jeff. Now I am using NodeService/Search Service to retrieve the FolderContent.