[ISSUE #2992] Use new code style for nacos-address module. (#3239)

* reformat with new code style for nacos-address

* Use new code style for nacos-address module.
This commit is contained in:
杨翊 SionYang 2020-07-03 17:30:31 +08:00 committed by GitHub
parent 1a0df78d8a
commit 8c9622fbda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 257 additions and 235 deletions

View File

@ -15,26 +15,26 @@
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>nacos-all</artifactId> <artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId> <groupId>com.alibaba.nacos</groupId>
<version>1.3.1-BETA</version> <version>1.3.1-BETA</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>nacos-address</artifactId> <artifactId>nacos-address</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>nacos-address ${project.version}</name> <name>nacos-address ${project.version}</name>
<url>http://nacos.io</url> <url>http://nacos.io</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -62,7 +62,7 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<resources> <resources>
<resource> <resource>
@ -70,7 +70,7 @@
</resource> </resource>
</resources> </resources>
</build> </build>
<reporting> <reporting>
<plugins> <plugins>
<plugin> <plugin>
@ -79,7 +79,7 @@
</plugin> </plugin>
</plugins> </plugins>
</reporting> </reporting>
<profiles> <profiles>
<profile> <profile>
<id>release-address</id> <id>release-address</id>

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address; package com.alibaba.nacos.address;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
@ -26,8 +27,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/ */
@SpringBootApplication(scanBasePackages = "com.alibaba.nacos") @SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
public class AddressServer { public class AddressServer {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(AddressServer.class, args); SpringApplication.run(AddressServer.class, args);
} }
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address.component; package com.alibaba.nacos.address.component;
import com.alibaba.nacos.address.constant.AddressServerConstants; import com.alibaba.nacos.address.constant.AddressServerConstants;
@ -34,32 +35,38 @@ import java.util.List;
*/ */
@Component @Component
public class AddressServerGeneratorManager { public class AddressServerGeneratorManager {
/**
* Generate product name.
*
* @param name name
* @return product
*/
public String generateProductName(String name) { public String generateProductName(String name) {
if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) { if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) {
return AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME; return AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME;
} }
return String.format(AddressServerConstants.ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE, name); return String.format(AddressServerConstants.ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE, name);
} }
/** /**
* Note: if the parameter inputted is empty then will return the empty list. * Note: if the parameter inputted is empty then will return the empty list.
* *
* @param serviceName * @param serviceName service name
* @param clusterName * @param clusterName cluster name
* @param ipArray * @param ipArray array of ips
* @return * @return instance list
*/ */
public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName, String[] ipArray) { public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName,
if (StringUtils.isEmpty(serviceName) String[] ipArray) {
|| StringUtils.isEmpty(clusterName) if (StringUtils.isEmpty(serviceName) || StringUtils.isEmpty(clusterName) || ipArray == null
|| ipArray == null || ipArray.length == 0) { || ipArray.length == 0) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<Instance> instanceList = new ArrayList<>(ipArray.length); List<Instance> instanceList = new ArrayList<>(ipArray.length);
for (String ip : ipArray) { for (String ip : ipArray) {
String[] ipAndPort = generateIpAndPort(ip); String[] ipAndPort = generateIpAndPort(ip);
@ -73,46 +80,50 @@ public class AddressServerGeneratorManager {
instance.setEphemeral(false); instance.setEphemeral(false);
instanceList.add(instance); instanceList.add(instance);
} }
return instanceList; return instanceList;
} }
public String[] generateIpAndPort(String ip) { private String[] generateIpAndPort(String ip) {
int index = ip.indexOf(AddressServerConstants.IP_PORT_SEPARATOR); int index = ip.indexOf(AddressServerConstants.IP_PORT_SEPARATOR);
if (index != -1) { if (index != -1) {
return new String[]{ip.substring(0, index), ip.substring(index + 1)}; return new String[] {ip.substring(0, index), ip.substring(index + 1)};
} }
return new String[]{ip, String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)}; return new String[] {ip, String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)};
} }
/** /**
* Generate response ips.
*
* @param instanceList a instance set will generate string response to client. * @param instanceList a instance set will generate string response to client.
* @return the result of response to client * @return the result of response to client
*/ */
public String generateResponseIps(List<Instance> instanceList) { public String generateResponseIps(List<Instance> instanceList) {
StringBuilder ips = new StringBuilder(); StringBuilder ips = new StringBuilder();
instanceList.forEach(instance -> { instanceList.forEach(instance -> {
ips.append(instance.getIp() + ":" + instance.getPort()); ips.append(instance.getIp() + ":" + instance.getPort());
ips.append("\n"); ips.append("\n");
}); });
return ips.toString(); return ips.toString();
} }
/** /**
* @param rawServiceName the raw service name will not contains the {@Constans.DEFAULT_GROUP} * Generate nacos service name.
*
* @param rawServiceName the raw service name will not contains the {@link Constants#DEFAULT_GROUP}.
* @return the nacos service name * @return the nacos service name
*/ */
public String generateNacosServiceName(String rawServiceName) { public String generateNacosServiceName(String rawServiceName) {
if (rawServiceName.indexOf(Constants.DEFAULT_GROUP) != -1) { if (rawServiceName.indexOf(Constants.DEFAULT_GROUP) != -1) {
return rawServiceName; return rawServiceName;
} }
return Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + rawServiceName; return Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + rawServiceName;
} }
} }

