Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
knightliao committed Jun 13, 2016
1 parent bee9e92 commit e2979b3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ public List<Config> getConfigList(Long appId, Long envId, String version, Boolea
matchs.add(new Match(Columns.ENV_ID, envId));
matchs.add(new Match(Columns.VERSION, version));
matchs.add(new Match(Columns.STATUS, Constants.STATUS_NORMAL));
if (hasValue)
if (hasValue) {
return find(matchs, new ArrayList<Order>());
else
return findColumns(matchs,new String[]{Columns.CONFIG_ID,Columns.TYPE,Columns.NAME,Columns.CREATE_TIME
,Columns.UPDATE_TIME,Columns.STATUS,Columns.APP_ID,Columns.ENV_ID,Columns.VERSION});
} else {
return findColumns(matchs, new String[] {Columns.CONFIG_ID, Columns.TYPE, Columns.NAME, Columns.CREATE_TIME
, Columns.UPDATE_TIME, Columns.STATUS, Columns.APP_ID, Columns.ENV_ID, Columns.VERSION});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.baidu.disconf.web.service.config.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,8 +15,6 @@
import com.baidu.disconf.web.service.config.service.ConfigFetchMgr;
import com.baidu.disconf.web.service.config.utils.ConfigUtils;

import java.util.List;

/**
* @author knightliao
*/
Expand Down Expand Up @@ -56,7 +56,7 @@ public ValueVo getConfItemByParameter(Long appId, Long envId, String version, St
/**
* 根据详细参数获取配置列表返回
*/
public List<Config> getConfListByParameter(Long appId, Long envId, String version, Boolean hasValue){
public List<Config> getConfListByParameter(Long appId, Long envId, String version, Boolean hasValue) {
return configDao.getConfigList(appId, envId, version, hasValue);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.baidu.disconf.web.utils;

import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import org.springframework.web.multipart.MultipartFile;

/**
* Created by knightliao on 15/1/26.
*/
public class MyStringUtils {


public static boolean isDouble(String str) {
try {
Double.parseDouble(str);
Expand All @@ -24,24 +23,26 @@ public static boolean isDouble(String str) {
}

private final static int BUFFER_SIZE = 4096;
private final static String DEFAULT_ENCODING = "UTF-8" ;
private final static String DEFAULT_ENCODING = "UTF-8";

/**
* 将InputStream转换成指定编码的String
*/
public static String inputStreamToString(InputStream in, String encoding) throws IOException {
private static String inputStreamToString(InputStream in, String encoding) throws IOException {

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1) {
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
outStream.write(data, 0, count);
}
return new String(outStream.toByteArray(),encoding);
return new String(outStream.toByteArray(), encoding);
}

public static String multipartFileToString(MultipartFile file) throws IOException {
BOMInputStream bomInputStream = new BOMInputStream(file.getInputStream());
ByteOrderMark bom = bomInputStream.getBOM();
String charsetName = bom == null ? DEFAULT_ENCODING : bom.getCharsetName();
return inputStreamToString(bomInputStream,charsetName);
return inputStreamToString(bomInputStream, charsetName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.net.URLEncoder;
import java.util.List;

import com.baidu.dsp.common.controller.BaseController;
import com.baidu.dsp.common.vo.JsonObjectBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,7 +25,9 @@
import com.baidu.disconf.web.web.config.validator.ConfigValidator4Fetch;
import com.baidu.dsp.common.annotation.NoAuth;
import com.baidu.dsp.common.constant.WebConstants;
import com.baidu.dsp.common.controller.BaseController;
import com.baidu.dsp.common.exception.DocumentNotFoundException;
import com.baidu.dsp.common.vo.JsonObjectBase;

/**
* 配置获取Controller, Disconf-client专门使用的
Expand Down Expand Up @@ -61,17 +61,16 @@ public class ConfigFetcherController extends BaseController {
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public JsonObjectBase getList(ConfForm confForm) {
return getListImp(confForm,true);
return getListImp(confForm, true);
}

@NoAuth
@RequestMapping(value = "/simple/list", method = RequestMethod.GET)
@ResponseBody
public JsonObjectBase getSimpleList(ConfForm confForm) {
return getListImp(confForm,false);
return getListImp(confForm, false);
}


/**
* 获取配置项 Item
*
Expand All @@ -91,7 +90,7 @@ public ValueVo getItem(ConfForm confForm) {
//
ConfigFullModel configModel = null;
try {
configModel = configValidator4Fetch.verifyConfForm(confForm,false);
configModel = configValidator4Fetch.verifyConfForm(confForm, false);
} catch (Exception e) {
LOG.warn(e.toString());
return ConfigUtils.getErrorVo(e.getMessage());
Expand All @@ -118,7 +117,7 @@ public HttpEntity<byte[]> getFile(ConfForm confForm) {
//
ConfigFullModel configModel = null;
try {
configModel = configValidator4Fetch.verifyConfForm(confForm,false);
configModel = configValidator4Fetch.verifyConfForm(confForm, false);
} catch (Exception e) {
LOG.error(e.toString());
hasError = true;
Expand Down Expand Up @@ -175,17 +174,17 @@ public HttpEntity<byte[]> downloadDspBill(String fileName, String value) {
return new HttpEntity<byte[]>(res, header);
}


private JsonObjectBase getListImp(ConfForm confForm,boolean hasValue) {
private JsonObjectBase getListImp(ConfForm confForm, boolean hasValue) {
LOG.info(confForm.toString());

//
// 校验
//
ConfigFullModel configModel = null;
configModel = configValidator4Fetch.verifyConfForm(confForm,true);
ConfigFullModel configModel = configValidator4Fetch.verifyConfForm(confForm, true);

List<Config> configs = configFetchMgr.getConfListByParameter(configModel.getApp().getId(), configModel.getEnv().getId(),
configModel.getVersion(),hasValue);
List<Config> configs =
configFetchMgr.getConfListByParameter(configModel.getApp().getId(), configModel.getEnv().getId(),
configModel.getVersion(), hasValue);

return buildListSuccess(configs, configs.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.baidu.disconf.web.utils.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,6 +17,7 @@
import com.baidu.disconf.web.service.config.form.ConfNewForm;
import com.baidu.disconf.web.service.config.form.ConfNewItemForm;
import com.baidu.disconf.web.service.config.service.ConfigMgr;
import com.baidu.disconf.web.utils.MyStringUtils;
import com.baidu.disconf.web.web.config.validator.ConfigValidator;
import com.baidu.disconf.web.web.config.validator.FileUploadValidator;
import com.baidu.dsp.common.constant.WebConstants;
Expand Down Expand Up @@ -93,7 +93,7 @@ public JsonObjectBase updateFile(@Valid ConfNewForm confNewForm, @RequestParam("
String fileContent = "";
try {

String str = MyStringUtils.multipartFileToString(file);
fileContent = MyStringUtils.multipartFileToString(file);
LOG.info("receive file: " + fileContent);

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.baidu.disconf.web.web.config.validator;

import com.baidu.dsp.common.exception.FieldException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
Expand All @@ -12,6 +11,7 @@
import com.baidu.disconf.web.service.env.bo.Env;
import com.baidu.disconf.web.service.env.service.EnvMgr;
import com.baidu.disconf.web.web.config.dto.ConfigFullModel;
import com.baidu.dsp.common.exception.FieldException;

/**
* @author knightliao
Expand All @@ -33,7 +33,7 @@ public class ConfigValidator4Fetch {
*
* @param confForm
*/
public ConfigFullModel verifyConfForm(ConfForm confForm,boolean unCheckKey) {
public ConfigFullModel verifyConfForm(ConfForm confForm, boolean unCheckKey) {

//
// app
Expand All @@ -44,7 +44,7 @@ public ConfigFullModel verifyConfForm(ConfForm confForm,boolean unCheckKey) {

App app = appMgr.getByName(confForm.getApp());
if (app == null) {
throw new FieldException("app", "app " + confForm.getApp() + " doesn't exist in db.",null);
throw new FieldException("app", "app " + confForm.getApp() + " doesn't exist in db.", null);
}

//
Expand Down

0 comments on commit e2979b3

Please sign in to comment.