03-20-2017 06:25 PM
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.
03-24-2017 02:50 PM
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?
03-30-2017 05:48 PM
Tq Jeff. Now I am using NodeService/Search Service to retrieve the FolderContent.
Explore our Alfresco products with the links below. Use labels to filter content by product module.