View File

@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address.component; package com.alibaba.nacos.address.component;
import com.alibaba.nacos.address.constant.AddressServerConstants; import com.alibaba.nacos.address.constant.AddressServerConstants;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -28,51 +30,51 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
public class AddressServerManager { public class AddressServerManager {
public String getRawProductName(String name) { public String getRawProductName(String name) {
if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) { if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) {
return AddressServerConstants.DEFAULT_PRODUCT; return AddressServerConstants.DEFAULT_PRODUCT;
} }
return name; return name;
} }
/** /**
* <p> * If the name is empty then return the default {@link UtilsAndCommons#DEFAULT_CLUSTER_NAME}, or return the source
* if the name is empty then return the default {@UtilAndCommons#DEFAULT_CLUSTER_NAME}, * name by input.
* <p>
* or return the source name by input
* *
* @param name * @param name name
* @return * @return default cluster name
*/ */
public String getDefaultClusterNameIfEmpty(String name) { public String getDefaultClusterNameIfEmpty(String name) {
if (StringUtils.isEmpty(name) || AddressServerConstants.DEFAULT_GET_CLUSTER.equals(name)) { if (StringUtils.isEmpty(name) || AddressServerConstants.DEFAULT_GET_CLUSTER.equals(name)) {
return AddressServerConstants.DEFAULT_GET_CLUSTER; return AddressServerConstants.DEFAULT_GET_CLUSTER;
} }
return name; return name;
} }
public String getRawClusterName(String name) { public String getRawClusterName(String name) {
return getDefaultClusterNameIfEmpty(name); return getDefaultClusterNameIfEmpty(name);
} }
/** /**
* Split ips.
*
* @param ips multi ip will separator by the ',' * @param ips multi ip will separator by the ','
* @return * @return array of ip
*/ */
public String[] splitIps(String ips) { public String[] splitIps(String ips) {
if (StringUtils.isBlank(ips)) { if (StringUtils.isBlank(ips)) {
return new String[0]; return new String[0];
} }
return ips.split(AddressServerConstants.MULTI_IPS_SEPARATOR); return ips.split(AddressServerConstants.MULTI_IPS_SEPARATOR);
} }
} }

View File

