/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flowable.ui.task.rest.runtime;

import org.flowable.engine.event.EventLogEntry;
import org.flowable.ui.task.model.debugger.BreakpointRepresentation;
import org.flowable.ui.task.model.debugger.ExecutionRepresentation;
import org.flowable.ui.task.service.debugger.DebuggerRestVariable;
import org.flowable.ui.task.service.debugger.DebuggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
 * REST controller for managing a debugger requests.
 *
 * @author martin.grofcik
 */
@RestController
@RequestMapping("/app")
public class DebuggerResource {

    @Autowired
    protected DebuggerService debuggerService;

    @Autowired
    protected Environment environment;

    @RequestMapping(value = "/rest/debugger/breakpoints", method = RequestMethod.GET, produces = "application/json")
    public Collection<BreakpointRepresentation> getBreakpoints() {
        return debuggerService.getBreakpoints();
    }

    @RequestMapping(value = "/rest/debugger/breakpoints", method = RequestMethod.POST)
    public void addBreakPoints(@RequestBody BreakpointRepresentation breakpointRepresentation) {
        debuggerService.addBreakpoint(breakpointRepresentation);
    }

    @RequestMapping(value = "/rest/debugger/breakpoints/{executionId}/continue", method = RequestMethod.PUT)
    public void continueExecution(@PathVariable String executionId) {
        debuggerService.continueExecution(executionId);
    }

    @RequestMapping(value = "/rest/debugger/breakpoints", method = RequestMethod.DELETE)
    public void deleteBreakPoints(@RequestBody BreakpointRepresentation breakpointRepresentation) {
        debuggerService.removeBreakpoint(breakpointRepresentation);
    }

    @RequestMapping(value = "/rest/debugger/eventlog/{processInstanceId}", method = RequestMethod.GET)
    public List<EventLogEntry> getEventLog(@PathVariable String processInstanceId) {
        return debuggerService.getProcessInstanceEventLog(processInstanceId);
    }

    @RequestMapping(value = "/rest/debugger/variables/{executionId}", method = RequestMethod.GET)
    public List<DebuggerRestVariable> getExecutionVariables(@PathVariable String executionId) {
        return debuggerService.getExecutionVariables(executionId);
    }

    @RequestMapping(value = "/rest/debugger/executions/{processInstanceId}", method = RequestMethod.GET)
    public List<ExecutionRepresentation> getExecutions(@PathVariable String processInstanceId) {
        return debuggerService.getExecutions(processInstanceId);
    }

    @RequestMapping(value = "/rest/debugger/evaluate/expression/{executionId}", method = RequestMethod.POST, produces = "application/json")
    public Object evaluateExpression(@PathVariable String executionId, @RequestBody String expression) {
        if (environment.getProperty("flowable.experimental.debugger.enabled", Boolean.class, false)) {
            return debuggerService.evaluateExpression(executionId, expression);
        }
        throw new RuntimeException("property flowable.experimental.debugger.enabled is not enabled");
    }

    @RequestMapping(value = "/rest/debugger/evaluate/{scriptLanguage}/{executionId}", method = RequestMethod.POST)
    public void evaluateScript(@PathVariable String executionId, @PathVariable String scriptLanguage, @RequestBody String script) {
        if (environment.getProperty("flowable.experimental.debugger.enabled", Boolean.class, false)) {
            debuggerService.evaluateScript(executionId, scriptLanguage, script);
        }
    }

    @RequestMapping(value = "/rest/debugger", method = RequestMethod.GET)
    public boolean isDebuggerAllowed() {
        return environment.getProperty("flowable.experimental.debugger.enabled", Boolean.class, false);
    }

}

@@ -1847,24 +1847,57 @@
 kpoints() %7B%0A
+        assertDebuggerEnabled();%0A
         retu
@@ -2116,32 +2116,65 @@
 presentation) %7B%0A
+        assertDebuggerEnabled();%0A
         debugger
@@ -2400,32 +2400,65 @@
  executionId) %7B%0A
+        assertDebuggerEnabled();%0A
         debugger
@@ -2685,32 +2685,65 @@
 presentation) %7B%0A
+        assertDebuggerEnabled();%0A
         debugger
@@ -2981,32 +2981,65 @@
 ssInstanceId) %7B%0A
+        assertDebuggerEnabled();%0A
         return d
@@ -3293,32 +3293,65 @@
  executionId) %7B%0A
+        assertDebuggerEnabled();%0A
         return d
@@ -3602,32 +3602,65 @@
 ssInstanceId) %7B%0A
+        assertDebuggerEnabled();%0A
         return d
@@ -3972,107 +3972,33 @@
     
-if (environment.getProperty(%22flowable.experimental.d
+assertD
 ebugger
-.e
+E
 nabled
-%22, Boolean.class, false)) %7B%0A    
+();%0A
     
@@ -4073,120 +4073,8 @@
 n);%0A
-        %7D%0A        throw new RuntimeException(%22property flowable.experimental.debugger.enabled is not enabled%22);%0A
     
@@ -4335,114 +4335,41 @@
     
-if (environment.getProperty(%22flowable.experimental.d
+assertD
 ebugger
-.e
+E
 nabled
-%22, Boolean.class, false)) %7B%0A   
+();%0A
         
  deb
@@ -4360,25 +4360,24 @@
 ();%0A        
- 
 debuggerServ
@@ -4437,18 +4437,8 @@
 t);%0A
-        %7D%0A
     
@@ -4666,10 +4666,282 @@
 %0A    %7D%0A%0A
+    protected void assertDebuggerEnabled() %7B%0A        if (!environment.getProperty(%22flowable.experimental.debugger.enabled%22, Boolean.class, false)) %7B%0A            throw new RuntimeException(%22property flowable.experimental.debugger.enabled is not enabled%22);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

disable all debugger resources when debugger is not enabled