All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.yaas.servicesdk.apiconsole.web.RamlStreamingOutput Maven / Gradle / Ivy

There is a newer version: 4.17.1
Show newest version
/*
 * © 2016 SAP SE or an SAP affiliate company.
 * All rights reserved.
 * Please see http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and
 * notices.
 */
package com.sap.cloud.yaas.servicesdk.apiconsole.web;

import com.sap.cloud.yaas.servicesdk.ramlrewriter.RamlStreamChain;
import com.sap.cloud.yaas.servicesdk.ramlrewriter.StreamMutator;
import com.sap.cloud.yaas.servicesdk.ramlrewriter.mutators.ExpandingMode;
import com.sap.cloud.yaas.servicesdk.ramlrewriter.mutators.LineMutator;
import com.sap.cloud.yaas.servicesdk.ramlrewriter.mutators.line.LineMutatorsFactory;
import com.sap.cloud.yaas.servicesdk.ramlrewriter.mutators.line.StreamMutatorsFactory;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

import javax.ws.rs.core.StreamingOutput;


/**
 * Streams an RAML file as output replacing the baseUri of the RAML file while streaming.
 */
public class RamlStreamingOutput implements StreamingOutput
{
	private final URL urlToRamlSource;
	private final StreamMutator lineBasedStreamMutator;
	private final StreamMutator expandingStreamMutator;

	/**
	 * Creates a new stream instance based on given raml resource.
	 *
	 * @param resourceUrl raml resource to stream
	 * @param lineMutators line modifiers
	 * @param expandingMode defined how much should the RAML be expanded, see {@link ExpandingMode}
	 */
	@SuppressWarnings("deprecation")
	public RamlStreamingOutput(final URL resourceUrl, final List lineMutators, final ExpandingMode expandingMode)
	{
		this.urlToRamlSource = resourceUrl;
		final StreamMutatorsFactory mutatorsFactory = new StreamMutatorsFactory(new LineMutatorsFactory());
		this.lineBasedStreamMutator = mutatorsFactory.createLineBasedStreamMutator(lineMutators);

		this.expandingStreamMutator = !ExpandingMode.NONE.equals(expandingMode)
				&& !ExpandingMode.FALSE.equals(expandingMode)
						? mutatorsFactory.createExpandingStreamMutator(lineBasedStreamMutator, expandingMode)
						: null;
	}

	/**
	 * Legacy constructor that creates a new stream instance based on given raml resource.
	 *
	 * @param resourceUrl raml resource to stream
	 * @param lineMutators line modifiers
	 * @param expandStream which {@link ExpandingMode} to use; {@code true} to use {@code FULL}, {@code false} to use
	 *           {@code NONE}.
	 * 
	 * @deprecated Use {@link #RamlStreamingOutput(URL, List, ExpandingMode)} instead.
	 */
	@Deprecated
	public RamlStreamingOutput(final URL resourceUrl, final List lineMutators, final boolean expandStream)
	{
		this(
				resourceUrl,
				lineMutators,
				expandStream ? ExpandingMode.FULL : ExpandingMode.NONE);
	}

	@Override
	public void write(final OutputStream outputStream) throws IOException
	{
		RamlStreamChain//
				.fromStream(urlToRamlSource)//
				.addMutator(lineBasedStreamMutator)//
				.addMutator(expandingStreamMutator)//
				.writeToStream(outputStream);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy