diff --git a/build.gradle b/build.gradle index 55cdb43..b80b5d3 100644 --- a/build.gradle +++ b/build.gradle @@ -229,6 +229,10 @@ dependencies { //implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.12.5' //-----------------------------------------------------------------------------------// + // https://mvnrepository.com/artifact/com.googlecode.juniversalchardet/juniversalchardet + implementation group: 'com.googlecode.juniversalchardet', name: 'juniversalchardet', version: '1.0.3' + + //-----------------------------------------------------------------------------------// // Test //-----------------------------------------------------------------------------------// diff --git a/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java index 4ddc027..ee20105 100644 --- a/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java +++ b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java @@ -2,10 +2,12 @@ package com.xit.biz.ctgy.v2.service.impl; import com.google.common.base.Objects; import com.xit.biz.ctgy.dto.BoardDto; +import com.xit.biz.ctgy.dto.JudgeListDto; import com.xit.biz.ctgy.dto.struct.MinCivBoard680Mapstruct; import com.xit.biz.ctgy.entity.MinCivBoard680; import com.xit.biz.ctgy.repository.IBoardRepository; import com.xit.biz.ctgy.v2.service.IBoardService; +import com.xit.core.config.database.BaseMybatisDaoSupport; import com.xit.core.constant.ErrorCode; import com.xit.core.exception.CustomBaseException; import com.xit.core.oauth2.utils.HeaderUtil; @@ -13,9 +15,16 @@ import com.xit.core.support.jpa.JpaUtil; import com.xit.core.util.Checks; import com.xit.core.util.CommUtil; import com.xit.core.util.mpower.MpowerUtil; +import com.xit.core.util.mpower.MpowerUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; import org.mapstruct.factory.Mappers; +import org.mybatis.spring.SqlSessionTemplate; import org.springframework.data.domain.Example; import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.domain.Page; @@ -24,6 +33,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.nio.charset.Charset; import java.util.List; import java.util.Map; @@ -35,9 +45,29 @@ import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatc public class BoardService implements IBoardService { private final PasswordEncoder passwordEncoder; + private final SqlSessionTemplate sqlSessionTemplate; @Transactional(readOnly = true) public List> findAll(final BoardDto dto, Pageable pageable) { + //MappedStatement ms = sqlSessionFactory.getConfiguration().getMappedStatement("com.xit.biz.ctgy.mapper.IParkingMapper.selectTotParkingJudgeResultGroupByTeamAndChasu"); + + + //SqlSession session = sqlSessionTemplate.getConfiguration().get; + //SqlSession session = sqlSessionTemplate.getConfiguration().getMappedStatement("com.xit.biz.ctgy.mapper.IParkingMapper.selectTotParkingJudgeResultGroupByTeamAndChasu"); + + JudgeListDto param = new JudgeListDto(); + param.setMsChasu(99L); + //param.get + + + String sql = CommUtil.getMybatisSql(sqlSessionTemplate, "com.xit.biz.ctgy.mapper.IParkingMapper.selectTotParkingJudgeResultGroupByTeamAndChasu", param); + System.out.println(sql); + System.out.println(Charset.defaultCharset().displayName()); + + + //SqlSession sqlSession = SqlMapClient.getSqlSession(); + + MpowerUtil sendXml = new MpowerUtil(); String query = "SELECT ci_code ciCode,\n" + " name ciName,\n" + diff --git a/src/main/java/com/xit/core/config/EncodingFilterConfig.java b/src/main/java/com/xit/core/config/EncodingFilterConfig.java index 8311be3..d561502 100644 --- a/src/main/java/com/xit/core/config/EncodingFilterConfig.java +++ b/src/main/java/com/xit/core/config/EncodingFilterConfig.java @@ -1,10 +1,14 @@ package com.xit.core.config; +import com.xit.core.config.support.EucKrEncodingFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CharacterEncodingFilter; +import java.nio.charset.StandardCharsets; + /** * 기본 UTF-8인데, encoding이 EUC-KR로 들어오는 경우가 있는 경우(외부에서 들어오는 - 결재 등 -) 사용 * spring.http.encoding.force = false 추가 @@ -13,14 +17,28 @@ import org.springframework.web.filter.CharacterEncodingFilter; //TODO :: 기본 UTF-8인데, encoding이 EUC-KR로 들어오는 경우가 있는 경우(외부에서 들어오는 - 결재 등 -) 사용 public class EncodingFilterConfig { + @Bean - public FilterRegistrationBean encodingFilterBean() { - FilterRegistrationBean registrationBean = new FilterRegistrationBean(); + public FilterRegistrationBean encodingFilterBean() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setForceEncoding(true); - filter.setEncoding("MS949"); + filter.setEncoding("utf-8"); registrationBean.setFilter(filter); - registrationBean.addUrlPatterns("/ms949filterUrl/*"); + registrationBean.addUrlPatterns("/api/v2/*"); return registrationBean; } + +/* + @Bean + public FilterRegistrationBean encodingFilterBean() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + //CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); + //filter.setForceEncoding(true); + //filter.setEncoding("EUC-KR"); + registrationBean.setFilter(new EucKrEncodingFilter()); + registrationBean.addUrlPatterns("/api/v2/*"); + return registrationBean; + } + */ } \ No newline at end of file diff --git a/src/main/java/com/xit/core/config/database/BaseMybatisDaoSupport.java b/src/main/java/com/xit/core/config/database/BaseMybatisDaoSupport.java new file mode 100644 index 0000000..d3fee6f --- /dev/null +++ b/src/main/java/com/xit/core/config/database/BaseMybatisDaoSupport.java @@ -0,0 +1,48 @@ +package com.xit.core.config.database; + +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionTemplate; +import org.springframework.dao.support.DaoSupport; +import org.springframework.util.Assert; + + +public abstract class BaseMybatisDaoSupport extends DaoSupport +{ + + private SqlSession sqlSession; + private SqlSessionFactory sqlSessionFactory; + private boolean extenalSqlSession = false; + + public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { + this.sqlSession = sqlSessionTemplate; + this.extenalSqlSession = true; + } + + public final SqlSession getSqlSession() { + return sqlSession; + } + + public final void setSqlSession(SqlSession sqlSession) { + this.sqlSession = sqlSession; + } + + public final SqlSessionFactory getSqlSessionFactory() { + return sqlSessionFactory; + } + + public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { + this.sqlSessionFactory = sqlSessionFactory; + if(!this.extenalSqlSession) { + this.sqlSession = new SqlSessionTemplate(sqlSessionFactory); + } + } + + @Override + protected void checkDaoConfig() throws IllegalArgumentException { + Assert.notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"); + } + + + +} diff --git a/src/main/java/com/xit/core/config/support/EucKrEncodingFilter.java b/src/main/java/com/xit/core/config/support/EucKrEncodingFilter.java new file mode 100644 index 0000000..addea40 --- /dev/null +++ b/src/main/java/com/xit/core/config/support/EucKrEncodingFilter.java @@ -0,0 +1,11 @@ +package com.xit.core.config.support; + +import org.springframework.web.filter.CharacterEncodingFilter; + +public class EucKrEncodingFilter extends CharacterEncodingFilter { + + public EucKrEncodingFilter() { + setEncoding("EUC-KR"); + setForceEncoding(true); + } +} \ No newline at end of file diff --git a/src/main/java/com/xit/core/util/CommUtil.java b/src/main/java/com/xit/core/util/CommUtil.java index 9226261..17101b7 100644 --- a/src/main/java/com/xit/core/util/CommUtil.java +++ b/src/main/java/com/xit/core/util/CommUtil.java @@ -4,6 +4,9 @@ import com.rits.cloning.Cloner; import eu.bitwalker.useragentutils.UserAgent; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.ParameterMapping; +import org.apache.ibatis.session.SqlSession; import org.mozilla.universalchardet.UniversalDetector; import org.springframework.http.ResponseCookie; import org.springframework.mobile.device.Device; @@ -18,9 +21,11 @@ import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import java.io.StringReader; import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.net.URLEncoder; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.*; @@ -645,6 +650,57 @@ public class CommUtil { System.out.println(getRanStrKey(16)); } } + + public static String detectCharset(byte[] bytes) { + UniversalDetector detector = new UniversalDetector(null); + detector.handleData(bytes, 0, bytes.length); + detector.dataEnd(); + String detectedCharset = detector.getDetectedCharset(); + if (detectedCharset != null && detector.isDone() && Charset.isSupported(detectedCharset)) { + return detectedCharset; + } + return detectedCharset; + } + + public static String getMybatisSql(SqlSession sqlSession, String id, Object param) { + BoundSql boundSql = sqlSession.getConfiguration().getMappedStatement(id).getBoundSql(param); + + List parameterMappings = new ArrayList<>(boundSql.getParameterMappings()); + Map parameters = new HashMap<>(); + if (param instanceof Map) { + parameters.putAll((Map) param); + } else { + for (ParameterMapping parameterMapping : parameterMappings) { + try { + String field = parameterMapping.getProperty(); + //try { + String methodNm = "get"+field.substring(0,1).toUpperCase()+field.substring(1); + Method method = param.getClass().getMethod(methodNm); + + parameters.put(field, method.invoke(param)); + //parameters.put(field, param.getClass().getMethod("get"+field.substring(0,1).toUpperCase()+field.substring(1)).invoke(param)); + + } catch (NoSuchMethodException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } catch (InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + + String sql = boundSql.getSql(); + + for (ParameterMapping par : parameterMappings) { + Object parameter = parameters.get(par.getProperty()); + + if (parameter == null) + sql = sql.replaceFirst("\\?", "NULL"); + else + sql = sql.replaceFirst("\\?", "'" + parameter.toString() + "'"); + } + return sql; + } } diff --git a/src/main/java/com/xit/core/util/mpower/MpowerUtil.java b/src/main/java/com/xit/core/util/mpower/MpowerUtil.java index d2ea85b..a4fbce9 100644 --- a/src/main/java/com/xit/core/util/mpower/MpowerUtil.java +++ b/src/main/java/com/xit/core/util/mpower/MpowerUtil.java @@ -1,5 +1,8 @@ package com.xit.core.util.mpower; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.sql.CallableStatement; import java.sql.Connection; @@ -11,7 +14,9 @@ import java.sql.Statement; import java.util.*; import com.plf.client.Client; +import com.xit.core.util.CommUtil; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.binary.StringUtils; @Slf4j public class MpowerUtil { @@ -39,7 +44,7 @@ public class MpowerUtil { static int firstIndex = 0; static int lastIndex = 10; static int recordCountPerPage = 10; - static String beforeXml = ""; static String afterXmlsp = "]]>"; @@ -655,14 +660,27 @@ public class MpowerUtil { int mListCount = 0; try { mpower = new Client(hostip,port); + mpower.setCryptEnable(false); mpower.getConnection("MPowerXmlToQuery.xmlQuery1"); mpower.setInput("SQLXML", query); mpower.Request(); String result = mpower.getString("result", 0, 0); - System.out.println("#######message = "+mpower.getMessage()); - String s = mpower.getMessage(); + + System.out.println("#######message = "+mpower.getFile("list1")); + + String s = "답변드립니다"; + //System.out.println(CommUtil.detectCharset(s.getBytes())); + + System.out.println(URLEncoder.encode(s, "euc-kr")); + System.out.println(URLDecoder.decode(s, "euc-kr")); + String ss = new String("답변드립니다".getBytes("euc-kr"), "euc-kr"); + System.out.println(CommUtil.detectCharset(ss.getBytes())); + + System.out.println((new String("답변드립니다".getBytes("utf-8"), "euc-kr"))); + System.out.println((new String("답변드립니다".getBytes("euc-kr"), "euc-kr"))); + // System.out.println(new String(s.getBytes("unicode"), "unicode")); // System.out.println(new String(s.getBytes("utf-8"), "utf-8")); @@ -672,19 +690,35 @@ public class MpowerUtil { // System.out.println(new String(s.getBytes("ms949"), "ms949")); // System.out.println(new String(s.getBytes("iso-8859-1"), "iso-8859-1")); - System.out.println(new String(s.getBytes("unicode"), "unicode")); - System.out.println(new String(s.getBytes("utf-8"), "utf-8")); - System.out.println(new String(s.getBytes("utf-16"), "utf-16")); - System.out.println(new String(s.getBytes("euc-kr"), "euc-kr")); - System.out.println(new String(s.getBytes("ksc5601"), "ksc5601")); - System.out.println(new String(s.getBytes("ms949"), "ms949")); - System.out.println(new String(s.getBytes("iso-8859-1"), "iso-8859-1")); + //String originalStr = URLDecoder.decode(mpower.getMessage(), "utf-8"); + System.out.println(URLDecoder.decode(mpower.getMessage(), "utf-8")); + System.out.println(URLDecoder.decode(mpower.getMessage(), "euc-kr")); + System.out.println(StringUtils.newStringUtf16Le(mpower.getMessage().getBytes())); + System.out.println(StringUtils.newStringUtf16(mpower.getMessage().getBytes())); + System.out.println(StringUtils.newStringUtf16Be(mpower.getMessage().getBytes())); + System.out.println(StringUtils.newStringIso8859_1(mpower.getMessage().getBytes())); + + String originalStr = mpower.getMessage(); + String [] charSet = {"utf-8","euc-kr","ksc5601","iso-8859-1","x-windows-949", "ms949"}; + + for (int i=0; i>>>>>"+result); int row; if (result.equals("true")){ @@ -695,8 +729,10 @@ public class MpowerUtil { Map m = new HashMap<>(); if(!"".equals(feild) && feild != null){ m.put("cbContent", mpower.getString("list1", i, 4)); + //m.put("cbContent", new String(mpower.getString("list1", i, 4).getBytes("utf-8"), "utf-8")); }else{ for(int j=0;j