2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.nettyutil.handler;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import org.openexi.proc.HeaderOptionsOutputType;
16 import org.openexi.proc.common.EXIOptions;
17 import org.openexi.proc.common.EXIOptionsException;
18 import org.openexi.proc.common.GrammarOptions;
19 import org.openexi.proc.grammars.GrammarCache;
20 import org.openexi.sax.EXIReader;
21 import org.openexi.sax.Transmogrifier;
22 import org.openexi.sax.TransmogrifierException;
23 import org.xml.sax.EntityResolver;
24 import org.xml.sax.InputSource;
26 public final class NetconfEXICodec {
28 * NETCONF is XML environment, so the use of EXI cookie is not really needed. Adding it
29 * decreases efficiency of encoding by adding human-readable 4 bytes "EXI$" to the head
30 * of the stream. This is really useful, so let's output it now.
32 private static final boolean OUTPUT_EXI_COOKIE = true;
34 * OpenEXI does not allow us to directly prevent resolution of external entities. In order
35 * to prevent XXE attacks, we reuse a single no-op entity resolver.
37 private static final EntityResolver ENTITY_RESOLVER = new EntityResolver() {
39 public InputSource resolveEntity(final String publicId, final String systemId) {
40 return new InputSource();
45 * Since we have a limited number of options we can have, instantiating a weak cache
46 * will allow us to reuse instances where possible.
48 private static final LoadingCache<Short, GrammarCache> GRAMMAR_CACHES = CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Short, GrammarCache>() {
50 public GrammarCache load(final Short key) {
51 return new GrammarCache(key);
56 * Grammar cache acts as a template and is duplicated by the Transmogrifier and the Reader
57 * before use. It is safe to reuse a single instance.
59 private final GrammarCache exiGrammarCache;
60 private final EXIOptions exiOptions;
62 public NetconfEXICodec(final EXIOptions exiOptions) {
63 this.exiOptions = Preconditions.checkNotNull(exiOptions);
64 this.exiGrammarCache = createGrammarCache(exiOptions);
67 private static GrammarCache createGrammarCache(final EXIOptions exiOptions) {
68 short go = GrammarOptions.DEFAULT_OPTIONS;
69 if (exiOptions.getPreserveComments()) {
70 go = GrammarOptions.addCM(go);
72 if (exiOptions.getPreserveDTD()) {
73 go = GrammarOptions.addDTD(go);
75 if (exiOptions.getPreserveNS()) {
76 go = GrammarOptions.addNS(go);
78 if (exiOptions.getPreservePIs()) {
79 go = GrammarOptions.addPI(go);
82 return GRAMMAR_CACHES.getUnchecked(go);
85 EXIReader getReader() throws EXIOptionsException {
86 final EXIReader r = new EXIReader();
87 r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues());
88 r.setGrammarCache(exiGrammarCache);
89 r.setEntityResolver(ENTITY_RESOLVER);
93 Transmogrifier getTransmogrifier() throws EXIOptionsException, TransmogrifierException {
94 final Transmogrifier transmogrifier = new Transmogrifier();
95 transmogrifier.setAlignmentType(exiOptions.getAlignmentType());
96 transmogrifier.setBlockSize(exiOptions.getBlockSize());
97 transmogrifier.setGrammarCache(exiGrammarCache);
98 transmogrifier.setOutputCookie(OUTPUT_EXI_COOKIE);
99 transmogrifier.setOutputOptions(HeaderOptionsOutputType.all);
100 transmogrifier.setResolveExternalGeneralEntities(false);
101 return transmogrifier;