@ -13,64 +13,65 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address.constant; package com.alibaba.nacos.address.constant;
import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.misc.UtilsAndCommons;
/** /**
* Uniform constant parameter naming for address servers and default values for related parameters * Uniform constant parameter naming for address servers and default values for related parameters.
* *
* @author pbting * @author pbting
* @date 2019-06-17 7:23 PM * @date 2019-06-17 7:23 PM
* @since 1.1.0 * @since 1.1.0
*/ */
public interface AddressServerConstants { public interface AddressServerConstants {
/** /**
* the default server port when create the Instance object. * the default server port when create the Instance object.
*/ */
int DEFAULT_SERVER_PORT = 8848; int DEFAULT_SERVER_PORT = 8848;
/** /**
* when post ips is not given the product,then use the default. * when post ips is not given the product,then use the default.
*/ */
String DEFAULT_PRODUCT = "nacos"; String DEFAULT_PRODUCT = "nacos";
/** /**
* the separator between ip and port. * the separator between ip and port.
*/ */
String IP_PORT_SEPARATOR = ":"; String IP_PORT_SEPARATOR = ":";
/** /**
* the separator for {@Service#name} between raw service name and group * the separator for service name between raw service name and group.
*/ */
String GROUP_SERVICE_NAME_SEP = "@@"; String GROUP_SERVICE_NAME_SEP = "@@";
/** /**
* when post ips is not given the cluster,then use the default. * when post ips is not given the cluster,then use the default.
*/ */
String DEFAULT_GET_CLUSTER = "serverlist"; String DEFAULT_GET_CLUSTER = "serverlist";
/** /**
* post multi ip will use the "," to separator * post multi ip will use the "," to separator.
*/ */
String MULTI_IPS_SEPARATOR = ","; String MULTI_IPS_SEPARATOR = ",";
/** /**
* the default product name when deploy nacos with naming and config * the default product name when deploy nacos with naming and config.
*/ */
String ALIWARE_NACOS_DEFAULT_PRODUCT_NAME = "nacos.as.default"; String ALIWARE_NACOS_DEFAULT_PRODUCT_NAME = "nacos.as.default";
/** /**
* when the config and naming will separate deploy,then must specify product name by the client * when the config and naming will separate deploy,then must specify product name by the client.
*/ */
String ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE = "nacos.as.%s"; String ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE = "nacos.as.%s";
/** /**
* the url for address server prefix * the url for address server prefix.
*/ */
String ADDRESS_SERVER_REQUEST_URL = String ADDRESS_SERVER_REQUEST_URL =
UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/as"; UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/as";
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address.controller; package com.alibaba.nacos.address.controller;
import com.alibaba.nacos.address.component.AddressServerGeneratorManager; import com.alibaba.nacos.address.component.AddressServerGeneratorManager;
@ -38,46 +39,49 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
/** /**
* Address server cluster controller.
*
* @author pbting * @author pbting
* @date 2019-06-10 9:59 AM
* @since 1.1.0 * @since 1.1.0
*/ */
@RestController @RestController
@RequestMapping({AddressServerConstants.ADDRESS_SERVER_REQUEST_URL + "/nodes"}) @RequestMapping({AddressServerConstants.ADDRESS_SERVER_REQUEST_URL + "/nodes"})
public class AddressServerClusterController { public class AddressServerClusterController {
@Autowired @Autowired
private ServiceManager serviceManager; private ServiceManager serviceManager;
@Autowired @Autowired
private AddressServerManager addressServerManager; private AddressServerManager addressServerManager;
@Autowired @Autowired
private AddressServerGeneratorManager addressServerGeneratorManager; private AddressServerGeneratorManager addressServerGeneratorManager;
/** /**
* Create new cluster.
*
* @param product Ip list of products to be associated * @param product Ip list of products to be associated
* @param cluster Ip list of product cluster to be associated * @param cluster Ip list of product cluster to be associated
* @param ips will post ip list. * @param ips will post ip list.
* @return * @return result of create new cluster
*/ */
@RequestMapping(value = "", method = RequestMethod.POST) @RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity postCluster(@RequestParam(required = false) String product, public ResponseEntity postCluster(@RequestParam(required = false) String product,
@RequestParam(required = false) String cluster, @RequestParam(required = false) String cluster, @RequestParam(name = "ips") String ips) {
@RequestParam(name = "ips") String ips) {
//1. prepare the storage name for product and cluster //1. prepare the storage name for product and cluster
String productName = addressServerGeneratorManager.generateProductName(product); String productName = addressServerGeneratorManager.generateProductName(product);
String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster); String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster);
//2. prepare the response name for product and cluster to client //2. prepare the response name for product and cluster to client
String rawProductName = addressServerManager.getRawProductName(product); String rawProductName = addressServerManager.getRawProductName(product);
String rawClusterName = addressServerManager.getRawClusterName(cluster); String rawClusterName = addressServerManager.getRawClusterName(cluster);
Loggers.addressLogger.info("put cluster node,the cluster name is " + cluster + "; the product name=" + product + "; the ip list=" + ips); Loggers.ADDRESS_LOGGER.info("put cluster node,the cluster name is " + cluster + "; the product name=" + product
+ "; the ip list=" + ips);
ResponseEntity responseEntity; ResponseEntity responseEntity;
try { try {
String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName); String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
Cluster clusterObj = new Cluster(); Cluster clusterObj = new Cluster();
clusterObj.setName(clusterName); clusterObj.setName(clusterName);
clusterObj.setHealthChecker(new AbstractHealthChecker.None()); clusterObj.setHealthChecker(new AbstractHealthChecker.None());
@ -85,48 +89,54 @@ public class AddressServerClusterController {
String[] ipArray = addressServerManager.splitIps(ips); String[] ipArray = addressServerManager.splitIps(ips);
String checkResult = AddressServerParamCheckUtil.checkIps(ipArray); String checkResult = AddressServerParamCheckUtil.checkIps(ipArray);
if (AddressServerParamCheckUtil.CHECK_OK.equals(checkResult)) { if (AddressServerParamCheckUtil.CHECK_OK.equals(checkResult)) {
List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray); List<Instance> instanceList = addressServerGeneratorManager
.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
for (Instance instance : instanceList) { for (Instance instance : instanceList) {
serviceManager.registerInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, instance); serviceManager.registerInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, instance);
} }
responseEntity = ResponseEntity.ok("product=" + rawProductName + ",cluster=" + rawClusterName + "; put success with size=" + instanceList.size()); responseEntity = ResponseEntity
.ok("product=" + rawProductName + ",cluster=" + rawClusterName + "; put success with size="
+ instanceList.size());
} else { } else {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult); responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
} }
} catch (Exception e) { } catch (Exception e) {
responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
} }
return responseEntity; return responseEntity;
} }
/** /**
* Delete cluster.
*
* @param product Ip list of products to be associated * @param product Ip list of products to be associated
* @param cluster Ip list of product cluster to be associated * @param cluster Ip list of product cluster to be associated
* @param ips will delete ips. * @param ips will delete ips.
* @return * @return delete result
*/ */
@RequestMapping(value = "", method = RequestMethod.DELETE) @RequestMapping(value = "", method = RequestMethod.DELETE)
public ResponseEntity deleteCluster(@RequestParam(required = false) String product, public ResponseEntity deleteCluster(@RequestParam(required = false) String product,
@RequestParam(required = false) String cluster, @RequestParam(required = false) String cluster, @RequestParam String ips) {
@RequestParam String ips) {
//1. prepare the storage name for product and cluster //1. prepare the storage name for product and cluster
String productName = addressServerGeneratorManager.generateProductName(product); String productName = addressServerGeneratorManager.generateProductName(product);
String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster); String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster);
//2. prepare the response name for product and cluster to client //2. prepare the response name for product and cluster to client
String rawProductName = addressServerManager.getRawProductName(product); String rawProductName = addressServerManager.getRawProductName(product);
String rawClusterName = addressServerManager.getRawClusterName(cluster); String rawClusterName = addressServerManager.getRawClusterName(cluster);
ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.OK).body("product=" + rawProductName + ", cluster=" + rawClusterName + " delete success."); ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.OK)
.body("product=" + rawProductName + ", cluster=" + rawClusterName + " delete success.");
try { try {
String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName); String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName); Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName);
if (service == null) { if (service == null) {
responseEntity = ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + rawProductName + " not found."); responseEntity = ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("product=" + rawProductName + " not found.");
} else { } else {
if (StringUtils.isBlank(ips)) { if (StringUtils.isBlank(ips)) {
// delete all ips from the cluster // delete all ips from the cluster
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ips must not be empty."); responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ips must not be empty.");
@ -135,19 +145,21 @@ public class AddressServerClusterController {
String[] ipArray = addressServerManager.splitIps(ips); String[] ipArray = addressServerManager.splitIps(ips);
String checkResult = AddressServerParamCheckUtil.checkIps(ipArray); String checkResult = AddressServerParamCheckUtil.checkIps(ipArray);
if (AddressServerParamCheckUtil.CHECK_OK.equals(checkResult)) { if (AddressServerParamCheckUtil.CHECK_OK.equals(checkResult)) {
List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray); List<Instance> instanceList = addressServerGeneratorManager
serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray(new Instance[instanceList.size()])); .generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false,
instanceList.toArray(new Instance[instanceList.size()]));
} else { } else {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult); responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getCause()); responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getCause());
} }
return responseEntity; return responseEntity;
} }
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address.controller; package com.alibaba.nacos.address.controller;
import com.alibaba.nacos.address.component.AddressServerGeneratorManager; import com.alibaba.nacos.address.component.AddressServerGeneratorManager;
@ -29,42 +30,46 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* Server list controller.
*
* @author pbting * @author pbting
* @date 2019-06-18 5:04 PM
* @since 1.1.0 * @since 1.1.0
*/ */
@RestController @RestController
public class ServerListController { public class ServerListController {
@Autowired @Autowired
private ServiceManager serviceManager; private ServiceManager serviceManager;
@Autowired @Autowired
private AddressServerGeneratorManager addressServerBuilderManager; private AddressServerGeneratorManager addressServerBuilderManager;
/** /**
* Get cluster.
*
* @param product will get Ip list of that products to be associated * @param product will get Ip list of that products to be associated
* @param cluster will get Ip list of that product cluster to be associated * @param cluster will get Ip list of that product cluster to be associated
* @return * @return result of get
*/ */
@RequestMapping(value = "/{product}/{cluster}", method = RequestMethod.GET) @RequestMapping(value = "/{product}/{cluster}", method = RequestMethod.GET)
public ResponseEntity getCluster(@PathVariable String product, public ResponseEntity getCluster(@PathVariable String product, @PathVariable String cluster) {
@PathVariable String cluster) {
String productName = addressServerBuilderManager.generateProductName(product); String productName = addressServerBuilderManager.generateProductName(product);
String serviceName = addressServerBuilderManager.generateNacosServiceName(productName); String serviceName = addressServerBuilderManager.generateNacosServiceName(productName);
Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName); Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName);
if (service == null) { if (service == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + product + " not found."); return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + product + " not found.");
} }
if (!service.getClusterMap().containsKey(cluster)) { if (!service.getClusterMap().containsKey(cluster)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + product + ",cluster=" + cluster + " not found."); return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("product=" + product + ",cluster=" + cluster + " not found.");
} }
Cluster clusterObj = service.getClusterMap().get(cluster); Cluster clusterObj = service.getClusterMap().get(cluster);
return ResponseEntity.status(HttpStatus.OK).body(addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false))); return ResponseEntity.status(HttpStatus.OK)
.body(addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false)));
} }
} }

