001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.sanitizer;
016    
017    import java.io.ByteArrayInputStream;
018    import java.io.ByteArrayOutputStream;
019    import java.io.InputStream;
020    import java.io.OutputStream;
021    
022    import java.util.Map;
023    
024    /**
025     * @author Zsolt Balogh
026     * @author Brian Wing Shun Chan
027     */
028    public abstract class BaseSanitizer implements Sanitizer {
029    
030            @Override
031            public byte[] sanitize(
032                            long companyId, long groupId, long userId, String className,
033                            long classPK, String contentType, String[] modes, byte[] bytes,
034                            Map<String, Object> options)
035                    throws SanitizerException {
036    
037                    ByteArrayOutputStream byteArrayOutputStream =
038                            new ByteArrayOutputStream();
039    
040                    sanitize(
041                            companyId, groupId, userId, className, classPK, contentType, modes,
042                            new ByteArrayInputStream(bytes), byteArrayOutputStream, options);
043    
044                    return byteArrayOutputStream.toByteArray();
045            }
046    
047            @Override
048            public abstract void sanitize(
049                            long companyId, long groupId, long userId, String className,
050                            long classPK, String contentType, String[] modes,
051                            InputStream inputStream, OutputStream outputStream,
052                            Map<String, Object> options)
053                    throws SanitizerException;
054    
055            @Override
056            public String sanitize(
057                            long companyId, long groupId, long userId, String className,
058                            long classPK, String contentType, String[] modes, String s,
059                            Map<String, Object> options)
060                    throws SanitizerException {
061    
062                    ByteArrayOutputStream byteArrayOutputStream =
063                            new ByteArrayOutputStream();
064    
065                    sanitize(
066                            companyId, groupId, userId, className, classPK, contentType, modes,
067                            new ByteArrayInputStream(s.getBytes()), byteArrayOutputStream,
068                            options);
069    
070                    return byteArrayOutputStream.toString();
071            }
072    
073    }