001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest;
014
015import java.lang.annotation.*;
016import java.util.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.http.*;
020import org.apache.juneau.reflect.*;
021import org.apache.juneau.rest.annotation.*;
022import org.apache.juneau.svl.*;
023import java.lang.reflect.Method;
024
025/**
026 * Builder class for {@link RestMethodContext} objects.
027 */
028@SuppressWarnings("deprecation")
029public class RestMethodContextBuilder extends BeanContextBuilder {
030
031   RestContext context;
032   java.lang.reflect.Method method;
033
034   boolean dotAll;
035
036   RestMethodProperties properties;
037
038   RestMethodContextBuilder(Object servlet, java.lang.reflect.Method method, RestContext context) throws RestServletException {
039      this.context = context;
040      this.method = method;
041
042      String sig = method.getDeclaringClass().getName() + '.' + method.getName();
043      MethodInfo mi = MethodInfo.of(servlet.getClass(), method, method);
044
045      try {
046
047         RestMethod m = mi.getLastAnnotation(RestMethod.class);
048
049         // Also include methods on @Rest-annotated interfaces.
050         if (m == null) {
051            for (Method mi2 : mi.getMatching()) {
052               Class<?> ci2 = mi2.getDeclaringClass();
053               if (ci2.isInterface() && ci2.getAnnotation(Rest.class) != null)
054                  m = new RestMethodAnnotation();
055            }
056         }
057
058         if (m == null)
059            throw new RestServletException("@RestMethod annotation not found on method ''{0}''", sig);
060
061         VarResolver vr = context.getVarResolver();
062         VarResolverSession vrs = vr.createSession();
063
064         applyAnnotations(mi.getAnnotationList(ConfigAnnotationFilter.INSTANCE), vrs);
065
066         properties = new RestMethodProperties(context.getProperties());
067
068         if (m.properties().length > 0 || m.flags().length > 0) {
069            properties = new RestMethodProperties(properties);
070            for (Property p1 : m.properties())
071               properties.put(p1.name(), p1.value());
072            for (String p1 : m.flags())
073               properties.put(p1, true);
074         }
075
076      } catch (RestServletException e) {
077         throw e;
078      } catch (Exception e) {
079         throw new RestServletException(e, "Exception occurred while initializing method ''{0}''", sig);
080      }
081   }
082
083   /**
084    * When enabled, append <js>"/*"</js> to path patterns if not already present.
085    *
086    * @return This object (for method chaining).
087    */
088   public RestMethodContextBuilder dotAll() {
089      this.dotAll = true;
090      return this;
091   }
092
093   // <FluentSetters>
094
095   @Override /* GENERATED - ContextBuilder */
096   public RestMethodContextBuilder add(Map<String,Object> properties) {
097      super.add(properties);
098      return this;
099   }
100
101   @Override /* GENERATED - ContextBuilder */
102   public RestMethodContextBuilder addTo(String name, Object value) {
103      super.addTo(name, value);
104      return this;
105   }
106
107   @Override /* GENERATED - ContextBuilder */
108   public RestMethodContextBuilder appendTo(String name, Object value) {
109      super.appendTo(name, value);
110      return this;
111   }
112
113   @Override /* GENERATED - ContextBuilder */
114   public RestMethodContextBuilder apply(PropertyStore copyFrom) {
115      super.apply(copyFrom);
116      return this;
117   }
118
119   @Override /* GENERATED - ContextBuilder */
120   public RestMethodContextBuilder applyAnnotations(java.lang.Class<?>...fromClasses) {
121      super.applyAnnotations(fromClasses);
122      return this;
123   }
124
125   @Override /* GENERATED - ContextBuilder */
126   public RestMethodContextBuilder applyAnnotations(Method...fromMethods) {
127      super.applyAnnotations(fromMethods);
128      return this;
129   }
130
131   @Override /* GENERATED - ContextBuilder */
132   public RestMethodContextBuilder applyAnnotations(AnnotationList al, VarResolverSession r) {
133      super.applyAnnotations(al, r);
134      return this;
135   }
136
137   @Override /* GENERATED - ContextBuilder */
138   public RestMethodContextBuilder debug() {
139      super.debug();
140      return this;
141   }
142
143   @Override /* GENERATED - ContextBuilder */
144   public RestMethodContextBuilder locale(Locale value) {
145      super.locale(value);
146      return this;
147   }
148
149   @Override /* GENERATED - ContextBuilder */
150   public RestMethodContextBuilder mediaType(MediaType value) {
151      super.mediaType(value);
152      return this;
153   }
154
155   @Override /* GENERATED - ContextBuilder */
156   public RestMethodContextBuilder prependTo(String name, Object value) {
157      super.prependTo(name, value);
158      return this;
159   }
160
161   @Override /* GENERATED - ContextBuilder */
162   public RestMethodContextBuilder putAllTo(String name, Object value) {
163      super.putAllTo(name, value);
164      return this;
165   }
166
167   @Override /* GENERATED - ContextBuilder */
168   public RestMethodContextBuilder putTo(String name, String key, Object value) {
169      super.putTo(name, key, value);
170      return this;
171   }
172
173   @Override /* GENERATED - ContextBuilder */
174   public RestMethodContextBuilder removeFrom(String name, Object value) {
175      super.removeFrom(name, value);
176      return this;
177   }
178
179   @Override /* GENERATED - ContextBuilder */
180   public RestMethodContextBuilder set(Map<String,Object> properties) {
181      super.set(properties);
182      return this;
183   }
184
185   @Override /* GENERATED - ContextBuilder */
186   public RestMethodContextBuilder set(String name, Object value) {
187      super.set(name, value);
188      return this;
189   }
190
191   @Override /* GENERATED - ContextBuilder */
192   public RestMethodContextBuilder timeZone(TimeZone value) {
193      super.timeZone(value);
194      return this;
195   }
196
197   @Override /* GENERATED - BeanContextBuilder */
198   public RestMethodContextBuilder annotations(Annotation...values) {
199      super.annotations(values);
200      return this;
201   }
202
203   @Override /* GENERATED - BeanContextBuilder */
204   public RestMethodContextBuilder beanClassVisibility(Visibility value) {
205      super.beanClassVisibility(value);
206      return this;
207   }
208
209   @Override /* GENERATED - BeanContextBuilder */
210   public RestMethodContextBuilder beanConstructorVisibility(Visibility value) {
211      super.beanConstructorVisibility(value);
212      return this;
213   }
214
215   @Override /* GENERATED - BeanContextBuilder */
216   public RestMethodContextBuilder beanFieldVisibility(Visibility value) {
217      super.beanFieldVisibility(value);
218      return this;
219   }
220
221   @Override /* GENERATED - BeanContextBuilder */
222   public RestMethodContextBuilder beanInterceptor(Class<?> on, Class<? extends org.apache.juneau.transform.BeanInterceptor<?>> value) {
223      super.beanInterceptor(on, value);
224      return this;
225   }
226
227   @Override /* GENERATED - BeanContextBuilder */
228   public RestMethodContextBuilder beanMapPutReturnsOldValue() {
229      super.beanMapPutReturnsOldValue();
230      return this;
231   }
232
233   @Override /* GENERATED - BeanContextBuilder */
234   public RestMethodContextBuilder beanMethodVisibility(Visibility value) {
235      super.beanMethodVisibility(value);
236      return this;
237   }
238
239   @Override /* GENERATED - BeanContextBuilder */
240   public RestMethodContextBuilder beansDontRequireSomeProperties() {
241      super.beansDontRequireSomeProperties();
242      return this;
243   }
244
245   @Override /* GENERATED - BeanContextBuilder */
246   public RestMethodContextBuilder beansRequireDefaultConstructor() {
247      super.beansRequireDefaultConstructor();
248      return this;
249   }
250
251   @Override /* GENERATED - BeanContextBuilder */
252   public RestMethodContextBuilder beansRequireSerializable() {
253      super.beansRequireSerializable();
254      return this;
255   }
256
257   @Override /* GENERATED - BeanContextBuilder */
258   public RestMethodContextBuilder beansRequireSettersForGetters() {
259      super.beansRequireSettersForGetters();
260      return this;
261   }
262
263   @Override /* GENERATED - BeanContextBuilder */
264   public RestMethodContextBuilder bpi(Map<String,Object> values) {
265      super.bpi(values);
266      return this;
267   }
268
269   @Override /* GENERATED - BeanContextBuilder */
270   public RestMethodContextBuilder bpi(Class<?> beanClass, String properties) {
271      super.bpi(beanClass, properties);
272      return this;
273   }
274
275   @Override /* GENERATED - BeanContextBuilder */
276   public RestMethodContextBuilder bpi(String beanClassName, String properties) {
277      super.bpi(beanClassName, properties);
278      return this;
279   }
280
281   @Override /* GENERATED - BeanContextBuilder */
282   public RestMethodContextBuilder bpro(Map<String,Object> values) {
283      super.bpro(values);
284      return this;
285   }
286
287   @Override /* GENERATED - BeanContextBuilder */
288   public RestMethodContextBuilder bpro(Class<?> beanClass, String properties) {
289      super.bpro(beanClass, properties);
290      return this;
291   }
292
293   @Override /* GENERATED - BeanContextBuilder */
294   public RestMethodContextBuilder bpro(String beanClassName, String properties) {
295      super.bpro(beanClassName, properties);
296      return this;
297   }
298
299   @Override /* GENERATED - BeanContextBuilder */
300   public RestMethodContextBuilder bpwo(Map<String,Object> values) {
301      super.bpwo(values);
302      return this;
303   }
304
305   @Override /* GENERATED - BeanContextBuilder */
306   public RestMethodContextBuilder bpwo(Class<?> beanClass, String properties) {
307      super.bpwo(beanClass, properties);
308      return this;
309   }
310
311   @Override /* GENERATED - BeanContextBuilder */
312   public RestMethodContextBuilder bpwo(String beanClassName, String properties) {
313      super.bpwo(beanClassName, properties);
314      return this;
315   }
316
317   @Override /* GENERATED - BeanContextBuilder */
318   public RestMethodContextBuilder bpx(Map<String,Object> values) {
319      super.bpx(values);
320      return this;
321   }
322
323   @Override /* GENERATED - BeanContextBuilder */
324   public RestMethodContextBuilder bpx(Class<?> beanClass, String properties) {
325      super.bpx(beanClass, properties);
326      return this;
327   }
328
329   @Override /* GENERATED - BeanContextBuilder */
330   public RestMethodContextBuilder bpx(String beanClassName, String properties) {
331      super.bpx(beanClassName, properties);
332      return this;
333   }
334
335   @Override /* GENERATED - BeanContextBuilder */
336   public RestMethodContextBuilder dictionary(Object...values) {
337      super.dictionary(values);
338      return this;
339   }
340
341   @Override /* GENERATED - BeanContextBuilder */
342   public RestMethodContextBuilder dictionaryOn(Class<?> on, java.lang.Class<?>...values) {
343      super.dictionaryOn(on, values);
344      return this;
345   }
346
347   @Override /* GENERATED - BeanContextBuilder */
348   public RestMethodContextBuilder dontIgnorePropertiesWithoutSetters() {
349      super.dontIgnorePropertiesWithoutSetters();
350      return this;
351   }
352
353   @Override /* GENERATED - BeanContextBuilder */
354   public RestMethodContextBuilder dontIgnoreTransientFields() {
355      super.dontIgnoreTransientFields();
356      return this;
357   }
358
359   @Override /* GENERATED - BeanContextBuilder */
360   public RestMethodContextBuilder dontIgnoreUnknownNullBeanProperties() {
361      super.dontIgnoreUnknownNullBeanProperties();
362      return this;
363   }
364
365   @Override /* GENERATED - BeanContextBuilder */
366   public RestMethodContextBuilder dontUseInterfaceProxies() {
367      super.dontUseInterfaceProxies();
368      return this;
369   }
370
371   @Override /* GENERATED - BeanContextBuilder */
372   public <T> RestMethodContextBuilder example(Class<T> pojoClass, T o) {
373      super.example(pojoClass, o);
374      return this;
375   }
376
377   @Override /* GENERATED - BeanContextBuilder */
378   public <T> RestMethodContextBuilder exampleJson(Class<T> pojoClass, String json) {
379      super.exampleJson(pojoClass, json);
380      return this;
381   }
382
383   @Override /* GENERATED - BeanContextBuilder */
384   public RestMethodContextBuilder fluentSetters() {
385      super.fluentSetters();
386      return this;
387   }
388
389   @Override /* GENERATED - BeanContextBuilder */
390   public RestMethodContextBuilder fluentSetters(Class<?> on) {
391      super.fluentSetters(on);
392      return this;
393   }
394
395   @Override /* GENERATED - BeanContextBuilder */
396   public RestMethodContextBuilder ignoreInvocationExceptionsOnGetters() {
397      super.ignoreInvocationExceptionsOnGetters();
398      return this;
399   }
400
401   @Override /* GENERATED - BeanContextBuilder */
402   public RestMethodContextBuilder ignoreInvocationExceptionsOnSetters() {
403      super.ignoreInvocationExceptionsOnSetters();
404      return this;
405   }
406
407   @Override /* GENERATED - BeanContextBuilder */
408   public RestMethodContextBuilder ignoreUnknownBeanProperties() {
409      super.ignoreUnknownBeanProperties();
410      return this;
411   }
412
413   @Override /* GENERATED - BeanContextBuilder */
414   public RestMethodContextBuilder implClass(Class<?> interfaceClass, Class<?> implClass) {
415      super.implClass(interfaceClass, implClass);
416      return this;
417   }
418
419   @Override /* GENERATED - BeanContextBuilder */
420   public RestMethodContextBuilder implClasses(Map<Class<?>,Class<?>> values) {
421      super.implClasses(values);
422      return this;
423   }
424
425   @Override /* GENERATED - BeanContextBuilder */
426   public RestMethodContextBuilder interfaceClass(Class<?> on, Class<?> value) {
427      super.interfaceClass(on, value);
428      return this;
429   }
430
431   @Override /* GENERATED - BeanContextBuilder */
432   public RestMethodContextBuilder interfaces(java.lang.Class<?>...value) {
433      super.interfaces(value);
434      return this;
435   }
436
437   @Override /* GENERATED - BeanContextBuilder */
438   public RestMethodContextBuilder notBeanClasses(Object...values) {
439      super.notBeanClasses(values);
440      return this;
441   }
442
443   @Override /* GENERATED - BeanContextBuilder */
444   public RestMethodContextBuilder notBeanPackages(Object...values) {
445      super.notBeanPackages(values);
446      return this;
447   }
448
449   @Override /* GENERATED - BeanContextBuilder */
450   public RestMethodContextBuilder propertyNamer(Class<? extends org.apache.juneau.PropertyNamer> value) {
451      super.propertyNamer(value);
452      return this;
453   }
454
455   @Override /* GENERATED - BeanContextBuilder */
456   public RestMethodContextBuilder propertyNamer(Class<?> on, Class<? extends org.apache.juneau.PropertyNamer> value) {
457      super.propertyNamer(on, value);
458      return this;
459   }
460
461   @Override /* GENERATED - BeanContextBuilder */
462   public RestMethodContextBuilder sortProperties() {
463      super.sortProperties();
464      return this;
465   }
466
467   @Override /* GENERATED - BeanContextBuilder */
468   public RestMethodContextBuilder sortProperties(java.lang.Class<?>...on) {
469      super.sortProperties(on);
470      return this;
471   }
472
473   @Override /* GENERATED - BeanContextBuilder */
474   public RestMethodContextBuilder stopClass(Class<?> on, Class<?> value) {
475      super.stopClass(on, value);
476      return this;
477   }
478
479   @Override /* GENERATED - BeanContextBuilder */
480   public RestMethodContextBuilder swaps(Object...values) {
481      super.swaps(values);
482      return this;
483   }
484
485   @Override /* GENERATED - BeanContextBuilder */
486   public RestMethodContextBuilder typeName(Class<?> on, String value) {
487      super.typeName(on, value);
488      return this;
489   }
490
491   @Override /* GENERATED - BeanContextBuilder */
492   public RestMethodContextBuilder typePropertyName(String value) {
493      super.typePropertyName(value);
494      return this;
495   }
496
497   @Override /* GENERATED - BeanContextBuilder */
498   public RestMethodContextBuilder typePropertyName(Class<?> on, String value) {
499      super.typePropertyName(on, value);
500      return this;
501   }
502
503   @Override /* GENERATED - BeanContextBuilder */
504   public RestMethodContextBuilder useEnumNames() {
505      super.useEnumNames();
506      return this;
507   }
508
509   @Override /* GENERATED - BeanContextBuilder */
510   public RestMethodContextBuilder useJavaBeanIntrospector() {
511      super.useJavaBeanIntrospector();
512      return this;
513   }
514
515   // </FluentSetters>
516}