From: Sanjay Dalal Date: Wed, 18 Mar 2009 21:12:04 +0000 (+0000) Subject: prototype using nuxeo restful apis X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=b7cd73c9de4a26fa3e3d050632eaed3e56e1938c;p=tmp%2Fjakarta-migration.git prototype using nuxeo restful apis --- diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/build.properties b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/build.properties new file mode 100644 index 000000000..50c373854 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/build.properties @@ -0,0 +1,4 @@ +jboss.dir=C:/dev/jboss-4.2.3.GA +nuxeo.dir=${jboss.dir}/server/default/deploy/nuxeo.ear/system +nuxeo.local.repo.dir=file:///C:/dev/jboss-4.2.3.GA/server/default/deploy/nuxeo.ear/system +nuxeo.local.repo.client.dir=file:///C:/dev/nuxeo/nuxeo-core/nuxeo-core-client/target diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/pom.all.xml b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/pom.all.xml new file mode 100644 index 000000000..5aa2f743c --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/pom.all.xml @@ -0,0 +1,1079 @@ + + + + helloworld + org.collectionspace.hello.services + 0.1 + + 4.0.0 + org.collectionspace.hello.services + helloworld-doc-service + war + 0.1 + Helloworld Document Service + + + + java.net + http://download.java.net/maven/1 + legacy + + + maven repo + maven repo + http://repo1.maven.org/maven2/ + + + + jboss + jboss repo + http://repository.jboss.org/maven2 + + + mojo + mojo repo + http://svn.codehaus.org/mojo/trunk/mojo/jboss-maven-plugin + + + cspace.local.nuxeo + ${nuxeo.local.repo.dir} + + + public + http://maven.nuxeo.org/public + + + + false + + + + public-snapshot + http://maven.nuxeo.org/public-snapshot + + false + + + + + + + + + + + public + http://maven.nuxeo.org/public + Nuxeo virtual release repository + + + + false + + + + public-snapshot + http://maven.nuxeo.org/public-snapshot + Nuxeo virtual snapshot repository + + false + + + + + + + + + 4.2.3.GA + 3.0 + UTF-8 + 1.5.1 + 0.9.7 + 5.2-SNAPSHOT + 1.5-SNAPSHOT + + + + + + org.collectionspace.hello.services + helloworld-jaxb + 0.1 + + + org.jboss.resteasy + resteasy-jaxrs + 1.0.2.GA + + + + tjws + webserver + + + + + org.jboss.resteasy + resteasy-jaxb-provider + 1.0.2.GA + + + junit + junit + 4.1 + test + + + + + + + javax.ejb + ejb + 3.0 + provided + + + + javax.ejb + ejb-api + 3.0 + provided + + + + javax.annotation + jsr250-api + 1.0 + provided + + + + javax.security + jaas + 1.0.01 + provided + + + + + + + + + dom4j + dom4j + 1.6.1 + provided + + + + + + + + + + + + + + + + + org.nuxeo.ecm.core + nuxeo-core-api + ${nuxeo.version.1.5} + + + + org.nuxeo.ecm.platform + nuxeo-platform-api + ${nuxeo.version.5.2} + + + + + + + + helloworld + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + surefire-it + integration-test + + test + + + false + + + + + + org.apache.maven.plugins + maven-war-plugin + 2.0 + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + + diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/pom.xml b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/pom.xml new file mode 100644 index 000000000..41b3e6279 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/pom.xml @@ -0,0 +1,270 @@ + + + + helloworld + org.collectionspace.hello.services + 0.1 + + 4.0.0 + org.collectionspace.hello.services + helloworld-doc-service + war + 0.1 + Helloworld Document Service + + + + java.net + http://download.java.net/maven/1 + legacy + + + maven repo + maven repo + http://repo1.maven.org/maven2/ + + + + jboss + jboss repo + http://repository.jboss.org/maven2 + + + mojo + mojo repo + http://svn.codehaus.org/mojo/trunk/mojo/jboss-maven-plugin + + + + maven-restlet + Public online Restlet repository + http://maven.restlet.org + + + + cspace.local.nuxeo + ${nuxeo.local.repo.dir} + + + cspace.local.nuxeo.client + ${nuxeo.local.repo.client.dir} + + + public + http://maven.nuxeo.org/public + + + + false + + + + public-snapshot + http://maven.nuxeo.org/public-snapshot + + false + + + + + + + + + + + public + http://maven.nuxeo.org/public + Nuxeo virtual release repository + + + + false + + + + public-snapshot + http://maven.nuxeo.org/public-snapshot + Nuxeo virtual snapshot repository + + false + + + + + + + + + 4.2.3.GA + 3.0 + UTF-8 + 1.5.1 + 0.9.7 + 5.2-SNAPSHOT + 1.5-SNAPSHOT + + + + + + org.collectionspace.hello.services + helloworld-jaxb + 0.1 + + + org.jboss.resteasy + resteasy-jaxrs + 1.0.2.GA + + + tjws + webserver + + + + + org.jboss.resteasy + resteasy-jaxb-provider + 1.0.2.GA + + + junit + junit + 4.1 + test + + + + + + + javax.security + jaas + 1.0.01 + provided + + + + dom4j + dom4j + 1.6.1 + provided + + + + + + + + org.nuxeo.ecm.core + nuxeo-core-api + ${nuxeo.version.1.5} + + + jboss-remoting + jboss + + + + + + + + org.restlet + org.restlet + 1.0.7 + + + com.noelios.restlet + com.noelios.restlet.ext.httpclient + 1.0.7 + + + com.noelios.restlet + com.noelios.restlet + 1.0.7 + + + + jaxen + jaxen + 1.1.1 + + + + + + + helloworld + + + org.codehaus.mojo + jboss-maven-plugin + + ${jboss.dir} + + + + jboss-undeploy + pre-integration-test + + undeploy + + + ${basedir}/target/helloworld.war + + + + jboss-deploy + pre-integration-test + + deploy + + + ${basedir}/target/helloworld.war + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + surefire-it + integration-test + + test + + + false + + + + + + org.apache.maven.plugins + maven-war-plugin + 2.0 + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + + diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/HelloworldDocApplication.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/HelloworldDocApplication.java new file mode 100644 index 000000000..1ccfcac41 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/HelloworldDocApplication.java @@ -0,0 +1,25 @@ +package org.collectionspace.hello.services; + +import javax.ws.rs.core.Application; +import java.util.HashSet; +import java.util.Set; + +public class HelloworldDocApplication extends Application { + + private Set singletons = new HashSet(); + private Set> empty = new HashSet>(); + + public HelloworldDocApplication() { + singletons.add(new PersonDocResource()); + } + + @Override + public Set> getClasses() { + return empty; + } + + @Override + public Set getSingletons() { + return singletons; + } +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/PersonDocResource.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/PersonDocResource.java new file mode 100644 index 000000000..9679b06bd --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/PersonDocResource.java @@ -0,0 +1,273 @@ +package org.collectionspace.hello.services; + +import org.collectionspace.hello.services.nuxeo.NuxeoRESTClient; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.UriInfo; +import org.collectionspace.hello.Person; +import org.collectionspace.hello.Persons; + +import org.restlet.resource.Representation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("/persons") +@Consumes("application/xml") +@Produces("application/xml") +public class PersonDocResource { + + final Logger logger = LoggerFactory.getLogger(PersonDocResource.class); + private Map personDB = new ConcurrentHashMap(); + private AtomicLong idCounter = new AtomicLong(); + + public PersonDocResource() { + } + + @GET + public Persons getPersons(@Context UriInfo ui) { + try { + getRepository(); + getQueryModel(); + getVocabulary(); + } catch (Exception e) { + e.printStackTrace(); + } + Persons persons = new Persons(); + return persons; + } + + private void getRepository() throws IOException { + NuxeoRESTClient nxClient = getClient(); + + List pathParams = new ArrayList(); + Map queryParams = new HashMap(); + //browse default repository + pathParams = Arrays.asList("default", "*", "browse"); + Representation res = nxClient.get(pathParams, queryParams); + String resStr = res.getText(); + verbose("getRepository:" + resStr); + } + + private void getQueryModel() throws IOException { + NuxeoRESTClient nxClient = getClient(); + + List pathParams = new ArrayList(); + Map queryParams = new HashMap(); + + //query model for user documents + pathParams = Arrays.asList("execQueryModel", "USER_DOCUMENTS"); + queryParams.put("QP1", "Administrator"); + queryParams.put("format", "XML"); + + + Representation res = nxClient.get(pathParams, queryParams); + String resStr = res.getText(); + verbose("getQueryModel:" + resStr); + + } + + private void getVocabulary() throws IOException { + NuxeoRESTClient nxClient = getClient(); + + List pathParams = new ArrayList(); + Map queryParams = new HashMap(); + //get vocabulary + pathParams = Arrays.asList("vocabulary", "continent_country"); + queryParams.put("lang", "en"); + + Representation res = nxClient.get(pathParams, queryParams); + String resStr = res.getText(); + verbose("getVocabulary:" + resStr); + + } + + private NuxeoRESTClient getClient() { + NuxeoRESTClient nxClient = new NuxeoRESTClient("http://127.0.0.1:8080/nuxeo"); + nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC); + nxClient.setBasicAuthentication("Administrator", "Administrator"); + return nxClient; + } + +// @GET +// public Persons getPersons(@Context UriInfo ui) { +// String repoName = "default";// = (String) req.getAttributes().get("repoName"); +// String docid = "";// = (String) req.getAttributes().get("docid"); +// +// //fixme: how the heck navigationContext is initialized +//// NavigationContext navigationContext = null; +// CoreSession session = null; +// +// DOMDocumentFactory domfactory = new DOMDocumentFactory(); +// DOMDocument result = (DOMDocument) domfactory.createDocument(); +// // Element root = result.createElement("browse"); +// // result.setRootElement((org.dom4j.Element) root); +// +//// if (repoName == null || repoName.equals("*")) { +// try { +// getQueryModel(); +//// login(); +// +// // Connect to default repoName +//// NuxeoClient.getInstance().connect("localhost", 62474); +//// session = Framework.getService(RepositoryManager.class).getDefaultRepository().open(); +// +//// RepositoryManager rm = getRepositoryManager(); +//// String repoURI = rm.getRepository(repoName).getRepositoryUri(); +//// Repository repo = rm.getRepository(repoName); +//// if (repoURI == null) { +//// repoURI = repo.getName(); +//// } +//// +//// session = getSession(); +//// String sid = session.connect(repoURI, new HashMap()); +//// +//// // get the root +//// DocumentModel root = session.getRootDocument(); +//// System.out.print(root.getRef()); +//// +//// // get workspace root (expect default repoName layout) +//// DocumentModel ws = session.getDocument(new PathRef("/default-domain/workspaces")); +//// String title = ws.getTitle(); +//// +//// +// +//// RepositoryManager repmanager = Framework.getService(RepositoryManager.class); +//// Collection repos = repmanager.getRepositories(); +//// +//// Element serversNode = result.createElement("availableServers"); +//// result.setRootElement((org.dom4j.Element) serversNode); +//// +//// for (Repository availableRepo : repos) { +//// Element server = result.createElement("server"); +//// server.setAttribute("title", availableRepo.getName()); +//// server.setAttribute("url", getRelURL(availableRepo.getName(), "*")); +//// serversNode.appendChild(server); +//// } +// } catch (Exception e) { +//// handleError(result, res, e); +//// return; +// e.printStackTrace(); +// } +//// } else { +//// DocumentModel dm = null; +//// try { +//// //how t +//// navigationContext.setCurrentServerLocation(new RepositoryLocation( +//// repoName)); +//// session = navigationContext.getOrCreateDocumentManager(); +//// if (docid == null || docid.equals("*")) { +//// dm = session.getRootDocument(); +//// } else { +//// dm = session.getDocument(new IdRef(docid)); +//// } +//// } catch (ClientException e) { +//// //handleError(result, res, e); +//// //return; +//// e.printStackTrace(); +//// } +//// +//// Element current = result.createElement("document"); +//// try { +//// current.setAttribute("title", dm.getTitle()); +//// } catch (DOMException e1) { +//// //handleError(res, e1); +//// e1.printStackTrace(); +//// } catch (ClientException e1) { +//// //handleError(res, e1); +//// e1.printStackTrace(); +//// } +//// current.setAttribute("type", dm.getType()); +//// current.setAttribute("id", dm.getId()); +//// current.setAttribute("url", getRelURL(repoName, dm.getRef().toString())); +//// result.setRootElement((org.dom4j.Element) current); +//// +//// if (dm.isFolder()) { +//// // Element childrenElem = result.createElement("children"); +//// // root.appendChild(childrenElem); +//// +//// DocumentModelList children = null; +//// try { +//// children = session.getChildren(dm.getRef()); +//// } catch (ClientException e) { +//// //handleError(result, res, e); +//// //return; +//// e.printStackTrace(); +//// } +//// +//// for (DocumentModel child : children) { +//// Element el = result.createElement("document"); +//// try { +//// el.setAttribute("title", child.getTitle()); +//// } catch (DOMException e) { +//// //handleError(res, e); +//// e.printStackTrace(); +//// } catch (ClientException e) { +//// ///handleError(res, e); +//// e.printStackTrace(); +//// } +//// el.setAttribute("type", child.getType()); +//// el.setAttribute("id", child.getId()); +//// el.setAttribute("url", getRelURL(repoName, child.getRef().toString())); +//// current.appendChild(el); +//// } +//// } +//// } +// Persons persons = new Persons(); +// return persons; +// } +// private void login() throws LoginException { +// CallbackHandler handler = new NuxeoCallbackHandler("Administrator", +// "Administrator"); +// LoginContext lc = NuxeoLoginContextFactory.getLoginContext(handler); +// try { +// lc.login(); +// } catch (LoginException le) { +// System.out.print("Unable to login :" + le); +// } +// } +// +// private static RepositoryManager getRepositoryManager() +// throws NamingException { +// String beanRemoteLocation = "nuxeo/RepositoryManagerBean/remote"; +// javax.naming.Context ctx = getInitialContext(); +// Object proxy = ctx.lookup(beanRemoteLocation); +// return (RepositoryManager) proxy; +// } +// +// private static CoreSession getSession() +// throws Exception { +// +// String beanRemoteLocation = "nuxeo/DocumentManagerBean/remote"; +// javax.naming.Context ctx = getInitialContext(); +// Object proxy = ctx.lookup(beanRemoteLocation); +// return (CoreSession) proxy; +// +// } +// +// private static javax.naming.Context getInitialContext() throws NamingException { +//// Hashtable env = new Hashtable(); +//// env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); +//// env.put(javax.naming.Context.PROVIDER_URL, "jnp://localhost:1099"); +//// env.put(javax.naming.Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); +// return new InitialContext(); +// +// } +// +// private static String getRelURL(String repo, String uuid) { +// return '/' + repo + '/' + uuid; +// } + private void verbose(String msg) { + System.out.println("PersonDocResource: " + msg); + } +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoCallback.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoCallback.java new file mode 100644 index 000000000..8e78e0076 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoCallback.java @@ -0,0 +1,50 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.collectionspace.hello.services.nuxeo; + + +import javax.security.auth.callback.Callback; + +/** + * Copied from jbossx + * + * An implementation of Callback that simply obtains an Object to be used + * as the authentication credential. Interpretation of the Object is up to + * the LoginModules that validate the credential. + * + * @author Scott.Stark@jboss.org + */ +public class NuxeoCallback implements Callback { + + private final String prompt; + + private Object credential; + + public NuxeoCallback() { + this(""); + } + + public NuxeoCallback(String prompt) { + this.prompt = prompt; + } + + public String getPrompt() { + return prompt; + } + + public Object getCredential() { + return credential; + } + + public void setCredential(Object credential) { + this.credential = credential; + } + + public void clearCredential() { + credential = null; + } + +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoCallbackHandler.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoCallbackHandler.java new file mode 100644 index 000000000..e8fff8c25 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoCallbackHandler.java @@ -0,0 +1,85 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.collectionspace.hello.services.nuxeo; + +/** + * + * @author sanjaydalal + */ +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; + +/** + * Copied from jbossx. + * + * @author Scott.Stark@jboss.org + */ +public class NuxeoCallbackHandler implements CallbackHandler { + + private final String username; + private char[] password; + private final Object credential; + + /** + * Initializes the UsernamePasswordHandler with the username and password to + * use. + * + * @param username the user name + * @param password the password for this user + */ + public NuxeoCallbackHandler(String username, char[] password) { + this.username = username; + this.password = password; + credential = password; + } + + public NuxeoCallbackHandler(String username, Object credential) { + this.username = username; + this.credential = credential; + if (credential instanceof char[]) { + password = (char[]) credential; + } else if (credential instanceof CharSequence) { + password = credential.toString().toCharArray(); + } + } + + /** + * Sets any NameCallback name property to the instance username, sets any + * PasswordCallback password property to the instance, and any password. + * + * @exception UnsupportedCallbackException, + * thrown if any callback of type other than NameCallback or + * PasswordCallback are seen. + */ + public void handle(Callback[] callbacks) + throws UnsupportedCallbackException { + for (Callback c : callbacks) { + if (c instanceof NameCallback) { + NameCallback nc = (NameCallback) c; + nc.setName(username); + } else if (c instanceof PasswordCallback) { + PasswordCallback pc = (PasswordCallback) c; + if (password == null) { + // We were given an opaque Object credential but a char[] is + // requested? + if (credential != null) { + String tmp = credential.toString(); + password = tmp.toCharArray(); + } + } + pc.setPassword(password); + } else if (c instanceof NuxeoCallback) { + NuxeoCallback oc = (NuxeoCallback) c; + oc.setCredential(credential); + } else { + throw new UnsupportedCallbackException(c, + "Unrecognized Callback"); + } + } + } +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoLoginConfiguration.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoLoginConfiguration.java new file mode 100644 index 000000000..81cedc3c4 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoLoginConfiguration.java @@ -0,0 +1,53 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.collectionspace.hello.services.nuxeo; + +/** + * + * @author sanjaydalal + */ +import java.util.HashMap; +import java.util.Map; + +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag; + +public class NuxeoLoginConfiguration extends Configuration { + + private final Configuration parent; + public static final String LOGIN_DOMAIN = "nuxeo-client-login"; + + public NuxeoLoginConfiguration(Configuration parent) { + this.parent = parent; + } + + @Override + public AppConfigurationEntry[] getAppConfigurationEntry(String name) { + + if (LOGIN_DOMAIN.equals(name)) { + AppConfigurationEntry[] entries = new AppConfigurationEntry[1]; + + Map options = new HashMap(); + + options.put("restore-login-identity", "True"); + options.put("multi-threaded", "True"); + + entries[0] = new AppConfigurationEntry("org.jboss.security.ClientLoginModule", LoginModuleControlFlag.REQUIRED, options); + + + return entries; + } else { + return parent.getAppConfigurationEntry(name); + } + } + + @Override + public void refresh() { + if (parent != null) { + parent.refresh(); + } + } +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoLoginContextFactory.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoLoginContextFactory.java new file mode 100644 index 000000000..d1d23f7fc --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoLoginContextFactory.java @@ -0,0 +1,48 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.collectionspace.hello.services.nuxeo; + +/** + * + * @author sanjaydalal + */ + +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; + +public class NuxeoLoginContextFactory { + + + private static boolean initDone=false; + + private static void initLoginConfig() + { + if (initDone) + return; + + Configuration parentConfig = null; + try { + parentConfig = Configuration.getConfiguration(); + } catch (Exception e) { + // do nothing - this can happen if default configuration provider is not correctly configured + // for examnple FileConfig fails if no config file was defined + } + Configuration config = new NuxeoLoginConfiguration(parentConfig); + Configuration.setConfiguration(config); + + initDone=true; + + } + + + public static LoginContext getLoginContext(CallbackHandler handler) throws LoginException + { + initLoginConfig(); + return new LoginContext(NuxeoLoginConfiguration.LOGIN_DOMAIN, handler); + } +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoRESTClient.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoRESTClient.java new file mode 100644 index 000000000..175dca70a --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/NuxeoRESTClient.java @@ -0,0 +1,308 @@ +/* + * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser General Public License + * (LGPL) version 2.1 which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl.html + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * Contributors: + * Nuxeo - initial API and implementation + * + * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ + */ +package org.collectionspace.hello.services.nuxeo; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; + + +import org.restlet.Client; +import org.restlet.data.ChallengeResponse; +import org.restlet.data.ChallengeScheme; +import org.restlet.data.Cookie; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Method; +import org.restlet.data.Parameter; +import org.restlet.data.Protocol; +import org.restlet.data.Request; +import org.restlet.resource.OutputRepresentation; +import org.restlet.resource.Representation; +import org.restlet.util.Series; + +public class NuxeoRESTClient { + + public static final int AUTH_TYPE_NONE = 0; + public static final int AUTH_TYPE_BASIC = 1; + public static final int AUTH_TYPE_SECRET = 2; + protected String baseURL = "http://127.0.0.1:8080/nuxeo"; + protected String restPrefix = "restAPI"; + protected String davPrefix = "dav"; + protected List cookies; + protected int authType = AUTH_TYPE_NONE; + protected String userName; + protected String password; + protected String secretToken; + protected Client restClient; + + public NuxeoRESTClient(String baseURL) { + this.baseURL = baseURL; + } + + public NuxeoRESTClient(String protocol, String serverIP, String serverPort) { + this(protocol, serverIP, serverPort, "nuxeo"); + } + + public NuxeoRESTClient(String protocol, String serverIP, String serverPort, + String servletPath) { + StringBuffer sb = new StringBuffer(); + sb.append(protocol); + sb.append("://"); + sb.append(serverIP); + if (serverPort != null && !serverIP.equals("80")) { + sb.append(':'); + sb.append(serverPort); + } + sb.append(servletPath); + sb.append('/'); + baseURL = sb.toString(); + } + + public void setBasicAuthentication(String userName, String password) { + authType = AUTH_TYPE_BASIC; + this.userName = userName; + this.password = password; + } + + public void setSharedSecretAuthentication(String userName, + String secretToken) { + authType = AUTH_TYPE_SECRET; + this.userName = userName; + this.secretToken = secretToken; + } + + public void setCookies(List cookies) { + this.cookies = cookies; + } + + public Representation post(List pathParams, + Map queryParams, InputStream istream) { + String path = ""; + StringBuffer pathBuffer = new StringBuffer(); + + if (pathParams != null) { + for (String p : pathParams) { + pathBuffer.append(p); + pathBuffer.append('/'); + } + path = pathBuffer.toString(); + } + + return post(path, queryParams, istream); + } + + public Representation post(String subPath, + Map queryParams, InputStream istream) { + StringBuffer urlBuffer = new StringBuffer(); + + if (subPath.startsWith("/")) { + subPath = subPath.substring(1); + } + if (subPath.endsWith("/")) { + subPath = subPath.substring(0, subPath.length() - 1); + } + + urlBuffer.append(baseURL); + urlBuffer.append('/'); + urlBuffer.append(restPrefix); + urlBuffer.append('/'); + urlBuffer.append(subPath); + + if (queryParams != null) { + urlBuffer.append('?'); + for (String qpName : queryParams.keySet()) { + urlBuffer.append(qpName); + urlBuffer.append('='); + urlBuffer.append(queryParams.get(qpName).replaceAll(" ", "%20")); + urlBuffer.append('&'); + } + } + + String completeURL = urlBuffer.toString(); + + Request request = new Request(Method.POST, completeURL); + + setupAuth(request); + setupCookies(request); + final InputStream in = istream; + request.setEntity(new OutputRepresentation( + MediaType.MULTIPART_FORM_DATA) { + + @Override + public void write(OutputStream outputStream) throws IOException { + byte[] buffer = new byte[1024 * 64]; + int read; + while ((read = in.read(buffer)) != -1) { + outputStream.write(buffer, 0, read); + } + + } + }); + + return getRestClient().handle(request).getEntity(); + } + + public Representation get(List pathParams, + Map queryParams) { + String path = ""; + StringBuffer pathBuffer = new StringBuffer(); + + if (pathParams != null) { + for (String p : pathParams) { + pathBuffer.append(p); + pathBuffer.append('/'); + } + path = pathBuffer.toString(); + } + + return get(path, queryParams); + } + + public Representation get(String subPath, + Map queryParams) { + StringBuffer urlBuffer = new StringBuffer(); + + if (subPath.startsWith("/")) { + subPath = subPath.substring(1); + } + if (subPath.endsWith("/")) { + subPath = subPath.substring(0, subPath.length() - 1); + } + + urlBuffer.append(baseURL); + urlBuffer.append('/'); + urlBuffer.append(restPrefix); + urlBuffer.append('/'); + urlBuffer.append(subPath); + + if (queryParams != null) { + urlBuffer.append('?'); + for (String qpName : queryParams.keySet()) { + urlBuffer.append(qpName); + urlBuffer.append('='); + urlBuffer.append(queryParams.get(qpName).replaceAll(" ", "%20")); + urlBuffer.append('&'); + } + } + + String completeURL = urlBuffer.toString(); + System.out.println("\nNuxeoRESTClient: calling " + completeURL); + Request request = new Request(Method.GET, completeURL); + setupAuth(request); + setupCookies(request); + + return getRestClient().handle(request).getEntity(); + } + + protected void setupAuth(Request request) { + + if (authType == AUTH_TYPE_BASIC) { + ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; + ChallengeResponse authentication = new ChallengeResponse(scheme, + userName, password); + request.setChallengeResponse(authentication); + + } else if (authType == AUTH_TYPE_SECRET) { + Series additionnalHeaders = new Form(); + + Map securityHeaders = PortalSSOAuthenticationProvider.getHeaders( + secretToken, userName); + + for (String hn : securityHeaders.keySet()) { + additionnalHeaders.add(hn, securityHeaders.get(hn)); + } + + request.getAttributes().put("org.restlet.http.headers", + additionnalHeaders); + } + } + + protected void setupCookies(Request request) { + if (cookies != null) { + request.getCookies().clear(); + for (Cookie cookie : cookies) { + request.getCookies().add(cookie); + } + } + + } + + protected Client getRestClient() { + if (restClient == null) { + if (baseURL.startsWith("https")) { + restClient = new Client(Protocol.HTTPS); + } else { + restClient = new Client(Protocol.HTTP); + } + } + + return restClient; + } + + public int getAuthType() { + return authType; + } + + public void setAuthType(int authType) { + this.authType = authType; + } + + public String getDavPrefix() { + return davPrefix; + } + + public void setDavPrefix(String davPrefix) { + this.davPrefix = davPrefix; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRestPrefix() { + return restPrefix; + } + + public void setRestPrefix(String restPrefix) { + this.restPrefix = restPrefix; + } + + public String getSecretToken() { + return secretToken; + } + + public void setSecretToken(String secretToken) { + this.secretToken = secretToken; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/PortalSSOAuthenticationProvider.java b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/PortalSSOAuthenticationProvider.java new file mode 100644 index 000000000..ef1a61a40 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/java/org/collectionspace/hello/services/nuxeo/PortalSSOAuthenticationProvider.java @@ -0,0 +1,72 @@ +/* + * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser General Public License + * (LGPL) version 2.1 which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl.html + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * Contributors: + * Nuxeo - initial API and implementation + * + * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ + */ + +package org.collectionspace.hello.services.nuxeo; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +import com.noelios.restlet.util.Base64; + +public class PortalSSOAuthenticationProvider { + + private static final String TOKEN_SEP = ":"; + + private static final String TS_HEADER = "NX_TS"; + + private static final String RANDOM_HEADER = "NX_RD"; + + private static final String TOKEN_HEADER = "NX_TOKEN"; + + private static final String USER_HEADER = "NX_USER"; + + public static Map getHeaders(String secretKey, + String userName) { + + Map headers = new HashMap(); + + Date timestamp = new Date(); + int randomData = new Random(timestamp.getTime()).nextInt(); + + String clearToken = timestamp.getTime() + TOKEN_SEP + randomData + + TOKEN_SEP + secretKey + TOKEN_SEP + userName; + + byte[] hashedToken; + + try { + hashedToken = MessageDigest.getInstance("MD5").digest( + clearToken.getBytes()); + } catch (NoSuchAlgorithmException e) { + return null; + } + + String base64HashedToken = Base64.encodeBytes(hashedToken); + + headers.put(TS_HEADER, String.valueOf(timestamp.getTime())); + headers.put(RANDOM_HEADER, String.valueOf(randomData)); + headers.put(TOKEN_HEADER, base64HashedToken); + headers.put(USER_HEADER, userName); + + return headers; + } + +} diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/resources/jndi.properties b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/resources/jndi.properties new file mode 100644 index 000000000..a3aa40712 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/resources/jndi.properties @@ -0,0 +1,3 @@ +java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory +java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces +java.naming.provider.url=jnp://localhost:1099 \ No newline at end of file diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/resources/nuxeo.properties b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/resources/nuxeo.properties new file mode 100644 index 000000000..3c9d5afb5 --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/resources/nuxeo.properties @@ -0,0 +1,2 @@ +org.nuxeo.runtime.server.enabled=false +org.nuxeo.runtime.streaming.isServer=false diff --git a/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/webapp/WEB-INF/web.xml b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..d6187fc0d --- /dev/null +++ b/sandbox/sanjay/prototypes/restws/HelloWorldDocService/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,34 @@ + + + + HelloworldDoc + + + javax.ws.rs.Application + org.collectionspace.hello.services.HelloworldDocApplication + + + + resteasy.servlet.mapping.prefix + /cspace-doc + + + + + org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap + + + + + Resteasy + + org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher + + + + + Resteasy + /cspace-doc/* + + +