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.debug;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.rest.annotation.RestOpAnnotation.*;
017
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.cp.*;
022import org.apache.juneau.reflect.*;
023import org.apache.juneau.rest.*;
024import org.apache.juneau.rest.annotation.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Default implementation of the {@link DebugEnablement} interface.
029 *
030 * <p>
031 * Enables debug mode based on the following annotations:
032 * <ul>
033 *    <li class='ja'>{@link Rest#debug()}
034 *    <li class='ja'>{@link RestOp#debug()}
035 *    <li class='ja'>{@link Rest#debugOn()}
036 * </ul>
037 *
038 * <h5 class='section'>See Also:</h5><ul>
039 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.LoggingAndDebugging">Logging / Debugging</a>
040 * </ul>
041 */
042public class BasicDebugEnablement extends DebugEnablement {
043
044   /**
045    * Constructor.
046    *
047    * @param beanStore The bean store containing injectable beans for this enablement.
048    */
049   public BasicDebugEnablement(BeanStore beanStore) {
050      super(beanStore);
051   }
052
053   @Override
054   protected Builder init(BeanStore beanStore) {
055      Builder b = super.init(beanStore);
056
057      DefaultSettingsMap defaultSettings = beanStore.getBean(DefaultSettingsMap.class).get();
058      RestContext.Builder builder = beanStore.getBean(RestContext.Builder.class).get();
059      ResourceSupplier resource = beanStore.getBean(ResourceSupplier.class).get();
060      VarResolver varResolver = beanStore.getBean(VarResolver.class).get();
061
062      // Default debug enablement if not overridden at class/method level.
063      Enablement debugDefault = defaultSettings.get(Enablement.class, "RestContext.debugDefault").orElse(builder.isDebug() ? Enablement.ALWAYS : Enablement.NEVER);
064      b.defaultEnable(debugDefault);
065
066      ClassInfo ci = ClassInfo.ofProxy(resource.get());
067
068      // Gather @Rest(debug) settings.
069      ci.forEachAnnotation(
070         Rest.class,
071         x -> true,
072         x -> {
073            String x2 = varResolver.resolve(x.debug());
074            if (! x2.isEmpty())
075               b.enable(Enablement.fromString(x2), ci.getFullName());
076         }
077      );
078
079      // Gather @RestOp(debug) settings.
080      ci.forEachPublicMethod(
081         x -> true,
082         x -> {
083            x.getAnnotationList(REST_OP_GROUP).forEachValue(
084               String.class,
085               "debug",
086               y -> true,
087               y -> {
088                  String y2 = varResolver.resolve(y);
089                  if (! y2.isEmpty())
090                     b.enable(Enablement.fromString(y2), x.getFullName());
091               }
092            );
093         }
094      );
095
096      // Gather @Rest(debugOn) settings.
097      ci.forEachAnnotation(
098         Rest.class,
099         x -> true,
100         x -> {
101            String x2 = varResolver.resolve(x.debugOn());
102            for (Map.Entry<String,String> e : splitMap(x2, true).entrySet()) {
103               String k = e.getKey(), v = e.getValue();
104               if (v.isEmpty())
105                  v = "ALWAYS";
106               if (! k.isEmpty())
107                  b.enable(Enablement.fromString(v), k);
108            }
109         }
110      );
111
112      return b;
113   }
114}