]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9679b06bdbdc9a4fb997c535972e7375216d9a4a
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.services;
2
3 import org.collectionspace.hello.services.nuxeo.NuxeoRESTClient;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.List;
9 import javax.ws.rs.Consumes;
10 import javax.ws.rs.GET;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.Produces;
13 import java.util.Map;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.atomic.AtomicLong;
16 import javax.ws.rs.core.Context;
17 import javax.ws.rs.core.UriInfo;
18 import org.collectionspace.hello.Person;
19 import org.collectionspace.hello.Persons;
20
21 import org.restlet.resource.Representation;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Path("/persons")
26 @Consumes("application/xml")
27 @Produces("application/xml")
28 public class PersonDocResource {
29
30     final Logger logger = LoggerFactory.getLogger(PersonDocResource.class);
31     private Map<Long, Person> personDB = new ConcurrentHashMap<Long, Person>();
32     private AtomicLong idCounter = new AtomicLong();
33
34     public PersonDocResource() {
35     }
36
37     @GET
38     public Persons getPersons(@Context UriInfo ui) {
39         try {
40             getRepository();
41             getQueryModel();
42             getVocabulary();
43         } catch (Exception e) {
44             e.printStackTrace();
45         }
46         Persons persons = new Persons();
47         return persons;
48     }
49
50     private void getRepository() throws IOException {
51         NuxeoRESTClient nxClient = getClient();
52
53         List<String> pathParams = new ArrayList<String>();
54         Map<String, String> queryParams = new HashMap<String, String>();
55         //browse default repository
56         pathParams = Arrays.asList("default", "*", "browse");
57         Representation res = nxClient.get(pathParams, queryParams);
58         String resStr = res.getText(); 
59         verbose("getRepository:" + resStr);
60     }
61
62     private void getQueryModel() throws IOException {
63         NuxeoRESTClient nxClient = getClient();
64
65         List<String> pathParams = new ArrayList<String>();
66         Map<String, String> queryParams = new HashMap<String, String>();
67
68         //query model for user documents
69         pathParams = Arrays.asList("execQueryModel", "USER_DOCUMENTS");
70         queryParams.put("QP1", "Administrator");
71         queryParams.put("format", "XML");
72
73
74         Representation res = nxClient.get(pathParams, queryParams);
75         String resStr = res.getText();
76         verbose("getQueryModel:" + resStr);
77
78     }
79
80     private void getVocabulary() throws IOException {
81         NuxeoRESTClient nxClient = getClient();
82
83         List<String> pathParams = new ArrayList<String>();
84         Map<String, String> queryParams = new HashMap<String, String>();
85         //get vocabulary
86         pathParams = Arrays.asList("vocabulary", "continent_country");
87         queryParams.put("lang", "en");
88
89         Representation res = nxClient.get(pathParams, queryParams);
90         String resStr = res.getText();
91         verbose("getVocabulary:" + resStr);
92
93     }
94
95     private NuxeoRESTClient getClient() {
96         NuxeoRESTClient nxClient = new NuxeoRESTClient("http://127.0.0.1:8080/nuxeo");
97         nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC);
98         nxClient.setBasicAuthentication("Administrator", "Administrator");
99         return nxClient;
100     }
101
102 //        @GET
103 //    public Persons getPersons(@Context UriInfo ui) {
104 //        String repoName = "default";// = (String) req.getAttributes().get("repoName");
105 //        String docid = "";// = (String) req.getAttributes().get("docid");
106 //
107 //        //fixme: how the heck navigationContext is initialized
108 ////        NavigationContext navigationContext = null;
109 //        CoreSession session = null;
110 //
111 //        DOMDocumentFactory domfactory = new DOMDocumentFactory();
112 //        DOMDocument result = (DOMDocument) domfactory.createDocument();
113 //        // Element root = result.createElement("browse");
114 //        // result.setRootElement((org.dom4j.Element) root);
115 //
116 ////        if (repoName == null || repoName.equals("*")) {
117 //        try {
118 //            getQueryModel();
119 ////            login();
120 //
121 //        // Connect to default repoName
122 ////        NuxeoClient.getInstance().connect("localhost", 62474);
123 ////        session = Framework.getService(RepositoryManager.class).getDefaultRepository().open();
124 //
125 ////            RepositoryManager rm = getRepositoryManager();
126 ////            String repoURI = rm.getRepository(repoName).getRepositoryUri();
127 ////            Repository repo = rm.getRepository(repoName);
128 ////            if (repoURI == null) {
129 ////                repoURI = repo.getName();
130 ////            }
131 ////
132 ////            session = getSession();
133 ////            String sid = session.connect(repoURI, new HashMap<String, Serializable>());
134 ////
135 ////            // get the root
136 ////            DocumentModel root = session.getRootDocument();
137 ////            System.out.print(root.getRef());
138 ////
139 ////            // get workspace root (expect default repoName layout)
140 ////            DocumentModel ws = session.getDocument(new PathRef("/default-domain/workspaces"));
141 ////            String title = ws.getTitle();
142 ////
143 ////
144 //
145 ////                RepositoryManager repmanager = Framework.getService(RepositoryManager.class);
146 ////                Collection<Repository> repos = repmanager.getRepositories();
147 ////
148 ////                Element serversNode = result.createElement("availableServers");
149 ////                result.setRootElement((org.dom4j.Element) serversNode);
150 ////
151 ////                for (Repository availableRepo : repos) {
152 ////                    Element server = result.createElement("server");
153 ////                    server.setAttribute("title", availableRepo.getName());
154 ////                    server.setAttribute("url", getRelURL(availableRepo.getName(), "*"));
155 ////                    serversNode.appendChild(server);
156 ////                }
157 //        } catch (Exception e) {
158 ////                handleError(result, res, e);
159 ////                return;
160 //            e.printStackTrace();
161 //        }
162 ////        } else {
163 ////            DocumentModel dm = null;
164 ////            try {
165 ////                //how t
166 ////                navigationContext.setCurrentServerLocation(new RepositoryLocation(
167 ////                        repoName));
168 ////                session = navigationContext.getOrCreateDocumentManager();
169 ////                if (docid == null || docid.equals("*")) {
170 ////                    dm = session.getRootDocument();
171 ////                } else {
172 ////                    dm = session.getDocument(new IdRef(docid));
173 ////                }
174 ////            } catch (ClientException e) {
175 ////                //handleError(result, res, e);
176 ////                //return;
177 ////                e.printStackTrace();
178 ////            }
179 ////
180 ////            Element current = result.createElement("document");
181 ////            try {
182 ////                current.setAttribute("title", dm.getTitle());
183 ////            } catch (DOMException e1) {
184 ////                //handleError(res, e1);
185 ////                e1.printStackTrace();
186 ////            } catch (ClientException e1) {
187 ////                //handleError(res, e1);
188 ////                e1.printStackTrace();
189 ////            }
190 ////            current.setAttribute("type", dm.getType());
191 ////            current.setAttribute("id", dm.getId());
192 ////            current.setAttribute("url", getRelURL(repoName, dm.getRef().toString()));
193 ////            result.setRootElement((org.dom4j.Element) current);
194 ////
195 ////            if (dm.isFolder()) {
196 ////                // Element childrenElem = result.createElement("children");
197 ////                // root.appendChild(childrenElem);
198 ////
199 ////                DocumentModelList children = null;
200 ////                try {
201 ////                    children = session.getChildren(dm.getRef());
202 ////                } catch (ClientException e) {
203 ////                    //handleError(result, res, e);
204 ////                    //return;
205 ////                    e.printStackTrace();
206 ////                }
207 ////
208 ////                for (DocumentModel child : children) {
209 ////                    Element el = result.createElement("document");
210 ////                    try {
211 ////                        el.setAttribute("title", child.getTitle());
212 ////                    } catch (DOMException e) {
213 ////                        //handleError(res, e);
214 ////                        e.printStackTrace();
215 ////                    } catch (ClientException e) {
216 ////                        ///handleError(res, e);
217 ////                        e.printStackTrace();
218 ////                    }
219 ////                    el.setAttribute("type", child.getType());
220 ////                    el.setAttribute("id", child.getId());
221 ////                    el.setAttribute("url", getRelURL(repoName, child.getRef().toString()));
222 ////                    current.appendChild(el);
223 ////                }
224 ////            }
225 ////        }
226 //        Persons persons = new Persons();
227 //        return persons;
228 //    }
229 //    private void login() throws LoginException {
230 //        CallbackHandler handler = new NuxeoCallbackHandler("Administrator",
231 //                "Administrator");
232 //        LoginContext lc = NuxeoLoginContextFactory.getLoginContext(handler);
233 //        try {
234 //            lc.login();
235 //        } catch (LoginException le) {
236 //            System.out.print("Unable to login :" + le);
237 //        }
238 //    }
239 //
240 //    private static RepositoryManager getRepositoryManager()
241 //            throws NamingException {
242 //        String beanRemoteLocation = "nuxeo/RepositoryManagerBean/remote";
243 //        javax.naming.Context ctx = getInitialContext();
244 //        Object proxy = ctx.lookup(beanRemoteLocation);
245 //        return (RepositoryManager) proxy;
246 //    }
247 //
248 //    private static CoreSession getSession()
249 //            throws Exception {
250 //
251 //        String beanRemoteLocation = "nuxeo/DocumentManagerBean/remote";
252 //        javax.naming.Context ctx = getInitialContext();
253 //        Object proxy = ctx.lookup(beanRemoteLocation);
254 //        return (CoreSession) proxy;
255 //
256 //    }
257 //
258 //    private static javax.naming.Context getInitialContext() throws NamingException {
259 ////        Hashtable env = new Hashtable();
260 ////        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
261 ////        env.put(javax.naming.Context.PROVIDER_URL, "jnp://localhost:1099");
262 ////        env.put(javax.naming.Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
263 //        return new InitialContext();
264 //
265 //    }
266 //
267 //    private static String getRelURL(String repo, String uuid) {
268 //        return '/' + repo + '/' + uuid;
269 //    }
270     private void verbose(String msg) {
271         System.out.println("PersonDocResource: " + msg);
272     }
273 }