001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     matic
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
013
014import java.io.IOException;
015import java.util.HashSet;
016import java.util.Set;
017
018import org.codehaus.jackson.JsonGenerator;
019import org.codehaus.jackson.JsonParser;
020import org.codehaus.jackson.JsonToken;
021import org.nuxeo.ecm.automation.client.LoginInfo;
022import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
023
024/**
025 * @author matic
026 */
027public class LoginMarshaller implements JsonMarshaller<LoginInfo> {
028
029    @Override
030    public String getType() {
031        return "login";
032    }
033
034    @Override
035    public Class<LoginInfo> getJavaType() {
036        return LoginInfo.class;
037    }
038
039    public String getReference(LoginInfo info) {
040        throw new UnsupportedOperationException();
041    }
042
043    @Override
044    public LoginInfo read(JsonParser jp) throws IOException {
045        boolean isAdmin = false;
046        String username = null;
047        Set<String> groups = null;
048        JsonToken tok = jp.nextToken();
049        while (tok != null && tok != JsonToken.END_OBJECT) {
050            String key = jp.getCurrentName();
051            if ("username".equals(key)) {
052                username = jp.getText();
053            } else if ("isAdministrator".equals(key)) {
054                isAdmin = Boolean.parseBoolean(jp.getText());
055            } else if ("groups".equals(key)) {
056                jp.nextToken();
057                groups = readGroups(jp);
058            }
059            tok = jp.nextToken();
060        }
061        if (tok == null) {
062            throw new IllegalArgumentException("Unexpected end of stream.");
063        }
064        return new LoginInfo(username, groups, isAdmin);
065    }
066
067    protected Set<String> readGroups(JsonParser jp) throws IOException {
068        HashSet<String> groups = new HashSet<String>();
069        JsonToken tok = jp.nextToken();
070        while (tok != JsonToken.END_ARRAY) {
071            groups.add(jp.getText());
072            tok = jp.nextToken();
073        }
074        return groups;
075    }
076
077    @Override
078    public void write(JsonGenerator jg, Object value) throws IOException {
079        LoginInfo loginInfo = (LoginInfo) value;
080        jg.writeStringField("username", loginInfo.getUsername());
081        jg.writeBooleanField("isAdministrator", loginInfo.isAdministrator());
082        jg.writeArrayFieldStart("groups");
083        String[] groups = loginInfo.getGroups();
084        if (groups != null) {
085            for (String g : groups) {
086                jg.writeString(g);
087            }
088        }
089        jg.writeEndArray();
090    }
091
092}