X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-netty-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fnettyutil%2Fhandler%2FNetconfEXICodec.java;h=415afe3d0abad056a01a6b6301ec05f972177025;hb=refs%2Fheads%2Fmaster;hp=235b5378c26736ea5c02b49621b22650e55b0920;hpb=56754e685563cf98a77e2e0772753f95165293b8;p=netconf.git diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java index 235b5378c2..415afe3d0a 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java @@ -5,100 +5,56 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.netconf.nettyutil.handler; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; -import org.openexi.proc.HeaderOptionsOutputType; -import org.openexi.proc.common.EXIOptions; -import org.openexi.proc.common.EXIOptionsException; -import org.openexi.proc.common.GrammarOptions; -import org.openexi.proc.grammars.GrammarCache; -import org.openexi.sax.EXIReader; -import org.openexi.sax.Transmogrifier; -import org.openexi.sax.TransmogrifierException; +import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters; +import org.opendaylight.netconf.shaded.exificient.core.EXIFactory; +import org.opendaylight.netconf.shaded.exificient.core.exceptions.EXIException; +import org.opendaylight.netconf.shaded.exificient.main.api.sax.SAXEncoder; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; public final class NetconfEXICodec { - /** - * NETCONF is XML environment, so the use of EXI cookie is not really needed. Adding it - * decreases efficiency of encoding by adding human-readable 4 bytes "EXI$" to the head - * of the stream. This is really useful, so let's output it now. - */ - private static final boolean OUTPUT_EXI_COOKIE = true; /** * OpenEXI does not allow us to directly prevent resolution of external entities. In order * to prevent XXE attacks, we reuse a single no-op entity resolver. */ - private static final EntityResolver ENTITY_RESOLVER = new EntityResolver() { - @Override - public InputSource resolveEntity(final String publicId, final String systemId) { - return new InputSource(); - } - }; + private static final EntityResolver ENTITY_RESOLVER = (publicId, systemId) -> new InputSource(); /** * Since we have a limited number of options we can have, instantiating a weak cache * will allow us to reuse instances where possible. */ - private static final LoadingCache GRAMMAR_CACHES = - CacheBuilder.newBuilder().weakValues().build(new CacheLoader() { + private static final LoadingCache CODECS = + CacheBuilder.newBuilder().weakValues().build(new CacheLoader() { @Override - public GrammarCache load(final Short key) { - return new GrammarCache(key); + public NetconfEXICodec load(final EXIParameters key) { + return new NetconfEXICodec(key.getFactory()); } }); - /** - * Grammar cache acts as a template and is duplicated by the Transmogrifier and the Reader - * before use. It is safe to reuse a single instance. - */ - private final GrammarCache exiGrammarCache; - private final EXIOptions exiOptions; + private final ThreadLocalSAXFactory exiFactory; - public NetconfEXICodec(final EXIOptions exiOptions) { - this.exiOptions = Preconditions.checkNotNull(exiOptions); - this.exiGrammarCache = createGrammarCache(exiOptions); + private NetconfEXICodec(final EXIFactory exiFactory) { + this.exiFactory = new ThreadLocalSAXFactory(requireNonNull(exiFactory)); } - private static GrammarCache createGrammarCache(final EXIOptions exiOptions) { - short go = GrammarOptions.DEFAULT_OPTIONS; - if (exiOptions.getPreserveComments()) { - go = GrammarOptions.addCM(go); - } - if (exiOptions.getPreserveDTD()) { - go = GrammarOptions.addDTD(go); - } - if (exiOptions.getPreserveNS()) { - go = GrammarOptions.addNS(go); - } - if (exiOptions.getPreservePIs()) { - go = GrammarOptions.addPI(go); - } - - return GRAMMAR_CACHES.getUnchecked(go); + public static NetconfEXICodec forParameters(final EXIParameters parameters) { + return CODECS.getUnchecked(parameters); } - EXIReader getReader() throws EXIOptionsException { - final EXIReader r = new EXIReader(); - r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues()); - r.setGrammarCache(exiGrammarCache); - r.setEntityResolver(ENTITY_RESOLVER); - return r; + ThreadLocalSAXDecoder getReader() throws EXIException { + final ThreadLocalSAXDecoder reader = exiFactory.createEXIReader(); + reader.setEntityResolver(ENTITY_RESOLVER); + return reader; } - Transmogrifier getTransmogrifier() throws EXIOptionsException, TransmogrifierException { - final Transmogrifier transmogrifier = new Transmogrifier(); - transmogrifier.setAlignmentType(exiOptions.getAlignmentType()); - transmogrifier.setBlockSize(exiOptions.getBlockSize()); - transmogrifier.setGrammarCache(exiGrammarCache); - transmogrifier.setOutputCookie(OUTPUT_EXI_COOKIE); - transmogrifier.setOutputOptions(HeaderOptionsOutputType.all); - transmogrifier.setResolveExternalGeneralEntities(false); - return transmogrifier; + SAXEncoder getWriter() throws EXIException { + return exiFactory.createEXIWriter(); } }