001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Mariana Cedica
018 */
019package org.nuxeo.adobe.cc;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.DocumentModelList;
028import org.nuxeo.ecm.directory.DirectoryException;
029import org.nuxeo.ecm.directory.Session;
030import org.nuxeo.ecm.directory.api.DirectoryService;
031import org.nuxeo.ecm.platform.oauth2.clients.OAuth2ClientService;
032import org.nuxeo.runtime.api.Framework;
033import org.nuxeo.runtime.model.ComponentContext;
034import org.nuxeo.runtime.model.DefaultComponent;
035
036/**
037 * @since 10.2
038 */
039public class NuxeoAdobeConnectorServiceImpl extends DefaultComponent implements NuxeoAdobeConnectorService {
040
041    private static final Log log = LogFactory.getLog(NuxeoAdobeConnectorService.class);
042
043    /**
044     * Component activated notification. Called when the component is activated. All component dependencies are resolved
045     * at that moment. Use this method to initialize the component.
046     *
047     * @param context the component context.
048     */
049    @Override
050    public void activate(ComponentContext context) {
051        super.activate(context);
052    }
053
054    /**
055     * Component deactivated notification. Called before a component is unregistered. Use this method to do cleanup if
056     * any and free any resources held by the component.
057     *
058     * @param context the component context.
059     */
060    @Override
061    public void deactivate(ComponentContext context) {
062        super.deactivate(context);
063    }
064
065    /**
066     * Application started notification. Called after the application started. You can do here any initialization that
067     * requires a working application (all resolved bundles and components are active at that moment)
068     *
069     * @param context the component context. Use it to get the current bundle context
070     */
071    @Override
072    public void start(ComponentContext context) {
073        registerAdobeCCClient();
074    }
075
076    protected void registerAdobeCCClient() {
077        DirectoryService ds = Framework.getService(DirectoryService.class);
078        Framework.doPrivileged(() -> {
079            try (Session session = ds.open(OAuth2ClientService.OAUTH2CLIENT_DIRECTORY_NAME)) {
080
081                Map<String, Serializable> filter = new HashMap<>();
082                filter.put("clientId", ADOBE_CC_CLIENT_ID);
083                DocumentModelList res = session.query(filter);
084                if (res == null || res.size() == 0) {
085                    Map<String, Object> data = new HashMap<>();
086                    data.put("clientId", ADOBE_CC_CLIENT_ID);
087                    data.put("name", ADOBE_CC_NAME);
088                    data.put("redirectURIs", ADOBE_CC_CLIENT_URL);
089                    data.put("autoGrant", true);
090                    session.createEntry(data);
091                }
092
093            } catch (DirectoryException e) {
094                log.error("Error during registering adobe cc app", e);
095            }
096        });
097    }
098
099}