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 org.apache.juneau.*;
016import org.apache.juneau.reflect.*;
017import org.apache.juneau.rest.annotation.*;
018import org.apache.juneau.svl.*;
019
020/**
021 * Builder class for {@link RestMethodContext} objects.
022 */
023@SuppressWarnings("deprecation")
024public class RestMethodContextBuilder extends BeanContextBuilder {
025
026   RestContext context;
027   java.lang.reflect.Method method;
028
029   RestMethodProperties properties;
030
031   RestMethodContextBuilder(Object servlet, java.lang.reflect.Method method, RestContext context) throws RestServletException {
032      this.context = context;
033      this.method = method;
034
035      String sig = method.getDeclaringClass().getName() + '.' + method.getName();
036      MethodInfo mi = MethodInfo.of(servlet.getClass(), method, method);
037
038      try {
039
040         RestMethod m = mi.getAnnotation(RestMethod.class);
041         if (m == null)
042            throw new RestServletException("@RestMethod annotation not found on method ''{0}''", sig);
043
044         VarResolver vr = context.getVarResolver();
045         VarResolverSession vrs = vr.createSession();
046
047         applyAnnotations(mi.getAnnotationListParentFirst(ConfigAnnotationFilter.INSTANCE), vrs);
048
049         properties = new RestMethodProperties(context.getProperties());
050
051         if (m.properties().length > 0 || m.flags().length > 0) {
052            properties = new RestMethodProperties(properties);
053            for (Property p1 : m.properties())
054               properties.put(p1.name(), p1.value());
055            for (String p1 : m.flags())
056               properties.put(p1, true);
057         }
058
059      } catch (RestServletException e) {
060         throw e;
061      } catch (Exception e) {
062         throw new RestServletException("Exception occurred while initializing method ''{0}''", sig).initCause(e);
063      }
064   }
065}