- 1.7.6 초기 코드
[Tadpole.git] / com.hangum.tadpole.commons.sql / src / com / hangum / tadpole / engine / restful / RESTfulAPIUtils.java
blobac909ee01789baa4d7b8f69ee6c7438e17c885e8
1 /*******************************************************************************
2 * Copyright (c) 2015 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 *
8 * Contributors:
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com.hangum.tadpole.engine.restful;
13 import java.io.UnsupportedEncodingException;
14 import java.math.BigDecimal;
15 import java.net.URLDecoder;
16 import java.sql.Timestamp;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.log4j.Logger;
27 import com.hangum.tadpole.commons.libs.core.utils.VelocityUtils;
28 import com.hangum.tadpole.commons.util.DateUtil;
29 import com.hangum.tadpole.engine.sql.paremeter.lang.OracleStyleSQLNamedParameterUtil;
30 import com.hangum.tadpole.preference.define.GetAdminPreference;
32 /**
33 * RESTful API UTILS
35 * @author hangum
38 public class RESTfulAPIUtils {
39 private static final Logger logger = Logger.getLogger(RESTfulAPIUtils.class);
41 /** define variable */
42 public enum TDB_DATA_TYPE {_VARCHAR, _BIT, _NUM, _TINYINT, _SMALLINT, _INT, _BIGINT, _FLOAT, _DOUBLE, _VARBINARY, _DATE, _TIME};
44 /**
45 * make original sql
47 * @param strName
48 * @param strSQLs
49 * @param strArgument
50 * @return
52 public static String makeTemplateTOSQL(String strName, String strSQLs, String strArgument) throws SQLTemplateException {
53 String strResult = "";
54 try {
55 Map<String, Object> params = RESTfulAPIUtils.maekArgumentTOMap(strArgument);
57 strResult = VelocityUtils.getTemplate(strName, strSQLs, params);
58 if(logger.isDebugEnabled()) logger.debug(strResult);
60 } catch(Exception e) {
61 logger.error("Make Template", e);
62 throw new SQLTemplateException(e.getMessage());
65 return strResult;
68 /**
69 * make argument to map
70 * @param strArgument
71 * @return
72 * @throws UnsupportedEncodingException
74 public static Map<String, Object> maekArgumentTOMap(String strArgument) throws RESTFULUnsupportedEncodingException {
75 if(StringUtils.split(strArgument, "&") == null) return new HashMap<String, Object>();
77 if(logger.isDebugEnabled()) logger.debug("original URL is ===> " + strArgument);
78 Map<String, Object> params = new HashMap<String, Object>();
80 try {
81 for (String param : StringUtils.split(strArgument, "&")) {
82 String pair[] = StringUtils.split(param, "=");
83 String key = URLDecoder.decode(pair[0], "UTF-8");
85 Object value = null;
86 if (pair.length > 1) {
87 try {
88 value = convertExistObject(key, pair[1]);
89 } catch(Exception e) {
90 value = pair[1];
94 params.put(key, value);
96 } catch (UnsupportedEncodingException e1) {
97 logger.error(e1);
98 throw new RESTFULUnsupportedEncodingException(e1.getMessage());
101 return params;
106 * @param variableType
107 * @param value
108 * @return
109 * @throws UnsupportedEncodingException
111 public static Object convertExistObject(String variableType, String value) throws UnsupportedEncodingException {
112 value = URLDecoder.decode(value, "UTF-8");
114 if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._BIT.name())) {
115 return new Boolean(value);
116 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._NUM.name())) {
117 return new BigDecimal(value);
118 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._TINYINT.name())) {
119 return new Byte(value);
120 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._SMALLINT.name())) {
121 return new Short(value);
122 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._INT.name())) {
123 return new Integer(value);
124 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._BIGINT.name())) {
125 return new Long(value);
126 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._FLOAT.name())) {
127 return new Float(value);
128 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._DOUBLE.name())) {
129 return new Double(value);
131 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._VARBINARY.name())) {
132 return new Byte(value);
133 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._DATE.name())) {
134 return DateUtil.convertToDate(value);
135 } else if(StringUtils.startsWithIgnoreCase(variableType, TDB_DATA_TYPE._TIME.name())) {
136 return new Timestamp(new Long(value).longValue());
139 return value;
143 * Return oracle style argument to java list
145 * @param mapIndex
146 * @param strArgument
147 * @return
148 * @throws RESTFulArgumentNotMatchException
149 * @throws UnsupportedEncodingException
151 public static List<Object> makeArgumentToOracleList(Map<Integer, String> mapIndex, String strArgument) throws RESTFulArgumentNotMatchException, RESTFULUnsupportedEncodingException {
152 List<Object> listParam = new ArrayList<Object>();
154 Map<String, Object> params = maekArgumentTOMap(strArgument);
156 for(int i=1; i<=mapIndex.size(); i++ ) {
157 String strKey = mapIndex.get(i);
159 if(!params.containsKey(strKey)) {
160 throw new RESTFulArgumentNotMatchException("SQL Parameter not found. Must have to key name is " + strKey);
161 } else {
162 listParam.add( params.get(strKey) );
165 return listParam;
169 * return argument to java list
171 * @param strArgument
172 * @return
173 * @throws UnsupportedEncodingException
175 public static List<Object> makeArgumentToJavaList(String strArgument) throws RESTFULUnsupportedEncodingException {
176 List<Object> listParam = new ArrayList<Object>();
178 Map<String, Object> params = maekArgumentTOMap(strArgument);
180 // assume this count... no way i'll argument is over 100..... --;;
181 for(int i=1; i<100; i++) {
182 if(params.containsKey(String.valueOf(i))) {
183 listParam.add(params.get(""+i));
184 } else {
185 break;
189 return listParam;
193 * Return SQL Paramter
195 * @param strSQL
196 * @return
198 public static String getParameter(String strSQL) {
199 if("".equals(strSQL)) return "";
201 String strArguments = "";
202 OracleStyleSQLNamedParameterUtil oracleNamedParamUtil = new OracleStyleSQLNamedParameterUtil();
203 oracleNamedParamUtil.parse(strSQL);
204 Map<Integer, String> mapIndex = oracleNamedParamUtil.getMapIndexToName();
205 if(!mapIndex.isEmpty()) {
206 for(String strParam : mapIndex.values()) {
207 strArguments += String.format("%s={%s}&", strParam, strParam);
209 strArguments = StringUtils.removeEnd(strArguments, "&");
211 } else {
212 strArguments = "";//1={FirstParameter}&2={SecondParameter}";
215 return strArguments;
219 * make url
221 * @param strSQL
222 * @param strRestURL
223 * @return
225 public static String makeURL(String strSQL, String strRestURL) {
226 String strServerURL = GetAdminPreference.getApiServerURL();
228 return String.format("%s%s?%s",
229 strServerURL,
230 strRestURL,
231 getParameter(strSQL));
235 * user validate
237 * @param url
238 * @return
240 public static boolean validateURL(String url) {
241 Pattern p = Pattern.compile("[/][-A-Za-z0-9+&amp;@#/%=~_()|]{2}", Pattern.CASE_INSENSITIVE);
242 Matcher m = p.matcher(url);
244 return m.find();