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.
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?
Tq Jeff. Now I am using NodeService/Search Service to retrieve the FolderContent.
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.