001package com.nimbusds.common.appendable; 002 003 004import java.io.IOException; 005import java.io.Writer; 006import java.util.HashSet; 007import java.util.Set; 008import java.util.function.Consumer; 009import javax.ws.rs.WebApplicationException; 010 011import net.minidev.json.JSONAware; 012 013 014/** 015 * JSON array writer for JAX-RS result streaming. 016 * 017 * <p>Use {@link com.nimbusds.common.json.JSONArrayWriter}. 018 */ 019@Deprecated 020public class JSONArrayWriter<T extends JSONAware> implements Consumer<T>, Appendable<T> { 021 022 023 /** 024 * Writer for the appended elements. 025 */ 026 private final Writer writer; 027 028 029 /** 030 * No duplicates switch. 031 */ 032 private final boolean noDuplicates; 033 034 035 /** 036 * Keeps track of the written elements if {@link #noDuplicates} is 037 * {@code true}. 038 */ 039 private final Set<T> writtenItems; 040 041 042 /** 043 * The position of the appender. 044 */ 045 private boolean first; 046 047 048 /** 049 * Creates a new JSON array writer. The output JSON array may contain 050 * duplicates. 051 * 052 * @param writer Writer for the JSON array. Must not be {@code null}. 053 */ 054 public JSONArrayWriter(final Writer writer) { 055 056 this(writer, false); 057 } 058 059 060 /** 061 * Creates a new JSON array writer. 062 * 063 * @param writer Writer for the JSON array. Must not be 064 * {@code null}. 065 * @param noDuplicates {@code true} to ensure no duplicates are written 066 * to the JSON array. 067 */ 068 public JSONArrayWriter(final Writer writer, 069 final boolean noDuplicates) { 070 071 if (writer == null) { 072 throw new IllegalArgumentException("The writer must not be null"); 073 } 074 075 this.writer = writer; 076 077 this.noDuplicates = noDuplicates; 078 079 if (noDuplicates) { 080 writtenItems = new HashSet<>(); 081 } else { 082 writtenItems = null; 083 } 084 085 first = true; 086 } 087 088 089 /** 090 * Writes out the opening '[' of the JSON array. 091 */ 092 public void writeStart() { 093 094 try { 095 writer.write('['); 096 097 } catch (IOException e) { 098 099 throw new WebApplicationException("Couldn't write JSON array: " + e.getMessage(), e); 100 } 101 } 102 103 104 @Override 105 public void accept(final T element) { 106 107 if (element == null) { 108 return; // Nothing to append 109 } 110 111 if (noDuplicates) { 112 if (writtenItems.contains(element)) { 113 return; // item already written out 114 } else { 115 writtenItems.add(element); 116 } 117 } 118 119 StringBuilder sb = new StringBuilder(); 120 121 if (first) { 122 first = false; 123 } else { 124 sb.append(','); 125 } 126 127 sb.append(element.toJSONString()); 128 129 try { 130 writer.write(sb.toString()); 131 writer.flush(); 132 133 } catch (IOException e) { 134 135 throw new WebApplicationException("Couldn't write JSON array: " + e.getMessage(), e); 136 } 137 } 138 139 140 @Deprecated 141 @Override 142 public void append(final T element) { 143 accept(element); 144 } 145 146 147 /** 148 * Writes out the closing ']' of the JSON array and closes the writer. 149 */ 150 public void writeEnd() { 151 152 try { 153 writer.write(']'); 154 writer.close(); 155 156 } catch (IOException e) { 157 158 throw new WebApplicationException("Couldn't write JSON array: " + e.getMessage(), e); 159 } 160 } 161}