com.unboundid.scim.wink.AbstractStaticResource Maven / Gradle / Ivy
/*
* Copyright 2011-2019 Ping Identity Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (GPLv2 only)
* or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*/
package com.unboundid.scim.wink;
import com.unboundid.scim.marshal.Marshaller;
import com.unboundid.scim.marshal.json.JsonMarshaller;
import com.unboundid.scim.marshal.xml.XmlMarshaller;
import com.unboundid.scim.sdk.Debug;
import com.unboundid.scim.sdk.SCIMResponse;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.OutputStream;
/**
* This class is an abstract base class for JAX-RS static SCIM resources
* such as the Service Provider Configuration.
*/
public class AbstractStaticResource
{
/**
* Sets the response entity (content) for a SCIM response.
*
* @param builder A JAX-RS response builder.
* @param mediaType The media type to be returned.
* @param scimResponse The SCIM response to be returned.
*/
protected static void setResponseEntity(
final Response.ResponseBuilder builder, final MediaType mediaType,
final SCIMResponse scimResponse)
{
final Marshaller marshaller;
builder.type(mediaType);
if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE))
{
marshaller = new JsonMarshaller();
}
else
{
marshaller = new XmlMarshaller();
}
final StreamingOutput output = new StreamingOutput()
{
public void write(final OutputStream outputStream)
throws IOException, WebApplicationException
{
try
{
scimResponse.marshal(marshaller, outputStream);
}
catch (Exception e)
{
Debug.debugException(e);
throw new WebApplicationException(
e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
};
builder.entity(output);
}
}