Modify exception
[holmes/engine-management.git] / engine-d / src / main / java / org / openo / holmes / engine / resources / EngineResources.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.holmes.engine.resources;
17
18
19 import com.codahale.metrics.annotation.Timed;
20 import io.swagger.annotations.Api;
21 import io.swagger.annotations.ApiOperation;
22 import java.util.Locale;
23 import javax.inject.Inject;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.MediaType;
33 import lombok.extern.slf4j.Slf4j;
34 import org.jvnet.hk2.annotations.Service;
35 import org.openo.holmes.common.exception.CorrelationException;
36 import org.openo.holmes.common.utils.ExceptionUtil;
37 import org.openo.holmes.common.utils.LanguageUtil;
38 import org.openo.holmes.engine.manager.DroolsEngine;
39 import org.openo.holmes.engine.request.CompileRuleRequest;
40 import org.openo.holmes.engine.request.DeployRuleRequest;
41 import org.openo.holmes.engine.response.CorrelationRuleResponse;
42
43 @Service
44 @Path("/rule")
45 @Api(tags = {"Engine Manager"})
46 @Produces(MediaType.APPLICATION_JSON)
47 @Slf4j
48 public class EngineResources {
49
50     @Inject
51     DroolsEngine droolsEngine;
52
53     @PUT
54     @ApiOperation(value = "Add rule to Engine and Cache", response = CorrelationRuleResponse.class)
55     @Produces(MediaType.APPLICATION_JSON)
56     @Timed
57     public CorrelationRuleResponse deployRule(DeployRuleRequest deployRuleRequest,
58         @Context HttpServletRequest httpRequest) {
59
60         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
61         Locale locale = LanguageUtil.getLocale(httpRequest);
62         try {
63
64             String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
65             crResponse.setPackageName(packageName);
66
67         } catch (CorrelationException correlationException) {
68             log.error(correlationException.getMessage(), correlationException);
69             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
70         }
71
72         return crResponse;
73     }
74
75     @DELETE
76     @ApiOperation(value = "delete rule")
77     @Produces(MediaType.APPLICATION_JSON)
78     @Timed
79     @Path("/{packageName}")
80     public boolean undeployRule(@PathParam("packageName") String packageName,
81         @Context HttpServletRequest httpRequest) {
82
83         Locale locale = LanguageUtil.getLocale(httpRequest);
84
85         try {
86
87             droolsEngine.undeployRule(packageName, locale);
88
89         } catch (CorrelationException correlationException) {
90             log.error(correlationException.getMessage(), correlationException);
91             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
92         }
93         return true;
94     }
95
96
97     @POST
98     @ApiOperation(value = "compile rule")
99     @Produces(MediaType.APPLICATION_JSON)
100     @Timed
101     public boolean compileRule(CompileRuleRequest compileRuleRequest,
102         @Context HttpServletRequest httpRequest) {
103
104         Locale locale = LanguageUtil.getLocale(httpRequest);
105
106         try {
107             droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
108         } catch (CorrelationException correlationException) {
109             log.error(correlationException.getMessage(), correlationException);
110             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
111         }
112         return true;
113     }
114 }