diff options
Diffstat (limited to 'common-util/src/main')
12 files changed, 326 insertions, 53 deletions
diff --git a/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java new file mode 100644 index 0000000..1e0ef33 --- /dev/null +++ b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterService.java @@ -0,0 +1,185 @@ +/* + * Copyright 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.baseservice.bus.util; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import javax.ws.rs.core.Response; + +import org.apache.cxf.jaxrs.client.WebClient; +import org.codehaus.jackson.jaxrs.JacksonJsonProvider; +import org.openo.baseservice.util.impl.SystemEnvVariablesFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Provide the service register cbb for common use. + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class RegisterService { + + private static final Logger LOGGER = LoggerFactory.getLogger(RegisterService.class); + + private static final String BUS_CONFIGURE_FILE = "/etc/microservice.ini"; + + private static final String BUS_SERVICE_URL = "/openoapi/microservices/v1/services"; + + private static final String BUS_ADDRESS_KEY = "msb.address"; + + private static String busPath = null; + + /** + * Constructor<br/> + * <p> + * </p> + * @throws IOException + * + * @since + */ + private RegisterService() { + } + + /** + * register the micro service. + * <br/> + * + * @param jsonPath: the service json object to register to the bus. + * @param createOrUpdate: true, create and update the old ip port. false, create and delete the + * old one; + * @return + * @throws IOException + * @since + */ + public static Response registerService(String jsonPath, boolean createOrUpdate) throws IOException { + + String serviceInfo = getServiceModel(jsonPath); + + WebClient client = initializeClient(); + + client.type("application/json"); + + client.accept("application/json"); + + client.path(BUS_SERVICE_URL); + + client.query("createOrUpdate", createOrUpdate); + + LOGGER.info("Connecting bus address : " + busPath + BUS_SERVICE_URL); + + return client.invoke("POST", serviceInfo); + + } + + /** + * get the service's model. and return it as a string ; + * <br/> + * + * @param jsonPath + * @return + * @since + */ + private static String getServiceModel(String jsonPath) { + + String serviceInfo = ""; + + try { + LOGGER.info("begin to read file micro service json " + jsonPath); + + FileInputStream busFile = new FileInputStream(jsonPath); + + int size = busFile.available(); + + byte[] buffer = new byte[size]; + + busFile.read(buffer); + + busFile.close(); + + serviceInfo = new String(buffer); + LOGGER.info("finished to read micro service json file. "); + } catch(Exception ex) { + LOGGER.error("Read the micro service json file error :", ex); + } + return serviceInfo; + } + /** + * initialize the bus ip and port. + * <br/> + * + * @return + * @throws IOException + * @since + */ + private static String getBusAdderss() throws IOException { + + LOGGER.info("begin to get the bus baseurl."); + FileInputStream busFile = null; + String url = "msb.openo.org:80"; + + String filePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + BUS_CONFIGURE_FILE; + LOGGER.info("bus base url file:" + filePath); + + Properties properties = new Properties(); + + try { + busFile = new FileInputStream(filePath); + properties.load(busFile); + url = properties.getProperty(BUS_ADDRESS_KEY); + } catch(IOException e) { + if (busFile != null) { + busFile.close(); + } + LOGGER.error("Read the bus url failed: ", e); + } + + LOGGER.info("initialize the bus baseurl is: " + url); + return "http://" + url; + } + + /** + * get the bus's client's address. and initialize the web client. + * <br/> + * + * @return + * @throws IOException + * @since + */ + private static WebClient initializeClient() throws IOException { + + final List<Object> providers = new ArrayList<Object>(); + + JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider(); + + providers.add(jacksonJsonProvider); + + if (busPath == null) { + busPath = getBusAdderss(); + } + + return WebClient.create(busPath, providers); + } +} diff --git a/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java new file mode 100644 index 0000000..85800b8 --- /dev/null +++ b/common-util/src/main/java/org/openo/baseservice/bus/util/RegisterServiceListener.java @@ -0,0 +1,76 @@ +/* + * Copyright 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.baseservice.bus.util; + +import java.io.File; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.openo.baseservice.util.impl.SystemEnvVariablesFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Initialize the service register listener. + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class RegisterServiceListener implements ServletContextListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(RegisterServiceListener.class); + + private static final String JSON = "json"; + + @Override + public void contextInitialized(ServletContextEvent sce) { + + String servicePath = SystemEnvVariablesFactory.getInstance().getAppRoot() + "/etc/microservice"; + LOGGER.info("microservices json file path is" + servicePath); + + File file = new File(servicePath); + + File[] fileList = file.listFiles(); + + for(File tempFile : fileList) { + String fileName = tempFile.getName(); + if (fileName.substring(fileName.lastIndexOf(".") + 1).equalsIgnoreCase(JSON)) { + LOGGER.info("begin to initialize the service file" + tempFile.getAbsolutePath()); + + /** now because ZTE do not provide the service bus.commont this code first. + try { + RegisterService.registerService(tempFile.getAbsolutePath(), true); + } catch(IOException e) { + LOGGER.error("Faile to register the service file :" + tempFile.getPath() + ", exception:" + e); + } + */ + } + } + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // TODO Auto-generated method stub + + } + + +} diff --git a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/CipherCreator.java b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/CipherCreator.java index c3e59b9..d964cbc 100644 --- a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/CipherCreator.java +++ b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/CipherCreator.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -27,7 +27,7 @@ import org.openo.baseservice.encrypt.cbb.inf.AbstractCipherFactory; * </p> * * @author - * @version SDNO 0.5 08-Jun-2016 + * @version 08-Jun-2016 */ public final class CipherCreator { @@ -41,7 +41,7 @@ public final class CipherCreator { * private * </p> * - * @since SDNO 0.5 + * @since */ private CipherCreator() { @@ -52,7 +52,7 @@ public final class CipherCreator { * <br/> * * @return - * @since SDNO 0.5 + * @since */ public static CipherCreator instance() { return instance; @@ -63,7 +63,7 @@ public final class CipherCreator { * <br/> * * @return cipher instance with default key. - * @since SDNO 0.5 + * @since */ public AbstractCipher create() { return factory.createCipherManager(); @@ -75,7 +75,7 @@ public final class CipherCreator { * * @param key the key to be used for encryption and decryption. * @return cipher instance with specified key. - * @since SDNO 0.5 + * @since */ public AbstractCipher create(final String key) { return factory.createCipherManager(key); @@ -86,7 +86,7 @@ public final class CipherCreator { * <br/> * * @param factory cipher factory. - * @since SDNO 0.5 + * @since */ public void setFactory(final AbstractCipherFactory factory) { this.factory = factory; diff --git a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipher.java b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipher.java index c9d7123..2d36dd8 100644 --- a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipher.java +++ b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipher.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -38,7 +38,7 @@ import org.slf4j.LoggerFactory; * </p> * * @author - * @version SDNO 0.5 31-May-2016 + * @version 31-May-2016 */ public class AesCipher implements AbstractCipher { @@ -56,7 +56,7 @@ public class AesCipher implements AbstractCipher { * Creates default key. * </p> * - * @since SDNO 0.5 + * @since */ public AesCipher() { super(); @@ -64,11 +64,12 @@ public class AesCipher implements AbstractCipher { } /** + * * Constructor<br/> * <p> * </p> * - * @since SDNO 0.5 + * @since * @param ckey: key. */ public AesCipher(final String key) { diff --git a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipherFactory.java b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipherFactory.java index 39fe605..95e7336 100644 --- a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipherFactory.java +++ b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/impl/AesCipherFactory.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -24,7 +24,7 @@ import org.openo.baseservice.encrypt.cbb.inf.AbstractCipherFactory; * </p> * * @author - * @version SDNO 0.5 03-Jun-2016 + * @version 03-Jun-2016 */ public class AesCipherFactory implements AbstractCipherFactory { @@ -32,7 +32,7 @@ public class AesCipherFactory implements AbstractCipherFactory { * Creates new CipherManager instance.<br/> * * @return new cipher manager instance. - * @since SDNO 0.5 + * @since */ @Override public AbstractCipher createCipherManager() { @@ -44,7 +44,7 @@ public class AesCipherFactory implements AbstractCipherFactory { * * @param key new cipher manager instance. * @return - * @since SDNO 0.5 + * @since */ @Override public AbstractCipher createCipherManager(final String key) { diff --git a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipher.java b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipher.java index 4357d86..608dd82 100644 --- a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipher.java +++ b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipher.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -23,7 +23,7 @@ package org.openo.baseservice.encrypt.cbb.inf; * </p> * * @author - * @version SDNO 0.5 31-May-2016 + * @version 31-May-2016 */ public interface AbstractCipher { @@ -33,7 +33,7 @@ public interface AbstractCipher { * * @param plain string to be encrypted. * @return encrypted string. - * @since SDNO 0.5 + * @since */ String encrypt(String plain); @@ -43,7 +43,7 @@ public interface AbstractCipher { * * @param encrypted String is encrypted by AES 128 * @return plain after decrypt - * @since SDNO 0.5 + * @since */ String decrypt(String encrypted); diff --git a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipherFactory.java b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipherFactory.java index 2d6f2ad..e820250 100644 --- a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipherFactory.java +++ b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/inf/AbstractCipherFactory.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -21,7 +21,7 @@ package org.openo.baseservice.encrypt.cbb.inf; * </p> * * @author - * @version SDNO 0.5 03-Jun-2016 + * @version 03-Jun-2016 */ public interface AbstractCipherFactory { @@ -29,7 +29,7 @@ public interface AbstractCipherFactory { * Creates new CipherManager instance.<br/> * * @return new cipher manager instance. - * @since SDNO 0.5 + * @since */ AbstractCipher createCipherManager(); @@ -38,7 +38,7 @@ public interface AbstractCipherFactory { * * @param key new cipher manager instance. * @return - * @since SDNO 0.5 + * @since */ AbstractCipher createCipherManager(final String key); diff --git a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/sha/Sha256.java b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/sha/Sha256.java index 74cc2bb..bbead3e 100644 --- a/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/sha/Sha256.java +++ b/common-util/src/main/java/org/openo/baseservice/encrypt/cbb/sha/Sha256.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -34,7 +34,7 @@ import javax.xml.bind.DatatypeConverter; * </p> * * @author - * @version SDNO 0.5 03-Jun-2016 + * @version 03-Jun-2016 */ public final class Sha256 { @@ -49,7 +49,7 @@ public final class Sha256 { * * @param data: The data to be digested. * @return Hex encoded digested data. - * @since SDNO 0.5 + * @since */ public static String digest(final String data) { final byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); @@ -71,7 +71,7 @@ public final class Sha256 { * @param key: The signing key. * @return Hex encoded HMAC signature. * @throws InvalidKeyException if the key is invalid. - * @since SDNO 0.5 + * @since */ public static String mac(final String data, final Key key) throws InvalidKeyException { final byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); @@ -95,7 +95,7 @@ public final class Sha256 { * @param data: The data to be signed. * @param secret: The signing key. * @return Hex encoded HMAC signature. - * @since SDNO 0.5 + * @since */ public static String mac(final String data, final byte[] secret) { final Key key = new SecretKeySpec(secret, "HmacSHA256"); diff --git a/common-util/src/main/java/org/openo/baseservice/util/RestUtils.java b/common-util/src/main/java/org/openo/baseservice/util/RestUtils.java index ee250b1..2661d69 100644 --- a/common-util/src/main/java/org/openo/baseservice/util/RestUtils.java +++ b/common-util/src/main/java/org/openo/baseservice/util/RestUtils.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -31,7 +31,7 @@ import javax.servlet.http.HttpServletRequest; * </p> * * @author - * @version SDNO 0.5 31-May-2016 + * @version 31-May-2016 */ public final class RestUtils { @@ -46,7 +46,7 @@ public final class RestUtils { * * @param request : request object. * @return Request body as string. - * @since SDNO 0.5 + * @since */ public static String getRequestBody(final HttpServletRequest request) { String body = null; diff --git a/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImpl.java b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImpl.java index bd98fb9..0ac707b 100644 --- a/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImpl.java +++ b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesDefImpl.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -29,9 +29,9 @@ import java.io.IOException; * </p> * * @author - * @version SDNO 0.5 24-Jun-2016 + * @version 24-Jun-2016 */ -public class SystemEnvVariablesDefImpl extends SystemEnvVariables { +public class SystemEnvVariablesDefImpl implements SystemEnvVariables { private static final Logger LOG = LoggerFactory.getLogger(SystemEnvVariablesDefImpl.class); @@ -50,7 +50,7 @@ public class SystemEnvVariablesDefImpl extends SystemEnvVariables { * * @param inPath input path * @return the canonical path. - * @since SDNO 0.5 + * @since */ private String getCanonicalPath(final String inPath) { String path = null; diff --git a/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesFactory.java b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesFactory.java index e40ab53..e75c997 100644 --- a/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesFactory.java +++ b/common-util/src/main/java/org/openo/baseservice/util/impl/SystemEnvVariablesFactory.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -25,18 +25,29 @@ import org.openo.baseservice.util.inf.SystemEnvVariables; * </p> * * @author - * @version SDNO 0.5 2016年8月13日 + * @version */ public class SystemEnvVariablesFactory { private static SystemEnvVariables systemEnvVariablesInstance = new SystemEnvVariablesDefImpl(); /** + * + * Constructor<br/> + * <p> + * </p> + * + * @since + */ + private SystemEnvVariablesFactory() { + + } + /** * get the SystemEnvVariables instance * <br/> * * @return SystemEnvVariables instance - * @since SDNO 0.5 + * @since */ public static SystemEnvVariables getInstance() { return systemEnvVariablesInstance; diff --git a/common-util/src/main/java/org/openo/baseservice/util/inf/SystemEnvVariables.java b/common-util/src/main/java/org/openo/baseservice/util/inf/SystemEnvVariables.java index 014cdbd..cf0c182 100644 --- a/common-util/src/main/java/org/openo/baseservice/util/inf/SystemEnvVariables.java +++ b/common-util/src/main/java/org/openo/baseservice/util/inf/SystemEnvVariables.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * Copyright 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 + * 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, @@ -21,15 +21,15 @@ package org.openo.baseservice.util.inf; * </p> * * @author - * @version SDNO 0.5 31-May-2016 + * @version 31-May-2016 */ -public abstract class SystemEnvVariables { +public interface SystemEnvVariables { /** * Get application root path <br/> * * @return application root path. - * @since SDNO 0.5 + * @since */ - public abstract String getAppRoot(); + String getAppRoot(); } |