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 *     Gabriel Barata <gbarata@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.oauth2.clients;
020
021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
023import static org.nuxeo.ecm.platform.oauth2.clients.OAuth2Client.NAME_FIELD;
024import static org.nuxeo.ecm.platform.oauth2.clients.OAuth2Client.REDIRECT_URI_FIELD;
025
026import java.io.IOException;
027
028import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
029import org.nuxeo.ecm.core.io.registry.reflect.Setup;
030
031import com.fasterxml.jackson.core.JsonGenerator;
032
033/**
034 * @since 10.2
035 */
036@Setup(mode = SINGLETON, priority = REFERENCE)
037public class OAuth2ClientWriter extends ExtensibleEntityJsonWriter<OAuth2Client> {
038
039    public static final String ENTITY_TYPE = "oauth2Client";
040
041    /**
042     * @since 11.1
043     */
044    public static final String ID_FIELD = "id";
045
046    /**
047     * @since 11.1
048     */
049    public static final String SECRET_FIELD = "secret";
050
051    /**
052     * @since 11.1
053     */
054    public static final String ENABLED_FIELD = "isEnabled";
055
056    /**
057     * @since 11.1
058     */
059    public static final String AUTO_GRANT_FIELD = "isAutoGrant";
060
061    public OAuth2ClientWriter() {
062        super(ENTITY_TYPE, OAuth2Client.class);
063    }
064
065    @Override
066    protected void writeEntityBody(OAuth2Client client, JsonGenerator jg) throws IOException {
067        jg.writeStringField(NAME_FIELD, client.getName());
068        jg.writeArrayFieldStart(REDIRECT_URI_FIELD);
069        for (String url : client.getRedirectURIs()) {
070            jg.writeString(url);
071        }
072        jg.writeEndArray();
073        jg.writeStringField(SECRET_FIELD, client.getSecret());
074        jg.writeStringField(ID_FIELD, client.getId());
075        jg.writeBooleanField(AUTO_GRANT_FIELD, client.isAutoGrant());
076        jg.writeBooleanField(ENABLED_FIELD, client.isEnabled());
077    }
078}