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