AnsweredAssumed Answered

401 error with AtomPubBinding

Question asked by raghav on Mar 20, 2017
Latest reply on Mar 30, 2017 by raghav

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:

http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom" 

 

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:

 

<webscript>
<shortname>Get documents</shortname>
<description>Return documents </description>
<url>/getDocuments</url>
<format default="json">extension</format>
<authentication runas="admin">none</authentication>
<!--<transaction>required</transaction>-->
</webscript>

 

 

public class MyDocumentSearchWebScript extends AbstractWebScript{

@Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
try
{
   // 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.

Outcomes