2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.gso.gui.servicegateway.service.impl;
\r
19 import java.util.Map;
\r
21 import javax.servlet.http.HttpServletRequest;
\r
23 import org.apache.commons.lang.StringUtils;
\r
24 import org.openo.baseservice.remoteservice.exception.ServiceException;
\r
25 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
\r
26 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
\r
27 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
\r
28 import org.openo.gso.gui.servicegateway.constant.Constant;
\r
29 import org.openo.gso.gui.servicegateway.service.inf.IServiceGateway;
\r
30 import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
\r
31 import org.openo.gso.gui.servicegateway.util.validate.ValidateUtil;
\r
32 import org.slf4j.Logger;
\r
33 import org.slf4j.LoggerFactory;
\r
36 * ServiceGateway service class.<br/>
\r
41 * @version GSO 0.5 2016/8/4
\r
43 public class ServiceGatewayImpl implements IServiceGateway {
\r
48 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceGatewayImpl.class);
\r
52 * Create service instance.<br/>
\r
54 * @param reqContent content of request
\r
55 * @param httpRequest http request
\r
56 * @throws ServiceException when operate DB or parameter is wrong.
\r
59 @SuppressWarnings("unchecked")
\r
61 public String createService(String reqContent, HttpServletRequest httpRequest) throws ServiceException {
\r
63 if(StringUtils.isEmpty(reqContent))
\r
65 LOGGER.error("ServiceGatewayImpl createService reqContent is null.");
\r
70 Map<String, Object> requestBody = JsonUtil.unMarshal(reqContent, Map.class);
\r
71 Map<String, Object> service = (Map<String, Object>)requestBody.get(Constant.SERVICE_INDENTIFY);
\r
74 service = requestBody;
\r
76 ValidateUtil.assertObjectNotNull(requestBody);
\r
79 String gatewayUri = (String)service.get(Constant.SERVICE_GATEWAY_URI);
\r
80 ValidateUtil.assertStringNotNull(gatewayUri);
\r
81 service.remove(Constant.SERVICE_GATEWAY_URI);
\r
83 // call the restful
\r
86 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(gatewayUri,
\r
87 getRestfulParameters(JsonUtil.marshal(requestBody)));
\r
88 if (null != restfulRsp) {
\r
89 // Record the result of registration
\r
90 // (201:success;415:Invalid Parameter;500:Internal Server Error)
\r
91 LOGGER.info("restful call result:", restfulRsp.getStatus());
\r
92 id = restfulRsp.getRespHeaderStr(Constant.SERVICE_ID);
\r
93 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.NS_INSTANCE_ID) : id;
\r
94 id = (null == id) ? restfulRsp.getRespHeaderStr(Constant.JOB_ID) : id;
\r
96 } catch(ServiceException e) {
\r
97 LOGGER.error("service gateway create restful call result:", e);
\r
105 * get the parameters for restful<br/>
\r
110 * @return the RestfulParametes Instance
\r
111 * @since GSO 0.5, 2016-8-9
\r
113 private static RestfulParametes getRestfulParameters(final String bodyData) {
\r
114 RestfulParametes param = new RestfulParametes();
\r
115 param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
\r
116 param.setRawData(bodyData);
\r
121 * Delete service instances.<br/>
\r
123 * @param serviceId service instance ID
\r
124 * @param httpRequest http request
\r
125 * @throws ServiceException operate DB or parameter is wrong.
\r
129 public void deleteService(String serviceId, HttpServletRequest httpRequest) throws ServiceException {
\r