summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/HttpCode.java172
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/inf/IServiceGatewayRoaModule.java146
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/HttpUtil.java276
-rw-r--r--servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/json/JsonUtil.java266
-rw-r--r--servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/ErrorCodeTest.java78
-rw-r--r--servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/HttpCodeTest.java94
6 files changed, 516 insertions, 516 deletions
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/HttpCode.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/HttpCode.java
index 05f5220f..2577903f 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/HttpCode.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/exception/HttpCode.java
@@ -1,86 +1,86 @@
-/*
- * 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;
- }
-}
+/*
+ * 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;
+ }
+}
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/inf/IServiceGatewayRoaModule.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/inf/IServiceGatewayRoaModule.java
index 929af508..302b2bd9 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/inf/IServiceGatewayRoaModule.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/roa/inf/IServiceGatewayRoaModule.java
@@ -1,73 +1,73 @@
-/*
- * 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;
-
-}
+/*
+ * 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;
+
+}
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/HttpUtil.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/HttpUtil.java
index 81546493..d255f31a 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/HttpUtil.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/http/HttpUtil.java
@@ -1,138 +1,138 @@
-/*
- * 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;
- }
-}
+/*
+ * 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;
+ }
+}
diff --git a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/json/JsonUtil.java b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/json/JsonUtil.java
index afb3c238..683f5d9b 100644
--- a/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/json/JsonUtil.java
+++ b/servicegateway/service/src/main/java/org/openo/gso/gui/servicegateway/util/json/JsonUtil.java
@@ -1,133 +1,133 @@
-/*
- * 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;
- }
-}
+/*
+ * 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;
+ }
+}
diff --git a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/ErrorCodeTest.java b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/ErrorCodeTest.java
index 422c5e41..74f9f432 100644
--- a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/ErrorCodeTest.java
+++ b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/ErrorCodeTest.java
@@ -1,39 +1,39 @@
-/*
- * 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);
- }
-
-}
+/*
+ * 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);
+ }
+
+}
diff --git a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/HttpCodeTest.java b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/HttpCodeTest.java
index cefdb50b..06d247b7 100644
--- a/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/HttpCodeTest.java
+++ b/servicegateway/service/src/test/java/org/openo/gso/gui/servicegateway/exception/HttpCodeTest.java
@@ -1,47 +1,47 @@
-/*
- * 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));
- }
-
-}
+/*
+ * 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));
+ }
+
+}