View File

@ -13,16 +13,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address.misc; package com.alibaba.nacos.address.misc;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Loggers holder.
*
* @author pbting * @author pbting
* @date 2019-07-04 4:34 PM * @date 2019-07-04 4:34 PM
*/ */
public class Loggers { public class Loggers {
public static final Logger addressLogger = LoggerFactory.getLogger("com.alibaba.nacos.address.main"); public static final Logger ADDRESS_LOGGER = LoggerFactory.getLogger("com.alibaba.nacos.address.main");
} }

View File

@ -13,32 +13,39 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address.util; package com.alibaba.nacos.address.util;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* Provides a unified tool class for address server parameter verification * Provides a unified tool class for address server parameter verification.
* *
* @author pbting * @author pbting
* @date 2019-06-19 11:19 AM * @date 2019-06-19 11:19 AM
* @since 1.1.0 * @since 1.1.0
*/ */
public class AddressServerParamCheckUtil { public class AddressServerParamCheckUtil {
public static final String CHECK_OK = "ok"; public static final String CHECK_OK = "ok";
public static final String ILLEGAL_IP_PREFIX = "illegal ip: "; public static final String ILLEGAL_IP_PREFIX = "illegal ip: ";
private static final String IP_REGEX = "(2(5[0-5]{1}|[0-4]\\d{1})|[0-1]?\\d{1,2})(\\.(2(5[0-5]{1}|[0-4]\\d{1})|[0-1]?\\d{1,2})){3}"; private static final String IP_REGEX = "(2(5[0-5]{1}|[0-4]\\d{1})|[0-1]?\\d{1,2})(\\.(2(5[0-5]{1}|[0-4]\\d{1})|[0-1]?\\d{1,2})){3}";
private static final Pattern IP_PATTERN = Pattern.compile(IP_REGEX); private static final Pattern IP_PATTERN = Pattern.compile(IP_REGEX);
/**
* Check ips.
*
* @param ips ips
* @return 'ok' if check passed, otherwise illegal ip
*/
public static String checkIps(String... ips) { public static String checkIps(String... ips) {
if (ips == null || ips.length == 0) { if (ips == null || ips.length == 0) {
return CHECK_OK; return CHECK_OK;
} }
// illegal response // illegal response
@ -50,11 +57,11 @@ public class AddressServerParamCheckUtil {
} }
illegalResponse.append(ip + ","); illegalResponse.append(ip + ",");
} }
if (illegalResponse.length() == 0) { if (illegalResponse.length() == 0) {
return CHECK_OK; return CHECK_OK;
} }
return ILLEGAL_IP_PREFIX + illegalResponse.substring(0, illegalResponse.length() - 1); return ILLEGAL_IP_PREFIX + illegalResponse.substring(0, illegalResponse.length() - 1);
} }
} }

