Merge "Bug 8153: Enforce check-style rules for netconf - netconf-netty-util"
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / NetconfEXICodec.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.nettyutil.handler;
10
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;
25
26 public final class NetconfEXICodec {
27     /**
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.
31      */
32     private static final boolean OUTPUT_EXI_COOKIE = true;
33     /**
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.
36      */
37     private static final EntityResolver ENTITY_RESOLVER = new EntityResolver() {
38         @Override
39         public InputSource resolveEntity(final String publicId, final String systemId) {
40             return new InputSource();
41         }
42     };
43
44     /**
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.
47      */
48     private static final LoadingCache<Short, GrammarCache> GRAMMAR_CACHES =
49             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Short, GrammarCache>() {
50                 @Override
51                 public GrammarCache load(final Short key) {
52                     return new GrammarCache(key);
53                 }
54             });
55
56     /**
57      * Grammar cache acts as a template and is duplicated by the Transmogrifier and the Reader
58      * before use. It is safe to reuse a single instance.
59      */
60     private final GrammarCache exiGrammarCache;
61     private final EXIOptions exiOptions;
62
63     public NetconfEXICodec(final EXIOptions exiOptions) {
64         this.exiOptions = Preconditions.checkNotNull(exiOptions);
65         this.exiGrammarCache = createGrammarCache(exiOptions);
66     }
67
68     private static GrammarCache createGrammarCache(final EXIOptions exiOptions) {
69         short go = GrammarOptions.DEFAULT_OPTIONS;
70         if (exiOptions.getPreserveComments()) {
71             go = GrammarOptions.addCM(go);
72         }
73         if (exiOptions.getPreserveDTD()) {
74             go = GrammarOptions.addDTD(go);
75         }
76         if (exiOptions.getPreserveNS()) {
77             go = GrammarOptions.addNS(go);
78         }
79         if (exiOptions.getPreservePIs()) {
80             go = GrammarOptions.addPI(go);
81         }
82
83         return GRAMMAR_CACHES.getUnchecked(go);
84     }
85
86     EXIReader getReader() throws EXIOptionsException {
87         final EXIReader r = new EXIReader();
88         r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues());
89         r.setGrammarCache(exiGrammarCache);
90         r.setEntityResolver(ENTITY_RESOLVER);
91         return r;
92     }
93
94     Transmogrifier getTransmogrifier() throws EXIOptionsException, TransmogrifierException {
95         final Transmogrifier transmogrifier = new Transmogrifier();
96         transmogrifier.setAlignmentType(exiOptions.getAlignmentType());
97         transmogrifier.setBlockSize(exiOptions.getBlockSize());
98         transmogrifier.setGrammarCache(exiGrammarCache);
99         transmogrifier.setOutputCookie(OUTPUT_EXI_COOKIE);
100         transmogrifier.setOutputOptions(HeaderOptionsOutputType.all);
101         transmogrifier.setResolveExternalGeneralEntities(false);
102         return transmogrifier;
103     }
104 }