-/*\r
- * Copyright (c) 2016, Huawei Technologies Co., Ltd.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.openo.gso.gui.servicegateway.exception;\r
-\r
-/**\r
- * Constant Class.<br/>\r
- * <p>\r
- * Define constant for http operation.\r
- * </p>\r
- * \r
- * @author\r
- * @version GSO 0.5 2016/8/4\r
- */\r
-public class HttpCode {\r
-\r
- /**\r
- * Fail to request.\r
- */\r
- public static final int BAD_REQUEST = 400;\r
-\r
- /**\r
- * Inner error\r
- */\r
- public static final int INTERNAL_SERVER_ERROR = 500;\r
-\r
- /**\r
- * Not accept request.\r
- */\r
- public static final int NOT_ACCEPTABLE = 406;\r
-\r
- /**\r
- * Not found service.\r
- */\r
- public static final int NOT_FOUND = 404;\r
-\r
- /**\r
- * Accept request.\r
- */\r
- public static final int RESPOND_ACCEPTED = 202;\r
-\r
- /**\r
- * Http response is ok.\r
- */\r
- public static final int RESPOND_OK = 200;\r
-\r
- /**\r
- * Conflict\r
- */\r
- public static final int RESPOND_CONFLICT = 409;\r
-\r
- /**\r
- * Constructor<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @since GSO 0.5\r
- */\r
- private HttpCode() {\r
-\r
- }\r
-\r
- /**\r
- * Whether request is successful.<br/>\r
- * \r
- * @param httpCode response code\r
- * @return true or false\r
- * @since GSO 0.5\r
- */\r
- public static boolean isSucess(int httpCode) {\r
- return httpCode / 100 == 2;\r
- }\r
-}\r
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * 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.openo.gso.gui.servicegateway.exception;
+
+/**
+ * Constant Class.<br/>
+ * <p>
+ * Define constant for http operation.
+ * </p>
+ *
+ * @author
+ * @version GSO 0.5 2016/8/4
+ */
+public class HttpCode {
+
+ /**
+ * Fail to request.
+ */
+ public static final int BAD_REQUEST = 400;
+
+ /**
+ * Inner error
+ */
+ public static final int INTERNAL_SERVER_ERROR = 500;
+
+ /**
+ * Not accept request.
+ */
+ public static final int NOT_ACCEPTABLE = 406;
+
+ /**
+ * Not found service.
+ */
+ public static final int NOT_FOUND = 404;
+
+ /**
+ * Accept request.
+ */
+ public static final int RESPOND_ACCEPTED = 202;
+
+ /**
+ * Http response is ok.
+ */
+ public static final int RESPOND_OK = 200;
+
+ /**
+ * Conflict
+ */
+ public static final int RESPOND_CONFLICT = 409;
+
+ /**
+ * Constructor<br/>
+ * <p>
+ * </p>
+ *
+ * @since GSO 0.5
+ */
+ private HttpCode() {
+
+ }
+
+ /**
+ * Whether request is successful.<br/>
+ *
+ * @param httpCode response code
+ * @return true or false
+ * @since GSO 0.5
+ */
+ public static boolean isSucess(int httpCode) {
+ return httpCode / 100 == 2;
+ }
+}
-/*\r
- * Copyright (c) 2016, Huawei Technologies Co., Ltd.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.openo.gso.gui.servicegateway.roa.inf;\r
-\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.ws.rs.Consumes;\r
-import javax.ws.rs.DELETE;\r
-import javax.ws.rs.POST;\r
-import javax.ws.rs.Path;\r
-import javax.ws.rs.PathParam;\r
-import javax.ws.rs.Produces;\r
-import javax.ws.rs.core.Context;\r
-import javax.ws.rs.core.Response;\r
-\r
-import org.openo.baseservice.remoteservice.exception.ServiceException;\r
-\r
-/**\r
- * API for restful interface.<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @author\r
- * @version GSO 0.5 2016/8/4\r
- */\r
-@Path("/servicegateway/v1/services")\r
-@Consumes({"application/json"})\r
-@Produces({"application/json"})\r
-public interface IServiceGatewayRoaModule {\r
-\r
- /**\r
- * Create service instance.<br/>\r
- * \r
- * @param servletReq http request\r
- * @return response\r
- * @throws ServiceException when operate database or parameter is wrong.\r
- * @since GSO 0.5\r
- */\r
- @POST\r
- @Produces({"application/json"})\r
- @Consumes({"application/json"})\r
- Response createService(@Context HttpServletRequest servletReq) throws ServiceException;\r
-\r
- /**\r
- * Delete service instance.<br/>\r
- * \r
- * @param serviceId service instance id\r
- * @param servletReq http request\r
- * @return response\r
- * @throws ServiceException when operate database or parameter is wrong.\r
- * @since GSO 0.5\r
- */\r
- @DELETE\r
- @Produces({"application/json"})\r
- @Consumes({"application/json"})\r
- @Path("/{serviceId}")\r
- Response deleteService(@PathParam("serviceId") String serviceId, @Context HttpServletRequest servletReq)\r
- throws ServiceException;\r
-\r
-}\r
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * 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.openo.gso.gui.servicegateway.roa.inf;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+
+/**
+ * API for restful interface.<br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version GSO 0.5 2016/8/4
+ */
+@Path("/servicegateway/v1/services")
+@Consumes({"application/json"})
+@Produces({"application/json"})
+public interface IServiceGatewayRoaModule {
+
+ /**
+ * Create service instance.<br/>
+ *
+ * @param servletReq http request
+ * @return response
+ * @throws ServiceException when operate database or parameter is wrong.
+ * @since GSO 0.5
+ */
+ @POST
+ @Produces({"application/json"})
+ @Consumes({"application/json"})
+ Response createService(@Context HttpServletRequest servletReq) throws ServiceException;
+
+ /**
+ * Delete service instance.<br/>
+ *
+ * @param serviceId service instance id
+ * @param servletReq http request
+ * @return response
+ * @throws ServiceException when operate database or parameter is wrong.
+ * @since GSO 0.5
+ */
+ @DELETE
+ @Produces({"application/json"})
+ @Consumes({"application/json"})
+ @Path("/{serviceId}")
+ Response deleteService(@PathParam("serviceId") String serviceId, @Context HttpServletRequest servletReq)
+ throws ServiceException;
+
+}
-/*\r
- * Copyright (c) 2016, Huawei Technologies Co., Ltd.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.openo.gso.gui.servicegateway.util.http;\r
-\r
-import java.util.Map;\r
-\r
-import javax.servlet.http.HttpServletRequest;\r
-\r
-import org.openo.baseservice.remoteservice.exception.ServiceException;\r
-import org.openo.baseservice.roa.util.restclient.RestfulFactory;\r
-import org.openo.baseservice.roa.util.restclient.RestfulParametes;\r
-import org.openo.baseservice.roa.util.restclient.RestfulResponse;\r
-import org.openo.gso.gui.servicegateway.util.json.JsonUtil;\r
-\r
-/**\r
- * Method class that provides interface to do http request.<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @author\r
- * @version GSO 0.5 2016/9/1\r
- */\r
-public class HttpUtil {\r
-\r
- /**\r
- * Constructor<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @since GSO 0.5\r
- */\r
- private HttpUtil() {\r
-\r
- }\r
-\r
- /**\r
- * Get operation.<br/>\r
- * \r
- * @param url request location\r
- * @param httpHeaders request headers\r
- * @param httpRequest http request\r
- * @return response\r
- * @throws ServiceException when request is failure.\r
- * @since GSO 0.5\r
- */\r
- public static RestfulResponse get(final String url, final Map<String, String> httpHeaders,\r
- HttpServletRequest httpRequest) throws ServiceException {\r
- final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);\r
- for(Map.Entry<String, String> entry : httpHeaders.entrySet()) {\r
- restfulParametes.putHttpContextHeader(entry.getKey(), entry.getValue());\r
- }\r
-\r
- return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).get(url, restfulParametes);\r
- }\r
-\r
- /**\r
- * Post operation.<br/>\r
- * \r
- * @param url request location\r
- * @param sendObj request body\r
- * @param httpRequest http request\r
- * @return response\r
- * @throws ServiceException when request is failure.\r
- * @since GSO 0.5\r
- */\r
- public static RestfulResponse post(final String url, Object sendObj, HttpServletRequest httpRequest)\r
- throws ServiceException {\r
-\r
- final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);\r
- if(sendObj != null) {\r
- String strJsonReq = JsonUtil.marshal(sendObj);\r
- restfulParametes.setRawData(strJsonReq);\r
- }\r
-\r
- return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).post(url, restfulParametes);\r
- }\r
-\r
- /**\r
- * Delete operation.<br/>\r
- * \r
- * @param url request location\r
- * @param httpRequest http request\r
- * @return response\r
- * @throws ServiceException when request is failure.\r
- * @since GSO 0.5\r
- */\r
- public static RestfulResponse delete(final String url, HttpServletRequest httpRequest) throws ServiceException {\r
- final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);\r
- return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).delete(url, restfulParametes);\r
- }\r
-\r
- /**\r
- * Put operation.<br/>\r
- * \r
- * @param url request location\r
- * @param httpHeaders request headers\r
- * @param httpRequest http request\r
- * @return response\r
- * @throws ServiceException when request is failure.\r
- * @since GSO 0.5\r
- */\r
- public static RestfulResponse put(final String url, final Map<String, String> httpHeaders,\r
- HttpServletRequest httpRequest) throws ServiceException {\r
- final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);\r
- for(Map.Entry<String, String> entry : httpHeaders.entrySet()) {\r
- restfulParametes.putHttpContextHeader(entry.getKey(), entry.getValue());\r
- }\r
-\r
- return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).put(url, restfulParametes);\r
- }\r
-\r
- /**\r
- * Fill in request parameters.<br/>\r
- * \r
- * @param httpRequest http request\r
- * @return rest parameters\r
- * @since GSO 0.5\r
- */\r
- public static RestfulParametes getRestfulParametes(HttpServletRequest httpRequest) {\r
- final RestfulParametes restfulParametes = new RestfulParametes();\r
- restfulParametes.putHttpContextHeader("Content-Type", "application/json;charset=UTF-8");\r
- return restfulParametes;\r
- }\r
-}\r
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * 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.openo.gso.gui.servicegateway.util.http;
+
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.baseservice.roa.util.restclient.RestfulFactory;
+import org.openo.baseservice.roa.util.restclient.RestfulParametes;
+import org.openo.baseservice.roa.util.restclient.RestfulResponse;
+import org.openo.gso.gui.servicegateway.util.json.JsonUtil;
+
+/**
+ * Method class that provides interface to do http request.<br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version GSO 0.5 2016/9/1
+ */
+public class HttpUtil {
+
+ /**
+ * Constructor<br/>
+ * <p>
+ * </p>
+ *
+ * @since GSO 0.5
+ */
+ private HttpUtil() {
+
+ }
+
+ /**
+ * Get operation.<br/>
+ *
+ * @param url request location
+ * @param httpHeaders request headers
+ * @param httpRequest http request
+ * @return response
+ * @throws ServiceException when request is failure.
+ * @since GSO 0.5
+ */
+ public static RestfulResponse get(final String url, final Map<String, String> httpHeaders,
+ HttpServletRequest httpRequest) throws ServiceException {
+ final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);
+ for(Map.Entry<String, String> entry : httpHeaders.entrySet()) {
+ restfulParametes.putHttpContextHeader(entry.getKey(), entry.getValue());
+ }
+
+ return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).get(url, restfulParametes);
+ }
+
+ /**
+ * Post operation.<br/>
+ *
+ * @param url request location
+ * @param sendObj request body
+ * @param httpRequest http request
+ * @return response
+ * @throws ServiceException when request is failure.
+ * @since GSO 0.5
+ */
+ public static RestfulResponse post(final String url, Object sendObj, HttpServletRequest httpRequest)
+ throws ServiceException {
+
+ final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);
+ if(sendObj != null) {
+ String strJsonReq = JsonUtil.marshal(sendObj);
+ restfulParametes.setRawData(strJsonReq);
+ }
+
+ return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).post(url, restfulParametes);
+ }
+
+ /**
+ * Delete operation.<br/>
+ *
+ * @param url request location
+ * @param httpRequest http request
+ * @return response
+ * @throws ServiceException when request is failure.
+ * @since GSO 0.5
+ */
+ public static RestfulResponse delete(final String url, HttpServletRequest httpRequest) throws ServiceException {
+ final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);
+ return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).delete(url, restfulParametes);
+ }
+
+ /**
+ * Put operation.<br/>
+ *
+ * @param url request location
+ * @param httpHeaders request headers
+ * @param httpRequest http request
+ * @return response
+ * @throws ServiceException when request is failure.
+ * @since GSO 0.5
+ */
+ public static RestfulResponse put(final String url, final Map<String, String> httpHeaders,
+ HttpServletRequest httpRequest) throws ServiceException {
+ final RestfulParametes restfulParametes = getRestfulParametes(httpRequest);
+ for(Map.Entry<String, String> entry : httpHeaders.entrySet()) {
+ restfulParametes.putHttpContextHeader(entry.getKey(), entry.getValue());
+ }
+
+ return RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP).put(url, restfulParametes);
+ }
+
+ /**
+ * Fill in request parameters.<br/>
+ *
+ * @param httpRequest http request
+ * @return rest parameters
+ * @since GSO 0.5
+ */
+ public static RestfulParametes getRestfulParametes(HttpServletRequest httpRequest) {
+ final RestfulParametes restfulParametes = new RestfulParametes();
+ restfulParametes.putHttpContextHeader("Content-Type", "application/json;charset=UTF-8");
+ return restfulParametes;
+ }
+}
-/*\r
- * Copyright (c) 2016, Huawei Technologies Co., Ltd.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.openo.gso.gui.servicegateway.util.json;\r
-\r
-import java.io.IOException;\r
-\r
-import org.codehaus.jackson.map.ObjectMapper;\r
-import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;\r
-import org.codehaus.jackson.type.TypeReference;\r
-import org.openo.baseservice.remoteservice.exception.ServiceException;\r
-import org.openo.gso.gui.servicegateway.exception.ErrorCode;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import net.sf.json.JSON;\r
-\r
-/**\r
- * Interface for json analyzing.<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @author\r
- * @version GSO 0.5 2016/9/1\r
- */\r
-public class JsonUtil {\r
-\r
- /**\r
- * Log service\r
- */\r
- private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);\r
-\r
- /**\r
- * Mapper.\r
- */\r
- private static final ObjectMapper MAPPER = new ObjectMapper();\r
-\r
- static {\r
- MAPPER.setDeserializationConfig(MAPPER.getDeserializationConfig()\r
- .without(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES));\r
- MAPPER.setSerializationInclusion(Inclusion.NON_NULL);\r
- }\r
-\r
- /**\r
- * Constructor<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @since GSO 0.5\r
- */\r
- private JsonUtil() {\r
-\r
- }\r
-\r
- /**\r
- * Parse the string in form of json.<br/>\r
- * \r
- * @param jsonstr json string.\r
- * @param type that convert json string to\r
- * @return\r
- * @throws ServiceException\r
- * @since GSO 0.5\r
- */\r
- public static <T> T unMarshal(String jsonstr, Class<T> type) throws ServiceException {\r
- try {\r
- return MAPPER.readValue(jsonstr, type);\r
- } catch(IOException e) {\r
- LOGGER.error("jsonstr unMarshal failed!", e);\r
- throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, "jsonstr unMarshal failed!");\r
- }\r
- }\r
-\r
- /**\r
- * Parse the string in form of json.<br/>\r
- * \r
- * @param jsonstr json string.\r
- * @param type that convert json string to\r
- * @return\r
- * @throws ServiceException\r
- * @since GSO 0.5\r
- */\r
- public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ServiceException {\r
- try {\r
- return MAPPER.readValue(jsonstr, type);\r
- } catch(IOException e) {\r
- LOGGER.error("jsonstr unMarshal failed!", e);\r
- throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, "jsonstr unMarshal failed!");\r
- }\r
- }\r
-\r
- /**\r
- * Convert object to json string.<br/>\r
- * \r
- * @param srcObj data object\r
- * @return json string\r
- * @throws ServiceException when fail to convert.\r
- * @since GSO 0.5\r
- */\r
- public static String marshal(Object srcObj) throws ServiceException {\r
- if(srcObj instanceof JSON) {\r
- return srcObj.toString();\r
- }\r
- try {\r
- return MAPPER.writeValueAsString(srcObj);\r
- } catch(IOException e) {\r
- LOGGER.error("srcObj marshal failed!", e);\r
- throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, "srcObj marshal failed!");\r
- }\r
- }\r
-\r
- /**\r
- * Get mapper.<br/>\r
- * \r
- * @return mapper\r
- * @since GSO 0.5\r
- */\r
- public static ObjectMapper getMapper() {\r
- return MAPPER;\r
- }\r
-}\r
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * 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.openo.gso.gui.servicegateway.util.json;
+
+import java.io.IOException;
+
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
+import org.codehaus.jackson.type.TypeReference;
+import org.openo.baseservice.remoteservice.exception.ServiceException;
+import org.openo.gso.gui.servicegateway.exception.ErrorCode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.sf.json.JSON;
+
+/**
+ * Interface for json analyzing.<br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version GSO 0.5 2016/9/1
+ */
+public class JsonUtil {
+
+ /**
+ * Log service
+ */
+ private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
+
+ /**
+ * Mapper.
+ */
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ static {
+ MAPPER.setDeserializationConfig(MAPPER.getDeserializationConfig()
+ .without(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES));
+ MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
+ }
+
+ /**
+ * Constructor<br/>
+ * <p>
+ * </p>
+ *
+ * @since GSO 0.5
+ */
+ private JsonUtil() {
+
+ }
+
+ /**
+ * Parse the string in form of json.<br/>
+ *
+ * @param jsonstr json string.
+ * @param type that convert json string to
+ * @return
+ * @throws ServiceException
+ * @since GSO 0.5
+ */
+ public static <T> T unMarshal(String jsonstr, Class<T> type) throws ServiceException {
+ try {
+ return MAPPER.readValue(jsonstr, type);
+ } catch(IOException e) {
+ LOGGER.error("jsonstr unMarshal failed!", e);
+ throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, "jsonstr unMarshal failed!");
+ }
+ }
+
+ /**
+ * Parse the string in form of json.<br/>
+ *
+ * @param jsonstr json string.
+ * @param type that convert json string to
+ * @return
+ * @throws ServiceException
+ * @since GSO 0.5
+ */
+ public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ServiceException {
+ try {
+ return MAPPER.readValue(jsonstr, type);
+ } catch(IOException e) {
+ LOGGER.error("jsonstr unMarshal failed!", e);
+ throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, "jsonstr unMarshal failed!");
+ }
+ }
+
+ /**
+ * Convert object to json string.<br/>
+ *
+ * @param srcObj data object
+ * @return json string
+ * @throws ServiceException when fail to convert.
+ * @since GSO 0.5
+ */
+ public static String marshal(Object srcObj) throws ServiceException {
+ if(srcObj instanceof JSON) {
+ return srcObj.toString();
+ }
+ try {
+ return MAPPER.writeValueAsString(srcObj);
+ } catch(IOException e) {
+ LOGGER.error("srcObj marshal failed!", e);
+ throw new ServiceException(ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM, "srcObj marshal failed!");
+ }
+ }
+
+ /**
+ * Get mapper.<br/>
+ *
+ * @return mapper
+ * @since GSO 0.5
+ */
+ public static ObjectMapper getMapper() {
+ return MAPPER;
+ }
+}
-/*\r
- * Copyright (c) 2016, Huawei Technologies Co., Ltd.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.openo.gso.gui.servicegateway.exception;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-\r
-import org.junit.Test;\r
-\r
-/**\r
- * Test Constant class.<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @author\r
- * @version GSO 0.5 2016/8/4\r
- */\r
-public class ErrorCodeTest {\r
-\r
- @Test\r
- public void test() {\r
- assertEquals("servicemgr.mysql.oper_mysql_db_error", ErrorCode.SVCMGR_OPER_MYSQL_DB_ERROR);\r
- assertEquals("servicemgr.bad_param", ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM);\r
- }\r
-\r
-}\r
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * 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.openo.gso.gui.servicegateway.exception;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Test Constant class.<br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version GSO 0.5 2016/8/4
+ */
+public class ErrorCodeTest {
+
+ @Test
+ public void test() {
+ assertEquals("servicemgr.mysql.oper_mysql_db_error", ErrorCode.SVCMGR_OPER_MYSQL_DB_ERROR);
+ assertEquals("servicemgr.bad_param", ErrorCode.SVCMGR_SERVICEMGR_BAD_PARAM);
+ }
+
+}
-/*\r
- * Copyright (c) 2016, Huawei Technologies Co., Ltd.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.openo.gso.gui.servicegateway.exception;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertFalse;\r
-import static org.junit.Assert.assertTrue;\r
-\r
-import org.junit.Test;\r
-\r
-/**\r
- * Test Constant class.<br/>\r
- * <p>\r
- * </p>\r
- * \r
- * @author\r
- * @version GSO 0.5 2016/8/4\r
- */\r
-public class HttpCodeTest {\r
-\r
- @Test\r
- public void test() {\r
- assertEquals(400, HttpCode.BAD_REQUEST);\r
- assertEquals(500, HttpCode.INTERNAL_SERVER_ERROR);\r
- assertEquals(406, HttpCode.NOT_ACCEPTABLE);\r
- assertEquals(404, HttpCode.NOT_FOUND);\r
- assertEquals(202, HttpCode.RESPOND_ACCEPTED);\r
- assertEquals(200, HttpCode.RESPOND_OK);\r
- assertTrue(HttpCode.isSucess(200));\r
- assertFalse(HttpCode.isSucess(400));\r
- }\r
-\r
-}\r
+/*
+ * Copyright (c) 2016, Huawei Technologies Co., Ltd.
+ *
+ * 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.openo.gso.gui.servicegateway.exception;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+/**
+ * Test Constant class.<br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version GSO 0.5 2016/8/4
+ */
+public class HttpCodeTest {
+
+ @Test
+ public void test() {
+ assertEquals(400, HttpCode.BAD_REQUEST);
+ assertEquals(500, HttpCode.INTERNAL_SERVER_ERROR);
+ assertEquals(406, HttpCode.NOT_ACCEPTABLE);
+ assertEquals(404, HttpCode.NOT_FOUND);
+ assertEquals(202, HttpCode.RESPOND_ACCEPTED);
+ assertEquals(200, HttpCode.RESPOND_OK);
+ assertTrue(HttpCode.isSucess(200));
+ assertFalse(HttpCode.isSucess(400));
+ }
+
+}