001/*
002 * Copyright 2011-2016 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   * {@inheritDoc}
082   */
083  public void marshal(final SCIMException response,
084                      final OutputStream outputStream)
085      throws SCIMException
086  {
087    final JsonStreamMarshaller jsonStreamMarshaller =
088        new JsonStreamMarshaller(outputStream);
089    try
090    {
091      jsonStreamMarshaller.marshal(response);
092    }
093    finally
094    {
095      jsonStreamMarshaller.close();
096    }
097  }
098
099
100
101  /**
102   * {@inheritDoc}
103   */
104  public void bulkMarshal(final OutputStream outputStream,
105                          final int failOnErrors,
106                          final List<BulkOperation> operations)
107      throws SCIMException
108  {
109    final JsonStreamMarshaller jsonStreamMarshaller =
110        new JsonStreamMarshaller(outputStream);
111    try
112    {
113      // Figure out what schemas are referenced by the resources.
114      final Set<String> schemaURIs = new HashSet<String>();
115      for (final BulkOperation o : operations)
116      {
117        final BaseResource resource = o.getData();
118        if (resource != null)
119        {
120          schemaURIs.addAll(
121              o.getData().getResourceDescriptor().getAttributeSchemas());
122        }
123      }
124
125      jsonStreamMarshaller.writeBulkStart(failOnErrors, schemaURIs);
126      for (final BulkOperation o : operations)
127      {
128        jsonStreamMarshaller.writeBulkOperation(o);
129      }
130
131      jsonStreamMarshaller.writeBulkFinish();
132    }
133    finally
134    {
135      jsonStreamMarshaller.close();
136    }
137  }
138}