001
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
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 =
132 new CopyOnWriteArrayList<Sanitizer>();
133 private final ServiceTracker<?, Sanitizer> _serviceTracker;
134
135 private class SanitizerServiceTrackerCustomizer
136 implements ServiceTrackerCustomizer<Sanitizer, Sanitizer> {
137
138 @Override
139 public Sanitizer addingService(
140 ServiceReference<Sanitizer> serviceReference) {
141
142 Registry registry = RegistryUtil.getRegistry();
143
144 Sanitizer sanitizer = registry.getService(serviceReference);
145
146 _sanitizers.add(sanitizer);
147
148 return sanitizer;
149 }
150
151 @Override
152 public void modifiedService(
153 ServiceReference<Sanitizer> serviceReference, Sanitizer sanitizer) {
154 }
155
156 @Override
157 public void removedService(
158 ServiceReference<Sanitizer> serviceReference, Sanitizer sanitizer) {
159
160 Registry registry = RegistryUtil.getRegistry();
161
162 registry.ungetService(serviceReference);
163
164 _sanitizers.remove(sanitizer);
165 }
166
167 }
168
169 }