001/* 002 * (C) Copyright 2006-2012 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 * Nuxeo 018 */ 019package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers; 020 021import java.io.IOException; 022import java.io.Serializable; 023import java.util.Map; 024 025import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller; 026import org.nuxeo.ecm.automation.client.model.RecordSet; 027 028import com.fasterxml.jackson.core.JsonGenerator; 029import com.fasterxml.jackson.core.JsonParser; 030import com.fasterxml.jackson.core.JsonToken; 031 032/** 033 * Manage JSON Decoding of RecordSet object returned by QueryAndFetch 034 * 035 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 036 * @since 5.7 037 */ 038public class RecordSetMarshaller implements JsonMarshaller<RecordSet> { 039 040 @Override 041 public String getType() { 042 return "recordSet"; 043 } 044 045 @Override 046 public Class<RecordSet> getJavaType() { 047 return RecordSet.class; 048 } 049 050 @Override 051 public RecordSet read(JsonParser jp) throws IOException { 052 jp.nextToken(); 053 String key = jp.getCurrentName(); 054 if ("isPaginable".equals(key)) { 055 jp.nextToken(); 056 boolean isPaginable = jp.getBooleanValue(); 057 if (isPaginable) { 058 jp.nextToken(); 059 return readPaginableRecordSet(jp); 060 } 061 } 062 return readRecord(jp); 063 } 064 065 protected RecordSet readPaginableRecordSet(JsonParser jp) throws IOException { 066 RecordSet record = new RecordSet(); 067 JsonToken tok = jp.getCurrentToken(); 068 while (tok != null && tok != JsonToken.END_OBJECT) { 069 String key = jp.getCurrentName(); 070 jp.nextToken(); 071 if ("pageSize".equals(key)) { 072 record.setPageSize(jp.getIntValue()); 073 } else if ("numberOfPages".equals(key)) { 074 record.setNumberOfPages(jp.getIntValue()); 075 } else if ("currentPageIndex".equals(key)) { 076 record.setCurrentPageIndex(jp.getIntValue()); 077 } else if ("entries".equals(key)) { 078 readRecordEntries(jp, record); 079 } 080 tok = jp.nextToken(); 081 } 082 if (tok == null) { 083 throw new IllegalArgumentException("Unexpected end of stream."); 084 } 085 return record; 086 } 087 088 protected RecordSet readRecord(JsonParser jp) throws IOException { 089 RecordSet record = new RecordSet(); 090 JsonToken tok = jp.nextToken(); 091 while (tok != JsonToken.END_ARRAY) { 092 String key = jp.getCurrentName(); 093 if ("entries".equals(key)) { 094 readRecordEntries(jp, record); 095 return record; 096 } 097 tok = jp.nextToken(); 098 } 099 return record; 100 } 101 102 protected void readRecordEntries(JsonParser jp, RecordSet record) throws IOException { 103 JsonToken tok = jp.nextToken(); 104 while (tok != JsonToken.END_ARRAY) { 105 @SuppressWarnings("unchecked") 106 Map<String, Serializable> entry = jp.readValueAs(Map.class); 107 record.add(entry); 108 tok = jp.nextToken(); 109 } 110 } 111 112 @Override 113 public void write(JsonGenerator jg, Object value) throws IOException { 114 } 115 116}