001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.oauth.consumers;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.directory.DirectoryException;
032import org.nuxeo.ecm.directory.Session;
033import org.nuxeo.ecm.directory.api.DirectoryService;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.runtime.model.DefaultComponent;
036
037/**
038 * Implementation of the {@link OAuthConsumerRegistry} Service. It's basically a simple Storage API on top of an SQL
039 * Directory.
040 *
041 * @author tiry
042 */
043public class OAuthConsumerRegistryImpl extends DefaultComponent implements OAuthConsumerRegistry {
044
045    protected static final Log log = LogFactory.getLog(OAuthConsumerRegistryImpl.class);
046
047    public static final String DIRECTORY_NAME = "oauthConsumers";
048
049    @Override
050    public NuxeoOAuthConsumer getConsumer(String consumerKey, String keyType) {
051        try {
052            NuxeoOAuthConsumer consumer = getEntry(consumerKey, keyType);
053            return consumer;
054        } catch (DirectoryException e) {
055            log.error("Unable to read consumer " + consumerKey + " from Directory backend", e);
056            return null;
057        }
058    }
059
060    @Override
061    public NuxeoOAuthConsumer getConsumer(String consumerKey) {
062        return getConsumer(consumerKey, null);
063    }
064
065    protected NuxeoOAuthConsumer getEntry(String consumerKey, String keyType) {
066        DirectoryService ds = Framework.getService(DirectoryService.class);
067        try (Session session = ds.open(DIRECTORY_NAME)) {
068            DocumentModel entry = session.getEntry(consumerKey);
069            if (entry == null) {
070                return null;
071            }
072            return NuxeoOAuthConsumer.createFromDirectoryEntry(entry, keyType);
073        }
074    }
075
076    @Override
077    public NuxeoOAuthConsumer storeConsumer(NuxeoOAuthConsumer consumer) {
078        DirectoryService ds = Framework.getService(DirectoryService.class);
079        try (Session session = ds.open(DIRECTORY_NAME)) {
080            Map<String, Object> init = new HashMap<String, Object>();
081            init.put("consumerKey", consumer.consumerKey);
082            DocumentModel entry = session.createEntry(init);
083            consumer.asDocumentModel(entry);
084            session.updateEntry(entry);
085            if (entry == null) {
086                return null;
087            }
088            consumer = NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null);
089            return consumer;
090        }
091    }
092
093    @Override
094    public void deleteConsumer(String consumerKey) {
095        try {
096            DirectoryService ds = Framework.getService(DirectoryService.class);
097            try (Session session = ds.open(DIRECTORY_NAME)) {
098                session.deleteEntry(consumerKey);
099            }
100        } catch (DirectoryException e) {
101            log.error("Unable to delete consumer " + consumerKey, e);
102        }
103    }
104
105    @Override
106    public List<NuxeoOAuthConsumer> listConsumers() {
107
108        List<NuxeoOAuthConsumer> result = new ArrayList<NuxeoOAuthConsumer>();
109        try {
110            DirectoryService ds = Framework.getService(DirectoryService.class);
111            try (Session session = ds.open(DIRECTORY_NAME)) {
112                DocumentModelList entries = session.getEntries();
113                for (DocumentModel entry : entries) {
114                    result.add(NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null));
115                }
116            }
117        } catch (DirectoryException e) {
118            log.error("Error while fetching consumer directory", e);
119        }
120        return result;
121    }
122
123}