1 package com.atlassian.plugins.rest.doclet.generators.grammars;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.util.List;
6
7 import javax.ws.rs.core.MediaType;
8 import javax.ws.rs.core.UriInfo;
9 import javax.xml.bind.JAXBContext;
10 import javax.xml.bind.Unmarshaller;
11
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import com.sun.jersey.api.model.AbstractMethod;
16 import com.sun.jersey.api.model.AbstractResource;
17 import com.sun.jersey.api.model.AbstractResourceMethod;
18 import com.sun.jersey.api.model.Parameter;
19 import com.sun.jersey.server.wadl.ApplicationDescription;
20 import com.sun.jersey.server.wadl.WadlGenerator;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class WadlGrammarsAdaptor implements WadlGenerator {
35 private static final Logger LOG = LoggerFactory.getLogger(WadlGrammarsAdaptor.class);
36
37 private WadlGenerator _delegate;
38 private File _grammarsFile;
39 private InputStream _grammarsStream;
40 private Boolean overrideGrammars = false;
41
42 private com.sun.research.ws.wadl.Grammars _grammars;
43
44 public WadlGrammarsAdaptor() {
45 }
46
47 public WadlGrammarsAdaptor(WadlGenerator delegate, com.sun.research.ws.wadl.Grammars grammars) {
48 _delegate = delegate;
49 _grammars = grammars;
50 }
51
52 public void setWadlGeneratorDelegate(WadlGenerator delegate) {
53 _delegate = delegate;
54 }
55
56
57
58
59 @Override
60 public void setEnvironment(Environment env) {
61 _delegate.setEnvironment(env);
62 }
63
64 public void setOverrideGrammars(Boolean overrideGrammars) {
65 this.overrideGrammars = overrideGrammars;
66 }
67
68 public String getRequiredJaxbContextPath() {
69 return _delegate.getRequiredJaxbContextPath();
70 }
71
72 public void setGrammarsFile(File grammarsFile) {
73 if (_grammarsStream != null) {
74 throw new IllegalStateException("The grammarsStream property is already set,"
75 + " therefore you cannot set the grammarsFile property. Only one of both can be set at a time.");
76 }
77 _grammarsFile = grammarsFile;
78 }
79
80 public void setGrammarsStream(InputStream grammarsStream) {
81 if (_grammarsFile != null) {
82 throw new IllegalStateException("The grammarsFile property is already set,"
83 + " therefore you cannot set the grammarsStream property. Only one of both can be set at a time.");
84 }
85 _grammarsStream = grammarsStream;
86 }
87
88 public void init() throws Exception {
89 if (_grammarsFile == null && _grammarsStream == null) {
90 throw new IllegalStateException("Neither the grammarsFile nor the grammarsStream"
91 + " is set, one of both is required.");
92 }
93 _delegate.init();
94
95
96
97 final JAXBContext c = JAXBContext.newInstance(com.sun.research.ws.wadl.Grammars.class, Grammars.class);
98 final Unmarshaller m = c.createUnmarshaller();
99 final Object obj = _grammarsFile != null ? m.unmarshal(_grammarsFile) : m.unmarshal(_grammarsStream);
100
101 if (obj.getClass() == Grammars.class) {
102 Grammars grm = Grammars.class.cast(obj);
103 _grammars = GrammarTransformer.transform(grm);
104 } else if (obj.getClass() == com.sun.research.ws.wadl.Grammars.class) {
105 _grammars = com.sun.research.ws.wadl.Grammars.class.cast(obj);
106 } else {
107
108 throw new RuntimeException("Unknown grammars class: " + obj.getClass());
109 }
110 }
111
112
113
114
115
116
117 public com.sun.research.ws.wadl.Application createApplication(UriInfo requestInfo) {
118 final com.sun.research.ws.wadl.Application result = _delegate.createApplication(requestInfo);
119 if (result.getGrammars() != null && !overrideGrammars) {
120 LOG.info("The wadl application created by the delegate (" + _delegate
121 + ") already contains a grammars element,"
122 + " we're adding elements of the provided grammars file.");
123 if (!_grammars.getAny().isEmpty()) {
124 result.getGrammars().getAny().addAll(_grammars.getAny());
125 }
126 if (!_grammars.getDoc().isEmpty()) {
127 result.getGrammars().getDoc().addAll(_grammars.getDoc());
128 }
129 if (!_grammars.getInclude().isEmpty()) {
130 result.getGrammars().getInclude().addAll(_grammars.getInclude());
131 }
132 } else {
133 result.setGrammars(_grammars);
134 }
135 return result;
136 }
137
138
139
140
141
142
143
144
145 public com.sun.research.ws.wadl.Method createMethod(AbstractResource ar, AbstractResourceMethod arm) {
146 return _delegate.createMethod(ar, arm);
147 }
148
149
150
151
152
153
154
155
156 public com.sun.research.ws.wadl.Request createRequest(AbstractResource ar, AbstractResourceMethod arm) {
157 return _delegate.createRequest(ar, arm);
158 }
159
160
161
162
163
164
165
166
167
168 public com.sun.research.ws.wadl.Param createParam(AbstractResource ar, AbstractMethod am, Parameter p) {
169 return _delegate.createParam(ar, am, p);
170 }
171
172
173
174
175
176
177
178
179
180 public com.sun.research.ws.wadl.Representation createRequestRepresentation(AbstractResource ar, AbstractResourceMethod arm, MediaType mt) {
181 return _delegate.createRequestRepresentation(ar, arm, mt);
182 }
183
184
185
186
187
188
189
190
191 public com.sun.research.ws.wadl.Resource createResource(AbstractResource ar, String path) {
192 return _delegate.createResource(ar, path);
193 }
194
195
196
197
198
199 public com.sun.research.ws.wadl.Resources createResources() {
200 return _delegate.createResources();
201 }
202
203
204
205
206
207
208
209
210 public List<com.sun.research.ws.wadl.Response> createResponses(AbstractResource ar, AbstractResourceMethod arm) {
211 return _delegate.createResponses(ar, arm);
212 }
213
214
215
216 @Override
217 public ExternalGrammarDefinition createExternalGrammar() {
218 if (overrideGrammars) {
219 return new ExternalGrammarDefinition();
220 }
221 return _delegate.createExternalGrammar();
222 }
223
224 @Override
225 public void attachTypes(ApplicationDescription egd) {
226 _delegate.attachTypes(egd);
227 }
228 }