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 if (entry == null) { 087 return null; 088 } 089 consumer = NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null); 090 return consumer; 091 } 092 } 093 094 @Override 095 public void deleteConsumer(String consumerKey) { 096 try { 097 DirectoryService ds = Framework.getService(DirectoryService.class); 098 try (Session session = ds.open(DIRECTORY_NAME)) { 099 session.deleteEntry(consumerKey); 100 } 101 } catch (DirectoryException e) { 102 log.error("Unable to delete consumer " + consumerKey, e); 103 } 104 } 105 106 @Override 107 public List<NuxeoOAuthConsumer> listConsumers() { 108 109 List<NuxeoOAuthConsumer> result = new ArrayList<NuxeoOAuthConsumer>(); 110 try { 111 DirectoryService ds = Framework.getService(DirectoryService.class); 112 try (Session session = ds.open(DIRECTORY_NAME)) { 113 DocumentModelList entries = session.query(Collections.emptyMap()); 114 for (DocumentModel entry : entries) { 115 result.add(NuxeoOAuthConsumer.createFromDirectoryEntry(entry, null)); 116 } 117 } 118 } catch (DirectoryException e) { 119 log.error("Error while fetching consumer directory", e); 120 } 121 return result; 122 } 123 124}