001    /**
002     * Copyright (c) 2000-present 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 com.liferay.portal.kernel.util.StreamUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.OutputStream;
024    import java.io.UnsupportedEncodingException;
025    
026    import java.util.Map;
027    
028    /**
029     * @author Zsolt Balogh
030     * @author Brian Wing Shun Chan
031     */
032    @SuppressWarnings("deprecation")
033    public abstract class BaseSanitizer implements Sanitizer {
034    
035            @Override
036            public byte[] sanitize(
037                            long companyId, long groupId, long userId, String className,
038                            long classPK, String contentType, String[] modes, byte[] bytes,
039                            Map<String, Object> options)
040                    throws SanitizerException {
041    
042                    if (bytes == null) {
043                            return null;
044                    }
045    
046                    try {
047                            String content = new String(bytes, StringPool.UTF8);
048    
049                            String result = sanitize(
050                                    companyId, groupId, userId, className, classPK, contentType,
051                                    modes, content, options);
052    
053                            return result.getBytes(StringPool.UTF8);
054                    }
055                    catch (UnsupportedEncodingException uee) {
056                            throw new SanitizerException(uee);
057                    }
058            }
059    
060            @Override
061            public void sanitize(
062                            long companyId, long groupId, long userId, String className,
063                            long classPK, String contentType, String[] modes,
064                            InputStream inputStream, OutputStream outputStream,
065                            Map<String, Object> options)
066                    throws SanitizerException {
067    
068                    if ((inputStream == null) || (outputStream == null)) {
069                            return;
070                    }
071    
072                    ByteArrayOutputStream byteArrayOutputStream =
073                            new ByteArrayOutputStream();
074    
075                    try {
076                            StreamUtil.transfer(inputStream, byteArrayOutputStream);
077    
078                            String content = new String(
079                                    byteArrayOutputStream.toByteArray(), StringPool.UTF8);
080    
081                            String result = sanitize(
082                                    companyId, groupId, userId, className, classPK, contentType,
083                                    modes, content, options);
084    
085                            byte[] bytes = result.getBytes(StringPool.UTF8);
086    
087                            outputStream.write(bytes);
088                    }
089                    catch (IOException ioe) {
090                            throw new SanitizerException(ioe);
091                    }
092            }
093    
094            @Override
095            public abstract String sanitize(
096                            long companyId, long groupId, long userId, String className,
097                            long classPK, String contentType, String[] modes, String content,
098                            Map<String, Object> options)
099                    throws SanitizerException;
100    
101    }