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
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
;
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
;
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
};
52 public static String
makeTemplateTOSQL(String strName
, String strSQLs
, String strArgument
) throws SQLTemplateException
{
53 String strResult
= "";
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());
69 * make argument to map
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
>();
81 for (String param
: StringUtils
.split(strArgument
, "&")) {
82 String pair
[] = StringUtils
.split(param
, "=");
83 String key
= URLDecoder
.decode(pair
[0], "UTF-8");
86 if (pair
.length
> 1) {
88 value
= convertExistObject(key
, pair
[1]);
89 } catch(Exception e
) {
94 params
.put(key
, value
);
96 } catch (UnsupportedEncodingException e1
) {
98 throw new RESTFULUnsupportedEncodingException(e1
.getMessage());
106 * @param variableType
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());
143 * Return oracle style argument to java list
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
);
162 listParam
.add( params
.get(strKey
) );
169 * return argument to java list
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
));
193 * Return SQL Paramter
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
, "&");
212 strArguments
= "";//1={FirstParameter}&2={SecondParameter}";
225 public static String
makeURL(String strSQL
, String strRestURL
) {
226 String strServerURL
= GetAdminPreference
.getApiServerURL();
228 return String
.format("%s%s?%s",
231 getParameter(strSQL
));
240 public static boolean validateURL(String url
) {
241 Pattern p
= Pattern
.compile("[/][-A-Za-z0-9+&@#/%=~_()|]{2}", Pattern
.CASE_INSENSITIVE
);
242 Matcher m
= p
.matcher(url
);