001/*
002 * Copyright 2011-2013 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim.marshal.json;
019
020import com.unboundid.scim.data.BaseResource;
021import com.unboundid.scim.marshal.Marshaller;
022import com.unboundid.scim.sdk.BulkOperation;
023import com.unboundid.scim.sdk.Resources;
024import com.unboundid.scim.sdk.SCIMException;
025
026import java.io.OutputStream;
027import java.util.HashSet;
028import java.util.List;
029import java.util.Set;
030
031
032
033/**
034 * This class provides a SCIM object marshaller implementation to write SCIM
035 * objects to their Json representation.
036 */
037public class JsonMarshaller implements Marshaller
038{
039  /**
040   * {@inheritDoc}
041   */
042  public void marshal(final BaseResource resource,
043                      final OutputStream outputStream)
044      throws SCIMException
045  {
046    final JsonStreamMarshaller jsonStreamMarshaller =
047        new JsonStreamMarshaller(outputStream);
048    try
049    {
050      jsonStreamMarshaller.marshal(resource);
051    }
052    finally
053    {
054      jsonStreamMarshaller.close();
055    }
056  }
057
058
059
060  /**
061   * {@inheritDoc}
062   */
063  public void marshal(final Resources<? extends BaseResource> response,
064                      final OutputStream outputStream)
065      throws SCIMException
066  {
067    final JsonStreamMarshaller jsonStreamMarshaller =
068        new JsonStreamMarshaller(outputStream);
069    try
070    {
071      jsonStreamMarshaller.marshal(response);
072    }
073    finally
074    {
075      jsonStreamMarshaller.close();
076    }
077  }
078
079
080
081  /**
082   * {@inheritDoc}
083   */
084  public void marshal(final SCIMException response,
085                      final OutputStream outputStream)
086      throws SCIMException
087  {
088    final JsonStreamMarshaller jsonStreamMarshaller =
089        new JsonStreamMarshaller(outputStream);
090    try
091    {
092      jsonStreamMarshaller.marshal(response);
093    }
094    finally
095    {
096      jsonStreamMarshaller.close();
097    }
098  }
099
100
101
102  /**
103   * {@inheritDoc}
104   */
105  public void bulkMarshal(final OutputStream outputStream,
106                          final int failOnErrors,
107                          final List<BulkOperation> operations)
108      throws SCIMException
109  {
110    final JsonStreamMarshaller jsonStreamMarshaller =
111        new JsonStreamMarshaller(outputStream);
112    try
113    {
114      // Figure out what schemas are referenced by the resources.
115      final Set<String> schemaURIs = new HashSet<String>();
116      for (final BulkOperation o : operations)
117      {
118        final BaseResource resource = o.getData();
119        if (resource != null)
120        {
121          schemaURIs.addAll(
122              o.getData().getResourceDescriptor().getAttributeSchemas());
123        }
124      }
125
126      jsonStreamMarshaller.writeBulkStart(failOnErrors, schemaURIs);
127      for (final BulkOperation o : operations)
128      {
129        jsonStreamMarshaller.writeBulkOperation(o);
130      }
131
132      jsonStreamMarshaller.writeBulkFinish();
133    }
134    finally
135    {
136      jsonStreamMarshaller.close();
137    }
138  }
139}