001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.oauth.consumers;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentModelList;
034import org.nuxeo.ecm.directory.DirectoryException;
035import org.nuxeo.ecm.directory.Session;
036import org.nuxeo.ecm.directory.api.DirectoryService;
037import org.nuxeo.runtime.api.Framework;
038import org.nuxeo.runtime.model.DefaultComponent;
039
040/**
041 * Implementation of the {@link OAuthConsumerRegistry} Service. It's basically a simple Storage API on top of an SQL
042 * Directory.
043 *
044 * @author tiry
045 */
046public class OAuthConsumerRegistryImpl extends DefaultComponent implements OAuthConsumerRegistry {
047
048    protected static final Log log = LogFactory.getLog(OAuthConsumerRegistryImpl.class);
049
050    public static final String DIRECTORY_NAME = "oauthConsumers";
051
052    @Override
053    public NuxeoOAuthConsumer getConsumer(String consumerKey, String keyType) {
054        try {
055            NuxeoOAuthConsumer consumer = getEntry(consumerKey, keyType);
056            return consumer;
057        } catch (DirectoryException e) {
058            log.error("Unable to read consumer " + consumerKey + " from Directory backend", e);
059            return null;
060        }
061    }
062
063    @Override
064    public NuxeoOAuthConsumer getConsumer(String consumerKey) {
065        return getConsumer(consumerKey, null);
066    }
067
068    protected NuxeoOAuthConsumer getEntry(String consumerKey, String keyType) {
069        DirectoryService ds = Framework.getService(DirectoryService.class);
070        try (Session session = ds.open(DIRECTORY_NAME)) {
071            DocumentModel entry = session.getEntry(consumerKey);
072            if (entry == null) {
073                return null;
074            }
075            return NuxeoOAuthConsumer.createFromDirectoryEntry(entry, keyType);
076        }
077    }
078
079    @Override
080    public NuxeoOAuthConsumer storeConsumer(NuxeoOAuthConsumer consumer) {
081        DirectoryService ds = Framework.getService(DirectoryService.class);
082        try (Session session = ds.open(DIRECTORY_NAME)) {
083            DocumentModel entry = session.createEntry(Collections.singletonMap("consumerKey", consumer.consumerKey));
084            consumer.asDocumentModel(entry);
085            session.updateEntry(entry);
086            consumer = NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null);
087            return consumer;
088        }
089    }
090
091    @Override
092    public void deleteConsumer(String consumerKey) {
093        try {
094            DirectoryService ds = Framework.getService(DirectoryService.class);
095            try (Session session = ds.open(DIRECTORY_NAME)) {
096                session.deleteEntry(consumerKey);
097            }
098        } catch (DirectoryException e) {
099            log.error("Unable to delete consumer " + consumerKey, e);
100        }
101    }
102
103    @Override
104    public List<NuxeoOAuthConsumer> listConsumers() {
105
106        List<NuxeoOAuthConsumer> result = new ArrayList<NuxeoOAuthConsumer>();
107        try {
108            DirectoryService ds = Framework.getService(DirectoryService.class);
109            try (Session session = ds.open(DIRECTORY_NAME)) {
110                DocumentModelList entries = session.query(Collections.emptyMap());
111                for (DocumentModel entry : entries) {
112                    result.add(NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null));
113                }
114            }
115        } catch (DirectoryException e) {
116            log.error("Error while fetching consumer directory", e);
117        }
118        return result;
119    }
120
121}