public String getVocabularyLabel(String voc_name, String key) { DirectoryService ds = Framework.getLocalService(DirectoryService.class); if (ds == null) { return key; } List<String> vocs = new ArrayList<String>(); List<String> keys = new ArrayList<String>(); if (voc_name.contains("/")) { String[] parts = voc_name.split("\\/"); for (String part : parts) { vocs.add(part); } parts = key.split("\\/"); for (String part : parts) { keys.add(part); } } else { vocs.add(voc_name); keys.add(key); } List<String> values = new ArrayList<String>(); for (int i = 0; i < vocs.size(); i++) { String voc = vocs.get(i); String keyValue = keys.get(i); if (ds.getDirectoryNames().contains(voc)) { Directory dir = ds.getDirectory(voc); String schema = dir.getSchema(); if ("vocabulary".equals(schema) || "xvocabulary".equals(schema)) { try (Session session = dir.getSession()) { DocumentModel entry = session.getEntry(keyValue); if (entry != null) { values.add((String) entry.getProperty(schema, "label")); } } } } } if (values.size() == 0) { return key; } else if (values.size() == 1) { return values.get(0); } else { String result = ""; for (int i = 0; i < values.size(); i++) { if (i > 0) { result = result + " / "; } result = result + values.get(i); } return result; } }
protected NuxeoOAuthConsumer getEntry(String consumerKey, String keyType) { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { DocumentModel entry = session.getEntry(consumerKey); if (entry == null) { return null; } return NuxeoOAuthConsumer.createFromDirectoryEntry(entry, keyType); } }
public static DocumentModel getDirectoryEntry(String directoryName, String entryId) throws ClientException { if (entryId == null) { return null; } DirectoryService dirService = Framework.getService(DirectoryService.class); try (Session session = dirService.open(directoryName)) { return session.getEntry(entryId); } }
@Override public void deleteConsumer(String consumerKey) { try { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { session.deleteEntry(consumerKey); } } catch (ClientException e) { log.error("Unable to delete consumer " + consumerKey, e); } }
@Override public void registerContribution( Object contribution, String extensionPoint, ComponentInstance contributor) { MockMemoryDirectoryDescriptor desc = (MockMemoryDirectoryDescriptor) contribution; String directoryName = desc.name; reg.put(directoryName, desc); try { DirectoryService directoryService = Framework.getService(DirectoryService.class); directoryService.registerDirectory(directoryName, this); } catch (Exception e) { throw new DirectoryException("Error in Directory Service lookup", e); } }
@Override public void unregisterContribution( Object contribution, String extensionPoint, ComponentInstance contributor) { String directoryName = ((MockMemoryDirectoryDescriptor) contribution).name; reg.remove(directoryName); try { DirectoryService directoryService = Framework.getService(DirectoryService.class); if (directoryService != null) { directoryService.unregisterDirectory(directoryName, this); } } catch (Exception e) { throw new DirectoryException("Error in Directory Service lookup", e); } }
@After public void tearDown() throws Exception { memoryDirectoryFactory.unregisterDirectory(memdir1); memoryDirectoryFactory.unregisterDirectory(memdir2); memoryDirectoryFactory.unregisterDirectory(memdir3); directoryService.unregisterDirectory("memdirs", memoryDirectoryFactory); }
@Test public void testUpdateReadonlyMultidir() throws Exception { MultiDirectory readonlyMultidir = (MultiDirectory) directoryService.getDirectory("readonlymulti"); MultiDirectorySession readonlyDir = (MultiDirectorySession) readonlyMultidir.getSession(); Session dir1 = memdir1.getSession(); Session dir2 = memdir2.getSession(); Session dir3 = memdir3.getSession(); // multi-subdirs update DocumentModel e = readonlyDir.getEntry("1"); assertEquals("foo1", e.getProperty("schema3", "thefoo")); assertEquals("bar1", e.getProperty("schema3", "thebar")); e.setProperty("schema3", "thefoo", "fffooo1"); e.setProperty("schema3", "thebar", "babar1"); readonlyDir.updateEntry(e); e = readonlyDir.getEntry("1"); // the multidirectory entry was not updated assertEquals("foo1", e.getProperty("schema3", "thefoo")); assertEquals("bar1", e.getProperty("schema3", "thebar")); // neither the underlying directories assertEquals("foo1", dir1.getEntry("1").getProperty("schema1", "foo")); assertEquals("bar1", dir2.getEntry("1").getProperty("schema2", "bar")); assertNull(dir3.getEntry("1")); }
@Override public NuxeoOAuthConsumer storeConsumer(NuxeoOAuthConsumer consumer) { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { Map<String, Object> init = new HashMap<String, Object>(); init.put("consumerKey", consumer.consumerKey); DocumentModel entry = session.createEntry(init); consumer.asDocumentModel(entry); session.updateEntry(entry); if (entry == null) { return null; } consumer = NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null); return consumer; } }
@Override public List<NuxeoOAuthConsumer> listConsumers() { List<NuxeoOAuthConsumer> result = new ArrayList<NuxeoOAuthConsumer>(); try { DirectoryService ds = Framework.getService(DirectoryService.class); try (Session session = ds.open(DIRECTORY_NAME)) { DocumentModelList entries = session.getEntries(); for (DocumentModel entry : entries) { result.add(NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null)); } } } catch (ClientException e) { log.error("Error while fetching consumer directory", e); } return result; }
public static DocumentModelList getDirectoryEntries(String directoryName, String... entryIds) throws ClientException { if (entryIds == null) { return null; } DirectoryService dirService = Framework.getService(DirectoryService.class); try (Session session = dirService.open(directoryName)) { DocumentModelList result = new DocumentModelListImpl(); for (String entryId : entryIds) { DocumentModel entry = session.getEntry(entryId); if (entry != null) { result.add(entry); } } return result; } }
protected void createUser(String userName, String password) { try (org.nuxeo.ecm.directory.Session userDir = directoryService.open("userDirectory")) { Map<String, Object> user = new HashMap<String, Object>(); user.put("username", userName); user.put("password", password); userDir.createEntry(user); } }
@Test public void testReadOnlyEntryFromMultidirectory() throws Exception { MultiDirectory readonlyMultidir = (MultiDirectory) directoryService.getDirectory("readonlymulti"); MultiDirectorySession readonlyDir = (MultiDirectorySession) readonlyMultidir.getSession(); // all should be readonly assertTrue(BaseSession.isReadOnlyEntry(readonlyDir.getEntry("1"))); assertTrue(BaseSession.isReadOnlyEntry(readonlyDir.getEntry("2"))); assertTrue(BaseSession.isReadOnlyEntry(readonlyDir.getEntry("3"))); assertTrue(BaseSession.isReadOnlyEntry(readonlyDir.getEntry("4"))); }
public static DirectoryEntry readJson(JsonParser jp, MultivaluedMap<String, String> httpHeaders) throws IOException { JsonToken tok = jp.nextToken(); // skip { if (jp.getCurrentToken() == JsonToken.START_OBJECT) { tok = jp.nextToken(); } String directoryName = null; JsonNode propertiesNode = null; while (tok != JsonToken.END_OBJECT) { String key = jp.getCurrentName(); jp.nextToken(); if ("directoryName".equals(key)) { directoryName = jp.readValueAs(String.class); } else if ("properties".equals(key)) { propertiesNode = jp.readValueAsTree(); } else if ("entity-type".equals(key)) { String entityType = jp.readValueAs(String.class); if (!DirectoryEntryWriter.ENTITY_TYPE.equals(entityType)) { throw new WebApplicationException(Response.Status.BAD_REQUEST); } } else { log.debug("Unknown key: " + key); jp.skipChildren(); } tok = jp.nextToken(); } DirectoryService ds = Framework.getLocalService(DirectoryService.class); Directory directory = ds.getDirectory(directoryName); if (directory == null) { throw new WebResourceNotFoundException("Directory " + directoryName + " does not exists"); } return getDirectoryEntryFromNode(propertiesNode, directory); }
@After public void tearDown() throws Exception { // delete certificates associated with user ids try (Session sqlSession = directoryService.open(CERTIFICATE_DIRECTORY_NAME)) { sqlSession.deleteEntry(DEFAULT_USER_ID); sqlSession.deleteEntry(SECOND_USER_ID); } // delete users userManager.deleteUser(DEFAULT_USER_ID); userManager.deleteUser(SECOND_USER_ID); }
protected void deleteUser(String userName) { try (org.nuxeo.ecm.directory.Session userDir = directoryService.open("userDirectory")) { userDir.deleteEntry(userName); } }
@Before public void setUp() { continentSession = directoryService.getDirectory("continent").getSession(); countrySession = directoryService.getDirectory("country").getSession(); }
@Before public void setUp() throws Exception { // mem dir factory directoryService = Framework.getLocalService(DirectoryService.class); memoryDirectoryFactory = new MemoryDirectoryFactory(); directoryService.registerDirectory("memdirs", memoryDirectoryFactory); // create and register mem directories Map<String, Object> e; // dir 1 Set<String> schema1Set = new HashSet<String>(Arrays.asList("uid", "foo")); memdir1 = new MemoryDirectory("dir1", "schema1", schema1Set, "uid", "foo"); memoryDirectoryFactory.registerDirectory(memdir1); Session dir1 = memdir1.getSession(); e = new HashMap<String, Object>(); e.put("uid", "1"); e.put("foo", "foo1"); dir1.createEntry(e); e = new HashMap<String, Object>(); e.put("uid", "2"); e.put("foo", "foo2"); dir1.createEntry(e); // dir 2 Set<String> schema2Set = new HashSet<String>(Arrays.asList("id", "bar")); memdir2 = new MemoryDirectory("dir2", "schema2", schema2Set, "id", null); memoryDirectoryFactory.registerDirectory(memdir2); Session dir2 = memdir2.getSession(); e = new HashMap<String, Object>(); e.put("id", "1"); e.put("bar", "bar1"); dir2.createEntry(e); e = new HashMap<String, Object>(); e.put("id", "2"); e.put("bar", "bar2"); dir2.createEntry(e); // dir 3 Set<String> schema3Set = new HashSet<String>(Arrays.asList("uid", "thefoo", "thebar")); memdir3 = new MemoryDirectory("dir3", "schema3", schema3Set, "uid", "thefoo"); memoryDirectoryFactory.registerDirectory(memdir3); Session dir3 = memdir3.getSession(); e = new HashMap<String, Object>(); e.put("uid", "3"); e.put("thefoo", "foo3"); e.put("thebar", "bar3"); dir3.createEntry(e); e = new HashMap<String, Object>(); e.put("uid", "4"); e.put("thefoo", "foo4"); e.put("thebar", "bar4"); dir3.createEntry(e); // the multi directory multiDir = (MultiDirectory) directoryService.getDirectory("multi"); dir = (MultiDirectorySession) multiDir.getSession(); }
/** Recomputes all the info needed for efficient access. */ private void recomputeSourceInfos() throws DirectoryException { final Schema schema = schemaManager.getSchema(schemaName); if (schema == null) { throw new DirectoryException( String.format("Directory '%s' has unknown schema '%s'", directory.getName(), schemaName)); } final Set<String> sourceFields = new HashSet<String>(); for (Field f : schema.getFields()) { sourceFields.add(f.getName().getLocalName()); } if (!sourceFields.contains(schemaIdField)) { throw new DirectoryException( String.format( "Directory '%s' schema '%s' has no id field '%s'", directory.getName(), schemaName, schemaIdField)); } List<SourceInfo> newSourceInfos = new ArrayList<SourceInfo>(2); for (SourceDescriptor source : descriptor.sources) { int ndirs = source.subDirectories.length; if (ndirs == 0) { throw new DirectoryException( String.format( "Directory '%s' source '%s' has no subdirectories", directory.getName(), source.name)); } final List<SubDirectoryInfo> subDirectoryInfos = new ArrayList<SubDirectoryInfo>(ndirs); SubDirectoryInfo authDirectoryInfo = null; boolean hasRequiredDir = false; for (SubDirectoryDescriptor subDir : source.subDirectories) { final String dirName = subDir.name; final String dirSchemaName = directoryService.getDirectorySchema(dirName); final String dirIdField = directoryService.getDirectoryIdField(dirName); final boolean dirIsAuth = directoryService.getDirectoryPasswordField(dirName) != null; final Map<String, String> fromSource = new HashMap<String, String>(); final Map<String, String> toSource = new HashMap<String, String>(); final Map<String, Serializable> defaultEntry = new HashMap<String, Serializable>(); final boolean dirIsOptional = subDir.isOptional; // XXX check authenticating final Schema dirSchema = schemaManager.getSchema(dirSchemaName); if (dirSchema == null) { throw new DirectoryException( String.format( "Directory '%s' source '%s' subdirectory '%s' " + "has unknown schema '%s'", directory.getName(), source.name, dirName, dirSchemaName)); } // record default field mappings if same name and record default // values final Set<String> dirSchemaFields = new HashSet<String>(); for (Field f : dirSchema.getFields()) { final String fieldName = f.getName().getLocalName(); dirSchemaFields.add(fieldName); if (sourceFields.contains(fieldName)) { // XXX check no duplicates! fromSource.put(fieldName, fieldName); toSource.put(fieldName, fieldName); } // XXX cast to Serializable defaultEntry.put(fieldName, (Serializable) f.getDefaultValue()); } // treat renamings // XXX id field ? for (FieldDescriptor field : subDir.fields) { final String sourceFieldName = field.forField; final String fieldName = field.name; if (!sourceFields.contains(sourceFieldName)) { throw new DirectoryException( String.format( "Directory '%s' source '%s' subdirectory '%s' " + "has mapping for unknown field '%s'", directory.getName(), source.name, dirName, sourceFieldName)); } if (!dirSchemaFields.contains(fieldName)) { throw new DirectoryException( String.format( "Directory '%s' source '%s' subdirectory '%s' " + "has mapping of unknown field' '%s'", directory.getName(), source.name, dirName, fieldName)); } fromSource.put(sourceFieldName, fieldName); toSource.put(fieldName, sourceFieldName); } SubDirectoryInfo subDirectoryInfo = new SubDirectoryInfo( dirName, dirSchemaName, dirIdField, dirIsAuth, fromSource, toSource, defaultEntry, dirIsOptional); subDirectoryInfos.add(subDirectoryInfo); if (dirIsAuth) { if (authDirectoryInfo != null) { throw new DirectoryException( String.format( "Directory '%s' source '%s' has two subdirectories " + "with a password field, '%s' and '%s'", directory.getName(), source.name, authDirectoryInfo.dirName, dirName)); } authDirectoryInfo = subDirectoryInfo; } if (!dirIsOptional) { hasRequiredDir = true; } } if (isAuthenticating() && authDirectoryInfo == null) { throw new DirectoryException( String.format( "Directory '%s' source '%s' has no subdirectory " + "with a password field", directory.getName(), source.name)); } if (!hasRequiredDir) { throw new DirectoryException( String.format( "Directory '%s' source '%s' only has optional subdirectories: " + "no directory can be used has a reference.", directory.getName(), source.name)); } newSourceInfos.add(new SourceInfo(source, subDirectoryInfos, authDirectoryInfo)); } sourceInfos = newSourceInfos; }