code cleanup

This commit is contained in:
东皇 2018-06-26 15:39:43 +08:00
parent a62117fada
commit dcbee28e48
15 changed files with 54 additions and 128 deletions

View File

@ -1,10 +1,10 @@
package me.chyxion.tigon.codegen.service;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.CharEncoding;
import java.nio.charset.StandardCharsets;
import me.chyxion.tigon.codegen.CodeGenCustomizer;
import me.chyxion.tigon.codegen.model.CodeGenArgs;
import org.springframework.beans.factory.annotation.Autowired;
@ -57,7 +57,7 @@ public abstract class CodeGenerator {
// write result
FileUtils.write(new File(baseTool.getProjDir(), args.getFile()),
baseTool.renderFtl(args.getFtl(), args.getModel()),
CharEncoding.UTF_8);
StandardCharsets.UTF_8);
return args.getFile();
}
catch (Exception e) {

View File

@ -19,7 +19,7 @@ public class ListForm extends BaseForm {
private int start;
@Min(1)
@Max(2048)
private int limit;
private int limit = 16;
private String search;
@Trim
@EmptyToNull

View File

@ -6,8 +6,8 @@ import java.util.Collection;
import me.chyxion.tigon.model.BaseModel;
import org.apache.ibatis.annotations.Param;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import org.springframework.validation.annotation.Validated;
/**

View File

@ -6,9 +6,8 @@ import me.chyxion.tigon.mybatis.Search;
import me.chyxion.tigon.model.BaseModel;
import me.chyxion.tigon.model.ViewModel;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.NotEmpty;
import me.chyxion.tigon.validation.annotation.NotNullOrBlank;
import org.hibernate.validator.constraints.NotEmpty;
/**
* @version 0.0.1

View File

@ -4,7 +4,7 @@ import java.util.List;
import me.chyxion.tigon.model.BaseModel;
import me.chyxion.tigon.model.ViewModel;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotEmpty;
/**
* @version 0.0.1

View File

@ -72,9 +72,9 @@ public class AuthUser<U> implements Serializable {
* @param authUser auth user
*/
public static void save(Session session, AuthUser<?> authUser) {
Assert.notNull(session);
Assert.notNull(authUser);
Assert.state(authUser.getUserId() != null, "User Id Is Null");
Assert.notNull(session, "Session could not be null");
Assert.notNull(authUser, "Auth user could not be null");
Assert.state(authUser.getUserId() != null, "Auth user id could not be null");
session.setAttribute(SESSION_AUTH_USER_KEY, authUser);
session.touch();
}
@ -86,7 +86,7 @@ public class AuthUser<U> implements Serializable {
*/
public AuthUser<U> setAttr(String name, Serializable value) {
if (attrs == null) {
attrs = new HashMap<String, Serializable>();
attrs = new HashMap<>();
}
attrs.put(name, value);
return this;

View File

@ -35,17 +35,17 @@ public class DefaultViewResolver implements ViewResolver, Ordered {
*/
@Override
public View resolveViewName(String rawViewName, Locale locale) throws Exception {
log.debug("Base View Resolver Resolve View Name [{}].", rawViewName);
log.debug("Base view resolver resolve view name [{}].", rawViewName);
// parse view name
View view = null;
if (rawViewName.startsWith(ResponseTool.PREFIX_DATA)) {
String viewName = rawViewName.substring(ResponseTool.PREFIX_DATA.length());
log.debug("Resolve View Name [{}] As JSON Data.", viewName);
log.debug("Resolve view name [{}] As JSON data.", viewName);
view = new JSONView(new JSONViewDataModel(viewName), jsonViewConfig);
}
if (view == null) {
log.debug("No View Type Found, Render As JSON View [{}].", rawViewName);
log.debug("No view type found, render as JSON view [{}].", rawViewName);
view = new JSONView(rawViewName, jsonViewConfig);
}
return view;

View File

@ -77,7 +77,7 @@ public class JSONView extends FastJsonJsonView {
config.getDataKey(), config.getCodeKey(), config.getMessageKey());
}
if (log.isDebugEnabled()) {
log.debug("Render JSON Response [{}].", JSON.toJSONString(dataResp, true));
log.debug("Render JSON response [{}].", JSON.toJSONString(dataResp, true));
}
return dataResp;
}
@ -100,21 +100,21 @@ public class JSONView extends FastJsonJsonView {
}
private JSONViewDataModel buildDataModel(Map<String, Object> model) {
log.debug("Build Data Model From Map Model.");
log.debug("Build data model from map model.");
Object objData = null;
if (Boolean.TRUE.equals(model.get(MAP_DATA))) {
objData = model.get(DATA_KEY);
log.debug("Render Map Data [{}].", objData);
log.debug("Render map data [{}].", objData);
}
else {
// default bean name
String beanName = getBeanName();
if (!requestURI.equals("/" + beanName)) {
objData = beanName;
log.debug("Could Not Find Any Resource, Render As String Data [{}].", objData);
log.debug("Could not find any resource, render as string data [{}].", objData);
}
else {
log.debug("View Name Is Request URI, No Data To Response..");
log.debug("View name is request URI, no data to response.");
}
}
return new JSONViewDataModel(objData);

View File

@ -18,13 +18,13 @@ import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
@Getter
public class JSONViewConfig {
@Value("${tigon.webmvc.jsonview.success_key:success}")
@Value("${tigon.webmvc.jsonview.success-key:success}")
private String successKey;
@Value("${tigon.webmvc.jsonview.data_key:data}")
@Value("${tigon.webmvc.jsonview.data-key:data}")
private String dataKey;
@Value("${tigon.webmvc.jsonview.code_key:errcode}")
@Value("${tigon.webmvc.jsonview.code-key:errcode}")
private String codeKey;
@Value("${tigon.webmvc.jsonview.message_key:errmsg}")
@Value("${tigon.webmvc.jsonview.message-key:errmsg}")
private String messageKey;
@Autowired(required = false)
private JSONViewDataModelAssembler dataModelAssembler;
@ -32,7 +32,7 @@ public class JSONViewConfig {
// init json serialize config
@PostConstruct
void init() {
log.info("Init FastJSON Global Serialize Config To Serialize ViewModelable.");
log.info("Init FastJSON global serialize config to serialize view modelable.");
final SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
final ViewModelableSerializer viewModelableSerializer = new ViewModelableSerializer();
serializeConfig.put(ViewModel.class, viewModelableSerializer);

View File

@ -1,11 +1,11 @@
package me.chyxion.tigon.webmvc;
import java.io.File;
import org.slf4j.Logger;
import lombok.Getter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.http.MediaType;
import org.springframework.core.io.Resource;
@ -21,21 +21,21 @@ import org.springframework.core.io.InputStreamResource;
* chyxion@163.com <br>
* Jan 16, 2015 5:33:30 PM
*/
@Slf4j
@Getter
public class ResourceModel {
private static final Logger log =
LoggerFactory.getLogger(ResourceModel.class);
private String contentType;
private long contentLength;
private String name;
private Resource resource;
public ResourceModel(Resource resource,
public ResourceModel(final Resource resource,
String contentType, long contentLength, String name) throws IOException {
Assert.notNull(resource);
Assert.notNull(resource, "Resource model resource could not be null");
Assert.state(!(InputStreamResource.class ==
resource.getClass() && contentLength <= 0),
"Input Stream Resource Content Length Must Provide.");
"Input stream resource content length must provide.");
this.resource = resource;
this.contentType = contentType;
this.contentLength = contentLength > 0 ?
@ -43,63 +43,32 @@ public class ResourceModel {
this.name = name;
}
public ResourceModel(File file, String contentType, String name) {
public ResourceModel(final File file, final String contentType, final String name) {
this.resource = new FileSystemResource(file);
this.contentType = contentType;
this.contentLength = file.length();
this.name = StringUtils.defaultIfBlank(name, file.getName());
}
public ResourceModel(InputStream inputStream,
public ResourceModel(final InputStream inputStream,
String contentType, long contentLength, String name) {
Assert.notNull(inputStream);
Assert.notNull(inputStream, "Resource model input stream could not be null");
Assert.state(contentLength > 0,
"Input Stream Resource Content Length Must Provide.");
"Input stream resource content length must provide.");
this.contentType = contentType;
this.contentLength = contentLength;
this.resource = new InputStreamResource(inputStream);
this.name = name;
}
public ResourceModel(byte[] data, String contentType, String name) {
Assert.notNull(data);
Assert.notNull(data, "Resource model data could not be null");
this.resource = new ByteArrayResource(data);
this.contentType = contentType;
this.contentLength = data.length;
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
* @return
*/
public ResourceModel setName(String name) {
this.name = name;
return this;
}
/**
* @return the resource
*/
public Resource getResource() {
return resource;
}
/**
* @param resource the resource to set
* @return
*/
public ResourceModel setResource(Resource resource) {
this.resource = resource;
return this;
}
/**
* @return the contentType
*/
@ -119,21 +88,4 @@ public class ResourceModel {
}
return contentType;
}
/**
* @param contentType the contentType to set
* @return
*/
public ResourceModel setContentType(String contentType) {
this.contentType = contentType;
return this;
}
/**
* @return the contentLength
* @throws IOException
*/
public long getContentLength() throws IOException {
return contentLength;
}
}

View File

@ -1,6 +1,7 @@
package me.chyxion.tigon.webmvc;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.net.URLEncoder;
import java.util.Collection;
@ -107,33 +108,33 @@ public class TigonReturnValueHandler
ResourceModel resourceModel = null;
HttpEntity<?> entity = null;
if (returnValue instanceof ResourceModel) {
log.debug("Found Download Model Return.");
log.debug("Found download model return.");
resourceModel = (ResourceModel) returnValue;
}
else if (returnValue instanceof HttpEntity) {
log.debug("Found HttpEntity Return.");
log.debug("Found http entity return.");
entity = (HttpEntity<?>) returnValue;
}
else if (returnValue instanceof File) {
log.debug("Found File [{}] Return.", returnValue);
log.debug("Found file [{}] return.", returnValue);
resourceModel = new ResourceModel((File) returnValue, null, null);
}
else if (returnValue instanceof Resource) {
log.debug("Found Resouce [{}] Return.", returnValue);
log.debug("Found resource [{}] return.", returnValue);
resourceModel = new ResourceModel((Resource) returnValue, null, 0, null);
}
else if (returnValue instanceof byte[]) {
log.debug("Found Byte Array Return.");
log.debug("Found byte array return.");
resourceModel = new ResourceModel((byte[]) returnValue, null, null);
}
// assemble http entity
if (resourceModel != null) {
HttpHeaders headers = new HttpHeaders();
final HttpHeaders headers = new HttpHeaders();
String name = resourceModel.getName();
if (StringUtils.isNotBlank(name)) {
log.info("Found Resource Name [{}], Set Content-Disposition Header.", name);
name = URLEncoder.encode(name, CharEncoding.UTF_8);
StringBuilder sbDispo =
log.info("Found resource name [{}], set Content-Disposition header.", name);
name = URLEncoder.encode(name, StandardCharsets.UTF_8.name());
final StringBuilder sbDispo =
new StringBuilder("attachment; filename=\"")
.append(name)
.append("\"; filename*=utf-8''")

View File

@ -20,14 +20,14 @@ public class StringToJSONArrayTypeConverter
* {@inheritDoc}
*/
@Override
public JSONArray convert(String text) {
public JSONArray convert(final String text) {
try {
return StringUtils.isNotBlank(text) ?
JSON.parseArray(text) : null;
}
catch (JSONException e) {
throw new IllegalArgumentException(
"Invalid JSONArray [" + text + "] Param", e);
"Invalid JSONArray [" + text + "] param", e);
}
}
}

View File

@ -20,14 +20,14 @@ public class StringToJSONObjectTypeConverter
* {@inheritDoc}
*/
@Override
public JSONObject convert(String text) {
public JSONObject convert(final String text) {
try {
return StringUtils.isNotBlank(text) ?
JSON.parseObject(text) : null;
}
catch (JSONException e) {
throw new IllegalArgumentException(
"Invalid JSONObject [" + text + "] Param", e);
"Invalid JSONObject [" + text + "] param", e);
}
}
}

View File

@ -33,32 +33,6 @@ public class DefaultExceptionResolver implements ExceptionResolver {
private static final long serialVersionUID = 1L;
{
// Database Exceptions
// put(CannotAcquireLockException.class, 5030);
// put(CannotSerializeTransactionException.class, 5031);
// put(CleanupFailureDataAccessException.class, 5032);
// put(ConcurrencyFailureException.class, 5033);
// put(DataAccessException.class, 5034);
// put(DataAccessResourceFailureException.class, 5035);
// put(DataIntegrityViolationException.class, 5036);
// put(DataRetrievalFailureException.class, 5037);
// put(DeadlockLoserDataAccessException.class, 5038);
// put(DuplicateKeyException.class, 5039);
// put(EmptyResultDataAccessException.class, 5040);
// put(IncorrectResultSizeDataAccessException.class, 5041);
// put(IncorrectUpdateSemanticsDataAccessException.class, 5042);
// put(InvalidDataAccessApiUsageException.class, 5043);
// put(InvalidDataAccessResourceUsageException.class, 5044);
// put(NonTransientDataAccessException.class, 5045);
// put(NonTransientDataAccessResourceException.class, 5046);
// put(OptimisticLockingFailureException.class, 5047);
// put(PermissionDeniedDataAccessException.class, 5048);
// put(PessimisticLockingFailureException.class, 5049);
// put(QueryTimeoutException.class, 5050);
// put(RecoverableDataAccessException.class, 5051);
// put(TransientDataAccessException.class, 5052);
// put(TransientDataAccessResourceException.class, 5053);
// put(TypeMismatchDataAccessException.class, 5054);
// put(UncategorizedDataAccessException.class, 5055);
// 4002
put(IllegalStateException.class, InvalidStateException.CODE);
put(ConversionFailedException.class, InvalidStateException.CODE);

View File

@ -6,8 +6,8 @@ import com.alibaba.fastjson.JSON;
import javax.annotation.PostConstruct;
import org.springframework.util.Assert;
import com.alibaba.fastjson.JSONObject;
import java.nio.charset.StandardCharsets;
import org.springframework.http.MediaType;
import org.apache.commons.lang3.CharEncoding;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
@ -173,7 +173,7 @@ public final class ControllerTestTool {
ResultActions request(
MockHttpServletRequestBuilder requestBuilder) {
requestBuilder.characterEncoding(CharEncoding.UTF_8);
requestBuilder.characterEncoding(StandardCharsets.UTF_8.name());
try {
return mvc.perform(requestBuilder);
}