001/*
002 * (C) Copyright 2006-2011 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 *     matic
018 */
019package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
020
021import java.io.IOException;
022import java.util.HashSet;
023import java.util.Set;
024
025import org.nuxeo.ecm.automation.client.LoginInfo;
026import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
027
028import com.fasterxml.jackson.core.JsonGenerator;
029import com.fasterxml.jackson.core.JsonParser;
030import com.fasterxml.jackson.core.JsonToken;
031
032/**
033 * @author matic
034 */
035public class LoginMarshaller implements JsonMarshaller<LoginInfo> {
036
037    @Override
038    public String getType() {
039        return "login";
040    }
041
042    @Override
043    public Class<LoginInfo> getJavaType() {
044        return LoginInfo.class;
045    }
046
047    public String getReference(LoginInfo info) {
048        throw new UnsupportedOperationException();
049    }
050
051    @Override
052    public LoginInfo read(JsonParser jp) throws IOException {
053        boolean isAdmin = false;
054        String username = null;
055        Set<String> groups = null;
056        JsonToken tok = jp.nextToken();
057        while (tok != null && tok != JsonToken.END_OBJECT) {
058            String key = jp.getCurrentName();
059            if ("username".equals(key)) {
060                username = jp.getText();
061            } else if ("isAdministrator".equals(key)) {
062                isAdmin = Boolean.parseBoolean(jp.getText());
063            } else if ("groups".equals(key)) {
064                jp.nextToken();
065                groups = readGroups(jp);
066            }
067            tok = jp.nextToken();
068        }
069        if (tok == null) {
070            throw new IllegalArgumentException("Unexpected end of stream.");
071        }
072        return new LoginInfo(username, groups, isAdmin);
073    }
074
075    protected Set<String> readGroups(JsonParser jp) throws IOException {
076        HashSet<String> groups = new HashSet<String>();
077        JsonToken tok = jp.nextToken();
078        while (tok != JsonToken.END_ARRAY) {
079            groups.add(jp.getText());
080            tok = jp.nextToken();
081        }
082        return groups;
083    }
084
085    @Override
086    public void write(JsonGenerator jg, Object value) throws IOException {
087        LoginInfo loginInfo = (LoginInfo) value;
088        jg.writeStringField("username", loginInfo.getUsername());
089        jg.writeBooleanField("isAdministrator", loginInfo.isAdministrator());
090        jg.writeArrayFieldStart("groups");
091        String[] groups = loginInfo.getGroups();
092        if (groups != null) {
093            for (String g : groups) {
094                jg.writeString(g);
095            }
096        }
097        jg.writeEndArray();
098    }
099
100}