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.test.randomizerbumpers;
016    
017    import com.liferay.portal.kernel.io.DummyWriter;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.test.randomizerbumpers.RandomizerBumper;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    
025    import org.apache.tika.config.TikaConfig;
026    import org.apache.tika.metadata.Metadata;
027    import org.apache.tika.parser.AutoDetectParser;
028    import org.apache.tika.parser.ParseContext;
029    import org.apache.tika.parser.Parser;
030    import org.apache.tika.sax.WriteOutContentHandler;
031    
032    /**
033     * @author Matthew Tambara
034     */
035    public class TikaSafeRandomizerBumper implements RandomizerBumper<byte[]> {
036    
037            public static final TikaSafeRandomizerBumper INSTANCE =
038                    new TikaSafeRandomizerBumper(null);
039    
040            public TikaSafeRandomizerBumper(String contentType) {
041                    _contentType = contentType;
042            }
043    
044            @Override
045            public boolean accept(byte[] randomValue) {
046                    try {
047                            ParseContext parserContext = new ParseContext();
048    
049                            Parser parser = new AutoDetectParser(new TikaConfig());
050    
051                            parserContext.set(Parser.class, parser);
052    
053                            Metadata metadata = new Metadata();
054    
055                            parser.parse(
056                                    new UnsyncByteArrayInputStream(randomValue),
057                                    new WriteOutContentHandler(new DummyWriter()), metadata,
058                                    parserContext);
059    
060                            if (_contentType == null) {
061                                    if (_log.isInfoEnabled()) {
062                                            _log.info("Accepted: " + byteArrayToString(randomValue));
063                                    }
064    
065                                    return true;
066                            }
067    
068                            String contentType = metadata.get("Content-Type");
069    
070                            if (contentType.contains(_contentType)) {
071                                    if (_log.isInfoEnabled()) {
072                                            _log.info("Accepted: " + byteArrayToString(randomValue));
073                                    }
074    
075                                    return true;
076                            }
077    
078                            return false;
079                    }
080                    catch (Throwable t) {
081                            return false;
082                    }
083            }
084    
085            protected static String byteArrayToString(byte[] byteArray) {
086                    StringBundler sb = new StringBundler((byteArray.length * 3) + 1);
087    
088                    sb.append(StringPool.OPEN_CURLY_BRACE);
089    
090                    for (byte b : byteArray) {
091                            sb.append("(byte)");
092                            sb.append(b);
093                            sb.append(StringPool.COMMA_AND_SPACE);
094                    }
095    
096                    sb.setIndex(sb.index() - 1);
097    
098                    sb.append(StringPool.CLOSE_CURLY_BRACE);
099    
100                    return sb.toString();
101            }
102    
103            private static final Log _log = LogFactoryUtil.getLog(
104                    TikaSafeRandomizerBumper.class);
105    
106            private final String _contentType;
107    
108    }