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.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentModelList;
033import org.nuxeo.ecm.directory.DirectoryException;
034import org.nuxeo.ecm.directory.Session;
035import org.nuxeo.ecm.directory.api.DirectoryService;
036import org.nuxeo.runtime.api.Framework;
037import org.nuxeo.runtime.model.DefaultComponent;
038
039/**
040 * Implementation of the {@link OAuthConsumerRegistry} Service. It's basically a simple Storage API on top of an SQL
041 * Directory.
042 *
043 * @author tiry
044 */
045public class OAuthConsumerRegistryImpl extends DefaultComponent implements OAuthConsumerRegistry {
046
047    protected static final Log log = LogFactory.getLog(OAuthConsumerRegistryImpl.class);
048
049    public static final String DIRECTORY_NAME = "oauthConsumers";
050
051    @Override
052    public NuxeoOAuthConsumer getConsumer(String consumerKey, String keyType) {
053        try {
054            NuxeoOAuthConsumer consumer = getEntry(consumerKey, keyType);
055            return consumer;
056        } catch (DirectoryException e) {
057            log.error("Unable to read consumer " + consumerKey + " from Directory backend", e);
058            return null;
059        }
060    }
061
062    @Override
063    public NuxeoOAuthConsumer getConsumer(String consumerKey) {
064        return getConsumer(consumerKey, null);
065    }
066
067    protected NuxeoOAuthConsumer getEntry(String consumerKey, String keyType) {
068        DirectoryService ds = Framework.getService(DirectoryService.class);
069        try (Session session = ds.open(DIRECTORY_NAME)) {
070            DocumentModel entry = session.getEntry(consumerKey);
071            if (entry == null) {
072                return null;
073            }
074            return NuxeoOAuthConsumer.createFromDirectoryEntry(entry, keyType);
075        }
076    }
077
078    @Override
079    public NuxeoOAuthConsumer storeConsumer(NuxeoOAuthConsumer consumer) {
080        DirectoryService ds = Framework.getService(DirectoryService.class);
081        try (Session session = ds.open(DIRECTORY_NAME)) {
082            Map<String, Object> init = new HashMap<String, Object>();
083            init.put("consumerKey", consumer.consumerKey);
084            DocumentModel entry = session.createEntry(init);
085            consumer.asDocumentModel(entry);
086            session.updateEntry(entry);
087            if (entry == null) {
088                return null;
089            }
090            consumer = NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null);
091            return consumer;
092        }
093    }
094
095    @Override
096    public void deleteConsumer(String consumerKey) {
097        try {
098            DirectoryService ds = Framework.getService(DirectoryService.class);
099            try (Session session = ds.open(DIRECTORY_NAME)) {
100                session.deleteEntry(consumerKey);
101            }
102        } catch (DirectoryException e) {
103            log.error("Unable to delete consumer " + consumerKey, e);
104        }
105    }
106
107    @Override
108    public List<NuxeoOAuthConsumer> listConsumers() {
109
110        List<NuxeoOAuthConsumer> result = new ArrayList<NuxeoOAuthConsumer>();
111        try {
112            DirectoryService ds = Framework.getService(DirectoryService.class);
113            try (Session session = ds.open(DIRECTORY_NAME)) {
114                DocumentModelList entries = session.getEntries();
115                for (DocumentModel entry : entries) {
116                    result.add(NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null));
117                }
118            }
119        } catch (DirectoryException e) {
120            log.error("Error while fetching consumer directory", e);
121        }
122        return result;
123    }
124
125}