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            public byte[] sanitize(
064                            long companyId, long groupId, long userId, String className,
065                            long classPK, String contentType, String[] modes, byte[] bytes,
066                            Map<String, Object> options)
067                    throws SanitizerException {
068    
069                    for (Sanitizer sanitizer : _sanitizers) {
070                            bytes = sanitizer.sanitize(
071                                    companyId, groupId, userId, className, classPK, contentType,
072                                    modes, bytes, options);
073                    }
074    
075                    return bytes;
076            }
077    
078            public void sanitize(
079                            long companyId, long groupId, long userId, String className,
080                            long classPK, String contentType, String[] modes,
081                            InputStream inputStream, OutputStream outputStream,
082                            Map<String, Object> options)
083                    throws SanitizerException {
084    
085                    if (_sanitizers.isEmpty()) {
086                            return;
087                    }
088    
089                    if (_sanitizers.size() == 1) {
090                            sanitize(
091                                    companyId, groupId, userId, className, classPK, contentType,
092                                    modes, inputStream, outputStream, options);
093    
094                            return;
095                    }
096    
097                    ByteArrayOutputStream byteArrayOutputStream =
098                            new ByteArrayOutputStream();
099    
100                    try {
101                            StreamUtil.transfer(inputStream, byteArrayOutputStream);
102                    }
103                    catch (IOException ioe) {
104                            throw new SanitizerException(ioe);
105                    }
106    
107                    byte[] bytes = sanitize(
108                            companyId, groupId, userId, className, classPK, contentType, modes,
109                            byteArrayOutputStream.toByteArray(), options);
110    
111                    try {
112                            outputStream.write(bytes);
113                    }
114                    catch (IOException ioe) {
115                            throw new SanitizerException(ioe);
116                    }
117            }
118    
119            public String sanitize(
120                            long companyId, long groupId, long userId, String className,
121                            long classPK, String contentType, String[] modes, String s,
122                            Map<String, Object> options)
123                    throws SanitizerException {
124    
125                    for (Sanitizer sanitizer : _sanitizers) {
126                            s = sanitizer.sanitize(
127                                    companyId, groupId, userId, className, classPK, contentType,
128                                    modes, s, options);
129                    }
130    
131                    return s;
132            }
133    
134            public void unregisterSanitizer(Sanitizer sanitizer) {
135                    _sanitizers.remove(sanitizer);
136            }
137    
138            private static Log _log = LogFactoryUtil.getLog(SanitizerImpl.class);
139    
140            private List<Sanitizer> _sanitizers = new CopyOnWriteArrayList<Sanitizer>();
141    
142    }