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

com.liferay.portal.kernel.sanitizer.SanitizerUtil Maven / Gradle / Ivy

There is a newer version: 7.4.3.112-ga112
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.sanitizer;

import com.liferay.portal.kernel.util.StreamUtil;
import com.liferay.registry.collections.ServiceTrackerCollections;
import com.liferay.registry.collections.ServiceTrackerList;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Map;

/**
 * @author Zsolt Balogh
 * @author Brian Wing Shun Chan
 */
public class SanitizerUtil {

	/**
	 * @deprecated As of Wilberforce (7.0.x), with no direct replacement
	 */
	@Deprecated
	public static Sanitizer getSanitizer() {
		return null;
	}

	/**
	 * @deprecated As of Wilberforce (7.0.x), replaced by {@link #sanitize(long,
	 *             long, long, String, long, String, String)}
	 */
	@Deprecated
	public static byte[] sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, byte[] bytes)
		throws SanitizerException {

		return sanitize(
			companyId, groupId, userId, className, classPK, contentType,
			Sanitizer.MODE_ALL, bytes, null);
	}

	/**
	 * @deprecated As of Wilberforce (7.0.x), replaced by {@link #sanitize(long,
	 *             long, long, String, long, String, String)}
	 */
	@Deprecated
	public static void sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, InputStream inputStream,
			OutputStream outputStream)
		throws SanitizerException {

		sanitize(
			companyId, groupId, userId, className, classPK, contentType,
			Sanitizer.MODE_ALL, inputStream, outputStream, null);
	}

	public static String sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, String content)
		throws SanitizerException {

		return sanitize(
			companyId, groupId, userId, className, classPK, contentType,
			Sanitizer.MODE_ALL, content, null);
	}

	/**
	 * @deprecated As of Wilberforce (7.0.x), replaced by {@link #sanitize(long,
	 *             long, long, String, long, String, String, String, Map)}
	 */
	@Deprecated
	public static byte[] sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, String mode, byte[] bytes,
			Map options)
		throws SanitizerException {

		return sanitize(
			companyId, groupId, userId, className, classPK, contentType,
			new String[] {mode}, bytes, options);
	}

	/**
	 * @deprecated As of Wilberforce (7.0.x), replaced by {@link #sanitize(long,
	 *             long, long, String, long, String, String, String, Map)}
	 */
	@Deprecated
	public static void sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, String mode,
			InputStream inputStream, OutputStream outputStream,
			Map options)
		throws SanitizerException {

		sanitize(
			companyId, groupId, userId, className, classPK, contentType,
			new String[] {mode}, inputStream, outputStream, options);
	}

	public static String sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, String mode, String s,
			Map options)
		throws SanitizerException {

		return sanitize(
			companyId, groupId, userId, className, classPK, contentType,
			new String[] {mode}, s, options);
	}

	/**
	 * @deprecated As of Wilberforce (7.0.x), replaced by {@link #sanitize(long,
	 *             long, long, String, long, String, String[], String, Map)}
	 */
	@Deprecated
	public static byte[] sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, String[] modes, byte[] bytes,
			Map options)
		throws SanitizerException {

		for (Sanitizer sanitizer : _sanitizers) {
			bytes = sanitizer.sanitize(
				companyId, groupId, userId, className, classPK, contentType,
				modes, bytes, options);
		}

		return bytes;
	}

	/**
	 * @deprecated As of Wilberforce (7.0.x), replaced by {@link #sanitize(long,
	 *             long, long, String, long, String, String[], String, Map)}
	 */
	@Deprecated
	public static void sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, String[] modes,
			InputStream inputStream, OutputStream outputStream,
			Map options)
		throws SanitizerException {

		ByteArrayOutputStream byteArrayOutputStream =
			new ByteArrayOutputStream();

		for (Sanitizer sanitizer : _sanitizers) {
			sanitizer.sanitize(
				companyId, groupId, userId, className, classPK, contentType,
				modes, inputStream, byteArrayOutputStream, options);

			inputStream = new ByteArrayInputStream(
				byteArrayOutputStream.toByteArray());

			byteArrayOutputStream.reset();
		}

		try {
			StreamUtil.transfer(inputStream, outputStream);
		}
		catch (IOException ioe) {
			throw new SanitizerException(ioe);
		}
	}

	public static String sanitize(
			long companyId, long groupId, long userId, String className,
			long classPK, String contentType, String[] modes, String content,
			Map options)
		throws SanitizerException {

		for (Sanitizer sanitizer : _sanitizers) {
			content = sanitizer.sanitize(
				companyId, groupId, userId, className, classPK, contentType,
				modes, content, options);
		}

		return content;
	}

	/**
	 * @deprecated As of Wilberforce (7.0.x), with no direct replacement
	 */
	@Deprecated
	public void setSanitizer(Sanitizer sanitizer) {
	}

	private static final ServiceTrackerList _sanitizers =
		ServiceTrackerCollections.openList(Sanitizer.class);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy