001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.frontoffice.search.requesttime;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.cocoon.environment.Request;
022
023import org.ametys.web.frontoffice.search.SearchService;
024import org.ametys.web.renderingcontext.RenderingContext;
025import org.ametys.web.renderingcontext.RenderingContextHandler;
026
027/**
028 * A helper to determine if {@link SearchService} is in debug mode.
029 */
030public final class SearchServiceDebugModeHelper
031{
032    /** The parameter name for debug mode */
033    public static final String DEBUG_MODE = "search.debug.mode";
034    
035    private SearchServiceDebugModeHelper()
036    { }
037    
038    /**
039     * Enumeration of debug modes
040     */
041    public static enum DebugMode
042    {
043        /** The "normal" debug mode, which sax additional debug information */
044        NORMAL(1),
045        /** The "debug-view-after-validate" debug mode, which declares the action URL with the {@link #NORMAL} debug mode and the service.debug cocoon-view, for debugging after entering user inputs */
046        DEBUG_VIEW_AFTER_VALIDATE(2);
047        
048        private static Map<Integer, DebugMode> __values;
049        static
050        {
051            DebugMode[] debugModes = values();
052            __values = new HashMap<>(debugModes.length);
053            for (DebugMode debugMode : debugModes)
054            {
055                __values.put(debugMode._value, debugMode);
056            }
057        }
058        
059        int _value;
060        private DebugMode(int value)
061        {
062            _value = value;
063        }
064        
065        /**
066         * Gets the debug mode from an int value
067         * @param mode The int value
068         * @return The debug mode
069         */
070        public static DebugMode valueOf(int mode)
071        {
072            return __values.get(mode);
073        }
074        
075        /**
076         * Gets the int value
077         * @return the int value
078         */
079        public int getInt()
080        {
081            return _value;
082        }
083    }
084    
085    /**
086     * Gets the debug mode
087     * @param request The request
088     * @param renderingContextHandler The handler of rendering context
089     * @return the debug mode
090     */
091    public static DebugMode debugMode(Request request, RenderingContextHandler renderingContextHandler)
092    {
093        if (renderingContextHandler.getRenderingContext() == RenderingContext.FRONT)
094        {
095            return null;
096        }
097        String parameter = request.getParameter(DEBUG_MODE);
098        return parameter == null ? null : DebugMode.valueOf(Integer.parseInt(parameter));
099    }
100}