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