====== Autenticazione e utilizzo dei webservice di alfresco senza la sdk remote ====== :!: Non mi interessa se voi usate la SDK remote o se la SDK remote sia migliore o peggiore di axis o altri strumenti per webservices. Di seguito ci sono le istruzioni per autenticarsi in alfresco tramite webservices con uno strumento "generico", nel mio caso AXIS __Per connettersi ai WS di alfresco è necessario usare WSS4j per passare credenziali utente e ticket. E' comunque necessario prestare attenzione ad alcune cose__ ===== client_deploy.wsdd ===== Il file per il deploy del client axis deve essere simile al seguente attenzione al **Timestamp** che va passato per forza! (la doc di alfresco non lo maneziona) ==== user e password handler ==== l'handler per gestire i ticket e le credenziali deve essere tipo il seguente public class AlfresoPWCallback implements CallbackHandler { public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof WSPasswordCallback) { WSPasswordCallback pc = (WSPasswordCallback)callbacks[i]; String currentTicket = __TROVA_UN_MODO_DI_PASSARE_UNA_PASSWORD_TIPO_CON_UN_THREADLOCAL_O_CHE_TI_PARE; pc.setPassword(currentTicket); } else { throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback"); } } } } ==== inizializzatore WS ==== Schema - Caricare engineconfig con file client_deploy.wsdd (si può fare anche in via programamtica e sarebbe meglio ma non ho studiato le API e non sono sicuro di come si faccia) - Autenticarsi tramite webservice prendendo il ticket - Chiamare i webservice con l'engineconfig creato precedentemente - Disconnette la sessione tramite webservice String alfresco_base = "http://alfresco.mio.dominio/alfresco"; EngineConfiguration econfig = new FileProvider("client_deploy.wsdd"); AuthenticationServiceLocator authenticationServiceLocator = new AuthenticationServiceLocator(); AuthenticationServiceSoapPort auth = authenticationServiceLocator. getAuthenticationService(new URL(alfresco_base + "/api/AuthenticationService")); String password = "password"; AuthenticationResult sess = auth.startSession("pippo", password); String ticket = sess.getTicket(); METTI_IL_TICKET_DA_QUALCHE_PARTE UN_THREADLOCAL_VA_BENE ... chiama WS AdministrationServiceLocator asl = new AdministrationServiceLocator(econfig); AdministrationServiceSoapPort adminService = asl.getAdministrationService(new URL ( alfresco_base + "/api/AdministrationService")); UserDetails u = adminService.getUser("pippo"); System.out.println(u.getUserName());; .. fine chiama WS auth.endSession(ticket); === Argument qname is mandatory === Se durante una chiamata al webservice viene generato un messaggio di errore tipo ... argument qname is mandatory ... allora è bene controllare i parametri passati ai metodi degli stub. L'argomento property deve essere **non vuoto** (basta anche una stringa con uno spazio) ===== File completi ===== ==== client_config.wsdd ==== ==== AlfresoPWCallback.java ==== package mains; import java.io.IOException; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.UnsupportedCallbackException; import mains.Staticone; import org.apache.ws.security.WSPasswordCallback; public class AlfresoPWCallback implements CallbackHandler { /** * The implementation of the password callback used by the WS Security * * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[]) */ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (int i = 0; i < callbacks.length; i++) { if (callbacks[i] instanceof WSPasswordCallback) { WSPasswordCallback pc = (WSPasswordCallback)callbacks[i]; String currentTicket = Staticone.getPass(); // Set the password to be the current users ticket pc.setPassword(currentTicket); } else { throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback"); } } } } ==== staticone ==== package mains; public class Staticone { private static ThreadLocal pass = new ThreadLocal(); public static void setPass(String p) { pass.set(p); } public static String getPass() { return pass.get(); } public static void remove() { pass.remove(); } } ==== main ==== package mains; import java.net.MalformedURLException; import java.net.URL; import javax.xml.rpc.ServiceException; import org.alfresco.www.ws.model.content._1_0.ContentFormat; import org.alfresco.www.ws.model.content._1_0.NamedValue; import org.alfresco.www.ws.model.content._1_0.Predicate; import org.alfresco.www.ws.model.content._1_0.Reference; import org.alfresco.www.ws.model.content._1_0.ResultSet; import org.alfresco.www.ws.model.content._1_0.ResultSetRow; import org.alfresco.www.ws.model.content._1_0.ResultSetRowNode; import org.alfresco.www.ws.model.content._1_0.Store; import org.alfresco.www.ws.service.administration._1_0.AdministrationServiceLocator; import org.alfresco.www.ws.service.administration._1_0.AdministrationServiceSoapPort; import org.alfresco.www.ws.service.administration._1_0.UserDetails; import org.alfresco.www.ws.service.authentication._1_0.AuthenticationResult; import org.alfresco.www.ws.service.authentication._1_0.AuthenticationServiceLocator; import org.alfresco.www.ws.service.authentication._1_0.AuthenticationServiceSoapPort; import org.alfresco.www.ws.service.content._1_0.Content; import org.alfresco.www.ws.service.content._1_0.ContentServiceLocator; import org.alfresco.www.ws.service.content._1_0.ContentServiceSoapPort; import org.alfresco.www.ws.service.repository._1_0.QueryResult; import org.alfresco.www.ws.service.repository._1_0.RepositoryServiceLocator; import org.alfresco.www.ws.service.repository._1_0.RepositoryServiceSoapPort; import org.apache.axis.EngineConfiguration; import org.apache.axis.configuration.FileProvider; import org.oasis_open.docs.ns.cmis.ws._200908.NavigationServiceLocator; import org.oasis_open.docs.ns.cmis.ws._200908.NavigationServicePort; import org.oasis_open.docs.ns.cmis.ws._200908.ObjectServiceLocator; import org.oasis_open.docs.ns.cmis.ws._200908.ObjectServicePort; public class MainAuth { /** * @param args * @throws ServiceException * @throws MalformedURLException */ public static void main(String[] args) throws Exception { String alfresco_base = "http://mioserveralfresco/alfresco"; EngineConfiguration econfig = new FileProvider("client_deploy.wsdd"); AuthenticationServiceLocator authenticationServiceLocator = new AuthenticationServiceLocator(); AuthenticationServiceSoapPort auth = authenticationServiceLocator.getAuthenticationService(new URL(alfresco_base + "/api/AuthenticationService")); String password = "lamiapassword_che_ti_frega"; AuthenticationResult sess = auth.startSession("pippo", password); String ticket = sess.getTicket(); Staticone.setPass(ticket); AdministrationServiceLocator asl = new AdministrationServiceLocator(econfig); RepositoryServiceLocator repsl = new RepositoryServiceLocator(econfig); ContentServiceLocator contentSl = new ContentServiceLocator(econfig); ObjectServiceLocator osl = new ObjectServiceLocator(econfig); NavigationServiceLocator nsl = new NavigationServiceLocator(econfig); AdministrationServiceSoapPort adminService = asl.getAdministrationService(new URL ( alfresco_base + "/api/AdministrationService")); RepositoryServiceSoapPort repoService = repsl.getRepositoryService(new URL ( alfresco_base + "/api/RepositoryService")); ContentServiceSoapPort contentService = contentSl.getContentService(new URL ( alfresco_base + "/api/ContentService") ); ObjectServicePort objectService = osl.getObjectServicePort(new URL(alfresco_base + "/cmis/ObjectService")); NavigationServicePort navSerivce = nsl.getNavigationServicePort(new URL(alfresco_base + "/cmis/NavigationService")); UserDetails u = adminService.getUser("pippo"); System.out.println(u.getUserName());; System.out.println( u.toString()); Store[] stores = repoService.getStores(); for (Store s : stores) { System.out.println("STORE " + s.getScheme() + " " + s.getAddress() ); Predicate p = new Predicate(); p.setStore(s); Content[] contenuti = contentService.read(p, " "); for (Content contenuto : contenuti) { System.out.println(">> url " + contenuto.getUrl()); ContentFormat formato = contenuto.getFormat(); if (formato != null) { System.out.println(">>> " + formato.getMimetype()); System.out.println(">>> " + formato.getEncoding()); } Reference n = contenuto.getNode(); if (n != null) { System.out.println(">>> uuid " + n.getUuid()); System.out.println(">>> path " + n.getPath()); } QueryResult qc = repoService.queryChildren(n); if (qc != null){ ResultSet qrs = qc.getResultSet(); if (qrs.getTotalRowCount() > 0 ) { ResultSetRow[] qrows = qrs.getRows(); for (ResultSetRow qrow : qrows) { NamedValue[] qrcols = qrow.getColumns(); ResultSetRowNode qnode = qrow.getNode(); for (String sas : qnode.getAspects() ) { System.out.println(">>> Aspect " + sas); } for (NamedValue qnv : qrcols) { System.out.println(">>> > nome colonna" + qnv.getName()); if (qnv.getIsMultiValue()) { for (String qnvs : qnv.getValues()) { System.out.println(">>> > " + qnvs); } } else { System.out.println(">>> > " + qnv.getValue()); } } } } } } } auth.endSession(ticket); } }