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.sanitizer;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.sanitizer.Sanitizer;
020    import com.liferay.portal.kernel.sanitizer.SanitizerException;
021    import com.liferay.portal.kernel.util.InstanceFactory;
022    import com.liferay.portal.kernel.util.StreamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.io.ByteArrayOutputStream;
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.io.OutputStream;
030    
031    import java.util.List;
032    import java.util.Map;
033    import java.util.concurrent.CopyOnWriteArrayList;
034    
035    /**
036     * @author Zsolt Balogh
037     * @author Brian Wing Shun Chan
038     */
039    public class SanitizerImpl implements Sanitizer {
040    
041            public SanitizerImpl() {
042                    for (String className : PropsValues.SANITIZER_IMPL) {
043                            if (Validator.isNull(className)) {
044                                    continue;
045                            }
046    
047                            try {
048                                    Sanitizer sanitizer = (Sanitizer)InstanceFactory.newInstance(
049                                            className);
050    
051                                    registerSanitizer(sanitizer);
052                            }
053                            catch (Exception e) {
054                                    _log.error(e, e);
055                            }
056                    }
057            }
058    
059            public void registerSanitizer(Sanitizer sanitizer) {
060                    _sanitizers.add(sanitizer);
061            }
062    
063            @Override
064            public byte[] sanitize(
065                            long companyId, long groupId, long userId, String className,
066                            long classPK, String contentType, String[] modes, byte[] bytes,
067                            Map<String, Object> options)
068                    throws SanitizerException {
069    
070                    for (Sanitizer sanitizer : _sanitizers) {
071                            bytes = sanitizer.sanitize(
072                                    companyId, groupId, userId, className, classPK, contentType,
073                                    modes, bytes, options);
074                    }
075    
076                    return bytes;
077            }
078    
079            @Override
080            public void sanitize(
081                            long companyId, long groupId, long userId, String className,
082                            long classPK, String contentType, String[] modes,
083                            InputStream inputStream, OutputStream outputStream,
084                            Map<String, Object> options)
085                    throws SanitizerException {
086    
087                    if (_sanitizers.isEmpty()) {
088                            return;
089                    }
090    
091                    if (_sanitizers.size() == 1) {
092                            sanitize(
093                                    companyId, groupId, userId, className, classPK, contentType,
094                                    modes, inputStream, outputStream, options);
095    
096                            return;
097                    }
098    
099                    ByteArrayOutputStream byteArrayOutputStream =
100                            new ByteArrayOutputStream();
101    
102                    try {
103                            StreamUtil.transfer(inputStream, byteArrayOutputStream);
104                    }
105                    catch (IOException ioe) {
106                            throw new SanitizerException(ioe);
107                    }
108    
109                    byte[] bytes = sanitize(
110                            companyId, groupId, userId, className, classPK, contentType, modes,
111                            byteArrayOutputStream.toByteArray(), options);
112    
113                    try {
114                            outputStream.write(bytes);
115                    }
116                    catch (IOException ioe) {
117                            throw new SanitizerException(ioe);
118                    }
119            }
120    
121            @Override
122            public String sanitize(
123                            long companyId, long groupId, long userId, String className,
124                            long classPK, String contentType, String[] modes, String s,
125                            Map<String, Object> options)
126                    throws SanitizerException {
127    
128                    for (Sanitizer sanitizer : _sanitizers) {
129                            s = sanitizer.sanitize(
130                                    companyId, groupId, userId, className, classPK, contentType,
131                                    modes, s, options);
132                    }
133    
134                    return s;
135            }
136    
137            public void unregisterSanitizer(Sanitizer sanitizer) {
138                    _sanitizers.remove(sanitizer);
139            }
140    
141            private static Log _log = LogFactoryUtil.getLog(SanitizerImpl.class);
142    
143            private List<Sanitizer> _sanitizers = new CopyOnWriteArrayList<Sanitizer>();
144    
145    }