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.sanitizer;
016    
017    import com.liferay.portal.kernel.sanitizer.Sanitizer;
018    import com.liferay.portal.kernel.sanitizer.SanitizerException;
019    import com.liferay.portal.kernel.util.StreamUtil;
020    import com.liferay.registry.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceReference;
023    import com.liferay.registry.ServiceTracker;
024    import com.liferay.registry.ServiceTrackerCustomizer;
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     * @author Peter Fellwock
039     */
040    public class SanitizerImpl implements Sanitizer {
041    
042            public SanitizerImpl() {
043                    Registry registry = RegistryUtil.getRegistry();
044    
045                    _serviceTracker = registry.trackServices(
046                            Sanitizer.class, new SanitizerServiceTrackerCustomizer());
047    
048                    _serviceTracker.open();
049            }
050    
051            @Override
052            public byte[] sanitize(
053                            long companyId, long groupId, long userId, String className,
054                            long classPK, String contentType, String[] modes, byte[] bytes,
055                            Map<String, Object> options)
056                    throws SanitizerException {
057    
058                    for (Sanitizer sanitizer : _sanitizers) {
059                            bytes = sanitizer.sanitize(
060                                    companyId, groupId, userId, className, classPK, contentType,
061                                    modes, bytes, options);
062                    }
063    
064                    return bytes;
065            }
066    
067            @Override
068            public void sanitize(
069                            long companyId, long groupId, long userId, String className,
070                            long classPK, String contentType, String[] modes,
071                            InputStream inputStream, OutputStream outputStream,
072                            Map<String, Object> options)
073                    throws SanitizerException {
074    
075                    if (_sanitizers.isEmpty()) {
076                            return;
077                    }
078    
079                    if (_sanitizers.size() == 1) {
080                            Sanitizer sanitizer = _sanitizers.get(0);
081    
082                            sanitizer.sanitize(
083                                    companyId, groupId, userId, className, classPK, contentType,
084                                    modes, inputStream, outputStream, options);
085    
086                            return;
087                    }
088    
089                    ByteArrayOutputStream byteArrayOutputStream =
090                            new ByteArrayOutputStream();
091    
092                    try {
093                            StreamUtil.transfer(inputStream, byteArrayOutputStream);
094                    }
095                    catch (IOException ioe) {
096                            throw new SanitizerException(ioe);
097                    }
098    
099                    byte[] bytes = sanitize(
100                            companyId, groupId, userId, className, classPK, contentType, modes,
101                            byteArrayOutputStream.toByteArray(), options);
102    
103                    try {
104                            outputStream.write(bytes);
105                    }
106                    catch (IOException ioe) {
107                            throw new SanitizerException(ioe);
108                    }
109            }
110    
111            @Override
112            public String sanitize(
113                            long companyId, long groupId, long userId, String className,
114                            long classPK, String contentType, String[] modes, String s,
115                            Map<String, Object> options)
116                    throws SanitizerException {
117    
118                    for (Sanitizer sanitizer : _sanitizers) {
119                            s = sanitizer.sanitize(
120                                    companyId, groupId, userId, className, classPK, contentType,
121                                    modes, s, options);
122                    }
123    
124                    return s;
125            }
126    
127            public void unregisterSanitizer(Sanitizer sanitizer) {
128                    _sanitizers.remove(sanitizer);
129            }
130    
131            private final List<Sanitizer> _sanitizers = new CopyOnWriteArrayList<>();
132            private final ServiceTracker<?, Sanitizer> _serviceTracker;
133    
134            private class SanitizerServiceTrackerCustomizer
135                    implements ServiceTrackerCustomizer<Sanitizer, Sanitizer> {
136    
137                    @Override
138                    public Sanitizer addingService(
139                            ServiceReference<Sanitizer> serviceReference) {
140    
141                            Registry registry = RegistryUtil.getRegistry();
142    
143                            Sanitizer sanitizer = registry.getService(serviceReference);
144    
145                            _sanitizers.add(sanitizer);
146    
147                            return sanitizer;
148                    }
149    
150                    @Override
151                    public void modifiedService(
152                            ServiceReference<Sanitizer> serviceReference, Sanitizer sanitizer) {
153                    }
154    
155                    @Override
156                    public void removedService(
157                            ServiceReference<Sanitizer> serviceReference, Sanitizer sanitizer) {
158    
159                            Registry registry = RegistryUtil.getRegistry();
160    
161                            registry.ungetService(serviceReference);
162    
163                            _sanitizers.remove(sanitizer);
164                    }
165    
166            }
167    
168    }