View File

@ -16,12 +16,12 @@
--> -->
<included> <included>
<springProperty scope="context" name="logPath" source="nacos.logs.path" defaultValue="${user.home}/nacos/logs"/> <springProperty scope="context" name="logPath" source="nacos.logs.path" defaultValue="${user.home}/nacos/logs"/>
<property name="LOG_HOME" value="${logPath}"/> <property name="LOG_HOME" value="${logPath}"/>
<appender name="nacos-address" <appender name="nacos-address"
class="ch.qos.logback.core.rolling.RollingFileAppender"> class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/nacos-address.log</file> <file>${LOG_HOME}/nacos-address.log</file>
<append>true</append> <append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
@ -36,7 +36,7 @@
<charset>UTF-8</charset> <charset>UTF-8</charset>
</encoder> </encoder>
</appender> </appender>
<logger name="com.alibaba.nacos.address.main" additivity="false"> <logger name="com.alibaba.nacos.address.main" additivity="false">
<level value="INFO"/> <level value="INFO"/>
<appender-ref ref="nacos-address"/> <appender-ref ref="nacos-address"/>

View File

@ -13,6 +13,5 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# #
server.port=8080 server.port=8080
server.servlet.context-path=/ server.servlet.context-path=/

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address; package com.alibaba.nacos.address;
import org.junit.Ignore; import org.junit.Ignore;
@ -20,43 +21,39 @@ import org.junit.Test;
import java.util.HashMap; import java.util.HashMap;
/**
* @author pbting
* @date 2019-06-18 2:37 PM
*/
@Ignore @Ignore
public class AddressServerControllerTests { public class AddressServerControllerTests {
private static final String PRODUCT_NACOS = "nacos"; private static final String PRODUCT_NACOS = "nacos";
private static final String PRODUCT_CONFIG = "config"; private static final String PRODUCT_CONFIG = "config";
private static final String PRODUCT_NAMING = "naming"; private static final String PRODUCT_NAMING = "naming";
private static final String DEFAULT_URL_CLUSTER = "serverlist"; private static final String DEFAULT_URL_CLUSTER = "serverlist";
private static final String GET_SERVERLIST_URL_FORMART = "http://127.0.0.1:8080/%s/%s"; private static final String GET_SERVERLIST_URL_FORMART = "http://127.0.0.1:8080/%s/%s";
//-----------------product=nacos,cluster=DEFAULT -------------------// //-----------------product=nacos,cluster=DEFAULT -------------------//
/**
* test the default product and cluster
*/
@Test @Test
public void postCluster() { public void postCluster() {
String ips = "127.0.0.100,127.0.0.102,127.0.0.104"; String ips = "127.0.0.100,127.0.0.102,127.0.0.104";
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
params.put("ips", ips); params.put("ips", ips);
String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void getCluster() { public void getCluster() {
String getUrl = String.format(GET_SERVERLIST_URL_FORMART, PRODUCT_NACOS, DEFAULT_URL_CLUSTER); String getUrl = String.format(GET_SERVERLIST_URL_FORMART, PRODUCT_NACOS, DEFAULT_URL_CLUSTER);
String response = SimpleHttpTestUtils.doGet(getUrl, new HashMap<>(), "UTF-8"); String response = SimpleHttpTestUtils.doGet(getUrl, new HashMap<>(), "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void deleteCluster() { public void deleteCluster() {
HashMap<String, String> deleteIp = new HashMap<>(); HashMap<String, String> deleteIp = new HashMap<>();
@ -64,7 +61,7 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", deleteIp, "UTF-8"); String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", deleteIp, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void deleteClusterWithSpecIp() { public void deleteClusterWithSpecIp() {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
@ -72,26 +69,22 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void putCluster() { public void putCluster() {
String ips = "127.0.0.114"; String ips = "127.0.0.114";
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
params.put("ips", ips); params.put("ips", ips);
String response = SimpleHttpTestUtils.doPut("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doPut("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
//-----------------product=config,cluster=cluster01 -------------------// //-----------------product=config,cluster=cluster01 -------------------//
/**
* test with product
*/
@Test @Test
public void postClusterWithProduct() { public void postClusterWithProduct() {
String ips = "127.0.0.101,127.0.0.102,127.0.0.103"; String ips = "127.0.0.101,127.0.0.102,127.0.0.103";
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
params.put("ips", ips); params.put("ips", ips);
@ -99,7 +92,7 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void getClusterWithProduct() { public void getClusterWithProduct() {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
@ -107,7 +100,7 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doGet(getUrl, params, "UTF-8"); String response = SimpleHttpTestUtils.doGet(getUrl, params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void deleteClusterWithProduct() { public void deleteClusterWithProduct() {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
@ -115,7 +108,7 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void deleteClusterWithProductAndIp() { public void deleteClusterWithProductAndIp() {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
@ -124,10 +117,10 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void putClusterWithProduct() { public void putClusterWithProduct() {
String ips = "127.0.0.196"; String ips = "127.0.0.196";
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
params.put("ips", ips); params.put("ips", ips);
@ -135,16 +128,12 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doPut("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doPut("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
//-----------------product=naming,cluster=cluster01 -------------------// //-----------------product=naming,cluster=cluster01 -------------------//
/**
* test with product and cluster
*/
@Test @Test
public void postClusterWithProductAndCluster() { public void postClusterWithProductAndCluster() {
String ips = "127.0.0.100,127.0.0.200,127.0.0.31"; String ips = "127.0.0.100,127.0.0.200,127.0.0.31";
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
params.put("ips", ips); params.put("ips", ips);
@ -153,7 +142,7 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doPost("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void getClusterWithProductAndCluster() { public void getClusterWithProductAndCluster() {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
@ -161,7 +150,7 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doGet(getUrl, params, "UTF-8"); String response = SimpleHttpTestUtils.doGet(getUrl, params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void deleteClusterWithProductAndCluster() { public void deleteClusterWithProductAndCluster() {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
@ -170,7 +159,7 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void deleteClusterWithProductAndClusterAndIp() { public void deleteClusterWithProductAndClusterAndIp() {
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
@ -180,10 +169,10 @@ public class AddressServerControllerTests {
String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8"); String response = SimpleHttpTestUtils.doDelete("http://127.0.0.1:8080/nacos/v1/as/nodes", params, "UTF-8");
System.err.println(response); System.err.println(response);
} }
@Test @Test
public void putClusterWithProductAndCluster() { public void putClusterWithProductAndCluster() {
String ips = "127.0.0.171"; String ips = "127.0.0.171";
HashMap<String, String> params = new HashMap<>(); HashMap<String, String> params = new HashMap<>();
params.put("ips", ips); params.put("ips", ips);

View File

@ -13,22 +13,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address; package com.alibaba.nacos.address;
import com.alibaba.nacos.address.util.AddressServerParamCheckUtil; import com.alibaba.nacos.address.util.AddressServerParamCheckUtil;
import org.junit.Test; import org.junit.Test;
/**
* @author pbting
* @date 2019-06-19 11:31 AM
*/
public class ParamCheckUtilTests { public class ParamCheckUtilTests {
@Test @Test
public void checkIps() { public void checkIps() {
String[] ips = {"127.0.0.1"}; String[] ips = {"127.0.0.1"};
System.out.println(AddressServerParamCheckUtil.checkIps(ips)); System.out.println(AddressServerParamCheckUtil.checkIps(ips));
String[] illlegalIps = {"127.100.19", "127.0.0.1"}; String[] illlegalIps = {"127.100.19", "127.0.0.1"};
System.err.println(AddressServerParamCheckUtil.checkIps(illlegalIps)); System.err.println(AddressServerParamCheckUtil.checkIps(illlegalIps));
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.address; package com.alibaba.nacos.address;
import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.IoUtils;
@ -26,49 +27,46 @@ import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Map; import java.util.Map;
/**
* @author pbting
* @date 2019-06-18 2:40 PM
*/
public class SimpleHttpTestUtils { public class SimpleHttpTestUtils {
private static final String REQUEST_METHOD_DELETE = "DELETE"; private static final String REQUEST_METHOD_DELETE = "DELETE";
private static final String REQUEST_METHOD_PUT = "PUT"; private static final String REQUEST_METHOD_PUT = "PUT";
private static final String REQUEST_METHOD_POST = "POST"; private static final String REQUEST_METHOD_POST = "POST";
private static final String REQUEST_METHOD_GET = "GET"; private static final String REQUEST_METHOD_GET = "GET";
/** /**
* 连接超时 * 连接超时.
*/ */
private static int CONNECT_TIME_OUT = 2000; private static final int CONNECT_TIME_OUT = 2000;
/** /**
* 读取数据超时 * 读取数据超时.
*/ */
private static int READ_TIME_OUT = 2000; private static final int READ_TIME_OUT = 2000;
/** /**
* 请求编码 * 请求编码.
*/ */
public static String REQUEST_ENCODING = "UTF-8"; public static final String REQUEST_ENCODING = "UTF-8";
/** /**
* 接收编码 * 接收编码.
*/ */
public static String RESPONSE_ENCODING = "UTF-8"; public static final String RESPONSE_ENCODING = "UTF-8";
public static final short OK = 200; public static final short OK = 200;
public static final short Bad_Request = 400; public static final short BAD_REQUEST = 400;
public static final short Internal_Server_Error = 500; public static final short INTERNAL_SERVER_ERROR = 500;
public static final short PARAM_ERROR_NO_ANALYSESOR = 1000; public static final short PARAM_ERROR_NO_ANALYSESOR = 1000;
/** /**
* <pre> * 发送带参数的GET的HTTP请求.
* 发送带参数的GET的HTTP请求
* </pre>
* *
* @param reqUrl HTTP请求URL * @param reqUrl HTTP请求URL
* @param paramMap 参数映射表 * @param paramMap 参数映射表
@ -77,11 +75,9 @@ public class SimpleHttpTestUtils {
public static String doGet(String reqUrl, Map<String, String> paramMap, String recvEncoding) { public static String doGet(String reqUrl, Map<String, String> paramMap, String recvEncoding) {
return doRequest(reqUrl, paramMap, REQUEST_METHOD_GET, recvEncoding); return doRequest(reqUrl, paramMap, REQUEST_METHOD_GET, recvEncoding);
} }
/** /**
* <pre> * 发送带参数的POST的HTTP请求.
* 发送带参数的POST的HTTP请求
* </pre>
* *
* @param reqUrl HTTP请求URL * @param reqUrl HTTP请求URL
* @param paramMap 参数映射表 * @param paramMap 参数映射表
@ -90,11 +86,9 @@ public class SimpleHttpTestUtils {
public static String doPost(String reqUrl, Map<String, String> paramMap, String recvEncoding) { public static String doPost(String reqUrl, Map<String, String> paramMap, String recvEncoding) {
return doRequest(reqUrl, paramMap, REQUEST_METHOD_POST, recvEncoding); return doRequest(reqUrl, paramMap, REQUEST_METHOD_POST, recvEncoding);
} }
/** /**
* <pre> * 发送带参数的 PUT HTTP 请求.
* 发送带参数的 PUT HTTP 请求
* </pre>
* *
* @param reqUrl HTTP请求URL * @param reqUrl HTTP请求URL
* @param paramMap 参数映射表 * @param paramMap 参数映射表
@ -103,11 +97,9 @@ public class SimpleHttpTestUtils {
public static String doPut(String reqUrl, Map<String, String> paramMap, String recvEncoding) { public static String doPut(String reqUrl, Map<String, String> paramMap, String recvEncoding) {
return doRequest(reqUrl, paramMap, REQUEST_METHOD_PUT, recvEncoding); return doRequest(reqUrl, paramMap, REQUEST_METHOD_PUT, recvEncoding);
} }
/** /**
* <pre> * 发送带参数的 DELETE HTTP 请求.
* 发送带参数的 DELETE HTTP 请求
* </pre>
* *
* @param reqUrl HTTP请求URL * @param reqUrl HTTP请求URL
* @param paramMap 参数映射表 * @param paramMap 参数映射表
@ -116,13 +108,15 @@ public class SimpleHttpTestUtils {
public static String doDelete(String reqUrl, Map<String, String> paramMap, String recvEncoding) { public static String doDelete(String reqUrl, Map<String, String> paramMap, String recvEncoding) {
return doRequest(reqUrl, paramMap, REQUEST_METHOD_DELETE, recvEncoding); return doRequest(reqUrl, paramMap, REQUEST_METHOD_DELETE, recvEncoding);
} }
private static String doRequest(String reqUrl, Map<String, String> paramMap, String reqMethod, String recvEncoding) { private static String doRequest(String reqUrl, Map<String, String> paramMap, String reqMethod,
String recvEncoding) {
return doExecute(reqUrl, paramMap, reqMethod, recvEncoding); return doExecute(reqUrl, paramMap, reqMethod, recvEncoding);
} }
private static String doExecute(String reqUrl, Map<String, String> paramMap, String reqMethod, String recvEncoding) { private static String doExecute(String reqUrl, Map<String, String> paramMap, String reqMethod,
String recvEncoding) {
HttpURLConnection urlCon = null; HttpURLConnection urlCon = null;
String responseContent = null; String responseContent = null;
try { try {
@ -134,13 +128,13 @@ public class SimpleHttpTestUtils {
params.append(URLEncoder.encode(element.getValue(), REQUEST_ENCODING)); params.append(URLEncoder.encode(element.getValue(), REQUEST_ENCODING));
params.append("&"); params.append("&");
} }
if (params.length() > 0) { if (params.length() > 0) {
params = params.deleteCharAt(params.length() - 1); params = params.deleteCharAt(params.length() - 1);
} }
if (params.length() > 0 && if (params.length() > 0 && (REQUEST_METHOD_GET.equals(reqMethod) || REQUEST_METHOD_DELETE
(REQUEST_METHOD_GET.equals(reqMethod) || REQUEST_METHOD_DELETE.equals(reqMethod))) { .equals(reqMethod))) {
reqUrl = reqUrl + "?" + params.toString(); reqUrl = reqUrl + "?" + params.toString();
} }
} }
@ -169,7 +163,7 @@ public class SimpleHttpTestUtils {
responseContent = tempStr.toString(); responseContent = tempStr.toString();
rd.close(); rd.close();
in.close(); in.close();
urlCon.getResponseMessage(); urlCon.getResponseMessage();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -178,5 +172,5 @@ public class SimpleHttpTestUtils {
} }
return responseContent; return responseContent;
} }
} }